Skip to content

Commit bb89e0a

Browse files
authored
Make create_gauge non-abstract method (#3817)
1 parent c2fc945 commit bb89e0a

File tree

4 files changed

+214
-3
lines changed

4 files changed

+214
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Make create_gauge non-abstract method
11+
([#3817](https://github.com/open-telemetry/opentelemetry-python/pull/3817))
1012
- Make `tracer.start_as_current_span()` decorator work with async functions
1113
([#3633](https://github.com/open-telemetry/opentelemetry-python/pull/3633))
1214
- Fix python 3.12 deprecation warning

opentelemetry-api/src/opentelemetry/metrics/_internal/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"""
4242

4343

44+
import warnings
4445
from abc import ABC, abstractmethod
4546
from logging import getLogger
4647
from os import environ
@@ -385,8 +386,7 @@ def create_histogram(
385386
description: A description for this instrument and what it measures.
386387
"""
387388

388-
@abstractmethod
389-
def create_gauge(
389+
def create_gauge( # type: ignore # pylint: disable=no-self-use
390390
self,
391391
name: str,
392392
unit: str = "",
@@ -400,6 +400,7 @@ def create_gauge(
400400
example, ``By`` for bytes. UCUM units are recommended.
401401
description: A description for this instrument and what it measures.
402402
"""
403+
warnings.warn("create_gauge() is not implemented and will be a no-op")
403404

404405
@abstractmethod
405406
def create_observable_gauge(

opentelemetry-api/tests/metrics/test_meter.py

-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ def test_create_gauge(self):
134134
"""
135135

136136
self.assertTrue(hasattr(Meter, "create_gauge"))
137-
self.assertTrue(Meter.create_gauge.__isabstractmethod__)
138137

139138
def test_create_observable_gauge(self):
140139
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# type: ignore
15+
16+
# NOTE: The tests in this file are intended to test the semver compatibility of the public API.
17+
# Any tests that fail here indicate that the public API has changed in a way that is not backwards compatible.
18+
# Either bump the major version of the API, or make the necessary changes to the API to remain semver compatible.
19+
20+
from typing import Optional
21+
22+
from opentelemetry.metrics import (
23+
Asynchronous,
24+
Counter,
25+
Histogram,
26+
Instrument,
27+
Meter,
28+
MeterProvider,
29+
ObservableCounter,
30+
ObservableGauge,
31+
ObservableUpDownCounter,
32+
Synchronous,
33+
UpDownCounter,
34+
_Gauge,
35+
)
36+
37+
38+
class MeterProviderImplTest(MeterProvider):
39+
def get_meter(
40+
self,
41+
name: str,
42+
version: Optional[str] = None,
43+
schema_url: Optional[str] = None,
44+
) -> Meter:
45+
return super().get_meter(name, version, schema_url)
46+
47+
48+
def test_meter_provider_subclass_instantiation():
49+
meter_provider = MeterProviderImplTest()
50+
assert isinstance(meter_provider, MeterProvider)
51+
52+
53+
class MeterImplTest(Meter):
54+
def create_counter(self, name, description, **kwargs):
55+
pass
56+
57+
def create_up_down_counter(self, name, description, **kwargs):
58+
pass
59+
60+
def create_observable_counter(self, name, description, **kwargs):
61+
pass
62+
63+
def create_histogram(self, name, description, **kwargs):
64+
pass
65+
66+
def create_observable_gauge(self, name, description, **kwargs):
67+
pass
68+
69+
def create_observable_up_down_counter(self, name, description, **kwargs):
70+
pass
71+
72+
73+
def test_meter_subclass_instantiation():
74+
meter = MeterImplTest("subclass_test")
75+
assert isinstance(meter, Meter)
76+
77+
78+
class SynchronousImplTest(Synchronous):
79+
def __init__(
80+
self, name: str, unit: str = "", description: str = ""
81+
) -> None:
82+
super().__init__(name, unit, description)
83+
84+
85+
def test_synchronous_subclass_instantiation():
86+
synchronous = SynchronousImplTest("subclass_test")
87+
assert isinstance(synchronous, Synchronous)
88+
89+
90+
class AsynchronousImplTest(Asynchronous):
91+
def __init__(
92+
self, name: str, unit: str = "", description: str = ""
93+
) -> None:
94+
super().__init__(name, unit, description)
95+
96+
97+
def test_asynchronous_subclass_instantiation():
98+
asynchronous = AsynchronousImplTest("subclass_test")
99+
assert isinstance(asynchronous, Asynchronous)
100+
101+
102+
class CounterImplTest(Counter):
103+
def __init__(
104+
self, name: str, unit: str = "", description: str = ""
105+
) -> None:
106+
super().__init__(name, unit, description)
107+
108+
def add(self, amount: int, **kwargs):
109+
pass
110+
111+
112+
def test_counter_subclass_instantiation():
113+
counter = CounterImplTest("subclass_test")
114+
assert isinstance(counter, Counter)
115+
116+
117+
class UpDownCounterImplTest(UpDownCounter):
118+
def __init__(
119+
self, name: str, unit: str = "", description: str = ""
120+
) -> None:
121+
super().__init__(name, unit, description)
122+
123+
def add(self, amount: int, **kwargs):
124+
pass
125+
126+
127+
def test_up_down_counter_subclass_instantiation():
128+
up_down_counter = UpDownCounterImplTest("subclass_test")
129+
assert isinstance(up_down_counter, UpDownCounter)
130+
131+
132+
class ObservableCounterImplTest(ObservableCounter):
133+
def __init__(
134+
self, name: str, unit: str = "", description: str = ""
135+
) -> None:
136+
super().__init__(name, unit, description)
137+
138+
139+
def test_observable_counter_subclass_instantiation():
140+
observable_counter = ObservableCounterImplTest("subclass_test")
141+
assert isinstance(observable_counter, ObservableCounter)
142+
143+
144+
class HistogramImplTest(Histogram):
145+
def __init__(
146+
self, name: str, unit: str = "", description: str = ""
147+
) -> None:
148+
super().__init__(name, unit, description)
149+
150+
def record(self, amount: int, **kwargs):
151+
pass
152+
153+
154+
def test_histogram_subclass_instantiation():
155+
histogram = HistogramImplTest("subclass_test")
156+
assert isinstance(histogram, Histogram)
157+
158+
159+
class GaugeImplTest(_Gauge):
160+
def __init__(
161+
self, name: str, unit: str = "", description: str = ""
162+
) -> None:
163+
super().__init__(name, unit, description)
164+
165+
def set(self, amount: int, **kwargs):
166+
pass
167+
168+
169+
def test_gauge_subclass_instantiation():
170+
gauge = GaugeImplTest("subclass_test")
171+
assert isinstance(gauge, _Gauge)
172+
173+
174+
class InstrumentImplTest(Instrument):
175+
def __init__(
176+
self, name: str, unit: str = "", description: str = ""
177+
) -> None:
178+
super().__init__(name, unit, description)
179+
180+
181+
def test_instrument_subclass_instantiation():
182+
instrument = InstrumentImplTest("subclass_test")
183+
assert isinstance(instrument, Instrument)
184+
185+
186+
class ObservableGaugeImplTest(ObservableGauge):
187+
def __init__(
188+
self, name: str, unit: str = "", description: str = ""
189+
) -> None:
190+
super().__init__(name, unit, description)
191+
192+
193+
def test_observable_gauge_subclass_instantiation():
194+
observable_gauge = ObservableGaugeImplTest("subclass_test")
195+
assert isinstance(observable_gauge, ObservableGauge)
196+
197+
198+
class ObservableUpDownCounterImplTest(ObservableUpDownCounter):
199+
def __init__(
200+
self, name: str, unit: str = "", description: str = ""
201+
) -> None:
202+
super().__init__(name, unit, description)
203+
204+
205+
def test_observable_up_down_counter_subclass_instantiation():
206+
observable_up_down_counter = ObservableUpDownCounterImplTest(
207+
"subclass_test"
208+
)
209+
assert isinstance(observable_up_down_counter, ObservableUpDownCounter)

0 commit comments

Comments
 (0)