From a61c75235351729764f76540bdbc1673e064677e Mon Sep 17 00:00:00 2001 From: ericmustin Date: Thu, 8 Jul 2021 15:16:52 -0400 Subject: [PATCH 01/11] [exporter/datadog]: fix service name resolution --- .../src/opentelemetry/exporter/datadog/constants.py | 1 + .../src/opentelemetry/exporter/datadog/exporter.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/constants.py b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/constants.py index 6f86c12cce..a534d4dd1f 100644 --- a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/constants.py +++ b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/constants.py @@ -14,3 +14,4 @@ DD_ERROR_TYPE_TAG_KEY = "error.type" DD_ERROR_MSG_TAG_KEY = "error.msg" DD_ERROR_STACK_TAG_KEY = "error.stack" +UNKNOWN_SERVICE_NAME = "unknown_service" \ No newline at end of file diff --git a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py index d6aa7d478b..dd9902f792 100644 --- a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py +++ b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py @@ -33,6 +33,7 @@ EXCEPTION_TYPE_ATTR_KEY, SAMPLE_RATE_METRIC_KEY, SERVICE_NAME_TAG, + UNKNOWN_SERVICE_NAME, VERSION_KEY, ) from opentelemetry.sdk.trace import sampling @@ -135,12 +136,12 @@ def _translate_to_datadog(self, spans): [ resource_tags, resource_service_name, - ] = _extract_tags_from_resource(span.resource) + ] = _extract_tags_from_resource(span.resource, self.service) datadog_span = DatadogSpan( tracer, _get_span_name(span), - service=resource_service_name or self.service, + service=resource_service_name, resource=_get_resource(span), span_type=_get_span_type(span), trace_id=trace_id, @@ -312,7 +313,7 @@ def _parse_tags_str(tags_str): return parsed_tags -def _extract_tags_from_resource(resource): +def _extract_tags_from_resource(resource, fallback_service_name): """Parse tags from resource.attributes, except service.name which has special significance within datadog""" tags = {} @@ -325,6 +326,10 @@ def _extract_tags_from_resource(resource): service_name = attribute_value else: tags[attribute_key] = attribute_value + + if service_name == UNKNOWN_SERVICE_NAME: + service_name = fallback_service_name + return [tags, service_name] From f094c6018147f390edf0243867d1dfb1114305d3 Mon Sep 17 00:00:00 2001 From: ericmustin Date: Thu, 8 Jul 2021 15:24:28 -0400 Subject: [PATCH 02/11] [exporter/datadog]: update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cfd7def70..1a082d8705 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#560](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/560)) - `opentelemetry-instrumentation-django` Migrated Django middleware to new-style. ([#533](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/533)) +- `opentelemetry-exporter-datadog` Fix service name resolution. + ([#540](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/570)) + ### Added - `opentelemetry-instrumentation-httpx` Add `httpx` instrumentation From 12ef531e490f9f580a3103dd815dbe9189225b46 Mon Sep 17 00:00:00 2001 From: ericmustin Date: Thu, 8 Jul 2021 15:24:53 -0400 Subject: [PATCH 03/11] [exporter/datadog]: update changelog again --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a082d8705..d8a92f5227 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#533](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/533)) - `opentelemetry-exporter-datadog` Fix service name resolution. ([#540](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/570)) - ### Added - `opentelemetry-instrumentation-httpx` Add `httpx` instrumentation From e45662ea02e7186e22c74a411bd19eb2aadda126 Mon Sep 17 00:00:00 2001 From: ericmustin Date: Thu, 8 Jul 2021 15:28:31 -0400 Subject: [PATCH 04/11] [exporter/datadog]: fix logic --- .../src/opentelemetry/exporter/datadog/exporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py index dd9902f792..2a07ad688e 100644 --- a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py +++ b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py @@ -327,7 +327,7 @@ def _extract_tags_from_resource(resource, fallback_service_name): else: tags[attribute_key] = attribute_value - if service_name == UNKNOWN_SERVICE_NAME: + if service_name == None or service_name == UNKNOWN_SERVICE_NAME: service_name = fallback_service_name return [tags, service_name] From 2156de44c4928c8cb3137a887ea05a0afae6ba2b Mon Sep 17 00:00:00 2001 From: ericmustin Date: Thu, 8 Jul 2021 15:35:31 -0400 Subject: [PATCH 05/11] [exporter/datadog]: fix logic again --- .../src/opentelemetry/exporter/datadog/exporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py index 2a07ad688e..00933a0eb9 100644 --- a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py +++ b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py @@ -319,7 +319,7 @@ def _extract_tags_from_resource(resource, fallback_service_name): tags = {} service_name = None if not (resource and getattr(resource, "attributes", None)): - return [tags, service_name] + return [tags, fallback_service_name] for attribute_key, attribute_value in resource.attributes.items(): if attribute_key == SERVICE_NAME_TAG: From e341d6722a157f1a0fea2772c8be0637ed2a813f Mon Sep 17 00:00:00 2001 From: ericmustin Date: Thu, 8 Jul 2021 15:48:43 -0400 Subject: [PATCH 06/11] [exporter/datadog]: linting hooray --- .../src/opentelemetry/exporter/datadog/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/constants.py b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/constants.py index a534d4dd1f..8c1093cb8b 100644 --- a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/constants.py +++ b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/constants.py @@ -14,4 +14,4 @@ DD_ERROR_TYPE_TAG_KEY = "error.type" DD_ERROR_MSG_TAG_KEY = "error.msg" DD_ERROR_STACK_TAG_KEY = "error.stack" -UNKNOWN_SERVICE_NAME = "unknown_service" \ No newline at end of file +UNKNOWN_SERVICE_NAME = "unknown_service" From 2829dd783f51775f45a3af53b91cbb8b48a71541 Mon Sep 17 00:00:00 2001 From: ericmustin Date: Thu, 8 Jul 2021 15:55:55 -0400 Subject: [PATCH 07/11] [exporter/datadog]: more linting hooray --- .../src/opentelemetry/exporter/datadog/exporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py index 00933a0eb9..3611c46246 100644 --- a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py +++ b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py @@ -327,7 +327,7 @@ def _extract_tags_from_resource(resource, fallback_service_name): else: tags[attribute_key] = attribute_value - if service_name == None or service_name == UNKNOWN_SERVICE_NAME: + if service_name is None or service_name == UNKNOWN_SERVICE_NAME: service_name = fallback_service_name return [tags, service_name] From 0da9fe02be264884e54463be8cc29b62624a5252 Mon Sep 17 00:00:00 2001 From: ericmustin Date: Fri, 9 Jul 2021 19:13:53 -0400 Subject: [PATCH 08/11] [exporter/datadog]: add feedback from code review --- CHANGELOG.md | 4 ++-- .../src/opentelemetry/exporter/datadog/constants.py | 1 - .../src/opentelemetry/exporter/datadog/exporter.py | 5 ++--- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8a92f5227..675467c0da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,8 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#560](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/560)) - `opentelemetry-instrumentation-django` Migrated Django middleware to new-style. ([#533](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/533)) -- `opentelemetry-exporter-datadog` Fix service name resolution. - ([#540](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/570)) +- `opentelemetry-exporter-datadog` Datadog exporter should not use `unknown_service` as fallback resource service name. + ([#570](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/570)) ### Added - `opentelemetry-instrumentation-httpx` Add `httpx` instrumentation diff --git a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/constants.py b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/constants.py index 8c1093cb8b..6f86c12cce 100644 --- a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/constants.py +++ b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/constants.py @@ -14,4 +14,3 @@ DD_ERROR_TYPE_TAG_KEY = "error.type" DD_ERROR_MSG_TAG_KEY = "error.msg" DD_ERROR_STACK_TAG_KEY = "error.stack" -UNKNOWN_SERVICE_NAME = "unknown_service" diff --git a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py index 3611c46246..f0fcbcf2c8 100644 --- a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py +++ b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py @@ -33,7 +33,6 @@ EXCEPTION_TYPE_ATTR_KEY, SAMPLE_RATE_METRIC_KEY, SERVICE_NAME_TAG, - UNKNOWN_SERVICE_NAME, VERSION_KEY, ) from opentelemetry.sdk.trace import sampling @@ -317,17 +316,17 @@ def _extract_tags_from_resource(resource, fallback_service_name): """Parse tags from resource.attributes, except service.name which has special significance within datadog""" tags = {} - service_name = None if not (resource and getattr(resource, "attributes", None)): return [tags, fallback_service_name] + service_name = None for attribute_key, attribute_value in resource.attributes.items(): if attribute_key == SERVICE_NAME_TAG: service_name = attribute_value else: tags[attribute_key] = attribute_value - if service_name is None or service_name == UNKNOWN_SERVICE_NAME: + if service_name is None or service_name == "unknown_service": service_name = fallback_service_name return [tags, service_name] From ad5aecdc337f2951b4c874600ab7b10d3c82a0dc Mon Sep 17 00:00:00 2001 From: ericmustin Date: Sun, 18 Jul 2021 22:03:41 +0300 Subject: [PATCH 09/11] [exporter/datadog]: add service name fallback test --- .../tests/test_datadog_exporter.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py b/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py index c366175f8c..b9b4735850 100644 --- a/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py +++ b/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py @@ -612,3 +612,36 @@ def test_sampling_rate(self): ] expected = [0.5] self.assertListEqual(actual, expected) + + def test_service_name_fallback(self): + context = trace_api.SpanContext( + trace_id=0x000000000000000000000000DEADBEEF, + span_id=0x34BF92DEEFC58C92, + is_remote=False, + trace_flags=trace_api.TraceFlags(trace_api.TraceFlags.SAMPLED), + ) + trace_api.get_tracer_provider().sampler = sampling.TraceIdRatioBased( + 0.5 + ) + + resource_with_default_name = Resource( + attributes={ + "key_resource": "some_resource", + "service.name": "unknown_service", + } + ) + + span = trace._Span(name="sampled", context=context, parent=None) + span.start() + span.end() + + # pylint: disable=protected-access + exporter = datadog.DatadogSpanExporter(service="fallback_service_name") + datadog_spans = [ + span.to_dict() for span in exporter._translate_to_datadog([span]) + ] + + self.assertEqual(len(datadog_spans), 1) + + span = datadog_spans[0] + self.assertEqual(span["service"], "fallback_service_name") From 9a96ef31f21df0b23e86b55a0495df5d1741ad4e Mon Sep 17 00:00:00 2001 From: ericmustin Date: Sun, 18 Jul 2021 22:05:09 +0300 Subject: [PATCH 10/11] [exporter/datadog]: add service name fallback test, fix tests --- .../tests/test_datadog_exporter.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py b/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py index b9b4735850..ab3071776a 100644 --- a/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py +++ b/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py @@ -631,7 +631,12 @@ def test_service_name_fallback(self): } ) - span = trace._Span(name="sampled", context=context, parent=None) + span = trace._Span( + name="sampled", + context=context, + parent=None, + resource=resource_with_default_name + ) span.start() span.end() From 995e59d860baf2cfba0471e099a6c99f5bb3da5f Mon Sep 17 00:00:00 2001 From: ericmustin Date: Sun, 18 Jul 2021 22:39:16 +0300 Subject: [PATCH 11/11] [exporter/datadog]: linting --- .../tests/test_datadog_exporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py b/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py index ab3071776a..c6fe2fc27a 100644 --- a/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py +++ b/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py @@ -635,7 +635,7 @@ def test_service_name_fallback(self): name="sampled", context=context, parent=None, - resource=resource_with_default_name + resource=resource_with_default_name, ) span.start() span.end()