Skip to content

Commit

Permalink
Create a single resource instance (#3118)
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanthccv authored Feb 6, 2023
1 parent e0e6a3a commit 209093b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 41 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bump min required api version for OTLP exporters
([#3156](https://github.com/open-telemetry/opentelemetry-python/pull/3156))

- Create a single resource instance
([#3118](https://github.com/open-telemetry/opentelemetry-python/pull/3118))

## Version 1.15.0/0.36b0 (2022-12-09)

- PeriodicExportingMetricsReader with +Inf interval
Expand Down
55 changes: 19 additions & 36 deletions opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,12 @@ def _init_tracing(
exporters: Dict[str, Type[SpanExporter]],
id_generator: IdGenerator = None,
sampler: Sampler = None,
auto_instrumentation_version: Optional[str] = None,
resource: Resource = None,
):
# if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name
# from the env variable else defaults to "unknown_service"
auto_resource = {}
# populate version if using auto-instrumentation
if auto_instrumentation_version:
auto_resource[
ResourceAttributes.TELEMETRY_AUTO_VERSION
] = auto_instrumentation_version
provider = TracerProvider(
id_generator=id_generator,
sampler=sampler,
resource=Resource.create(auto_resource),
resource=resource,
)
set_tracer_provider(provider)

Expand All @@ -219,17 +211,8 @@ def _init_tracing(

def _init_metrics(
exporters: Dict[str, Type[MetricExporter]],
auto_instrumentation_version: Optional[str] = None,
resource: Resource = None,
):
# if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name
# from the env variable else defaults to "unknown_service"
auto_resource = {}
# populate version if using auto-instrumentation
if auto_instrumentation_version:
auto_resource[
ResourceAttributes.TELEMETRY_AUTO_VERSION
] = auto_instrumentation_version

metric_readers = []

for _, exporter_class in exporters.items():
Expand All @@ -238,25 +221,15 @@ def _init_metrics(
PeriodicExportingMetricReader(exporter_class(**exporter_args))
)

provider = MeterProvider(
resource=Resource.create(auto_resource), metric_readers=metric_readers
)
provider = MeterProvider(resource=resource, metric_readers=metric_readers)
set_meter_provider(provider)


def _init_logging(
exporters: Dict[str, Type[LogExporter]],
auto_instrumentation_version: Optional[str] = None,
resource: Resource = None,
):
# if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name
# from the env variable else defaults to "unknown_service"
auto_resource = {}
# populate version if using auto-instrumentation
if auto_instrumentation_version:
auto_resource[
ResourceAttributes.TELEMETRY_AUTO_VERSION
] = auto_instrumentation_version
provider = LoggerProvider(resource=Resource.create(auto_resource))
provider = LoggerProvider(resource=resource)
set_logger_provider(provider)

for _, exporter_class in exporters.items():
Expand Down Expand Up @@ -359,18 +332,28 @@ def _initialize_components(auto_instrumentation_version):
sampler = _import_sampler(sampler_name)
id_generator_name = _get_id_generator()
id_generator = _import_id_generator(id_generator_name)
# if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name
# from the env variable else defaults to "unknown_service"
auto_resource = {}
# populate version if using auto-instrumentation
if auto_instrumentation_version:
auto_resource[
ResourceAttributes.TELEMETRY_AUTO_VERSION
] = auto_instrumentation_version
resource = Resource.create(auto_resource)

_init_tracing(
exporters=trace_exporters,
id_generator=id_generator,
sampler=sampler,
auto_instrumentation_version=auto_instrumentation_version,
resource=resource,
)
_init_metrics(metric_exporters, auto_instrumentation_version)
_init_metrics(metric_exporters, resource)
logging_enabled = os.getenv(
_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "false"
)
if logging_enabled.strip().lower() == "true":
_init_logging(log_exporters, auto_instrumentation_version)
_init_logging(log_exporters, resource)


class _BaseConfigurator(ABC):
Expand Down
56 changes: 51 additions & 5 deletions opentelemetry-sdk/tests/test_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,16 @@ def tearDown(self):
environ, {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-test-service"}
)
def test_trace_init_default(self):

auto_resource = Resource.create(
{
"telemetry.auto.version": "test-version",
}
)
_init_tracing(
{"zipkin": Exporter},
id_generator=RandomIdGenerator(),
auto_instrumentation_version="test-version",
resource=auto_resource,
)

self.assertEqual(self.set_provider_mock.call_count, 1)
Expand Down Expand Up @@ -578,7 +584,12 @@ def tearDown(self):
]

def test_logging_init_empty(self):
_init_logging({}, "auto-version")
auto_resource = Resource.create(
{
"telemetry.auto.version": "auto-version",
}
)
_init_logging({}, resource=auto_resource)
self.assertEqual(self.set_provider_mock.call_count, 1)
provider = self.set_provider_mock.call_args[0][0]
self.assertIsInstance(provider, DummyLoggerProvider)
Expand All @@ -593,7 +604,8 @@ def test_logging_init_empty(self):
{"OTEL_RESOURCE_ATTRIBUTES": "service.name=otlp-service"},
)
def test_logging_init_exporter(self):
_init_logging({"otlp": DummyOTLPLogExporter})
resource = Resource.create({})
_init_logging({"otlp": DummyOTLPLogExporter}, resource=resource)
self.assertEqual(self.set_provider_mock.call_count, 1)
provider = self.set_provider_mock.call_args[0][0]
self.assertIsInstance(provider, DummyLoggerProvider)
Expand Down Expand Up @@ -634,6 +646,34 @@ def test_logging_init_enable_env(self, logging_mock, tracing_mock):
self.assertEqual(logging_mock.call_count, 1)
self.assertEqual(tracing_mock.call_count, 1)

@patch.dict(
environ,
{
"OTEL_RESOURCE_ATTRIBUTES": "service.name=otlp-service",
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "True",
},
)
@patch("opentelemetry.sdk._configuration._init_tracing")
@patch("opentelemetry.sdk._configuration._init_logging")
@patch("opentelemetry.sdk._configuration._init_metrics")
def test_initialize_components_resource(
self, metrics_mock, logging_mock, tracing_mock
):
_initialize_components("auto-version")
self.assertEqual(logging_mock.call_count, 1)
self.assertEqual(tracing_mock.call_count, 1)
self.assertEqual(metrics_mock.call_count, 1)

_, args, _ = logging_mock.mock_calls[0]
logging_resource = args[1]
_, _, kwargs = tracing_mock.mock_calls[0]
tracing_resource = kwargs["resource"]
_, args, _ = metrics_mock.mock_calls[0]
metrics_resource = args[1]
self.assertEqual(logging_resource, tracing_resource)
self.assertEqual(logging_resource, metrics_resource)
self.assertEqual(tracing_resource, metrics_resource)


class TestMetricsInit(TestCase):
def setUp(self):
Expand All @@ -659,7 +699,12 @@ def tearDown(self):
self.provider_patch.stop()

def test_metrics_init_empty(self):
_init_metrics({}, "auto-version")
auto_resource = Resource.create(
{
"telemetry.auto.version": "auto-version",
}
)
_init_metrics({}, resource=auto_resource)
self.assertEqual(self.set_provider_mock.call_count, 1)
provider = self.set_provider_mock.call_args[0][0]
self.assertIsInstance(provider, DummyMeterProvider)
Expand All @@ -676,7 +721,8 @@ def test_metrics_init_empty(self):
{"OTEL_RESOURCE_ATTRIBUTES": "service.name=otlp-service"},
)
def test_metrics_init_exporter(self):
_init_metrics({"otlp": DummyOTLPMetricExporter})
resource = Resource.create({})
_init_metrics({"otlp": DummyOTLPMetricExporter}, resource=resource)
self.assertEqual(self.set_provider_mock.call_count, 1)
provider = self.set_provider_mock.call_args[0][0]
self.assertIsInstance(provider, DummyMeterProvider)
Expand Down

0 comments on commit 209093b

Please sign in to comment.