Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## 1.0.0 (2023-09-12)

### Features Added

- Add Azure resource detectors
([#32087](https://github.com/Azure/azure-sdk-for-python/pull/32087))

### Other Changes

- The `autoinstrumentation', 'diagnostics' and 'util' subnamespaces have been made internal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Licensed under the MIT License. See License in the project root for
# license information.
# --------------------------------------------------------------------------
import os

from logging import getLogger
from typing import Dict, cast

Expand All @@ -14,6 +16,7 @@
BaseInstrumentor,
)
from opentelemetry.metrics import set_meter_provider
from opentelemetry.sdk.environment_variables import OTEL_EXPERIMENTAL_RESOURCE_DETECTORS
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from opentelemetry.sdk.metrics import MeterProvider
Expand Down Expand Up @@ -54,6 +57,11 @@
"urllib3",
)

_SUPPORTED_RESOURCE_DETECTORS = (
"azure_app_service",
"azure_vm",
)


def configure_azure_monitor(**kwargs) -> None:
"""This function works as a configuration layer that allows the
Expand All @@ -77,6 +85,9 @@ def configure_azure_monitor(**kwargs) -> None:
disable_logging = configurations[DISABLE_LOGGING_ARG]
disable_metrics = configurations[DISABLE_METRICS_ARG]

# Setup resources
_setup_resources()

# Setup tracing pipeline
if not disable_tracing:
_setup_tracing(configurations)
Expand All @@ -94,6 +105,13 @@ def configure_azure_monitor(**kwargs) -> None:
# instanstiated in the other setup steps
_setup_instrumentations(configurations)

def _setup_resources():
detectors = os.environ.get(OTEL_EXPERIMENTAL_RESOURCE_DETECTORS, "")
if detectors:
detectors = detectors + ","
detectors += ",".join(_SUPPORTED_RESOURCE_DETECTORS)
os.environ[OTEL_EXPERIMENTAL_RESOURCE_DETECTORS] = detectors


def _setup_tracing(configurations: Dict[str, ConfigurationValue]):
sampling_ratio = configurations[SAMPLING_RATIO_ARG]
Expand Down
1 change: 1 addition & 0 deletions sdk/monitor/azure-monitor-opentelemetry/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"opentelemetry-instrumentation-requests~=0.41b0",
"opentelemetry-instrumentation-urllib~=0.41b0",
"opentelemetry-instrumentation-urllib3~=0.41b0",
"opentelemetry-resource-detector-azure~=0.1.0",
],
entry_points={
"opentelemetry_distro": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import unittest
from unittest.mock import Mock, patch

Expand All @@ -21,6 +21,7 @@
_setup_instrumentations,
_setup_logging,
_setup_metrics,
_setup_resources,
_setup_tracing,
configure_azure_monitor,
)
Expand All @@ -39,8 +40,12 @@ class TestConfigure(unittest.TestCase):
@patch(
"azure.monitor.opentelemetry._configure._setup_tracing",
)
@patch(
"azure.monitor.opentelemetry._configure._setup_resources",
)
def test_configure_azure_monitor(
self,
resource_mock,
tracing_mock,
logging_mock,
metrics_mock,
Expand All @@ -50,6 +55,7 @@ def test_configure_azure_monitor(
"connection_string": "test_cs",
}
configure_azure_monitor(**kwargs)
resource_mock.assert_called_once()
tracing_mock.assert_called_once()
logging_mock.assert_called_once()
metrics_mock.assert_called_once()
Expand All @@ -67,12 +73,16 @@ def test_configure_azure_monitor(
@patch(
"azure.monitor.opentelemetry._configure._setup_tracing",
)
@patch(
"azure.monitor.opentelemetry._configure._setup_resources",
)
@patch(
"azure.monitor.opentelemetry._configure._get_configurations",
)
def test_configure_azure_monitor_disable_tracing(
self,
config_mock,
resource_mock,
tracing_mock,
logging_mock,
metrics_mock,
Expand All @@ -86,6 +96,7 @@ def test_configure_azure_monitor_disable_tracing(
}
config_mock.return_value = configurations
configure_azure_monitor()
resource_mock.assert_called_once()
tracing_mock.assert_not_called()
logging_mock.assert_called_once_with(configurations)
metrics_mock.assert_called_once_with(configurations)
Expand All @@ -103,12 +114,16 @@ def test_configure_azure_monitor_disable_tracing(
@patch(
"azure.monitor.opentelemetry._configure._setup_tracing",
)
@patch(
"azure.monitor.opentelemetry._configure._setup_resources",
)
@patch(
"azure.monitor.opentelemetry._configure._get_configurations",
)
def test_configure_azure_monitor_disable_logging(
self,
config_mock,
resource_mock,
tracing_mock,
logging_mock,
metrics_mock,
Expand All @@ -122,6 +137,7 @@ def test_configure_azure_monitor_disable_logging(
}
config_mock.return_value = configurations
configure_azure_monitor()
resource_mock.assert_called_once()
tracing_mock.assert_called_once_with(configurations)
logging_mock.assert_not_called()
metrics_mock.assert_called_once_with(configurations)
Expand All @@ -139,12 +155,16 @@ def test_configure_azure_monitor_disable_logging(
@patch(
"azure.monitor.opentelemetry._configure._setup_tracing",
)
@patch(
"azure.monitor.opentelemetry._configure._setup_resources",
)
@patch(
"azure.monitor.opentelemetry._configure._get_configurations",
)
def test_configure_azure_monitor_disable_metrics(
self,
config_mock,
resource_mock,
tracing_mock,
logging_mock,
metrics_mock,
Expand All @@ -158,11 +178,28 @@ def test_configure_azure_monitor_disable_metrics(
}
config_mock.return_value = configurations
configure_azure_monitor()
resource_mock.assert_called_once()
tracing_mock.assert_called_once_with(configurations)
logging_mock.assert_called_once_with(configurations)
metrics_mock.assert_not_called()
instrumentation_mock.assert_called_once_with(configurations)

@patch.dict("os.environ", {"OTEL_EXPERIMENTAL_RESOURCE_DETECTORS": ""})
def test_setup_resources(self):
_setup_resources()
self.assertEqual(
os.environ["OTEL_EXPERIMENTAL_RESOURCE_DETECTORS"],
"azure_app_service,azure_vm"
)

@patch.dict("os.environ", {"OTEL_EXPERIMENTAL_RESOURCE_DETECTORS": "test_detector"})
def test_setup_resources_existing_detectors(self):
_setup_resources()
self.assertEqual(
os.environ["OTEL_EXPERIMENTAL_RESOURCE_DETECTORS"],
"test_detector,azure_app_service,azure_vm"
)

@patch(
"azure.monitor.opentelemetry._configure.settings",
)
Expand Down
1 change: 1 addition & 0 deletions shared_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ opentelemetry-instrumentation-psycopg2
opentelemetry-instrumentation-requests
opentelemetry-instrumentation-urllib
opentelemetry-instrumentation-urllib3
opentelemetry-resource-detector-azure
azure-nspkg
azure-ai-nspkg
azure-cognitiveservices-nspkg
Expand Down