Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

Commit 97b7c22

Browse files
nirskyalrextoumorokoshi
authored
ext/Zipkin - Transform resource to tags when exporting (open-telemetry#707)
Implement the missing part of exporting the TraceProvider resource into Zipkin. Same as in js. Resources are now included into span tags. Co-authored-by: alrex <[email protected]> Co-authored-by: Yusuke Tsutsumi <[email protected]>
1 parent b4e135b commit 97b7c22

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def _translate_to_zipkin(self, spans: Sequence[Span]):
175175
"duration": duration_mus,
176176
"localEndpoint": local_endpoint,
177177
"kind": SPAN_KIND_MAP[span.kind],
178-
"tags": _extract_tags_from_span(span.attributes),
178+
"tags": _extract_tags_from_span(span),
179179
"annotations": _extract_annotations_from_events(span.events),
180180
}
181181

@@ -196,11 +196,11 @@ def shutdown(self) -> None:
196196
pass
197197

198198

199-
def _extract_tags_from_span(attr):
200-
if not attr:
201-
return None
199+
def _extract_tags_from_dict(tags_dict):
202200
tags = {}
203-
for attribute_key, attribute_value in attr.items():
201+
if not tags_dict:
202+
return tags
203+
for attribute_key, attribute_value in tags_dict.items():
204204
if isinstance(attribute_value, (int, bool, float)):
205205
value = str(attribute_value)
206206
elif isinstance(attribute_value, str):
@@ -212,6 +212,13 @@ def _extract_tags_from_span(attr):
212212
return tags
213213

214214

215+
def _extract_tags_from_span(span: Span):
216+
tags = _extract_tags_from_dict(getattr(span, "attributes", None))
217+
if span.resource:
218+
tags.update(_extract_tags_from_dict(span.resource.labels))
219+
return tags
220+
221+
215222
def _extract_annotations_from_events(events):
216223
return (
217224
[

ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from opentelemetry import trace as trace_api
2020
from opentelemetry.ext.zipkin import ZipkinSpanExporter
2121
from opentelemetry.sdk import trace
22+
from opentelemetry.sdk.trace import Resource
2223
from opentelemetry.sdk.trace.export import SpanExportResult
2324
from opentelemetry.trace import TraceFlags
2425

@@ -95,7 +96,7 @@ def test_constructor_explicit(self):
9596
# pylint: disable=too-many-locals
9697
def test_export(self):
9798

98-
span_names = ("test1", "test2", "test3")
99+
span_names = ("test1", "test2", "test3", "test4")
99100
trace_id = 0x6E0C63257DE34C926F9EFCD03927272E
100101
span_id = 0x34BF92DEEFC58C92
101102
parent_id = 0x1111111111111111
@@ -106,12 +107,14 @@ def test_export(self):
106107
base_time,
107108
base_time + 150 * 10 ** 6,
108109
base_time + 300 * 10 ** 6,
110+
base_time + 400 * 10 ** 6,
109111
)
110-
durations = (50 * 10 ** 6, 100 * 10 ** 6, 200 * 10 ** 6)
112+
durations = (50 * 10 ** 6, 100 * 10 ** 6, 200 * 10 ** 6, 300 * 10 ** 6)
111113
end_times = (
112114
start_times[0] + durations[0],
113115
start_times[1] + durations[1],
114116
start_times[2] + durations[2],
117+
start_times[3] + durations[3],
115118
)
116119

117120
span_context = trace_api.SpanContext(
@@ -158,6 +161,7 @@ def test_export(self):
158161
name=span_names[1], context=parent_context, parent=None
159162
),
160163
trace.Span(name=span_names[2], context=other_context, parent=None),
164+
trace.Span(name=span_names[3], context=other_context, parent=None),
161165
]
162166

163167
otel_spans[0].start(start_time=start_times[0])
@@ -168,11 +172,21 @@ def test_export(self):
168172
otel_spans[0].end(end_time=end_times[0])
169173

170174
otel_spans[1].start(start_time=start_times[1])
175+
otel_spans[1].resource = Resource(
176+
labels={"key_resource": "some_resource"}
177+
)
171178
otel_spans[1].end(end_time=end_times[1])
172179

173180
otel_spans[2].start(start_time=start_times[2])
181+
otel_spans[2].set_attribute("key_string", "hello_world")
182+
otel_spans[2].resource = Resource(
183+
labels={"key_resource": "some_resource"}
184+
)
174185
otel_spans[2].end(end_time=end_times[2])
175186

187+
otel_spans[3].start(start_time=start_times[3])
188+
otel_spans[3].end(end_time=end_times[3])
189+
176190
service_name = "test-service"
177191
local_endpoint = {"serviceName": service_name, "port": 9411}
178192

@@ -208,7 +222,7 @@ def test_export(self):
208222
"duration": durations[1] // 10 ** 3,
209223
"localEndpoint": local_endpoint,
210224
"kind": None,
211-
"tags": None,
225+
"tags": {"key_resource": "some_resource"},
212226
"annotations": None,
213227
},
214228
{
@@ -219,7 +233,21 @@ def test_export(self):
219233
"duration": durations[2] // 10 ** 3,
220234
"localEndpoint": local_endpoint,
221235
"kind": None,
222-
"tags": None,
236+
"tags": {
237+
"key_string": "hello_world",
238+
"key_resource": "some_resource",
239+
},
240+
"annotations": None,
241+
},
242+
{
243+
"traceId": format(trace_id, "x"),
244+
"id": format(other_id, "x"),
245+
"name": span_names[3],
246+
"timestamp": start_times[3] // 10 ** 3,
247+
"duration": durations[3] // 10 ** 3,
248+
"localEndpoint": local_endpoint,
249+
"kind": None,
250+
"tags": {},
223251
"annotations": None,
224252
},
225253
]

0 commit comments

Comments
 (0)