From 112bade11f1c1eca473e274947b5806afba58dcf Mon Sep 17 00:00:00 2001 From: Andrew Xue Date: Fri, 3 Jul 2020 12:42:56 -0400 Subject: [PATCH] Strip letters (#877) --- .../exporter/cloud_trace/__init__.py | 10 ++++++++-- .../tests/test_cloud_trace_exporter.py | 18 ++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/ext/opentelemetry-exporter-cloud-trace/src/opentelemetry/exporter/cloud_trace/__init__.py b/ext/opentelemetry-exporter-cloud-trace/src/opentelemetry/exporter/cloud_trace/__init__.py index f6d1e78b5a..647ad21040 100644 --- a/ext/opentelemetry-exporter-cloud-trace/src/opentelemetry/exporter/cloud_trace/__init__.py +++ b/ext/opentelemetry-exporter-cloud-trace/src/opentelemetry/exporter/cloud_trace/__init__.py @@ -291,6 +291,10 @@ def _extract_events(events: Sequence[Event]) -> ProtoSpan.TimeEvents: ) +def _strip_characters(ot_version): + return "".join(filter(lambda x: x.isdigit() or x == ".", ot_version)) + + def _extract_attributes( attrs: types.Attributes, num_attrs_limit: int, @@ -310,8 +314,10 @@ def _extract_attributes( if add_agent_attr: attributes_dict["g.co/agent"] = _format_attribute_value( "opentelemetry-python {}; google-cloud-trace-exporter {}".format( - pkg_resources.get_distribution("opentelemetry-sdk").version, - cloud_trace_version, + _strip_characters( + pkg_resources.get_distribution("opentelemetry-sdk").version + ), + _strip_characters(cloud_trace_version), ) ) return ProtoSpan.Attributes( diff --git a/ext/opentelemetry-exporter-cloud-trace/tests/test_cloud_trace_exporter.py b/ext/opentelemetry-exporter-cloud-trace/tests/test_cloud_trace_exporter.py index bd5a9f073d..3b89b55046 100644 --- a/ext/opentelemetry-exporter-cloud-trace/tests/test_cloud_trace_exporter.py +++ b/ext/opentelemetry-exporter-cloud-trace/tests/test_cloud_trace_exporter.py @@ -32,6 +32,7 @@ _extract_links, _extract_status, _format_attribute_value, + _strip_characters, _truncate_str, ) from opentelemetry.exporter.cloud_trace.version import ( @@ -117,10 +118,12 @@ def test_export(self): attribute_map={ "g.co/agent": _format_attribute_value( "opentelemetry-python {}; google-cloud-trace-exporter {}".format( - pkg_resources.get_distribution( - "opentelemetry-sdk" - ).version, - cloud_trace_version, + _strip_characters( + pkg_resources.get_distribution( + "opentelemetry-sdk" + ).version + ), + _strip_characters(cloud_trace_version), ) ) } @@ -415,3 +418,10 @@ def test_truncate(self): ), MAX_EVENT_ATTRS, ) + + def test_strip_characters(self): + self.assertEqual("0.10.0", _strip_characters("0.10.0b")) + self.assertEqual("1.20.5", _strip_characters("1.20.5")) + self.assertEqual("3.1.0", _strip_characters("3.1.0beta")) + self.assertEqual("4.2.0", _strip_characters("4b.2rc.0a")) + self.assertEqual("6.20.15", _strip_characters("b6.20.15"))