From 74451fe894bbc5da4e3864ca36088c74108d19fc Mon Sep 17 00:00:00 2001 From: Vivek Date: Wed, 3 Jul 2024 11:12:28 +0530 Subject: [PATCH] Replace attributes with BoundedAttributes and expose the attributes field in the api --- .../src/opentelemetry/metrics/_internal/__init__.py | 1 + .../src/opentelemetry/sdk/util/instrumentation.py | 8 ++++++-- opentelemetry-sdk/tests/metrics/test_metrics.py | 6 ++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/metrics/_internal/__init__.py b/opentelemetry-api/src/opentelemetry/metrics/_internal/__init__.py index 9cbf14d2edc..a926a67b362 100644 --- a/opentelemetry-api/src/opentelemetry/metrics/_internal/__init__.py +++ b/opentelemetry-api/src/opentelemetry/metrics/_internal/__init__.py @@ -102,6 +102,7 @@ def get_meter( name: str, version: Optional[str] = None, schema_url: Optional[str] = None, + attributes: Optional[dict] = None, ) -> "Meter": """Returns a `Meter` for use by the given instrumentation library. diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/util/instrumentation.py b/opentelemetry-sdk/src/opentelemetry/sdk/util/instrumentation.py index ff4c6f25894..47cef7a628c 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/util/instrumentation.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/util/instrumentation.py @@ -16,6 +16,8 @@ from deprecated import deprecated +from opentelemetry.attributes import BoundedAttributes + class InstrumentationInfo: """Immutable information about an instrumentation library module. @@ -96,7 +98,7 @@ def __init__( if schema_url is None: schema_url = "" self._schema_url = schema_url - self._attributes = attributes + self._attributes = BoundedAttributes(attributes=attributes) def __repr__(self) -> str: return f"{type(self).__name__}({self._name}, {self._version}, {self._schema_url}, {self._attributes})" @@ -156,7 +158,9 @@ def to_json(self, indent=4) -> str: "name": self._name, "version": self._version, "schema_url": self._schema_url, - "attributes": self._attributes, + "attributes": ( + dict(self._attributes) if bool(self._attributes) else None + ), }, indent=indent, ) diff --git a/opentelemetry-sdk/tests/metrics/test_metrics.py b/opentelemetry-sdk/tests/metrics/test_metrics.py index 0f7ad73e24f..01c501b2056 100644 --- a/opentelemetry-sdk/tests/metrics/test_metrics.py +++ b/opentelemetry-sdk/tests/metrics/test_metrics.py @@ -43,6 +43,7 @@ from opentelemetry.sdk.resources import Resource from opentelemetry.test import TestCase from opentelemetry.test.concurrency_test import ConcurrencyTestBase, MockFunc +from opentelemetry.attributes import BoundedAttributes class DummyMetricReader(MetricReader): @@ -239,6 +240,11 @@ def test_get_meter_comparison_with_attributes(self): self.assertTrue( meter3._instrumentation_scope > meter4._instrumentation_scope ) + self.assertTrue( + isinstance( + meter4._instrumentation_scope.attributes, BoundedAttributes + ), + ) def test_shutdown(self):