2222import opentelemetry .ext .datadog as datadog_exporter
2323from opentelemetry import trace as trace_api
2424from opentelemetry .sdk import trace
25+ from opentelemetry .sdk .trace import export
2526
2627
2728class 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