Skip to content

Commit

Permalink
Add support for OTEL_SERVICE_NAME env var.
Browse files Browse the repository at this point in the history
  • Loading branch information
owais committed May 7, 2021
1 parent 3a0d38f commit c409212
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)


Expand Down
39 changes: 39 additions & 0 deletions opentelemetry-sdk/tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"}),
)

0 comments on commit c409212

Please sign in to comment.