Skip to content

Commit c3772de

Browse files
euri10aabmass
andauthored
Attempt at mapping google spans to opentelemetry ones better (#90)
* Trying to use predefined labels as per https://cloud.google.com/trace/docs/reference/v1/rest/v1/projects.traces#get * No need for a regexp * Ok pylint * Ok py35 no f-strings * Using a mapping * Update opentelemetry-exporter-google-cloud/src/opentelemetry/exporter/cloud_trace/__init__.py Co-authored-by: Aaron Abbott <aaronabbott@google.com> * Update opentelemetry-exporter-google-cloud/src/opentelemetry/exporter/cloud_trace/__init__.py Co-authored-by: Aaron Abbott <aaronabbott@google.com> * Update opentelemetry-exporter-google-cloud/src/opentelemetry/exporter/cloud_trace/__init__.py Co-authored-by: Aaron Abbott <aaronabbott@google.com> * Update opentelemetry-exporter-google-cloud/src/opentelemetry/exporter/cloud_trace/__init__.py Co-authored-by: Aaron Abbott <aaronabbott@google.com> * Update opentelemetry-exporter-google-cloud/src/opentelemetry/exporter/cloud_trace/__init__.py Co-authored-by: Aaron Abbott <aaronabbott@google.com> * Removed non-identified mappings * Pylint * Please mypy * Test wip, troubles to run locally * Corrected test * Removed error and stacktrace mapping for now * Removed pid * Instanciated label mapping inside test case * Useless line break * Black magic for CI * Used key directly Co-authored-by: Aaron Abbott <aaronabbott@google.com> * Modified CHANGELOG.md * Pylint Co-authored-by: Aaron Abbott <aaronabbott@google.com>
1 parent 79155b0 commit c3772de

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

opentelemetry-exporter-google-cloud/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- Add mapping between opentelemetry and google traces attributes
6+
([#90](https://github.com/GoogleCloudPlatform/opentelemetry-operations-python/pull/90))
7+
58
## Version 0.15b0
69

710
Released 2020-11-04

opentelemetry-exporter-google-cloud/src/opentelemetry/exporter/cloud_trace/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,21 @@ def _extract_resources(resource: Resource) -> Dict[str, str]:
364364
}
365365

366366

367+
LABELS_MAPPING = {
368+
# this one might be http.flavor? I'm not sure
369+
"http.scheme": "/http/client_protocol",
370+
"http.host": "/http/host",
371+
"http.method": "/http/method",
372+
# https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md#common-attributes
373+
"http.request_content_length": "/http/request/size",
374+
"http.response_content_length": "/http/response/size",
375+
"http.route": "/http/route",
376+
"http.status_code": "/http/status_code",
377+
"http.url": "/http/url",
378+
"http.user_agent": "/http/user_agent",
379+
}
380+
381+
367382
def _extract_attributes(
368383
attrs: types.Attributes,
369384
num_attrs_limit: int,
@@ -374,6 +389,8 @@ def _extract_attributes(
374389
invalid_value_dropped_count = 0
375390
for key, value in attrs.items() if attrs else []:
376391
key = _truncate_str(key, 128)[0]
392+
if key in LABELS_MAPPING: # pylint: disable=consider-using-get
393+
key = LABELS_MAPPING[key]
377394
value = _format_attribute_value(value)
378395

379396
if value:

opentelemetry-exporter-google-cloud/tests/test_cloud_trace_exporter.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,62 @@ def test_extract_variety_of_attributes(self):
213213
self.extracted_attributes_variety_pack,
214214
)
215215

216+
def test_extract_label_mapping_attributes(self):
217+
attributes_labels_mapping = {
218+
"http.scheme": "http",
219+
"http.host": "172.19.0.4:8000",
220+
"http.method": "POST",
221+
"http.request_content_length": 321,
222+
"http.response_content_length": 123,
223+
"http.route": "/fuzzy/search",
224+
"http.status_code": 200,
225+
"http.url": "http://172.19.0.4:8000/fuzzy/search",
226+
"http.user_agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36",
227+
}
228+
extracted_attributes_labels_mapping = ProtoSpan.Attributes(
229+
attribute_map={
230+
"/http/client_protocol": AttributeValue(
231+
string_value=TruncatableString(
232+
value="http", truncated_byte_count=0
233+
)
234+
),
235+
"/http/host": AttributeValue(
236+
string_value=TruncatableString(
237+
value="172.19.0.4:8000", truncated_byte_count=0
238+
)
239+
),
240+
"/http/method": AttributeValue(
241+
string_value=TruncatableString(
242+
value="POST", truncated_byte_count=0
243+
)
244+
),
245+
"/http/request/size": AttributeValue(int_value=321),
246+
"/http/response/size": AttributeValue(int_value=123),
247+
"/http/route": AttributeValue(
248+
string_value=TruncatableString(
249+
value="/fuzzy/search", truncated_byte_count=0
250+
)
251+
),
252+
"/http/status_code": AttributeValue(int_value=200),
253+
"/http/url": AttributeValue(
254+
string_value=TruncatableString(
255+
value="http://172.19.0.4:8000/fuzzy/search",
256+
truncated_byte_count=0,
257+
)
258+
),
259+
"/http/user_agent": AttributeValue(
260+
string_value=TruncatableString(
261+
value="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36",
262+
truncated_byte_count=0,
263+
)
264+
),
265+
}
266+
)
267+
self.assertEqual(
268+
_extract_attributes(attributes_labels_mapping, num_attrs_limit=9),
269+
extracted_attributes_labels_mapping,
270+
)
271+
216272
def test_ignore_invalid_attributes(self):
217273
self.assertEqual(
218274
_extract_attributes(

0 commit comments

Comments
 (0)