def set_attribute_label(span, resource_type, resource_labels, attribute_key, canonical_key=None, label_value_prefix=''): """Set a label to span that can be used for tracing. :param span: Span object :param resource_type: resource type :param resource_labels: collection of labels :param attribute_key: actual label key :param canonical_key: exporter specific label key, Optional :param label_value_prefix: exporter specific label value prefix, Optional """ if attribute_key in resource_labels: if canonical_key is None: canonical_key = attribute_key pair = { RESOURCE_LABEL % (resource_type, canonical_key): label_value_prefix + resource_labels[attribute_key] } pair_attrs = Attributes(pair).format_attributes_json()\ .get('attributeMap') _update_attr_map(span, pair_attrs)
def set_common_attributes(span): """Set the common attributes.""" common = { attributes_helper.COMMON_ATTRIBUTES.get('AGENT'): AGENT, } common_attrs = Attributes(common)\ .format_attributes_json()\ .get('attributeMap') _update_attr_map(span, common_attrs)
def set_gae_attributes(span): """Set the GAE environment common attributes.""" for env_var, attribute_key in GAE_ATTRIBUTES.items(): attribute_value = os.environ.get(env_var) if attribute_value is not None: pair = {attribute_key: attribute_value} pair_attrs = Attributes(pair)\ .format_attributes_json()\ .get('attributeMap') _update_attr_map(span, pair_attrs)
def set_attribute_label(attribute_key, label_key, label_value_prefix=''): """Add the attribute to the span attribute map. Update the span attribute map (`span['attributes']['attributeMap']`) to include a given resource label. """ if attribute_key not in resource_labels: return pair = {RESOURCE_LABEL % (resource_type, label_key): label_value_prefix + resource_labels[attribute_key] } pair_attrs = Attributes(pair).format_attributes_json()\ .get('attributeMap') _update_attr_map(span, pair_attrs)
def set_monitored_resource_attributes(span): monitored_resource = MonitoredResourceUtil.get_instance() if monitored_resource is not None: resource_labels = monitored_resource.get_resource_labels() for attribute_key, attribute_value in resource_labels.items(): attribute_value = 'aws:' + attribute_value if \ attribute_key == 'region' else attribute_value pair = { RESOURCE_LABEL % (monitored_resource.resource_type, attribute_key): attribute_value } pair_attrs = Attributes(pair) \ .format_attributes_json() \ .get('attributeMap') _update_attr_map(span, pair_attrs)