Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for OTEL_SERVICE_NAME env var. #1829

Merged
merged 3 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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` takes 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"}),
)