Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 54d11c9

Browse files
wkiserliyanhui1228
authored andcommitted
Fix zipkin_exporter to export span names and tags correctly (#110)
1 parent 8a13b0a commit 54d11c9

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

opencensus/trace/exporters/zipkin_exporter.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def get_url(self):
8787
self.endpoint)
8888

8989
def emit(self, trace):
90-
"""Send trace to Zipkin server, default using the v1 API.
90+
"""Send trace to Zipkin server, default using the v2 API.
9191
9292
:type trace: dict
9393
:param trace: Trace data in dictionary format.
@@ -151,11 +151,11 @@ def translate_to_zipkin(self, trace_id, spans):
151151
zipkin_span = {
152152
'traceId': trace_id,
153153
'id': str(span.get('spanId')),
154-
'name': span.get('name'),
154+
'name': span.get('displayName', {}).get('value'),
155155
'timestamp': int(round(start_timestamp_ms)),
156156
'duration': int(round(duration_ms)),
157157
'localEndpoint': local_endpoint,
158-
'tags': span.get('attributes'),
158+
'tags': _extract_tags_from_span(span),
159159
}
160160

161161
span_kind = span.get('kind')
@@ -174,3 +174,22 @@ def translate_to_zipkin(self, trace_id, spans):
174174
zipkin_spans.append(zipkin_span)
175175

176176
return zipkin_spans
177+
178+
179+
def _extract_tags_from_span(span):
180+
tags = {}
181+
for attribute_key, attribute_value in span.get(
182+
'attributes', {}).get('attributeMap', {}).items():
183+
if not isinstance(attribute_value, dict):
184+
continue
185+
if attribute_value.get('string_value') is not None:
186+
value = attribute_value.get('string_value').get('value')
187+
elif attribute_value.get('int_value') is not None:
188+
value = str(attribute_value.get('int_value'))
189+
elif attribute_value.get('bool_value') is not None:
190+
value = str(attribute_value.get('bool_value'))
191+
else:
192+
logging.warn('Could not serialize tag {}'.format(attribute_key))
193+
continue
194+
tags[attribute_key] = value
195+
return tags

tests/unit/trace/exporters/test_zipkin_exporter.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,39 +91,55 @@ def test_emit_failed(self, translate_mock, requests_mock):
9191

9292
def test_translate_to_zipkin_span_kind_none(self):
9393
span1 = {
94-
'name': 'child_span',
94+
'displayName': {'value': 'child_span'},
9595
'parentSpanId': 1111111111,
9696
'spanId': 1234567890,
9797
'startTime': '2017-08-15T18:02:26.071158Z',
9898
'endTime': '2017-08-15T18:02:36.071158Z',
9999
'attributes': {
100-
'key': 'test_key',
101-
'value': 'test_value',
100+
'attributeMap': {
101+
'test_key': {
102+
'string_value': {
103+
'value': 'test_value'
104+
}
105+
}
106+
}
102107
},
103108
}
104109

105110
span2 = {
106-
'name': 'child_span',
111+
'displayName': {'value': 'child_span'},
107112
'kind': 0,
108113
'parentSpanId': 1111111111,
109114
'spanId': 1234567890,
110115
'startTime': '2017-08-15T18:02:26.071158Z',
111116
'endTime': '2017-08-15T18:02:36.071158Z',
112117
'attributes': {
113-
'key': 'test_key',
114-
'value': 'test_value',
118+
'attributeMap': {
119+
'test_key': {
120+
'int_value': 1
121+
}
122+
}
115123
},
116124
}
117125

118126
span3 = {
119-
'name': 'child_span',
127+
'displayName': {'value': 'child_span'},
120128
'kind': 1,
121129
'spanId': 1234567890,
122130
'startTime': '2017-08-15T18:02:26.071158Z',
123131
'endTime': '2017-08-15T18:02:36.071158Z',
124132
'attributes': {
125-
'key': 'test_key',
126-
'value': 'test_value',
133+
'attributeMap': {
134+
'test_key': {
135+
'bool_value': False
136+
},
137+
# these tags are malformed and should be omitted
138+
'test_key2': 'raw_value',
139+
'test_key3': {
140+
'float_value': 0.1
141+
},
142+
}
127143
},
128144
}
129145

@@ -145,10 +161,7 @@ def test_translate_to_zipkin_span_kind_none(self):
145161
'timestamp': 1502820146000000,
146162
'duration': 10000000,
147163
'localEndpoint': local_endpoint,
148-
'tags': {
149-
'key': 'test_key',
150-
'value': 'test_value',
151-
},
164+
'tags': {'test_key': 'test_value'},
152165
},
153166
{
154167
'traceId': '6e0c63257de34c92bf9efcd03927272e',
@@ -158,10 +171,7 @@ def test_translate_to_zipkin_span_kind_none(self):
158171
'timestamp': 1502820146000000,
159172
'duration': 10000000,
160173
'localEndpoint': local_endpoint,
161-
'tags': {
162-
'key': 'test_key',
163-
'value': 'test_value',
164-
},
174+
'tags': {'test_key': '1'},
165175
},
166176
{
167177
'traceId': '6e0c63257de34c92bf9efcd03927272e',
@@ -170,10 +180,7 @@ def test_translate_to_zipkin_span_kind_none(self):
170180
'timestamp': 1502820146000000,
171181
'duration': 10000000,
172182
'localEndpoint': local_endpoint,
173-
'tags': {
174-
'key': 'test_key',
175-
'value': 'test_value',
176-
},
183+
'tags': {'test_key': 'False'},
177184
'kind': 'SERVER',
178185
}
179186
]

0 commit comments

Comments
 (0)