From 81769739e752bb53066cca68eaf4597831636abc Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Wed, 16 Dec 2020 19:58:02 +0530 Subject: [PATCH 1/7] Update zipkin status code to error tag --- .../opentelemetry/exporter/zipkin/__init__.py | 28 +++++++++++-------- .../tests/test_zipkin_exporter.py | 24 +++++----------- .../opentelemetry/instrumentation/utils.py | 10 +++++++ 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py b/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py index c6d3559b905..5dc25d80072 100644 --- a/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py +++ b/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py @@ -82,6 +82,8 @@ from opentelemetry.exporter.zipkin.gen import zipkin_pb2 from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult from opentelemetry.trace import Span, SpanContext, SpanKind +from opentelemetry.trace.status import StatusCode +from opentelemetry.instrumentation.utils import status_code_to_str TRANSPORT_FORMAT_JSON = "json" TRANSPORT_FORMAT_PROTOBUF = "protobuf" @@ -237,14 +239,14 @@ def _translate_to_json(self, spans: Sequence[Span]): "otel.instrumentation_library.version" ] = span.instrumentation_info.version - if span.status is not None: - zipkin_span["tags"]["otel.status_code"] = str( - span.status.status_code.value + if span.status.status_code is not StatusCode.UNSET: + zipkin_span["tags"]["otel.status_code"] = status_code_to_str( + span.status.status_code ) - if span.status.description is not None: - zipkin_span["tags"][ - "otel.status_description" - ] = span.status.description + if span.status.status_code is StatusCode.ERROR: + zipkin_span["tags"]["error"] = ( + span.status.description or "" + ) if context.trace_flags.sampled: zipkin_span["debug"] = True @@ -317,13 +319,17 @@ def _translate_to_protobuf(self, spans: Sequence[Span]): } ) - if span.status is not None: + if span.status.status_code is not StatusCode.UNSET: pbuf_span.tags.update( - {"otel.status_code": str(span.status.status_code.value)} + { + "otel.status_code": status_code_to_str( + span.status.status_code + ) + } ) - if span.status.description is not None: + if span.status.status_code is StatusCode.ERROR: pbuf_span.tags.update( - {"otel.status_description": span.status.description} + {"error": span.status.description or ""} ) if context.trace_flags.sampled: diff --git a/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py b/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py index a21199659b8..e4ce874d60c 100644 --- a/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py +++ b/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py @@ -246,8 +246,8 @@ def test_export_json(self): "key_bool": "False", "key_string": "hello_world", "key_float": "111.22", - "otel.status_code": "2", - "otel.status_description": "Example description", + "otel.status_code": "ERROR", + "error": "Example description", }, "debug": True, "parentId": format(parent_id, "x"), @@ -272,10 +272,7 @@ def test_export_json(self): "duration": durations[1] // 10 ** 3, "localEndpoint": local_endpoint, "kind": span_kind, - "tags": { - "key_resource": "some_resource", - "otel.status_code": "1", - }, + "tags": {"key_resource": "some_resource",}, "annotations": None, }, { @@ -289,7 +286,6 @@ def test_export_json(self): "tags": { "key_string": "hello_world", "key_resource": "some_resource", - "otel.status_code": "1", }, "annotations": None, }, @@ -304,7 +300,6 @@ def test_export_json(self): "tags": { "otel.instrumentation_library.name": "name", "otel.instrumentation_library.version": "version", - "otel.status_code": "1", }, "annotations": None, }, @@ -383,7 +378,7 @@ def test_export_json_zero_padding(self): "duration": duration // 10 ** 3, "localEndpoint": local_endpoint, "kind": SPAN_KIND_MAP_JSON[SpanKind.INTERNAL], - "tags": {"otel.status_code": "1"}, + "tags": {}, "annotations": None, "debug": True, "parentId": "0aaaaaaaaaaaaaaa", @@ -775,8 +770,8 @@ def test_export_protobuf(self): "key_bool": "False", "key_string": "hello_world", "key_float": "111.22", - "otel.status_code": "2", - "otel.status_description": "Example description", + "otel.status_code": "ERROR", + "error": "Example description", }, debug=True, parent_id=ZipkinSpanExporter.format_pbuf_span_id( @@ -807,10 +802,7 @@ def test_export_protobuf(self): duration=nsec_to_usec_round(durations[1]), local_endpoint=local_endpoint, kind=span_kind, - tags={ - "key_resource": "some_resource", - "otel.status_code": "1", - }, + tags={"key_resource": "some_resource",}, ), zipkin_pb2.Span( trace_id=trace_id.to_bytes( @@ -825,7 +817,6 @@ def test_export_protobuf(self): tags={ "key_string": "hello_world", "key_resource": "some_resource", - "otel.status_code": "1", }, ), zipkin_pb2.Span( @@ -841,7 +832,6 @@ def test_export_protobuf(self): tags={ "otel.instrumentation_library.name": "name", "otel.instrumentation_library.version": "version", - "otel.status_code": "1", }, ), ], diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py index 22ba7a20572..cc3fe3b7dca 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py @@ -18,6 +18,12 @@ from opentelemetry.trace.status import StatusCode +CODE_TO_STR = { + StatusCode.UNSET: "UNSET", + StatusCode.OK: "OK", + StatusCode.ERROR: "ERROR", +} + def extract_attributes_from_object( obj: any, attributes: Sequence[str], existing: Dict[str, str] = None @@ -81,3 +87,7 @@ def unwrap(obj, attr: str): func = getattr(obj, attr, None) if func and isinstance(func, ObjectProxy) and hasattr(func, "__wrapped__"): setattr(obj, attr, func.__wrapped__) + + +def status_code_to_str(code: StatusCode) -> str: + return CODE_TO_STR[code] From eed4f16bf5103fb425aa499bf1d65142095c5da8 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Wed, 16 Dec 2020 20:03:20 +0530 Subject: [PATCH 2/7] Fix lint --- .../src/opentelemetry/exporter/zipkin/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py b/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py index 5dc25d80072..d170ed86739 100644 --- a/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py +++ b/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py @@ -80,10 +80,10 @@ from opentelemetry.configuration import Configuration from opentelemetry.exporter.zipkin.gen import zipkin_pb2 +from opentelemetry.instrumentation.utils import status_code_to_str from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult from opentelemetry.trace import Span, SpanContext, SpanKind from opentelemetry.trace.status import StatusCode -from opentelemetry.instrumentation.utils import status_code_to_str TRANSPORT_FORMAT_JSON = "json" TRANSPORT_FORMAT_PROTOBUF = "protobuf" From 11065b38fbb378bd88ec0017920556f357bde724 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Wed, 16 Dec 2020 20:14:25 +0530 Subject: [PATCH 3/7] Update test to check OK code --- .../tests/test_zipkin_exporter.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py b/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py index e4ce874d60c..8aa3b748169 100644 --- a/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py +++ b/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py @@ -732,6 +732,7 @@ def test_export_protobuf(self): otel_spans[1].resource = Resource( attributes={"key_resource": "some_resource"} ) + otel_spans[1].set_status(Status(StatusCode.OK)) otel_spans[1].end(end_time=end_times[1]) otel_spans[2].start(start_time=start_times[2]) @@ -802,7 +803,10 @@ def test_export_protobuf(self): duration=nsec_to_usec_round(durations[1]), local_endpoint=local_endpoint, kind=span_kind, - tags={"key_resource": "some_resource",}, + tags={ + "key_resource": "some_resource", + "otel.status_code": "OK", + }, ), zipkin_pb2.Span( trace_id=trace_id.to_bytes( From 5f4921b636793eac9ea17095f35649f8baf345ea Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Wed, 16 Dec 2020 20:16:28 +0530 Subject: [PATCH 4/7] Fix lint --- .../opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py b/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py index 8aa3b748169..eb3f4894c12 100644 --- a/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py +++ b/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py @@ -272,7 +272,7 @@ def test_export_json(self): "duration": durations[1] // 10 ** 3, "localEndpoint": local_endpoint, "kind": span_kind, - "tags": {"key_resource": "some_resource",}, + "tags": {"key_resource": "some_resource"}, "annotations": None, }, { From 032478fc63b7a1de5346f026f51d0acd92a6e223 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Wed, 16 Dec 2020 22:22:09 +0530 Subject: [PATCH 5/7] Use .name for status code --- .../src/opentelemetry/exporter/zipkin/__init__.py | 13 ++++--------- .../src/opentelemetry/instrumentation/utils.py | 10 ---------- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py b/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py index d170ed86739..6b376c203c5 100644 --- a/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py +++ b/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py @@ -80,7 +80,6 @@ from opentelemetry.configuration import Configuration from opentelemetry.exporter.zipkin.gen import zipkin_pb2 -from opentelemetry.instrumentation.utils import status_code_to_str from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult from opentelemetry.trace import Span, SpanContext, SpanKind from opentelemetry.trace.status import StatusCode @@ -240,9 +239,9 @@ def _translate_to_json(self, spans: Sequence[Span]): ] = span.instrumentation_info.version if span.status.status_code is not StatusCode.UNSET: - zipkin_span["tags"]["otel.status_code"] = status_code_to_str( - span.status.status_code - ) + zipkin_span["tags"][ + "otel.status_code" + ] = span.status.status_code.name if span.status.status_code is StatusCode.ERROR: zipkin_span["tags"]["error"] = ( span.status.description or "" @@ -321,11 +320,7 @@ def _translate_to_protobuf(self, spans: Sequence[Span]): if span.status.status_code is not StatusCode.UNSET: pbuf_span.tags.update( - { - "otel.status_code": status_code_to_str( - span.status.status_code - ) - } + {"otel.status_code": span.status.status_code.name} ) if span.status.status_code is StatusCode.ERROR: pbuf_span.tags.update( diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py index cc3fe3b7dca..22ba7a20572 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py @@ -18,12 +18,6 @@ from opentelemetry.trace.status import StatusCode -CODE_TO_STR = { - StatusCode.UNSET: "UNSET", - StatusCode.OK: "OK", - StatusCode.ERROR: "ERROR", -} - def extract_attributes_from_object( obj: any, attributes: Sequence[str], existing: Dict[str, str] = None @@ -87,7 +81,3 @@ def unwrap(obj, attr: str): func = getattr(obj, attr, None) if func and isinstance(func, ObjectProxy) and hasattr(func, "__wrapped__"): setattr(obj, attr, func.__wrapped__) - - -def status_code_to_str(code: StatusCode) -> str: - return CODE_TO_STR[code] From 21f537d01c94cf8552a7e20c8ab756546496acf8 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Thu, 17 Dec 2020 09:06:56 +0530 Subject: [PATCH 6/7] Add CHANGELOG entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dad442d2522..ff3496d7732 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1420](https://github.com/open-telemetry/opentelemetry-python/pull/1420)) - `opentelemetry-exporter-zipkin` Add support for array attributes in Span and Resource exports ([#1285](https://github.com/open-telemetry/opentelemetry-python/pull/1285)) +- `opentelemetry-exporter-zipkin` Update zipkin exporter status code and error tag + ([#1486](https://github.com/open-telemetry/opentelemetry-python/pull/1486)) ## [0.16b1](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v0.16b1) - 2020-11-26 ### Added From 160ec1e4c5f8de473c87dc782f856bfc835a1ffb Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Thu, 17 Dec 2020 09:10:07 +0530 Subject: [PATCH 7/7] Move changelog entry to changed --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff3496d7732..caf1f236483 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,7 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1420](https://github.com/open-telemetry/opentelemetry-python/pull/1420)) - `opentelemetry-exporter-zipkin` Add support for array attributes in Span and Resource exports ([#1285](https://github.com/open-telemetry/opentelemetry-python/pull/1285)) -- `opentelemetry-exporter-zipkin` Update zipkin exporter status code and error tag +### Changed +- `opentelemetry-exporter-zipkin` Updated zipkin exporter status code and error tag ([#1486](https://github.com/open-telemetry/opentelemetry-python/pull/1486)) ## [0.16b1](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v0.16b1) - 2020-11-26