Skip to content

Commit 4405774

Browse files
committed
add span type
1 parent 3d9267d commit 4405774

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

ext/opentelemetry-ext-datadog/src/opentelemetry/ext/datadog/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
log = logging.getLogger(__name__)
1515

1616
DEFAULT_AGENT_URL = "http://localhost:8126"
17+
DATADOG_SPAN_TYPES = (
18+
"sql",
19+
"mongodb",
20+
"http",
21+
)
1722

1823

1924
class DatadogSpanExporter(SpanExporter):
@@ -73,6 +78,7 @@ def _translate_to_datadog(self, spans):
7378
span.name,
7479
service=self.service,
7580
resource=_get_resource(span),
81+
span_type=_get_span_type(span),
7682
trace_id=trace_id,
7783
span_id=span_id,
7884
parent_id=parent_id,
@@ -129,3 +135,13 @@ def _get_resource(span):
129135
)
130136

131137
return span.attributes.get("component", span.name)
138+
139+
140+
def _get_span_type(span):
141+
# use db.type for database integrations (sql, mongodb) otherwise component
142+
span_type = span.attributes.get(
143+
"db.type", span.attributes.get("component")
144+
)
145+
span_type = span_type if span_type in DATADOG_SPAN_TYPES else None
146+
147+
return span_type

ext/opentelemetry-ext-datadog/tests/test_datadog_exporter.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,37 @@ def test_resources(self):
223223
self.assertEqual(len(datadog_spans), 1)
224224
span = datadog_spans[0].to_dict()
225225
self.assertEqual(span["resource"], resources[index])
226+
227+
def test_span_types(self):
228+
span_types = [None, "http", "sql", "mongodb"]
229+
attributes = [
230+
{"component": "custom"},
231+
{"component": "http"},
232+
{"db.type": "sql"},
233+
{"db.type": "mongodb"},
234+
]
235+
236+
with self.tracer.start_span("foo", attributes=attributes[0]):
237+
pass
238+
239+
with self.tracer.start_span("bar", attributes=attributes[1]):
240+
pass
241+
242+
with self.tracer.start_span("xxx", attributes=attributes[2]):
243+
pass
244+
245+
with self.tracer.start_span("yyy", attributes=attributes[3]):
246+
pass
247+
248+
self.assertEqual(self.agent_writer_mock.write.call_count, 4)
249+
250+
for index, call_args in enumerate(
251+
self.agent_writer_mock.write.call_args_list
252+
):
253+
datadog_spans = call_args.kwargs["spans"]
254+
self.assertEqual(len(datadog_spans), 1)
255+
span = datadog_spans[0].to_dict()
256+
if span_types[index]:
257+
self.assertEqual(span["type"], span_types[index])
258+
else:
259+
self.assertTrue("type" not in span)

0 commit comments

Comments
 (0)