Skip to content

Commit

Permalink
add span type
Browse files Browse the repository at this point in the history
  • Loading branch information
majorgreys committed Apr 15, 2020
1 parent 3d9267d commit 7664e6d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
log = logging.getLogger(__name__)

DEFAULT_AGENT_URL = "http://localhost:8126"
DATADOG_SPAN_TYPES = (
"sql",
"mongodb",
"http",
)


class DatadogSpanExporter(SpanExporter):
Expand Down Expand Up @@ -73,6 +78,7 @@ def _translate_to_datadog(self, spans):
span.name,
service=self.service,
resource=_get_resource(span),
span_type=_get_span_type(span),
trace_id=trace_id,
span_id=span_id,
parent_id=parent_id,
Expand All @@ -86,8 +92,6 @@ def _translate_to_datadog(self, spans):
)
datadog_span.set_tags(span.attributes)

# TODO: Add span type
# TODO: Add span tags
# TODO: Add exception info

datadog_spans.append(datadog_span)
Expand Down Expand Up @@ -129,3 +133,13 @@ def _get_resource(span):
)

return span.attributes.get("component", span.name)


def _get_span_type(span):
# use db.type for database integrations (sql, mongodb) otherwise component
span_type = span.attributes.get(
"db.type", span.attributes.get("component")
)
span_type = span_type if span_type in DATADOG_SPAN_TYPES else None

return span_type
34 changes: 34 additions & 0 deletions ext/opentelemetry-ext-datadog/tests/test_datadog_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,37 @@ def test_resources(self):
self.assertEqual(len(datadog_spans), 1)
span = datadog_spans[0].to_dict()
self.assertEqual(span["resource"], resources[index])

def test_span_types(self):
span_types = [None, "http", "sql", "mongodb"]
attributes = [
{"component": "custom"},
{"component": "http"},
{"db.type": "sql"},
{"db.type": "mongodb"},
]

with self.tracer.start_span("foo", attributes=attributes[0]):
pass

with self.tracer.start_span("bar", attributes=attributes[1]):
pass

with self.tracer.start_span("xxx", attributes=attributes[2]):
pass

with self.tracer.start_span("yyy", attributes=attributes[3]):
pass

self.assertEqual(self.agent_writer_mock.write.call_count, 4)

for index, call_args in enumerate(
self.agent_writer_mock.write.call_args_list
):
datadog_spans = call_args.kwargs["spans"]
self.assertEqual(len(datadog_spans), 1)
span = datadog_spans[0].to_dict()
if span_types[index]:
self.assertEqual(span["type"], span_types[index])
else:
self.assertTrue("type" not in span)

0 comments on commit 7664e6d

Please sign in to comment.