From 116f98df72aac6c620217d5060089a737e21b4cd Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Thu, 21 Nov 2024 17:52:22 +0100 Subject: [PATCH 1/2] opentelemetry-instrumentation-httpx: drop unused private classes (#3036) --- CHANGELOG.md | 2 + .../instrumentation/httpx/__init__.py | 71 ------------------- 2 files changed, 2 insertions(+), 71 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1298977849..51ecc94d8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#2971](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2971)) - Drop `opentelemetry-instrumentation-test` package from default instrumentation list ([#2969](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2969)) +- `opentelemetry-instrumentation-httpx`: remove private unused `_InstrumentedClient` and `_InstrumentedAsyncClient` classes + ([#3036](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3036)) ## Version 1.28.0/0.49b0 (2024-11-05) diff --git a/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py b/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py index 92e044bf06..195c784408 100644 --- a/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py @@ -641,77 +641,6 @@ async def aclose(self) -> None: await self._transport.aclose() -class _InstrumentedClient(httpx.Client): - _tracer_provider = None - _request_hook = None - _response_hook = None - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - - self._original_transport = self._transport - self._original_mounts = self._mounts.copy() - self._is_instrumented_by_opentelemetry = True - - self._transport = SyncOpenTelemetryTransport( - self._transport, - tracer_provider=_InstrumentedClient._tracer_provider, - request_hook=_InstrumentedClient._request_hook, - response_hook=_InstrumentedClient._response_hook, - ) - self._mounts.update( - { - url_pattern: ( - SyncOpenTelemetryTransport( - transport, - tracer_provider=_InstrumentedClient._tracer_provider, - request_hook=_InstrumentedClient._request_hook, - response_hook=_InstrumentedClient._response_hook, - ) - if transport is not None - else transport - ) - for url_pattern, transport in self._original_mounts.items() - } - ) - - -class _InstrumentedAsyncClient(httpx.AsyncClient): - _tracer_provider = None - _request_hook = None - _response_hook = None - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - - self._original_transport = self._transport - self._original_mounts = self._mounts.copy() - self._is_instrumented_by_opentelemetry = True - - self._transport = AsyncOpenTelemetryTransport( - self._transport, - tracer_provider=_InstrumentedAsyncClient._tracer_provider, - request_hook=_InstrumentedAsyncClient._request_hook, - response_hook=_InstrumentedAsyncClient._response_hook, - ) - - self._mounts.update( - { - url_pattern: ( - AsyncOpenTelemetryTransport( - transport, - tracer_provider=_InstrumentedAsyncClient._tracer_provider, - request_hook=_InstrumentedAsyncClient._request_hook, - response_hook=_InstrumentedAsyncClient._response_hook, - ) - if transport is not None - else transport - ) - for url_pattern, transport in self._original_mounts.items() - } - ) - - class HTTPXClientInstrumentor(BaseInstrumentor): # pylint: disable=protected-access,attribute-defined-outside-init """An instrumentor for httpx Client and AsyncClient From d9e14487b2bc2bdcc638d635091bfe2019ecbc2d Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Thu, 21 Nov 2024 23:12:51 +0100 Subject: [PATCH 2/2] opentelemetry-instrumentation-system-metrics: fix typo in metrics configs (#3025) --- CHANGELOG.md | 2 ++ .../instrumentation/system_metrics/__init__.py | 5 +++-- .../tests/test_system_metrics.py | 12 ++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51ecc94d8c..b5339972d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `opentelemetry-instrumentation-httpx`: instrument_client is a static method again ([#3003](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3003)) +- `opentelemetry-instrumentation-system_metrics`: fix callbacks reading wrong config + ([#3025](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3025)) - `opentelemetry-instrumentation-httpx`: Check if mount transport is none before wrap it ([#3022](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3022)) diff --git a/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py b/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py index 4e1ee2a5df..aff86ea77b 100644 --- a/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py @@ -40,6 +40,7 @@ "process.runtime.thread_count": None, "process.runtime.cpu.utilization": None, "process.runtime.context_switches": ["involuntary", "voluntary"], + "process.open_file_descriptor.count": None, } Usage @@ -595,7 +596,7 @@ def _get_system_network_packets( """Observer callback for network packets""" for device, counters in psutil.net_io_counters(pernic=True).items(): - for metric in self._config["system.network.dropped.packets"]: + for metric in self._config["system.network.packets"]: recv_sent = {"receive": "recv", "transmit": "sent"}[metric] if hasattr(counters, f"packets_{recv_sent}"): self._system_network_packets_labels["device"] = device @@ -626,7 +627,7 @@ def _get_system_network_io( """Observer callback for network IO""" for device, counters in psutil.net_io_counters(pernic=True).items(): - for metric in self._config["system.network.dropped.packets"]: + for metric in self._config["system.network.io"]: recv_sent = {"receive": "recv", "transmit": "sent"}[metric] if hasattr(counters, f"bytes_{recv_sent}"): self._system_network_io_labels["device"] = device diff --git a/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py b/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py index 83abcff4c0..92c30a66f0 100644 --- a/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py +++ b/instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py @@ -20,6 +20,7 @@ from unittest import mock, skipIf from opentelemetry.instrumentation.system_metrics import ( + _DEFAULT_CONFIG, SystemMetricsInstrumentor, ) from opentelemetry.sdk.metrics import MeterProvider @@ -865,3 +866,14 @@ def test_open_file_descriptor_count(self, mock_process_num_fds): expected, ) mock_process_num_fds.assert_called() + + +class TestConfigSystemMetrics(TestBase): + # pylint:disable=no-self-use + def test_that_correct_config_is_read(self): + for key, value in _DEFAULT_CONFIG.items(): + meter_provider = MeterProvider([InMemoryMetricReader()]) + instrumentor = SystemMetricsInstrumentor(config={key: value}) + instrumentor.instrument(meter_provider=meter_provider) + meter_provider.force_flush() + instrumentor.uninstrument()