Skip to content

Commit

Permalink
Configure exporters and sampler directly in configurator
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydvoss committed Jun 3, 2024
1 parent 16fbea0 commit 5db952d
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 40 deletions.
3 changes: 3 additions & 0 deletions sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

### Features Added

- Rework autoinstrumentation: Configure exporters and samplers directly
([#35890](https://github.com/Azure/azure-sdk-for-python/pull/35890))

### Breaking Changes

### Bugs Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
# --------------------------------------------------------------------------


from os import environ
from warnings import warn

from opentelemetry.environment_variables import (
OTEL_LOGS_EXPORTER,
OTEL_METRICS_EXPORTER,
OTEL_TRACES_EXPORTER,
)
from opentelemetry.sdk._configuration import _OTelSDKConfigurator

from azure.monitor.opentelemetry.exporter import ApplicationInsightsSampler # pylint: disable=import-error,no-name-in-module
from azure.monitor.opentelemetry.exporter._utils import _is_attach_enabled # pylint: disable=import-error,no-name-in-module
from azure.monitor.opentelemetry._constants import _PREVIEW_ENTRY_POINT_WARNING
from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import (
Expand All @@ -26,6 +33,17 @@ def _configure(self, **kwargs):
if not _is_attach_enabled():
warn(_PREVIEW_ENTRY_POINT_WARNING)
try:
if environ.get(OTEL_TRACES_EXPORTER, "").lower().strip() != "none":
kwargs.setdefault("trace_exporter_names", ["azure-monitor-opentelemetry-exporter"])
try:
sample_rate = float(environ.get("OTEL_TRACES_SAMPLER_ARG"))
except: # pylint: disable=bare-except
sample_rate = 1.0
kwargs.setdefault("sampler", ApplicationInsightsSampler(sample_rate))
if environ.get(OTEL_METRICS_EXPORTER, "").lower().strip() != "none":
kwargs.setdefault("metric_exporter_names", ["azure-monitor-opentelemetry-exporter"])
if environ.get(OTEL_LOGS_EXPORTER, "").lower().strip() != "none":
kwargs.setdefault("log_exporter_names", ["azure-monitor-opentelemetry-exporter"])
super()._configure(**kwargs)
AzureStatusLogger.log_status(True)
AzureDiagnosticLogging.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
from os import environ
from warnings import warn

from opentelemetry.environment_variables import (
OTEL_LOGS_EXPORTER,
OTEL_METRICS_EXPORTER,
OTEL_TRACES_EXPORTER,
)
from opentelemetry.instrumentation.distro import ( # type: ignore
BaseDistro,
)
from opentelemetry.sdk.environment_variables import (
_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED,
OTEL_EXPERIMENTAL_RESOURCE_DETECTORS,
OTEL_TRACES_SAMPLER,
)

from azure.core.settings import settings
Expand Down Expand Up @@ -62,18 +56,6 @@ def _configure(self, **kwargs) -> None:


def _configure_auto_instrumentation() -> None:
environ.setdefault(
OTEL_METRICS_EXPORTER, "azure_monitor_opentelemetry_exporter"
)
environ.setdefault(
OTEL_TRACES_EXPORTER, "azure_monitor_opentelemetry_exporter"
)
environ.setdefault(
OTEL_LOGS_EXPORTER, "azure_monitor_opentelemetry_exporter"
)
environ.setdefault(
OTEL_TRACES_SAMPLER, "azure_monitor_opentelemetry_sampler"
)
environ.setdefault(
_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "true"
)
Expand Down
15 changes: 8 additions & 7 deletions sdk/monitor/azure-monitor-opentelemetry/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@
"azure-core<2.0.0,>=1.28.0",
"azure-core-tracing-opentelemetry~=1.0.0b11",
"azure-monitor-opentelemetry-exporter~=1.0.0b26",
"opentelemetry-instrumentation-django~=0.42b0",
"opentelemetry-instrumentation-fastapi~=0.42b0",
"opentelemetry-instrumentation-flask~=0.42b0",
"opentelemetry-instrumentation-psycopg2~=0.42b0",
"opentelemetry-instrumentation-requests~=0.42b0",
"opentelemetry-instrumentation-urllib~=0.42b0",
"opentelemetry-instrumentation-urllib3~=0.42b0",
"opentelemetry-instrumentation-django~=0.46b0",
"opentelemetry-instrumentation-fastapi~=0.46b0",
"opentelemetry-instrumentation-flask~=0.46b0",
"opentelemetry-instrumentation-psycopg2~=0.46b0",
"opentelemetry-instrumentation-requests~=0.46b0",
"opentelemetry-instrumentation-urllib~=0.46b0",
"opentelemetry-instrumentation-urllib3~=0.46b0",
"opentelemetry-resource-detector-azure~=0.1.4",
"opentelemetry-sdk~=1.25.0",
],
entry_points={
"opentelemetry_distro": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,85 @@


class TestConfigurator(TestCase):
@patch.dict("os.environ", {}, clear=True)
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator.super")
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator.ApplicationInsightsSampler")
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=True)
@patch(
"azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging"
)
def test_configure(self, mock_diagnostics, attach_mock):
def test_configure(self, mock_diagnostics, attach_mock, sampler_mock, super_mock):
sampler_mock.return_value = "TEST_SAMPLER"
configurator = AzureMonitorConfigurator()
with warnings.catch_warnings():
warnings.simplefilter("error")
configurator._configure()
configurator._configure(auto_instrumentation_version="TEST_VERSION")
sampler_mock.assert_called_once_with(1.0)
super_mock()._configure.assert_called_once_with(
auto_instrumentation_version="TEST_VERSION",
trace_exporter_names=["azure-monitor-opentelemetry-exporter"],
metric_exporter_names=["azure-monitor-opentelemetry-exporter"],
log_exporter_names=["azure-monitor-opentelemetry-exporter"],
sampler="TEST_SAMPLER",
)
mock_diagnostics.info.assert_called_once_with(
"Azure Monitor Configurator configured successfully.",
_ATTACH_SUCCESS_CONFIGURATOR
)


@patch.dict("os.environ", {"OTEL_TRACES_SAMPLER_ARG": "0.5"}, clear=True)
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator.super")
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator.ApplicationInsightsSampler")
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=True)
@patch(
"azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging"
)
def test_configure_sampler_arg(self, mock_diagnostics, attach_mock, sampler_mock, super_mock):
sampler_mock.return_value = "TEST_SAMPLER"
configurator = AzureMonitorConfigurator()
with warnings.catch_warnings():
warnings.simplefilter("error")
configurator._configure(auto_instrumentation_version="TEST_VERSION")
sampler_mock.assert_called_once_with(0.5)
super_mock()._configure.assert_called_once_with(
auto_instrumentation_version="TEST_VERSION",
trace_exporter_names=["azure-monitor-opentelemetry-exporter"],
metric_exporter_names=["azure-monitor-opentelemetry-exporter"],
log_exporter_names=["azure-monitor-opentelemetry-exporter"],
sampler="TEST_SAMPLER",
)
mock_diagnostics.info.assert_called_once_with(
"Azure Monitor Configurator configured successfully.",
_ATTACH_SUCCESS_CONFIGURATOR
)


@patch.dict("os.environ", {}, clear=True)
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator.super")
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator.ApplicationInsightsSampler")
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=False)
@patch(
"azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging"
)
def test_configure_preview(self, mock_diagnostics, attach_mock):
def test_configure_preview(self, mock_diagnostics, attach_mock, sampler_mock, super_mock):
sampler_mock.return_value = "TEST_SAMPLER"
configurator = AzureMonitorConfigurator()
with self.assertWarns(Warning):
configurator._configure()
sampler_mock.assert_called_once_with(1.0)
super_mock()._configure.assert_called_once_with(
trace_exporter_names=["azure-monitor-opentelemetry-exporter"],
metric_exporter_names=["azure-monitor-opentelemetry-exporter"],
log_exporter_names=["azure-monitor-opentelemetry-exporter"],
sampler="TEST_SAMPLER",
)
mock_diagnostics.info.assert_called_once_with(
"Azure Monitor Configurator configured successfully.",
_ATTACH_SUCCESS_CONFIGURATOR
)

@patch.dict("os.environ", {}, clear=True)
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator.super")
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=True)
@patch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,16 @@ def test_configure(self, mock_diagnostics, azure_core_mock, attach_mock):
self.assertEqual(
environ,
{
"OTEL_METRICS_EXPORTER": "azure_monitor_opentelemetry_exporter",
"OTEL_TRACES_EXPORTER": "azure_monitor_opentelemetry_exporter",
"OTEL_LOGS_EXPORTER": "azure_monitor_opentelemetry_exporter",
"OTEL_TRACES_SAMPLER": "azure_monitor_opentelemetry_sampler",
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "true",
"OTEL_EXPERIMENTAL_RESOURCE_DETECTORS": "azure_app_service",
}
)

@patch.dict("os.environ", {
"OTEL_METRICS_EXPORTER": "custom_metrics_exporter",
"OTEL_TRACES_EXPORTER": "custom_traces_exporter",
"OTEL_LOGS_EXPORTER": "custom_logs_exporter",
"OTEL_TRACES_SAMPLER": "custom_traces_sampler",
# "OTEL_METRICS_EXPORTER": "custom_metrics_exporter",
# "OTEL_TRACES_EXPORTER": "custom_traces_exporter",
# "OTEL_LOGS_EXPORTER": "custom_logs_exporter",
# "OTEL_TRACES_SAMPLER": "custom_traces_sampler",
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "false",
"OTEL_EXPERIMENTAL_RESOURCE_DETECTORS": "custom_resource_detector",
}, clear=True)
Expand All @@ -72,10 +68,10 @@ def test_configure_env_vars_set(self, mock_diagnostics, azure_core_mock, attach_
self.assertEqual(
environ,
{
"OTEL_METRICS_EXPORTER": "custom_metrics_exporter",
"OTEL_TRACES_EXPORTER": "custom_traces_exporter",
"OTEL_LOGS_EXPORTER": "custom_logs_exporter",
"OTEL_TRACES_SAMPLER": "custom_traces_sampler",
# "OTEL_METRICS_EXPORTER": "custom_metrics_exporter",
# "OTEL_TRACES_EXPORTER": "custom_traces_exporter",
# "OTEL_LOGS_EXPORTER": "custom_logs_exporter",
# "OTEL_TRACES_SAMPLER": "custom_traces_sampler",
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "false",
"OTEL_EXPERIMENTAL_RESOURCE_DETECTORS": "custom_resource_detector",
}
Expand Down

0 comments on commit 5db952d

Please sign in to comment.