Skip to content

Commit 5acab91

Browse files
committed
test: add coverage for the configuration attribute
Covers BaseInstrumentor.configuration defaulting to None and being overridable on subclasses, and URLLib3Instrumentor's wiring of URLLib3InstrumentorConfig as its configuration dataclass.
1 parent 60d97f7 commit 5acab91

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import json
55
import typing
6+
import unittest
67
from unittest import mock
78

89
import urllib3
@@ -19,6 +20,7 @@
1920
from opentelemetry.instrumentation.urllib3 import (
2021
RequestInfo,
2122
URLLib3Instrumentor,
23+
URLLib3InstrumentorConfig,
2224
)
2325
from opentelemetry.instrumentation.utils import (
2426
suppress_http_instrumentation,
@@ -1022,3 +1024,29 @@ def test_urlopen_mixed_args(self):
10221024
self.assertEqual(
10231025
span.attributes["http.request.header.x_test"], ("Value",)
10241026
)
1027+
1028+
1029+
class TestURLLib3InstrumentorConfig(unittest.TestCase):
1030+
def test_configuration_attribute_is_config_dataclass(self):
1031+
self.assertIs(
1032+
URLLib3Instrumentor.configuration, URLLib3InstrumentorConfig
1033+
)
1034+
1035+
def test_default_fields_are_none(self):
1036+
config = URLLib3InstrumentorConfig()
1037+
self.assertIsNone(config.excluded_urls)
1038+
self.assertIsNone(config.captured_request_headers)
1039+
self.assertIsNone(config.captured_response_headers)
1040+
self.assertIsNone(config.sensitive_headers)
1041+
1042+
def test_fields_accept_declared_values(self):
1043+
config = URLLib3InstrumentorConfig(
1044+
excluded_urls="/healthz",
1045+
captured_request_headers=["X-Test"],
1046+
captured_response_headers=["X-Response"],
1047+
sensitive_headers=["Authorization"],
1048+
)
1049+
self.assertEqual(config.excluded_urls, "/healthz")
1050+
self.assertEqual(config.captured_request_headers, ["X-Test"])
1051+
self.assertEqual(config.captured_response_headers, ["X-Response"])
1052+
self.assertEqual(config.sensitive_headers, ["Authorization"])

opentelemetry-instrumentation/tests/test_instrumentor.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# SPDX-License-Identifier: Apache-2.0
33
# type: ignore
44

5+
from __future__ import annotations
6+
7+
from dataclasses import dataclass
58
from logging import WARNING
69
from unittest import TestCase
710
from unittest.mock import patch
@@ -43,6 +46,21 @@ def test_protect(self):
4346
def test_singleton(self):
4447
self.assertIs(self.Instrumentor(), self.Instrumentor())
4548

49+
def test_configuration_defaults_to_none(self):
50+
self.assertIsNone(BaseInstrumentor.configuration)
51+
self.assertIsNone(self.Instrumentor().configuration)
52+
53+
def test_configuration_can_be_overridden(self):
54+
@dataclass
55+
class FooConfig:
56+
bar: str | None = None
57+
58+
class ConfiguredInstrumentor(self.Instrumentor):
59+
configuration = FooConfig
60+
61+
self.assertIs(ConfiguredInstrumentor.configuration, FooConfig)
62+
self.assertIsNone(self.Instrumentor.configuration)
63+
4664
@patch("opentelemetry.instrumentation.instrumentor._LOG")
4765
@patch(
4866
"opentelemetry.instrumentation.instrumentor.BaseInstrumentor._check_dependency_conflicts"

0 commit comments

Comments
 (0)