Skip to content

Commit 3d9267d

Browse files
committed
handle http resources
1 parent 8ed8ee6 commit 3d9267d

2 files changed

Lines changed: 65 additions & 10 deletions

File tree

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _translate_to_datadog(self, spans):
7272
tracer,
7373
span.name,
7474
service=self.service,
75-
resource=span.attributes.get("component") or span.name,
75+
resource=_get_resource(span),
7676
trace_id=trace_id,
7777
span_id=span_id,
7878
parent_id=parent_id,
@@ -84,6 +84,7 @@ def _translate_to_datadog(self, spans):
8484
if span.status.canonical_code is not StatusCanonicalCode.OK
8585
else 0
8686
)
87+
datadog_span.set_tags(span.attributes)
8788

8889
# TODO: Add span type
8990
# TODO: Add span tags
@@ -114,3 +115,17 @@ def _get_trace_ids(span):
114115
def _convert_trace_id_uint64(otel_id):
115116
raw = otel_id.to_bytes(16, "big")
116117
return int.from_bytes(raw[8:], byteorder="big")
118+
119+
120+
def _get_resource(span):
121+
if "http.method" in span.attributes:
122+
route = span.attributes.get(
123+
"http.route", span.attributes.get("http.path")
124+
)
125+
return (
126+
span.attributes["http.method"] + " " + route
127+
if route
128+
else span.attributes["http.method"]
129+
)
130+
131+
return span.attributes.get("component", span.name)

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

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,28 @@
2222
import opentelemetry.ext.datadog as datadog_exporter
2323
from opentelemetry import trace as trace_api
2424
from opentelemetry.sdk import trace
25+
from opentelemetry.sdk.trace import export
2526

2627

2728
class TestDatadogSpanExporter(unittest.TestCase):
29+
def setUp(self):
30+
tracer_provider = trace.TracerProvider()
31+
32+
self.exporter = datadog_exporter.DatadogSpanExporter()
33+
34+
# just agent is configured now
35+
self.agent_writer_mock = mock.Mock(spec=AgentWriter)
36+
self.agent_writer_mock.started = True
37+
self.agent_writer_mock.exit_timeout = 1
38+
39+
# pylint: disable=protected-access
40+
self.exporter._agent_writer = self.agent_writer_mock
41+
42+
tracer_provider.add_span_processor(
43+
export.SimpleExportSpanProcessor(self.exporter)
44+
)
45+
self.tracer = tracer_provider.get_tracer(__name__)
46+
2847
def test_constructor_default(self):
2948
"""Test the default values assigned by constructor."""
3049
exporter = datadog_exporter.DatadogSpanExporter()
@@ -132,6 +151,7 @@ def test_translate_to_datadog(self):
132151
duration=durations[0],
133152
error=0,
134153
service="test-service",
154+
meta={"component": "testcomponent"},
135155
),
136156
dict(
137157
trace_id=trace_id_low,
@@ -162,13 +182,6 @@ def test_translate_to_datadog(self):
162182
@mock.patch.dict("os.environ", {"DD_SERVICE": "test-service"})
163183
def test_export(self):
164184
"""Test that agent and/or collector are invoked"""
165-
exporter = datadog_exporter.DatadogSpanExporter()
166-
167-
# just agent is configured now
168-
agent_writer_mock = mock.Mock(spec=AgentWriter)
169-
# pylint: disable=protected-access
170-
exporter._agent_writer = agent_writer_mock
171-
172185
# create and save span to be used in tests
173186
context = trace_api.SpanContext(
174187
trace_id=0x000000000000000000000000DEADBEEF,
@@ -180,6 +193,33 @@ def test_export(self):
180193
test_span.start()
181194
test_span.end()
182195

183-
exporter.export((test_span,))
196+
self.exporter.export((test_span,))
197+
198+
self.assertEqual(self.agent_writer_mock.write.call_count, 1)
199+
200+
def test_resources(self):
201+
resources = ["foo", "foo", "GET /foo", "GET /foo"]
202+
attributes = [
203+
{},
204+
{"component": "foo"},
205+
{"http.method": "GET", "http.route": "/foo"},
206+
{"http.method": "GET", "http.path": "/foo"},
207+
]
184208

185-
self.assertEqual(agent_writer_mock.write.call_count, 1)
209+
with self.tracer.start_span("foo", attributes=attributes[0]):
210+
with self.tracer.start_span("bar", attributes=attributes[1]):
211+
with self.tracer.start_span("xxx", attributes=attributes[2]):
212+
with self.tracer.start_span(
213+
"yyy", attributes=attributes[3]
214+
):
215+
pass
216+
217+
self.assertEqual(self.agent_writer_mock.write.call_count, 4)
218+
219+
for index, call_args in enumerate(
220+
reversed(self.agent_writer_mock.write.call_args_list)
221+
):
222+
datadog_spans = call_args.kwargs["spans"]
223+
self.assertEqual(len(datadog_spans), 1)
224+
span = datadog_spans[0].to_dict()
225+
self.assertEqual(span["resource"], resources[index])

0 commit comments

Comments
 (0)