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

Create a single resource instance #3118

Merged
merged 15 commits into from
Feb 6, 2023
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
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):
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved

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