Skip to content

Commit 209093b

Browse files
authored
Create a single resource instance (#3118)
1 parent e0e6a3a commit 209093b

File tree

3 files changed

+73
-41
lines changed

3 files changed

+73
-41
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
- Bump min required api version for OTLP exporters
2424
([#3156](https://github.com/open-telemetry/opentelemetry-python/pull/3156))
2525

26+
- Create a single resource instance
27+
([#3118](https://github.com/open-telemetry/opentelemetry-python/pull/3118))
28+
2629
## Version 1.15.0/0.36b0 (2022-12-09)
2730

2831
- PeriodicExportingMetricsReader with +Inf interval

opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,12 @@ def _init_tracing(
193193
exporters: Dict[str, Type[SpanExporter]],
194194
id_generator: IdGenerator = None,
195195
sampler: Sampler = None,
196-
auto_instrumentation_version: Optional[str] = None,
196+
resource: Resource = None,
197197
):
198-
# if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name
199-
# from the env variable else defaults to "unknown_service"
200-
auto_resource = {}
201-
# populate version if using auto-instrumentation
202-
if auto_instrumentation_version:
203-
auto_resource[
204-
ResourceAttributes.TELEMETRY_AUTO_VERSION
205-
] = auto_instrumentation_version
206198
provider = TracerProvider(
207199
id_generator=id_generator,
208200
sampler=sampler,
209-
resource=Resource.create(auto_resource),
201+
resource=resource,
210202
)
211203
set_tracer_provider(provider)
212204

@@ -219,17 +211,8 @@ def _init_tracing(
219211

220212
def _init_metrics(
221213
exporters: Dict[str, Type[MetricExporter]],
222-
auto_instrumentation_version: Optional[str] = None,
214+
resource: Resource = None,
223215
):
224-
# if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name
225-
# from the env variable else defaults to "unknown_service"
226-
auto_resource = {}
227-
# populate version if using auto-instrumentation
228-
if auto_instrumentation_version:
229-
auto_resource[
230-
ResourceAttributes.TELEMETRY_AUTO_VERSION
231-
] = auto_instrumentation_version
232-
233216
metric_readers = []
234217

235218
for _, exporter_class in exporters.items():
@@ -238,25 +221,15 @@ def _init_metrics(
238221
PeriodicExportingMetricReader(exporter_class(**exporter_args))
239222
)
240223

241-
provider = MeterProvider(
242-
resource=Resource.create(auto_resource), metric_readers=metric_readers
243-
)
224+
provider = MeterProvider(resource=resource, metric_readers=metric_readers)
244225
set_meter_provider(provider)
245226

246227

247228
def _init_logging(
248229
exporters: Dict[str, Type[LogExporter]],
249-
auto_instrumentation_version: Optional[str] = None,
230+
resource: Resource = None,
250231
):
251-
# if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name
252-
# from the env variable else defaults to "unknown_service"
253-
auto_resource = {}
254-
# populate version if using auto-instrumentation
255-
if auto_instrumentation_version:
256-
auto_resource[
257-
ResourceAttributes.TELEMETRY_AUTO_VERSION
258-
] = auto_instrumentation_version
259-
provider = LoggerProvider(resource=Resource.create(auto_resource))
232+
provider = LoggerProvider(resource=resource)
260233
set_logger_provider(provider)
261234

262235
for _, exporter_class in exporters.items():
@@ -359,18 +332,28 @@ def _initialize_components(auto_instrumentation_version):
359332
sampler = _import_sampler(sampler_name)
360333
id_generator_name = _get_id_generator()
361334
id_generator = _import_id_generator(id_generator_name)
335+
# if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name
336+
# from the env variable else defaults to "unknown_service"
337+
auto_resource = {}
338+
# populate version if using auto-instrumentation
339+
if auto_instrumentation_version:
340+
auto_resource[
341+
ResourceAttributes.TELEMETRY_AUTO_VERSION
342+
] = auto_instrumentation_version
343+
resource = Resource.create(auto_resource)
344+
362345
_init_tracing(
363346
exporters=trace_exporters,
364347
id_generator=id_generator,
365348
sampler=sampler,
366-
auto_instrumentation_version=auto_instrumentation_version,
349+
resource=resource,
367350
)
368-
_init_metrics(metric_exporters, auto_instrumentation_version)
351+
_init_metrics(metric_exporters, resource)
369352
logging_enabled = os.getenv(
370353
_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "false"
371354
)
372355
if logging_enabled.strip().lower() == "true":
373-
_init_logging(log_exporters, auto_instrumentation_version)
356+
_init_logging(log_exporters, resource)
374357

375358

376359
class _BaseConfigurator(ABC):

opentelemetry-sdk/tests/test_configurator.py

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,16 @@ def tearDown(self):
309309
environ, {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-test-service"}
310310
)
311311
def test_trace_init_default(self):
312+
313+
auto_resource = Resource.create(
314+
{
315+
"telemetry.auto.version": "test-version",
316+
}
317+
)
312318
_init_tracing(
313319
{"zipkin": Exporter},
314320
id_generator=RandomIdGenerator(),
315-
auto_instrumentation_version="test-version",
321+
resource=auto_resource,
316322
)
317323

318324
self.assertEqual(self.set_provider_mock.call_count, 1)
@@ -578,7 +584,12 @@ def tearDown(self):
578584
]
579585

580586
def test_logging_init_empty(self):
581-
_init_logging({}, "auto-version")
587+
auto_resource = Resource.create(
588+
{
589+
"telemetry.auto.version": "auto-version",
590+
}
591+
)
592+
_init_logging({}, resource=auto_resource)
582593
self.assertEqual(self.set_provider_mock.call_count, 1)
583594
provider = self.set_provider_mock.call_args[0][0]
584595
self.assertIsInstance(provider, DummyLoggerProvider)
@@ -593,7 +604,8 @@ def test_logging_init_empty(self):
593604
{"OTEL_RESOURCE_ATTRIBUTES": "service.name=otlp-service"},
594605
)
595606
def test_logging_init_exporter(self):
596-
_init_logging({"otlp": DummyOTLPLogExporter})
607+
resource = Resource.create({})
608+
_init_logging({"otlp": DummyOTLPLogExporter}, resource=resource)
597609
self.assertEqual(self.set_provider_mock.call_count, 1)
598610
provider = self.set_provider_mock.call_args[0][0]
599611
self.assertIsInstance(provider, DummyLoggerProvider)
@@ -634,6 +646,34 @@ def test_logging_init_enable_env(self, logging_mock, tracing_mock):
634646
self.assertEqual(logging_mock.call_count, 1)
635647
self.assertEqual(tracing_mock.call_count, 1)
636648

649+
@patch.dict(
650+
environ,
651+
{
652+
"OTEL_RESOURCE_ATTRIBUTES": "service.name=otlp-service",
653+
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "True",
654+
},
655+
)
656+
@patch("opentelemetry.sdk._configuration._init_tracing")
657+
@patch("opentelemetry.sdk._configuration._init_logging")
658+
@patch("opentelemetry.sdk._configuration._init_metrics")
659+
def test_initialize_components_resource(
660+
self, metrics_mock, logging_mock, tracing_mock
661+
):
662+
_initialize_components("auto-version")
663+
self.assertEqual(logging_mock.call_count, 1)
664+
self.assertEqual(tracing_mock.call_count, 1)
665+
self.assertEqual(metrics_mock.call_count, 1)
666+
667+
_, args, _ = logging_mock.mock_calls[0]
668+
logging_resource = args[1]
669+
_, _, kwargs = tracing_mock.mock_calls[0]
670+
tracing_resource = kwargs["resource"]
671+
_, args, _ = metrics_mock.mock_calls[0]
672+
metrics_resource = args[1]
673+
self.assertEqual(logging_resource, tracing_resource)
674+
self.assertEqual(logging_resource, metrics_resource)
675+
self.assertEqual(tracing_resource, metrics_resource)
676+
637677

638678
class TestMetricsInit(TestCase):
639679
def setUp(self):
@@ -659,7 +699,12 @@ def tearDown(self):
659699
self.provider_patch.stop()
660700

661701
def test_metrics_init_empty(self):
662-
_init_metrics({}, "auto-version")
702+
auto_resource = Resource.create(
703+
{
704+
"telemetry.auto.version": "auto-version",
705+
}
706+
)
707+
_init_metrics({}, resource=auto_resource)
663708
self.assertEqual(self.set_provider_mock.call_count, 1)
664709
provider = self.set_provider_mock.call_args[0][0]
665710
self.assertIsInstance(provider, DummyMeterProvider)
@@ -676,7 +721,8 @@ def test_metrics_init_empty(self):
676721
{"OTEL_RESOURCE_ATTRIBUTES": "service.name=otlp-service"},
677722
)
678723
def test_metrics_init_exporter(self):
679-
_init_metrics({"otlp": DummyOTLPMetricExporter})
724+
resource = Resource.create({})
725+
_init_metrics({"otlp": DummyOTLPMetricExporter}, resource=resource)
680726
self.assertEqual(self.set_provider_mock.call_count, 1)
681727
provider = self.set_provider_mock.call_args[0][0]
682728
self.assertIsInstance(provider, DummyMeterProvider)

0 commit comments

Comments
 (0)