Skip to content

Commit 1fd8659

Browse files
mauriciovasquezbernalc24t
authored andcommitted
Fix bad variable type in jaeger-ext (open-telemetry#230)
1 parent 3fa3a83 commit 1fd8659

File tree

2 files changed

+83
-16
lines changed

2 files changed

+83
-16
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ def shutdown(self):
123123
pass
124124

125125

126+
def _nsec_to_usec_round(nsec):
127+
"""Round nanoseconds to microseconds"""
128+
return (nsec + 500) // 10 ** 3
129+
130+
126131
def _translate_to_jaeger(spans: Span):
127132
"""Translate the spans to Jaeger format.
128133
@@ -137,8 +142,8 @@ def _translate_to_jaeger(spans: Span):
137142
trace_id = ctx.trace_id
138143
span_id = ctx.span_id
139144

140-
start_time_us = span.start_time // 1e3
141-
duration_us = (span.end_time - span.start_time) // 1e3
145+
start_time_us = _nsec_to_usec_round(span.start_time)
146+
duration_us = _nsec_to_usec_round(span.end_time - span.start_time)
142147

143148
parent_id = 0
144149
if isinstance(span.parent, trace_api.Span):
@@ -227,7 +232,7 @@ def _extract_logs_from_span(span):
227232
)
228233
)
229234

230-
event_timestamp_us = event.timestamp // 1e3
235+
event_timestamp_us = _nsec_to_usec_round(event.timestamp)
231236
logs.append(
232237
jaeger.Log(timestamp=int(event_timestamp_us), fields=fields)
233238
)

ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py

+75-13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515

1616
import unittest
17+
from unittest import mock
1718

1819
# pylint:disable=no-name-in-module
1920
# pylint:disable=import-error
@@ -24,7 +25,19 @@
2425

2526

2627
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+
2739
def test_constructor_default(self):
40+
"""Test the default values assigned by constructor."""
2841
service_name = "my-service-name"
2942
host_name = "localhost"
3043
thrift_port = None
@@ -44,13 +57,14 @@ def test_constructor_default(self):
4457
self.assertTrue(exporter.agent_client is not None)
4558

4659
def test_constructor_explicit(self):
60+
"""Test the constructor passing all the options."""
4761
service = "my-opentelemetry-jaeger"
4862
collector_host_name = "opentelemetry.io"
4963
collector_port = 15875
5064
collector_endpoint = "/myapi/traces?format=jaeger.thrift"
5165

5266
agent_port = 14268
53-
agent_host_name = "opentelemetry.com"
67+
agent_host_name = "opentelemetry.io"
5468

5569
username = "username"
5670
password = "password"
@@ -84,6 +98,14 @@ def test_constructor_explicit(self):
8498
self.assertNotEqual(exporter.collector, collector)
8599
self.assertTrue(exporter.collector.auth is None)
86100

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+
87109
# pylint: disable=too-many-locals
88110
def test_translate_to_jaeger(self):
89111
# pylint: disable=invalid-name
@@ -97,9 +119,13 @@ def test_translate_to_jaeger(self):
97119
parent_id = 0x1111111111111111
98120
other_id = 0x2222222222222222
99121

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)
103129
end_times = (
104130
start_times[0] + durations[0],
105131
start_times[1] + durations[1],
@@ -116,7 +142,7 @@ def test_translate_to_jaeger(self):
116142
"key_float": 0.3,
117143
}
118144

119-
event_timestamp = base_time + 50e6
145+
event_timestamp = base_time + 50 * 10 ** 6
120146
event = trace_api.Event(
121147
name="event0",
122148
timestamp=event_timestamp,
@@ -166,8 +192,8 @@ def test_translate_to_jaeger(self):
166192
traceIdLow=trace_id_low,
167193
spanId=span_id,
168194
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,
171197
flags=0,
172198
tags=[
173199
jaeger.Tag(
@@ -194,7 +220,7 @@ def test_translate_to_jaeger(self):
194220
],
195221
logs=[
196222
jaeger.Log(
197-
timestamp=event_timestamp / 1e3,
223+
timestamp=event_timestamp // 10 ** 3,
198224
fields=[
199225
jaeger.Tag(
200226
key="annotation_bool",
@@ -226,8 +252,8 @@ def test_translate_to_jaeger(self):
226252
traceIdLow=trace_id_low,
227253
spanId=parent_id,
228254
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,
231257
flags=0,
232258
),
233259
jaeger.Span(
@@ -236,14 +262,14 @@ def test_translate_to_jaeger(self):
236262
traceIdLow=trace_id_low,
237263
spanId=other_id,
238264
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,
241267
flags=0,
242268
),
243269
]
244270

245271
# 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
247273
self.assertCountEqual(
248274
spans[0].logs[0].fields, expected_spans[0].logs[0].fields
249275
)
@@ -252,3 +278,39 @@ def test_translate_to_jaeger(self):
252278
expected_spans[0].logs[0].fields = None
253279

254280
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

Comments
 (0)