Skip to content

Commit d992df2

Browse files
committed
Centralize instrument registration conflict logging
1 parent 38779a6 commit d992df2

File tree

2 files changed

+48
-70
lines changed
  • opentelemetry-api/src/opentelemetry/metrics/_internal
  • opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal

2 files changed

+48
-70
lines changed

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

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,25 @@ def _register_instrument(
277277
current_advisory=current_advisory,
278278
)
279279

280+
@staticmethod
281+
def _log_instrument_registration_conflict(
282+
name: str,
283+
instrumentation_type: str,
284+
unit: str,
285+
description: str,
286+
status: _InstrumentRegistrationStatus,
287+
):
288+
_logger.warning(
289+
"An instrument with name %s, type %s, unit %s and "
290+
"description %s has been created already with a "
291+
"different advisory value %s.",
292+
name,
293+
Counter.__name__,
294+
unit,
295+
description,
296+
status.current_advisory,
297+
)
298+
280299
@abstractmethod
281300
def create_counter(
282301
self,
@@ -654,16 +673,14 @@ def create_counter(
654673
name, NoOpCounter, unit, description, advisory
655674
)
656675
if status.conflict:
657-
_logger.warning(
658-
"An instrument with name %s, type %s, unit %s and "
659-
"description %s has been created already with a "
660-
"different advisory value %s.",
676+
self._log_instrument_registration_conflict(
661677
name,
662678
Counter.__name__,
663679
unit,
664680
description,
665-
status.current_advisory,
681+
status,
666682
)
683+
667684
return NoOpCounter(name, unit=unit, description=description)
668685

669686
def create_gauge(
@@ -678,15 +695,12 @@ def create_gauge(
678695
name, NoOpGauge, unit, description, advisory
679696
)
680697
if status.conflict:
681-
_logger.warning(
682-
"An instrument with name %s, type %s, unit %s and "
683-
"description %s has been created already with a "
684-
"different advisory value %s.",
698+
self._log_instrument_registration_conflict(
685699
name,
686700
Gauge.__name__,
687701
unit,
688702
description,
689-
status.current_advisory,
703+
status,
690704
)
691705
return NoOpGauge(name, unit=unit, description=description)
692706

@@ -702,15 +716,12 @@ def create_up_down_counter(
702716
name, NoOpUpDownCounter, unit, description, advisory
703717
)
704718
if status.conflict:
705-
_logger.warning(
706-
"An instrument with name %s, type %s, unit %s and "
707-
"description %s has been created already with a "
708-
"different advisory value %s.",
719+
self._log_instrument_registration_conflict(
709720
name,
710721
UpDownCounter.__name__,
711722
unit,
712723
description,
713-
status.current_advisory,
724+
status,
714725
)
715726
return NoOpUpDownCounter(name, unit=unit, description=description)
716727

@@ -727,15 +738,12 @@ def create_observable_counter(
727738
name, NoOpObservableCounter, unit, description, advisory
728739
)
729740
if status.conflict:
730-
_logger.warning(
731-
"An instrument with name %s, type %s, unit %s and "
732-
"description %s has been created already with a "
733-
"different advisory value %s.",
741+
self._log_instrument_registration_conflict(
734742
name,
735743
ObservableCounter.__name__,
736744
unit,
737745
description,
738-
status.current_advisory,
746+
status,
739747
)
740748
return NoOpObservableCounter(
741749
name,
@@ -756,15 +764,12 @@ def create_histogram(
756764
name, NoOpHistogram, unit, description, advisory
757765
)
758766
if status.conflict:
759-
_logger.warning(
760-
"An instrument with name %s, type %s, unit %s and "
761-
"description %s has been created already with a "
762-
"different advisory value %s.",
767+
self._log_instrument_registration_conflict(
763768
name,
764769
Histogram.__name__,
765770
unit,
766771
description,
767-
status.current_advisory,
772+
status,
768773
)
769774
return NoOpHistogram(
770775
name, unit=unit, description=description, advisory=advisory
@@ -783,15 +788,12 @@ def create_observable_gauge(
783788
name, NoOpObservableGauge, unit, description, advisory
784789
)
785790
if status.conflict:
786-
_logger.warning(
787-
"An instrument with name %s, type %s, unit %s and "
788-
"description %s has been created already with a "
789-
"different advisory value %s.",
791+
self._log_instrument_registration_conflict(
790792
name,
791793
ObservableGauge.__name__,
792794
unit,
793795
description,
794-
status.current_advisory,
796+
status,
795797
)
796798
return NoOpObservableGauge(
797799
name,
@@ -813,15 +815,12 @@ def create_observable_up_down_counter(
813815
name, NoOpObservableUpDownCounter, unit, description, advisory
814816
)
815817
if status.conflict:
816-
_logger.warning(
817-
"An instrument with name %s, type %s, unit %s and "
818-
"description %s has been created already with a "
819-
"different advisory value %s.",
818+
self._log_instrument_registration_conflict(
820819
name,
821820
ObservableUpDownCounter.__name__,
822821
unit,
823822
description,
824-
status.current_advisory,
823+
status,
825824
)
826825
return NoOpObservableUpDownCounter(
827826
name,

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

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,12 @@ def create_counter(
101101
# FIXME #2558 go through all views here and check if this
102102
# instrument registration conflict can be fixed. If it can be, do
103103
# not log the following warning.
104-
_logger.warning(
105-
"An instrument with name %s, type %s, unit %s and "
106-
"description %s has been created already with a "
107-
"different advisory value %s.",
104+
self._log_instrument_registration_conflict(
108105
name,
109106
APICounter.__name__,
110107
unit,
111108
description,
112-
status.current_advisory,
109+
status,
113110
)
114111
if status.already_registered:
115112
with self._instrument_id_instrument_lock:
@@ -138,15 +135,12 @@ def create_up_down_counter(
138135
# FIXME #2558 go through all views here and check if this
139136
# instrument registration conflict can be fixed. If it can be, do
140137
# not log the following warning.
141-
_logger.warning(
142-
"An instrument with name %s, type %s, unit %s and "
143-
"description %s has been created already with a "
144-
"different advisory value %s.",
138+
self._log_instrument_registration_conflict(
145139
name,
146140
APIUpDownCounter.__name__,
147141
unit,
148142
description,
149-
status.current_advisory,
143+
status,
150144
)
151145
if status.already_registered:
152146
with self._instrument_id_instrument_lock:
@@ -175,15 +169,12 @@ def create_observable_counter(
175169
# FIXME #2558 go through all views here and check if this
176170
# instrument registration conflict can be fixed. If it can be, do
177171
# not log the following warning.
178-
_logger.warning(
179-
"An instrument with name %s, type %s, unit %s and "
180-
"description %s has been created already with a "
181-
"different advisory value %s.",
172+
self._log_instrument_registration_conflict(
182173
name,
183174
APIObservableCounter.__name__,
184175
unit,
185176
description,
186-
status.current_advisory,
177+
status,
187178
)
188179
if status.already_registered:
189180
with self._instrument_id_instrument_lock:
@@ -236,15 +227,12 @@ def create_histogram(
236227
# FIXME #2558 go through all views here and check if this
237228
# instrument registration conflict can be fixed. If it can be, do
238229
# not log the following warning.
239-
_logger.warning(
240-
"An instrument with name %s, type %s, unit %s and "
241-
"description %s has been created already with a "
242-
"different advisory value %s.",
230+
self._log_instrument_registration_conflict(
243231
name,
244232
APIHistogram.__name__,
245233
unit,
246234
description,
247-
status.current_advisory,
235+
status,
248236
)
249237
if status.already_registered:
250238
with self._instrument_id_instrument_lock:
@@ -273,15 +261,12 @@ def create_gauge(
273261
# FIXME #2558 go through all views here and check if this
274262
# instrument registration conflict can be fixed. If it can be, do
275263
# not log the following warning.
276-
_logger.warning(
277-
"An instrument with name %s, type %s, unit %s and "
278-
"description %s has been created already with a "
279-
"different advisory value %s.",
264+
self._log_instrument_registration_conflict(
280265
name,
281266
APIGauge.__name__,
282267
unit,
283268
description,
284-
status.current_advisory,
269+
status,
285270
)
286271
if status.already_registered:
287272
with self._instrument_id_instrument_lock:
@@ -310,15 +295,12 @@ def create_observable_gauge(
310295
# FIXME #2558 go through all views here and check if this
311296
# instrument registration conflict can be fixed. If it can be, do
312297
# not log the following warning.
313-
_logger.warning(
314-
"An instrument with name %s, type %s, unit %s and "
315-
"description %s has been created already with a "
316-
"different advisory value %s.",
298+
self._log_instrument_registration_conflict(
317299
name,
318300
APIObservableGauge.__name__,
319301
unit,
320302
description,
321-
status.current_advisory,
303+
status,
322304
)
323305
if status.already_registered:
324306
with self._instrument_id_instrument_lock:
@@ -350,15 +332,12 @@ def create_observable_up_down_counter(
350332
# FIXME #2558 go through all views here and check if this
351333
# instrument registration conflict can be fixed. If it can be, do
352334
# not log the following warning.
353-
_logger.warning(
354-
"An instrument with name %s, type %s, unit %s and "
355-
"description %s has been created already with a "
356-
"different advisory value %s.",
335+
self._log_instrument_registration_conflict(
357336
name,
358337
APIObservableUpDownCounter.__name__,
359338
unit,
360339
description,
361-
status.current_advisory,
340+
status,
362341
)
363342
if status.already_registered:
364343
with self._instrument_id_instrument_lock:

0 commit comments

Comments
 (0)