From cb44c0cc4ae9a43ab96b9d98933fa6edc6a39c75 Mon Sep 17 00:00:00 2001 From: nirsky Date: Tue, 19 May 2020 13:00:38 +0300 Subject: [PATCH 1/4] ext/zipkin - Transform resource to tags when exporting --- .../src/opentelemetry/ext/zipkin/__init__.py | 19 ++++++++--- .../tests/test_zipkin_exporter.py | 34 +++++++++++++++++-- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/ext/opentelemetry-ext-zipkin/src/opentelemetry/ext/zipkin/__init__.py b/ext/opentelemetry-ext-zipkin/src/opentelemetry/ext/zipkin/__init__.py index 8b4aa93c720..58f863697c1 100644 --- a/ext/opentelemetry-ext-zipkin/src/opentelemetry/ext/zipkin/__init__.py +++ b/ext/opentelemetry-ext-zipkin/src/opentelemetry/ext/zipkin/__init__.py @@ -175,7 +175,7 @@ def _translate_to_zipkin(self, spans: Sequence[Span]): "duration": duration_mus, "localEndpoint": local_endpoint, "kind": SPAN_KIND_MAP[span.kind], - "tags": _extract_tags_from_span(span.attributes), + "tags": _extract_tags_from_span(span), "annotations": _extract_annotations_from_events(span.events), } @@ -196,11 +196,11 @@ def shutdown(self) -> None: pass -def _extract_tags_from_span(attr): - if not attr: +def _extract_tags_from_dict(tags_dict): + if not tags_dict: return None tags = {} - for attribute_key, attribute_value in attr.items(): + for attribute_key, attribute_value in tags_dict.items(): if isinstance(attribute_value, (int, bool, float)): value = str(attribute_value) elif isinstance(attribute_value, str): @@ -212,6 +212,17 @@ def _extract_tags_from_span(attr): return tags +def _extract_tags_from_span(span: Span): + tags = _extract_tags_from_dict(span.attributes) + if span.resource: + resource_tags = _extract_tags_from_dict(span.resource.labels) + if tags: + tags.update(resource_tags) + else: + tags = resource_tags + return tags + + def _extract_annotations_from_events(events): return ( [ diff --git a/ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py b/ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py index cd21839e3e8..61088698a32 100644 --- a/ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py +++ b/ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py @@ -19,6 +19,7 @@ from opentelemetry import trace as trace_api from opentelemetry.ext.zipkin import ZipkinSpanExporter from opentelemetry.sdk import trace +from opentelemetry.sdk.trace import Resource from opentelemetry.sdk.trace.export import SpanExportResult from opentelemetry.trace import TraceFlags @@ -95,7 +96,7 @@ def test_constructor_explicit(self): # pylint: disable=too-many-locals def test_export(self): - span_names = ("test1", "test2", "test3") + span_names = ("test1", "test2", "test3", "test4") trace_id = 0x6E0C63257DE34C926F9EFCD03927272E span_id = 0x34BF92DEEFC58C92 parent_id = 0x1111111111111111 @@ -106,12 +107,14 @@ def test_export(self): base_time, base_time + 150 * 10 ** 6, base_time + 300 * 10 ** 6, + base_time + 400 * 10 ** 6, ) - durations = (50 * 10 ** 6, 100 * 10 ** 6, 200 * 10 ** 6) + durations = (50 * 10 ** 6, 100 * 10 ** 6, 200 * 10 ** 6, 300 * 10 ** 6) end_times = ( start_times[0] + durations[0], start_times[1] + durations[1], start_times[2] + durations[2], + start_times[3] + durations[3], ) span_context = trace_api.SpanContext( @@ -158,6 +161,7 @@ def test_export(self): name=span_names[1], context=parent_context, parent=None ), trace.Span(name=span_names[2], context=other_context, parent=None), + trace.Span(name=span_names[3], context=other_context, parent=None), ] otel_spans[0].start(start_time=start_times[0]) @@ -168,11 +172,21 @@ def test_export(self): otel_spans[0].end(end_time=end_times[0]) otel_spans[1].start(start_time=start_times[1]) + otel_spans[1].resource = Resource( + labels={"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"} + ) otel_spans[2].end(end_time=end_times[2]) + otel_spans[3].start(start_time=start_times[3]) + otel_spans[3].end(end_time=end_times[3]) + service_name = "test-service" local_endpoint = {"serviceName": service_name, "port": 9411} @@ -208,7 +222,7 @@ def test_export(self): "duration": durations[1] // 10 ** 3, "localEndpoint": local_endpoint, "kind": None, - "tags": None, + "tags": {"key_resource": "some_resource",}, "annotations": None, }, { @@ -219,6 +233,20 @@ def test_export(self): "duration": durations[2] // 10 ** 3, "localEndpoint": local_endpoint, "kind": None, + "tags": { + "key_string": "hello_world", + "key_resource": "some_resource", + }, + "annotations": None, + }, + { + "traceId": format(trace_id, "x"), + "id": format(other_id, "x"), + "name": span_names[3], + "timestamp": start_times[3] // 10 ** 3, + "duration": durations[3] // 10 ** 3, + "localEndpoint": local_endpoint, + "kind": None, "tags": None, "annotations": None, }, From 285121ee2ba83fc47eba6d23f64d5964ff339db2 Mon Sep 17 00:00:00 2001 From: nirsky Date: Tue, 19 May 2020 13:30:35 +0300 Subject: [PATCH 2/4] lint fix --- ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py b/ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py index 61088698a32..d415d026635 100644 --- a/ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py +++ b/ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py @@ -222,7 +222,7 @@ def test_export(self): "duration": durations[1] // 10 ** 3, "localEndpoint": local_endpoint, "kind": None, - "tags": {"key_resource": "some_resource",}, + "tags": {"key_resource": "some_resource"}, "annotations": None, }, { From b505e0d4292984161687b4b8cad546f648ee8388 Mon Sep 17 00:00:00 2001 From: nirsky Date: Wed, 20 May 2020 13:04:07 +0300 Subject: [PATCH 3/4] CR --- .../src/opentelemetry/ext/zipkin/__init__.py | 10 +++------- .../tests/test_zipkin_exporter.py | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/ext/opentelemetry-ext-zipkin/src/opentelemetry/ext/zipkin/__init__.py b/ext/opentelemetry-ext-zipkin/src/opentelemetry/ext/zipkin/__init__.py index 58f863697c1..8221f93691f 100644 --- a/ext/opentelemetry-ext-zipkin/src/opentelemetry/ext/zipkin/__init__.py +++ b/ext/opentelemetry-ext-zipkin/src/opentelemetry/ext/zipkin/__init__.py @@ -197,9 +197,9 @@ def shutdown(self) -> None: def _extract_tags_from_dict(tags_dict): - if not tags_dict: - return None tags = {} + if not tags_dict: + return tags for attribute_key, attribute_value in tags_dict.items(): if isinstance(attribute_value, (int, bool, float)): value = str(attribute_value) @@ -215,11 +215,7 @@ def _extract_tags_from_dict(tags_dict): def _extract_tags_from_span(span: Span): tags = _extract_tags_from_dict(span.attributes) if span.resource: - resource_tags = _extract_tags_from_dict(span.resource.labels) - if tags: - tags.update(resource_tags) - else: - tags = resource_tags + tags.update(_extract_tags_from_dict(span.resource.labels)) return tags diff --git a/ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py b/ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py index d415d026635..1f2d53d3048 100644 --- a/ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py +++ b/ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py @@ -247,7 +247,7 @@ def test_export(self): "duration": durations[3] // 10 ** 3, "localEndpoint": local_endpoint, "kind": None, - "tags": None, + "tags": {}, "annotations": None, }, ] From 6c861cf22fc59cccbdf6eab05c3c4402b1f9fcae Mon Sep 17 00:00:00 2001 From: nirsky Date: Thu, 21 May 2020 10:34:20 +0300 Subject: [PATCH 4/4] Access span.attributes using getattr --- .../src/opentelemetry/ext/zipkin/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/opentelemetry-ext-zipkin/src/opentelemetry/ext/zipkin/__init__.py b/ext/opentelemetry-ext-zipkin/src/opentelemetry/ext/zipkin/__init__.py index 8221f93691f..8a487290ce1 100644 --- a/ext/opentelemetry-ext-zipkin/src/opentelemetry/ext/zipkin/__init__.py +++ b/ext/opentelemetry-ext-zipkin/src/opentelemetry/ext/zipkin/__init__.py @@ -213,7 +213,7 @@ def _extract_tags_from_dict(tags_dict): def _extract_tags_from_span(span: Span): - tags = _extract_tags_from_dict(span.attributes) + tags = _extract_tags_from_dict(getattr(span, "attributes", None)) if span.resource: tags.update(_extract_tags_from_dict(span.resource.labels)) return tags