From c31bd4b320693996b2ffc0881038abb63fdff15c Mon Sep 17 00:00:00 2001 From: Owais Lone Date: Sat, 8 May 2021 02:48:15 +0530 Subject: [PATCH] Add support for OTEL_SERVICE_NAME env var. Spec PR: https://github.com/open-telemetry/opentelemetry-specification/pull/1677 --- CHANGELOG.md | 2 + .../sdk/environment_variables/__init__.py | 17 ++++++++ .../opentelemetry/sdk/resources/__init__.py | 8 +++- .../tests/resources/test_resources.py | 39 +++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 164a02b669a..c5544d57249 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added example for running Django with auto instrumentation. ([#1803](https://github.com/open-telemetry/opentelemetry-python/pull/1803)) +- Added support for OTEL_SERVICE_NAME. + ([#1829](https://github.com/open-telemetry/opentelemetry-python/pull/1829)) ### Changed - Fixed OTLP gRPC exporter silently failing if scheme is not specified in endpoint. diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/environment_variables/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/environment_variables/__init__.py index 9289d1c44ad..a113d705e4f 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/environment_variables/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/environment_variables/__init__.py @@ -218,3 +218,20 @@ """ .. envvar:: OTEL_EXPORTER_JAEGER_AGENT_SPLIT_OVERSIZED_BATCHES """ + +OTEL_SERVICE_NAME = "OTEL_SERVICE_NAME" +""" +.. envvar:: OTEL_SERVICE_NAME + +Convenience environment variable for setting the service name resource attribute. +The following two environment variables have the same effect + +.. code-block:: console + + OTEL_SERVICE_NAME=my-python-service + + OTEL_RESOURCE_ATTRIBUTES=service.name=my-python-service + + +If both are set, :envvar:`OTEL_SERVICE_NAME` take precedence. +""" diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index 6cffff42639..77240b8ef8e 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -64,7 +64,10 @@ import pkg_resources -from opentelemetry.sdk.environment_variables import OTEL_RESOURCE_ATTRIBUTES +from opentelemetry.sdk.environment_variables import ( + OTEL_RESOURCE_ATTRIBUTES, + OTEL_SERVICE_NAME, +) from opentelemetry.semconv.resource import ResourceAttributes LabelValue = typing.Union[str, bool, int, float] @@ -231,6 +234,9 @@ def detect(self) -> "Resource": item.split("=") for item in env_resources_items.split(",") ) } + service_name = os.environ.get(OTEL_SERVICE_NAME) + if service_name: + env_resource_map[SERVICE_NAME] = service_name return Resource(env_resource_map) diff --git a/opentelemetry-sdk/tests/resources/test_resources.py b/opentelemetry-sdk/tests/resources/test_resources.py index 4b8fd71da27..211187f1ebb 100644 --- a/opentelemetry-sdk/tests/resources/test_resources.py +++ b/opentelemetry-sdk/tests/resources/test_resources.py @@ -232,6 +232,20 @@ def test_env_priority(self): self.assertEqual(resource_env_override.attributes["key1"], "value1") self.assertEqual(resource_env_override.attributes["key2"], "value2") + @mock.patch.dict( + os.environ, + { + resources.OTEL_SERVICE_NAME: "test-srv-name", + resources.OTEL_RESOURCE_ATTRIBUTES: "service.name=svc-name-from-resource", + }, + ) + def test_service_name_env(self): + resource = resources.Resource.create() + self.assertEqual(resource.attributes["service.name"], "test-srv-name") + + resource = resources.Resource.create({"service.name": "from-code"}) + self.assertEqual(resource.attributes["service.name"], "from-code") + class TestOTELResourceDetector(unittest.TestCase): def setUp(self) -> None: @@ -270,3 +284,28 @@ def test_multiple_with_whitespace(self): self.assertEqual( detector.detect(), resources.Resource({"k": "v", "k2": "v2"}) ) + + @mock.patch.dict( + os.environ, + {resources.OTEL_SERVICE_NAME: "test-srv-name"}, + ) + def test_service_name_env(self): + detector = resources.OTELResourceDetector() + self.assertEqual( + detector.detect(), + resources.Resource({"service.name": "test-srv-name"}), + ) + + @mock.patch.dict( + os.environ, + { + resources.OTEL_SERVICE_NAME: "from-service-name", + resources.OTEL_RESOURCE_ATTRIBUTES: "service.name=from-resource-attrs", + }, + ) + def test_service_name_env_precedence(self): + detector = resources.OTELResourceDetector() + self.assertEqual( + detector.detect(), + resources.Resource({"service.name": "from-service-name"}), + )