Skip to content

Commit

Permalink
sdk: rename resource labels to attributes (#1082)
Browse files Browse the repository at this point in the history
This aligns with the specification for Resources
  • Loading branch information
alrex authored Sep 9, 2020
1 parent 370cc6b commit 50478c2
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _translate_to_jaeger(spans: Span):
parent_id = span.parent.span_id if span.parent else 0

tags = _extract_tags(span.attributes)
tags.extend(_extract_tags(span.resource.labels))
tags.extend(_extract_tags(span.resource.attributes))

tags.extend(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def test_translate_to_jaeger(self):
otel_spans[0].set_attribute("key_float", 111.22)
otel_spans[0].set_attribute("key_tuple", ("tuple_element",))
otel_spans[0].resource = Resource(
labels={"key_resource": "some_resource"}
attributes={"key_resource": "some_resource"}
)
otel_spans[0].set_status(
Status(StatusCanonicalCode.UNKNOWN, "Example description")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,19 @@ def get_collector_point(metric_record: MetricRecord) -> metrics_pb2.Point:


def get_resource(metric_record: MetricRecord) -> resource_pb2.Resource:
resource_labels = metric_record.instrument.meter.resource.labels
resource_attributes = metric_record.instrument.meter.resource.attributes
return resource_pb2.Resource(
type=infer_oc_resource_type(resource_labels),
labels={k: str(v) for k, v in resource_labels.items()},
type=infer_oc_resource_type(resource_attributes),
labels={k: str(v) for k, v in resource_attributes.items()},
)


def infer_oc_resource_type(resource_labels: Dict[str, str]) -> str:
def infer_oc_resource_type(resource_attributes: Dict[str, str]) -> str:
"""Convert from OT resource labels to OC resource type"""
for (
ot_resource_key,
oc_resource_type,
) in _OT_LABEL_PRESENCE_TO_RESOURCE_TYPE:
if ot_resource_key in resource_labels:
if ot_resource_key in resource_attributes:
return oc_resource_type
return ""
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
# Resource can be required for some backends, e.g. Jaeger
# If resource wouldn't be set - traces wouldn't appears in Jaeger
resource = Resource(labels=labels={
resource = Resource(attributes={
"service.name": "service"
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _get_resource_data(

collector_resource = Resource()

for key, value in sdk_resource.labels.items():
for key, value in sdk_resource.attributes.items():

try:
# pylint: disable=no-member
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def _extract_tags_from_dict(tags_dict):
def _extract_tags_from_span(span: Span):
tags = _extract_tags_from_dict(getattr(span, "attributes", None))
if span.resource:
tags.update(_extract_tags_from_dict(span.resource.labels))
tags.update(_extract_tags_from_dict(span.resource.attributes))
return tags


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,14 @@ def test_export(self):

otel_spans[1].start(start_time=start_times[1])
otel_spans[1].resource = Resource(
labels={"key_resource": "some_resource"}
attributes={"key_resource": "some_resource"}
)
otel_spans[1].end(end_time=end_times[1])

otel_spans[2].start(start_time=start_times[2])
otel_spans[2].set_attribute("key_string", "hello_world")
otel_spans[2].resource = Resource(
labels={"key_resource": "some_resource"}
attributes={"key_resource": "some_resource"}
)
otel_spans[2].end(end_time=end_times[2])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ def _common_request( # pylint: disable=too-many-locals
if args:
http_method = args[0]
span.resource = Resource(
labels={
attributes={
"endpoint": endpoint_name,
"http_method": http_method.lower(),
}
)
else:
span.resource = Resource(labels={"endpoint": endpoint_name})
span.resource = Resource(
attributes={"endpoint": endpoint_name}
)

add_span_arg_tags(
span, endpoint_name, args, args_name, traced_args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_ec2_client(self):
self.assertEqual(
span.resource,
Resource(
labels={"endpoint": "ec2", "http_method": "runinstances"}
attributes={"endpoint": "ec2", "http_method": "runinstances"}
),
)
self.assertEqual(span.attributes["http.method"], "POST")
Expand Down Expand Up @@ -131,7 +131,7 @@ def test_s3_client(self):
assert_span_http_status_code(span, 200)
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "s3", "http_method": "head"}),
Resource(attributes={"endpoint": "s3", "http_method": "head"}),
)
self.assertEqual(span.attributes["http.method"], "HEAD")
self.assertEqual(span.attributes["aws.operation"], "head_bucket")
Expand All @@ -146,7 +146,7 @@ def test_s3_client(self):
span = spans[2]
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "s3", "http_method": "head"}),
Resource(attributes={"endpoint": "s3", "http_method": "head"}),
)

@mock_s3_deprecated
Expand All @@ -166,21 +166,21 @@ def test_s3_put(self):
assert_span_http_status_code(spans[0], 200)
self.assertEqual(
spans[0].resource,
Resource(labels={"endpoint": "s3", "http_method": "put"}),
Resource(attributes={"endpoint": "s3", "http_method": "put"}),
)
# get bucket
self.assertEqual(spans[1].attributes["aws.operation"], "head_bucket")
self.assertEqual(
spans[1].resource,
Resource(labels={"endpoint": "s3", "http_method": "head"}),
Resource(attributes={"endpoint": "s3", "http_method": "head"}),
)
# put object
self.assertEqual(
spans[2].attributes["aws.operation"], "_send_file_internal"
)
self.assertEqual(
spans[2].resource,
Resource(labels={"endpoint": "s3", "http_method": "put"}),
Resource(attributes={"endpoint": "s3", "http_method": "put"}),
)

@mock_lambda_deprecated
Expand Down Expand Up @@ -223,7 +223,7 @@ def test_lambda_client(self):
assert_span_http_status_code(span, 200)
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "lambda", "http_method": "get"}),
Resource(attributes={"endpoint": "lambda", "http_method": "get"}),
)
self.assertEqual(span.attributes["http.method"], "GET")
self.assertEqual(span.attributes["aws.region"], "us-east-2")
Expand All @@ -241,7 +241,10 @@ def test_sts_client(self):
self.assertEqual(
span.resource,
Resource(
labels={"endpoint": "sts", "http_method": "getfederationtoken"}
attributes={
"endpoint": "sts",
"http_method": "getfederationtoken",
}
),
)
self.assertEqual(span.attributes["aws.region"], "us-west-2")
Expand All @@ -268,6 +271,6 @@ def test_elasticache_client(self):
assert spans
span = spans[0]
self.assertEqual(
span.resource, Resource(labels={"endpoint": "elasticcache"})
span.resource, Resource(attributes={"endpoint": "elasticcache"})
)
self.assertEqual(span.attributes["aws.region"], "us-west-2")
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,16 @@ def _patched_api_call(self, original_func, instance, args, kwargs):
if args:
operation = args[0]
span.resource = Resource(
labels={
attributes={
"endpoint": endpoint_name,
"operation": operation.lower(),
}
)

else:
span.resource = Resource(labels={"endpoint": endpoint_name})
span.resource = Resource(
attributes={"endpoint": endpoint_name}
)

add_span_arg_tags(
span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def test_traced_client(self):
self.assertEqual(
span.resource,
Resource(
labels={"endpoint": "ec2", "operation": "describeinstances"}
attributes={
"endpoint": "ec2",
"operation": "describeinstances",
}
),
)
self.assertEqual(span.name, "ec2.command")
Expand Down Expand Up @@ -81,7 +84,9 @@ def test_s3_client(self):
assert_span_http_status_code(span, 200)
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "s3", "operation": "listbuckets"}),
Resource(
attributes={"endpoint": "s3", "operation": "listbuckets"}
),
)

# testing for span error
Expand All @@ -93,7 +98,9 @@ def test_s3_client(self):
span = spans[2]
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "s3", "operation": "listobjects"}),
Resource(
attributes={"endpoint": "s3", "operation": "listobjects"}
),
)

@mock_s3
Expand All @@ -111,12 +118,14 @@ def test_s3_put(self):
assert_span_http_status_code(span, 200)
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "s3", "operation": "createbucket"}),
Resource(
attributes={"endpoint": "s3", "operation": "createbucket"}
),
)
self.assertEqual(spans[1].attributes["aws.operation"], "PutObject")
self.assertEqual(
spans[1].resource,
Resource(labels={"endpoint": "s3", "operation": "putobject"}),
Resource(attributes={"endpoint": "s3", "operation": "putobject"}),
)
self.assertEqual(spans[1].attributes["params.Key"], str(params["Key"]))
self.assertEqual(
Expand All @@ -139,7 +148,9 @@ def test_sqs_client(self):
assert_span_http_status_code(span, 200)
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "sqs", "operation": "listqueues"}),
Resource(
attributes={"endpoint": "sqs", "operation": "listqueues"}
),
)

@mock_kinesis
Expand All @@ -160,7 +171,7 @@ def test_kinesis_client(self):
self.assertEqual(
span.resource,
Resource(
labels={"endpoint": "kinesis", "operation": "liststreams"}
attributes={"endpoint": "kinesis", "operation": "liststreams"}
),
)

Expand Down Expand Up @@ -205,7 +216,7 @@ def test_lambda_client(self):
self.assertEqual(
span.resource,
Resource(
labels={"endpoint": "lambda", "operation": "listfunctions"}
attributes={"endpoint": "lambda", "operation": "listfunctions"}
),
)

Expand All @@ -224,7 +235,7 @@ def test_kms_client(self):
assert_span_http_status_code(span, 200)
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "kms", "operation": "listkeys"}),
Resource(attributes={"endpoint": "kms", "operation": "listkeys"}),
)

# checking for protection on sts against security leak
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
([#1062](https://github.com/open-telemetry/opentelemetry-python/pull/1062))
- Populate resource attributes as per semantic conventions
([#1053](https://github.com/open-telemetry/opentelemetry-python/pull/1053))
- Rename Resource labels to attributes
([#1082](https://github.com/open-telemetry/opentelemetry-python/pull/1082))

## Version 0.12b0

Expand Down
30 changes: 15 additions & 15 deletions opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pkg_resources

LabelValue = typing.Union[str, bool, int, float]
Labels = typing.Dict[str, LabelValue]
Attributes = typing.Dict[str, LabelValue]
logger = logging.getLogger(__name__)


Expand All @@ -36,38 +36,38 @@


class Resource:
def __init__(self, labels: Labels):
self._labels = labels.copy()
def __init__(self, attributes: Attributes):
self._attributes = attributes.copy()

@staticmethod
def create(labels: Labels) -> "Resource":
if not labels:
def create(attributes: Attributes) -> "Resource":
if not attributes:
return _DEFAULT_RESOURCE
return _DEFAULT_RESOURCE.merge(Resource(labels))
return _DEFAULT_RESOURCE.merge(Resource(attributes))

@staticmethod
def create_empty() -> "Resource":
return _EMPTY_RESOURCE

@property
def labels(self) -> Labels:
return self._labels.copy()
def attributes(self) -> Attributes:
return self._attributes.copy()

def merge(self, other: "Resource") -> "Resource":
merged_labels = self.labels
merged_attributes = self.attributes
# pylint: disable=protected-access
for key, value in other._labels.items():
if key not in merged_labels or merged_labels[key] == "":
merged_labels[key] = value
return Resource(merged_labels)
for key, value in other._attributes.items():
if key not in merged_attributes or merged_attributes[key] == "":
merged_attributes[key] = value
return Resource(merged_attributes)

def __eq__(self, other: object) -> bool:
if not isinstance(other, Resource):
return False
return self._labels == other._labels
return self._attributes == other._attributes

def __hash__(self):
return hash(dumps(self._labels, sort_keys=True))
return hash(dumps(self._attributes, sort_keys=True))


_EMPTY_RESOURCE = Resource({})
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ def to_json(self, indent=4):
f_span["attributes"] = self._format_attributes(self.attributes)
f_span["events"] = self._format_events(self.events)
f_span["links"] = self._format_links(self.links)
f_span["resource"] = self.resource.labels
f_span["resource"] = self.resource.attributes

return json.dumps(f_span, indent=indent)

Expand Down
Loading

0 comments on commit 50478c2

Please sign in to comment.