14
14
# limitations under the License.
15
15
16
16
import unittest
17
+ from unittest import mock
17
18
18
19
# pylint:disable=no-name-in-module
19
20
# pylint:disable=import-error
24
25
25
26
26
27
class TestJaegerSpanExporter (unittest .TestCase ):
28
+ def setUp (self ):
29
+ # create and save span to be used in tests
30
+ context = trace_api .SpanContext (
31
+ trace_id = 0x000000000000000000000000DEADBEEF ,
32
+ span_id = 0x00000000DEADBEF0 ,
33
+ )
34
+
35
+ self ._test_span = trace .Span ("test_span" , context = context )
36
+ self ._test_span .start ()
37
+ self ._test_span .end ()
38
+
27
39
def test_constructor_default (self ):
40
+ """Test the default values assigned by constructor."""
28
41
service_name = "my-service-name"
29
42
host_name = "localhost"
30
43
thrift_port = None
@@ -44,13 +57,14 @@ def test_constructor_default(self):
44
57
self .assertTrue (exporter .agent_client is not None )
45
58
46
59
def test_constructor_explicit (self ):
60
+ """Test the constructor passing all the options."""
47
61
service = "my-opentelemetry-jaeger"
48
62
collector_host_name = "opentelemetry.io"
49
63
collector_port = 15875
50
64
collector_endpoint = "/myapi/traces?format=jaeger.thrift"
51
65
52
66
agent_port = 14268
53
- agent_host_name = "opentelemetry.com "
67
+ agent_host_name = "opentelemetry.io "
54
68
55
69
username = "username"
56
70
password = "password"
@@ -84,6 +98,14 @@ def test_constructor_explicit(self):
84
98
self .assertNotEqual (exporter .collector , collector )
85
99
self .assertTrue (exporter .collector .auth is None )
86
100
101
+ def test_nsec_to_usec_round (self ):
102
+ # pylint: disable=protected-access
103
+ nsec_to_usec_round = jaeger_exporter ._nsec_to_usec_round
104
+
105
+ self .assertEqual (nsec_to_usec_round (5000 ), 5 )
106
+ self .assertEqual (nsec_to_usec_round (5499 ), 5 )
107
+ self .assertEqual (nsec_to_usec_round (5500 ), 6 )
108
+
87
109
# pylint: disable=too-many-locals
88
110
def test_translate_to_jaeger (self ):
89
111
# pylint: disable=invalid-name
@@ -97,9 +119,13 @@ def test_translate_to_jaeger(self):
97
119
parent_id = 0x1111111111111111
98
120
other_id = 0x2222222222222222
99
121
100
- base_time = 683647322 * 1e9 # in ns
101
- start_times = (base_time , base_time + 150 * 1e6 , base_time + 300 * 1e6 )
102
- durations = (50 * 1e6 , 100 * 1e6 , 200 * 1e6 )
122
+ base_time = 683647322 * 10 ** 9 # in ns
123
+ start_times = (
124
+ base_time ,
125
+ base_time + 150 * 10 ** 6 ,
126
+ base_time + 300 * 10 ** 6 ,
127
+ )
128
+ durations = (50 * 10 ** 6 , 100 * 10 ** 6 , 200 * 10 ** 6 )
103
129
end_times = (
104
130
start_times [0 ] + durations [0 ],
105
131
start_times [1 ] + durations [1 ],
@@ -116,7 +142,7 @@ def test_translate_to_jaeger(self):
116
142
"key_float" : 0.3 ,
117
143
}
118
144
119
- event_timestamp = base_time + 50e6
145
+ event_timestamp = base_time + 50 * 10 ** 6
120
146
event = trace_api .Event (
121
147
name = "event0" ,
122
148
timestamp = event_timestamp ,
@@ -166,8 +192,8 @@ def test_translate_to_jaeger(self):
166
192
traceIdLow = trace_id_low ,
167
193
spanId = span_id ,
168
194
parentSpanId = parent_id ,
169
- startTime = start_times [0 ] / 1e3 ,
170
- duration = durations [0 ] / 1e3 ,
195
+ startTime = start_times [0 ] // 10 ** 3 ,
196
+ duration = durations [0 ] // 10 ** 3 ,
171
197
flags = 0 ,
172
198
tags = [
173
199
jaeger .Tag (
@@ -194,7 +220,7 @@ def test_translate_to_jaeger(self):
194
220
],
195
221
logs = [
196
222
jaeger .Log (
197
- timestamp = event_timestamp / 1e3 ,
223
+ timestamp = event_timestamp // 10 ** 3 ,
198
224
fields = [
199
225
jaeger .Tag (
200
226
key = "annotation_bool" ,
@@ -226,8 +252,8 @@ def test_translate_to_jaeger(self):
226
252
traceIdLow = trace_id_low ,
227
253
spanId = parent_id ,
228
254
parentSpanId = 0 ,
229
- startTime = int ( start_times [1 ] // 1e3 ) ,
230
- duration = int ( durations [1 ] // 1e3 ) ,
255
+ startTime = start_times [1 ] // 10 ** 3 ,
256
+ duration = durations [1 ] // 10 ** 3 ,
231
257
flags = 0 ,
232
258
),
233
259
jaeger .Span (
@@ -236,14 +262,14 @@ def test_translate_to_jaeger(self):
236
262
traceIdLow = trace_id_low ,
237
263
spanId = other_id ,
238
264
parentSpanId = 0 ,
239
- startTime = int ( start_times [2 ] // 1e3 ) ,
240
- duration = int ( durations [2 ] // 1e3 ) ,
265
+ startTime = start_times [2 ] // 10 ** 3 ,
266
+ duration = durations [2 ] // 10 ** 3 ,
241
267
flags = 0 ,
242
268
),
243
269
]
244
270
245
271
# events are complicated to compare because order of fields
246
- # (attributes) is otel is not important but in jeager it is
272
+ # (attributes) in otel is not important but in jeager it is
247
273
self .assertCountEqual (
248
274
spans [0 ].logs [0 ].fields , expected_spans [0 ].logs [0 ].fields
249
275
)
@@ -252,3 +278,39 @@ def test_translate_to_jaeger(self):
252
278
expected_spans [0 ].logs [0 ].fields = None
253
279
254
280
self .assertEqual (spans , expected_spans )
281
+
282
+ def test_export (self ):
283
+ """Test that agent and/or collector are invoked"""
284
+ exporter = jaeger_exporter .JaegerSpanExporter (
285
+ "test_export" , agent_host_name = "localhost" , agent_port = 6318
286
+ )
287
+
288
+ # just agent is configured now
289
+ agent_client_mock = mock .Mock (spec = jaeger_exporter .AgentClientUDP )
290
+ # pylint: disable=protected-access
291
+ exporter ._agent_client = agent_client_mock
292
+
293
+ exporter .export ((self ._test_span ,))
294
+ self .assertEqual (agent_client_mock .emit .call_count , 1 )
295
+
296
+ # add also a collector and test that both are called
297
+ collector_mock = mock .Mock (spec = jaeger_exporter .Collector )
298
+ # pylint: disable=protected-access
299
+ exporter ._collector = collector_mock
300
+
301
+ exporter .export ((self ._test_span ,))
302
+ self .assertEqual (agent_client_mock .emit .call_count , 2 )
303
+ self .assertEqual (collector_mock .submit .call_count , 1 )
304
+
305
+ def test_agent_client (self ):
306
+ agent_client = jaeger_exporter .AgentClientUDP (
307
+ host_name = "localhost" , port = 6354
308
+ )
309
+
310
+ batch = jaeger .Batch (
311
+ # pylint: disable=protected-access
312
+ spans = jaeger_exporter ._translate_to_jaeger ((self ._test_span ,)),
313
+ process = jaeger .Process (serviceName = "xxx" ),
314
+ )
315
+
316
+ agent_client .emit (batch )
0 commit comments