Skip to content

Commit acc11a0

Browse files
authored
Add line-specific filter to catch warning (#3164)
1 parent e183935 commit acc11a0

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import math
1516
import os
1617
from abc import ABC, abstractmethod
1718
from enum import Enum
@@ -23,7 +24,6 @@
2324
from typing import IO, Callable, Dict, Iterable, Optional
2425

2526
from typing_extensions import final
26-
import math
2727

2828
# This kind of import is needed to avoid Sphinx errors.
2929
import opentelemetry.sdk.metrics._internal

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
Type,
3838
Union,
3939
)
40+
from warnings import filterwarnings
4041

4142
from deprecated import deprecated
4243

@@ -1167,16 +1168,29 @@ def get_tracer(
11671168
logger.error("get_tracer called with missing module name.")
11681169
if instrumenting_library_version is None:
11691170
instrumenting_library_version = ""
1171+
1172+
filterwarnings(
1173+
"ignore",
1174+
message=(
1175+
r"Call to deprecated method __init__. \(You should use "
1176+
r"InstrumentationScope\) -- Deprecated since version 1.11.1."
1177+
),
1178+
category=DeprecationWarning,
1179+
module="opentelemetry.sdk.trace",
1180+
)
1181+
1182+
instrumentation_info = InstrumentationInfo(
1183+
instrumenting_module_name,
1184+
instrumenting_library_version,
1185+
schema_url,
1186+
)
1187+
11701188
return Tracer(
11711189
self.sampler,
11721190
self.resource,
11731191
self._active_span_processor,
11741192
self.id_generator,
1175-
InstrumentationInfo(
1176-
instrumenting_module_name,
1177-
instrumenting_library_version,
1178-
schema_url,
1179-
),
1193+
instrumentation_info,
11801194
self._span_limits,
11811195
InstrumentationScope(
11821196
instrumenting_module_name,

opentelemetry-sdk/tests/metrics/test_periodic_exporting_metric_reader.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import math
1516
from time import sleep, time_ns
1617
from typing import Sequence
1718
from unittest.mock import Mock
1819

1920
from flaky import flaky
20-
import math
2121

2222
from opentelemetry.sdk.metrics import Counter
2323
from opentelemetry.sdk.metrics._internal import _Counter
@@ -139,7 +139,9 @@ def test_ticker_not_called_on_infinity(self):
139139
collect_mock = Mock()
140140
exporter = FakeMetricsExporter()
141141
exporter.export = Mock()
142-
pmr = PeriodicExportingMetricReader(exporter, export_interval_millis=math.inf)
142+
pmr = PeriodicExportingMetricReader(
143+
exporter, export_interval_millis=math.inf
144+
)
143145
pmr._set_collect_callback(collect_mock)
144146
sleep(0.1)
145147
self.assertTrue(collect_mock.assert_not_called)
@@ -149,16 +151,20 @@ def test_ticker_value_exception_on_zero(self):
149151
exporter = FakeMetricsExporter()
150152
exporter.export = Mock()
151153
self.assertRaises(
152-
ValueError, PeriodicExportingMetricReader,
153-
exporter, export_interval_millis=0
154+
ValueError,
155+
PeriodicExportingMetricReader,
156+
exporter,
157+
export_interval_millis=0,
154158
)
155159

156160
def test_ticker_value_exception_on_negative(self):
157161
exporter = FakeMetricsExporter()
158162
exporter.export = Mock()
159163
self.assertRaises(
160-
ValueError, PeriodicExportingMetricReader,
161-
exporter, export_interval_millis=-100
164+
ValueError,
165+
PeriodicExportingMetricReader,
166+
exporter,
167+
export_interval_millis=-100,
162168
)
163169

164170
@flaky(max_runs=3, min_passes=1)

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from time import time_ns
2424
from typing import Optional
2525
from unittest import mock
26+
from unittest.mock import Mock
2627

2728
from opentelemetry import trace as trace_api
2829
from opentelemetry.context import Context
@@ -39,7 +40,7 @@
3940
OTEL_TRACES_SAMPLER,
4041
OTEL_TRACES_SAMPLER_ARG,
4142
)
42-
from opentelemetry.sdk.trace import Resource
43+
from opentelemetry.sdk.trace import Resource, TracerProvider
4344
from opentelemetry.sdk.trace.id_generator import RandomIdGenerator
4445
from opentelemetry.sdk.trace.sampling import (
4546
ALWAYS_OFF,
@@ -48,7 +49,7 @@
4849
ParentBased,
4950
StaticSampler,
5051
)
51-
from opentelemetry.sdk.util import ns_to_iso_str
52+
from opentelemetry.sdk.util import BoundedDict, ns_to_iso_str
5253
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
5354
from opentelemetry.test.spantestutil import (
5455
get_span_with_dropped_attributes_events_links,
@@ -58,6 +59,17 @@
5859

5960

6061
class TestTracer(unittest.TestCase):
62+
def test_no_deprecated_warning(self):
63+
with self.assertRaises(AssertionError):
64+
with self.assertWarns(DeprecationWarning):
65+
TracerProvider(Mock(), Mock()).get_tracer(Mock(), Mock())
66+
67+
# This is being added here to make sure the filter on
68+
# InstrumentationInfo does not affect other DeprecationWarnings that
69+
# may be raised.
70+
with self.assertWarns(DeprecationWarning):
71+
BoundedDict(0)
72+
6173
def test_extends_api(self):
6274
tracer = new_tracer()
6375
self.assertIsInstance(tracer, trace.Tracer)

0 commit comments

Comments
 (0)