Skip to content

Update zipkin exporter status code and error tag #1486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Dec 23, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +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

TRANSPORT_FORMAT_JSON = "json"
TRANSPORT_FORMAT_PROTOBUF = "protobuf"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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,
},
{
Expand All @@ -289,7 +286,6 @@ def test_export_json(self):
"tags": {
"key_string": "hello_world",
"key_resource": "some_resource",
"otel.status_code": "1",
},
"annotations": None,
},
Expand All @@ -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,
},
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -737,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])
Expand Down Expand Up @@ -775,8 +771,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(
Expand Down Expand Up @@ -809,7 +805,7 @@ def test_export_protobuf(self):
kind=span_kind,
tags={
"key_resource": "some_resource",
"otel.status_code": "1",
"otel.status_code": "OK",
},
),
zipkin_pb2.Span(
Expand All @@ -825,7 +821,6 @@ def test_export_protobuf(self):
tags={
"key_string": "hello_world",
"key_resource": "some_resource",
"otel.status_code": "1",
},
),
zipkin_pb2.Span(
Expand All @@ -841,7 +836,6 @@ def test_export_protobuf(self):
tags={
"otel.instrumentation_library.name": "name",
"otel.instrumentation_library.version": "version",
"otel.status_code": "1",
},
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]