Skip to content

Commit

Permalink
Setup eventloggerprovider in azure monitor distro (#38543)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Nov 15, 2024
1 parent 02c88cf commit 54d65c1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 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

- Distro to automatically configure event logger provider
([#38543](https://github.com/Azure/azure-sdk-for-python/pull/38543))

### Breaking Changes

### Bugs Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from logging import getLogger
from typing import Dict, List, cast

from opentelemetry._events import set_event_logger_provider
from opentelemetry._logs import set_logger_provider
from opentelemetry.instrumentation.dependencies import (
get_dist_dependency_conflicts,
Expand All @@ -15,6 +16,7 @@
BaseInstrumentor,
)
from opentelemetry.metrics import set_meter_provider
from opentelemetry.sdk._events import EventLoggerProvider
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from opentelemetry.sdk.metrics import MeterProvider
Expand Down Expand Up @@ -172,6 +174,10 @@ def _setup_logging(configurations: Dict[str, ConfigurationValue]):
logger_name: str = configurations[LOGGER_NAME_ARG] # type: ignore
getLogger(logger_name).addHandler(handler)

# Setup EventLoggerProvider
event_provider = EventLoggerProvider(logger_provider)
set_event_logger_provider(event_provider)


def _setup_metrics(configurations: Dict[str, ConfigurationValue]):
resource: Resource = configurations[RESOURCE_ARG] # type: ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ pytest
django
fastapi-slim
flask
psycopg2-binary; python_version >= '3.9'
psycopg2-binary
requests
urllib3
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
# Licensed under the MIT License. See License in the project root for
# license information.
# --------------------------------------------------------------------------
import platform
import os
import pytest
import sys
import unittest

# Skip for Python v3.13 until https://github.com/psycopg/psycopg2/pull/1729 is resolved
# Skip for Python v3.8 on windows due to https://github.com/psycopg/psycopg/issues/936
if not (platform.system() == "Windows" and sys.version_info < (3, 9)) and sys.implementation.name != "pypy":
if (os.name != "nt" or sys.version_info > (3, 8)) and sys.implementation.name != "pypy":
from opentelemetry.instrumentation.psycopg2 import (
Psycopg2Instrumentor,
)
Expand All @@ -19,7 +19,7 @@
class TestPsycopg2Instrumentation(unittest.TestCase):

@pytest.mark.skipif(
platform.system() == "Windows" and sys.version_info < (3, 9) or sys.implementation.name == "pypy",
(os.name == "nt" and sys.version_info < (3, 9)) or sys.implementation.name == "pypy",
reason="Psycopg2 not supported for pypy, Windows Py3.8",
)
def test_instrument(self):
Expand Down
13 changes: 13 additions & 0 deletions sdk/monitor/azure-monitor-opentelemetry/tests/test_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,13 @@ def test_setup_tracing(
tp_init_mock.add_span_processor.assert_has_calls([call(custom_sp), call(bsp_init_mock)])
self.assertEqual(azure_core_mock.tracing_implementation, OpenTelemetrySpan)

@patch(
"azure.monitor.opentelemetry._configure.set_event_logger_provider",
)
@patch(
"azure.monitor.opentelemetry._configure.EventLoggerProvider",
autospec=True,
)
@patch(
"azure.monitor.opentelemetry._configure.getLogger",
)
Expand Down Expand Up @@ -330,10 +337,14 @@ def test_setup_logging(
blrp_mock,
logging_handler_mock,
get_logger_mock,
elp_mock,
set_elp_mock,
):
lp_init_mock = Mock()
lp_mock.return_value = lp_init_mock
log_exp_init_mock = Mock()
elp_init_mock = Mock()
elp_mock.return_value = elp_init_mock
log_exporter_mock.return_value = log_exp_init_mock
blrp_init_mock = Mock()
blrp_mock.return_value = blrp_init_mock
Expand All @@ -359,6 +370,8 @@ def test_setup_logging(
logging_handler_mock.assert_called_once_with(logger_provider=lp_init_mock)
get_logger_mock.assert_called_once_with("test")
logger_mock.addHandler.assert_called_once_with(logging_handler_init_mock)
elp_mock.assert_called_once_with(lp_init_mock)
set_elp_mock.assert_called_once_with(elp_init_mock)

@patch(
"azure.monitor.opentelemetry._configure.PeriodicExportingMetricReader",
Expand Down

0 comments on commit 54d65c1

Please sign in to comment.