Skip to content

Commit cf2b08d

Browse files
committed
Fix Jaeger exporter to correctly translate span.kind attribute
According to opentracing semantic conventions, span.kind attribute should be one of the following four values: "server", "client", "producer", "consumer". The spec does not mention the casing of the strings but all examples show them in lower case. Moreoever, the OpenTelemetry collector recognizes these values only when they are lower case. Today if opentelemetry-python exports a span using the jaeger exporter to the OpenTelemetry collector, it loses the span.kind information. Using the debug logger exporter in the collector prints the following to the screen: Span #0 Trace ID : 31789e02d79452eaedb72769bed5fac7 Parent ID : a6398bc507d8a982 ID : 4a00eaa815e69785 Name : span Kind : SPAN_KIND_UNSPECIFIED Start time : 2020-11-03 18:06:29.684287 +0530 IST End time : 2020-11-03 18:06:29.684344 +0530 IST Status code : STATUS_CODE_CANCELLED Status message : Span #1 Trace ID : 31789e02d79452eaedb72769bed5fac7 Parent ID : ID : a6398bc507d8a982 Name : HelloWorldResource.on_get Kind : SPAN_KIND_UNSPECIFIED Start time : 2020-11-03 18:06:29.683881 +0530 IST End time : 2020-11-03 18:06:29.684421 +0530 IST Status code : STATUS_CODE_CANCELLED Status message : After the patch, span kind is correctly understood and represented by the collector: Span #0 Trace ID : 8e4aeaa621f7e67c813a9cf26c56a202 Parent ID : 13f4cc8db8dfe5b3 ID : e8032193bb9dca98 Name : span Kind : SPAN_KIND_INTERNAL Start time : 2020-11-03 18:03:39.964796 +0530 IST End time : 2020-11-03 18:03:39.964857 +0530 IST Status code : STATUS_CODE_CANCELLED Status message : Span #1 Trace ID : 8e4aeaa621f7e67c813a9cf26c56a202 Parent ID : ID : 13f4cc8db8dfe5b3 Name : HelloWorldResource.on_get Kind : SPAN_KIND_SERVER Start time : 2020-11-03 18:03:39.964335 +0530 IST End time : 2020-11-03 18:03:39.964936 +0530 IST Status code : STATUS_CODE_CANCELLED Status message :
1 parent f050819 commit cf2b08d

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,21 @@
7070
from opentelemetry.exporter.jaeger.gen.agent import Agent as agent
7171
from opentelemetry.exporter.jaeger.gen.jaeger import Collector as jaeger
7272
from opentelemetry.sdk.trace.export import Span, SpanExporter, SpanExportResult
73+
from opentelemetry.trace import SpanKind
7374
from opentelemetry.trace.status import StatusCode
7475

7576
DEFAULT_AGENT_HOST_NAME = "localhost"
7677
DEFAULT_AGENT_PORT = 6831
7778

7879
UDP_PACKET_MAX_LENGTH = 65000
7980

81+
OTLP_JAEGER_SPAN_KIND = {
82+
SpanKind.CLIENT: "client",
83+
SpanKind.SERVER: "server",
84+
SpanKind.CONSUMER: "consumer",
85+
SpanKind.PRODUCER: "producer",
86+
}
87+
8088
logger = logging.getLogger(__name__)
8189

8290

@@ -226,7 +234,7 @@ def _translate_to_jaeger(spans: Span):
226234
[
227235
_get_long_tag("status.code", status.status_code.value),
228236
_get_string_tag("status.message", status.description),
229-
_get_string_tag("span.kind", span.kind.name),
237+
_get_string_tag("span.kind", _otlp_to_jaeger_span_kind(span.kind)),
230238
]
231239
)
232240

@@ -299,6 +307,13 @@ def _convert_int_to_i64(val):
299307
return val
300308

301309

310+
def _otlp_to_jaeger_span_kind(kind: SpanKind) -> str:
311+
jaeger_kind = OTLP_JAEGER_SPAN_KIND.get(kind, "")
312+
if not jaeger_kind:
313+
jaeger_kind = kind.name.lower()
314+
return jaeger_kind
315+
316+
302317
def _get_trace_id_low(trace_id):
303318
return _convert_int_to_i64(trace_id & 0xFFFFFFFFFFFFFFFF)
304319

exporter/opentelemetry-exporter-jaeger/tests/test_jaeger_exporter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def test_translate_to_jaeger(self):
218218
jaeger.Tag(
219219
key="span.kind",
220220
vType=jaeger.TagType.STRING,
221-
vStr=trace_api.SpanKind.INTERNAL.name,
221+
vStr="internal",
222222
),
223223
]
224224

@@ -315,7 +315,7 @@ def test_translate_to_jaeger(self):
315315
jaeger.Tag(
316316
key="span.kind",
317317
vType=jaeger.TagType.STRING,
318-
vStr=trace_api.SpanKind.CLIENT.name,
318+
vStr="client",
319319
),
320320
jaeger.Tag(
321321
key="error", vType=jaeger.TagType.BOOL, vBool=True
@@ -391,7 +391,7 @@ def test_translate_to_jaeger(self):
391391
jaeger.Tag(
392392
key="span.kind",
393393
vType=jaeger.TagType.STRING,
394-
vStr=trace_api.SpanKind.INTERNAL.name,
394+
vStr="internal",
395395
),
396396
jaeger.Tag(
397397
key="otel.instrumentation_library.name",

0 commit comments

Comments
 (0)