Skip to content

Commit 41141c9

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 2701445 commit 41141c9

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,22 @@
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+
SpanKind.INTERNAL: "internal",
87+
}
88+
8089
logger = logging.getLogger(__name__)
8190

8291

@@ -226,7 +235,7 @@ def _translate_to_jaeger(spans: Span):
226235
[
227236
_get_long_tag("status.code", status.status_code.value),
228237
_get_string_tag("status.message", status.description),
229-
_get_string_tag("span.kind", span.kind.name),
238+
_get_string_tag("span.kind", OTLP_JAEGER_SPAN_KIND[span.kind]),
230239
]
231240
)
232241

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from opentelemetry.sdk import trace
2626
from opentelemetry.sdk.trace import Resource
2727
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
28+
from opentelemetry.trace import SpanKind
2829
from opentelemetry.trace.status import Status, StatusCode
2930

3031

@@ -151,6 +152,10 @@ def test_nsec_to_usec_round(self):
151152
self.assertEqual(nsec_to_usec_round(5499), 5)
152153
self.assertEqual(nsec_to_usec_round(5500), 6)
153154

155+
def test_all_otlp_span_kinds_are_mapped(self):
156+
for kind in SpanKind:
157+
self.assertIn(kind, jaeger_exporter.OTLP_JAEGER_SPAN_KIND)
158+
154159
# pylint: disable=too-many-locals
155160
def test_translate_to_jaeger(self):
156161
# pylint: disable=invalid-name
@@ -216,9 +221,7 @@ def test_translate_to_jaeger(self):
216221
key="status.message", vType=jaeger.TagType.STRING, vStr=None
217222
),
218223
jaeger.Tag(
219-
key="span.kind",
220-
vType=jaeger.TagType.STRING,
221-
vStr=trace_api.SpanKind.INTERNAL.name,
224+
key="span.kind", vType=jaeger.TagType.STRING, vStr="internal",
222225
),
223226
]
224227

@@ -315,7 +318,7 @@ def test_translate_to_jaeger(self):
315318
jaeger.Tag(
316319
key="span.kind",
317320
vType=jaeger.TagType.STRING,
318-
vStr=trace_api.SpanKind.CLIENT.name,
321+
vStr="client",
319322
),
320323
jaeger.Tag(
321324
key="error", vType=jaeger.TagType.BOOL, vBool=True
@@ -391,7 +394,7 @@ def test_translate_to_jaeger(self):
391394
jaeger.Tag(
392395
key="span.kind",
393396
vType=jaeger.TagType.STRING,
394-
vStr=trace_api.SpanKind.INTERNAL.name,
397+
vStr="internal",
395398
),
396399
jaeger.Tag(
397400
key="otel.instrumentation_library.name",

0 commit comments

Comments
 (0)