@@ -63,9 +63,17 @@ def __init__(
6363 super ().__init__ (instrumentation_scope )
6464 self ._instrumentation_scope = instrumentation_scope
6565 self ._measurement_consumer = measurement_consumer
66+ self ._instrument_id_instrument = {}
67+ self ._instrument_id_instrument_lock = Lock ()
6668
6769 def create_counter (self , name , unit = "" , description = "" ) -> APICounter :
68- if self ._check_instrument_id (name , Counter , unit , description ):
70+
71+ (
72+ is_instrument_registered ,
73+ instrument_id ,
74+ ) = self ._is_instrument_registered (name , Counter , unit , description )
75+
76+ if is_instrument_registered :
6977 # FIXME #2558 go through all views here and check if this
7078 # instrument registration conflict can be fixed. If it can be, do
7179 # not log the following warning.
@@ -77,19 +85,33 @@ def create_counter(self, name, unit="", description="") -> APICounter:
7785 unit ,
7886 description ,
7987 )
88+ with self ._instrument_id_instrument_lock :
89+ return self ._instrument_id_instrument [instrument_id ]
8090
81- return Counter (
91+ instrument = Counter (
8292 name ,
8393 self ._instrumentation_scope ,
8494 self ._measurement_consumer ,
8595 unit ,
8696 description ,
8797 )
8898
99+ with self ._instrument_id_instrument_lock :
100+ self ._instrument_id_instrument [instrument_id ] = instrument
101+ return instrument
102+
89103 def create_up_down_counter (
90104 self , name , unit = "" , description = ""
91105 ) -> APIUpDownCounter :
92- if self ._check_instrument_id (name , UpDownCounter , unit , description ):
106+
107+ (
108+ is_instrument_registered ,
109+ instrument_id ,
110+ ) = self ._is_instrument_registered (
111+ name , UpDownCounter , unit , description
112+ )
113+
114+ if is_instrument_registered :
93115 # FIXME #2558 go through all views here and check if this
94116 # instrument registration conflict can be fixed. If it can be, do
95117 # not log the following warning.
@@ -101,21 +123,33 @@ def create_up_down_counter(
101123 unit ,
102124 description ,
103125 )
126+ with self ._instrument_id_instrument_lock :
127+ return self ._instrument_id_instrument [instrument_id ]
104128
105- return UpDownCounter (
129+ instrument = UpDownCounter (
106130 name ,
107131 self ._instrumentation_scope ,
108132 self ._measurement_consumer ,
109133 unit ,
110134 description ,
111135 )
112136
137+ with self ._instrument_id_instrument_lock :
138+ self ._instrument_id_instrument [instrument_id ] = instrument
139+ return instrument
140+
113141 def create_observable_counter (
114142 self , name , callbacks = None , unit = "" , description = ""
115143 ) -> APIObservableCounter :
116- if self ._check_instrument_id (
144+
145+ (
146+ is_instrument_registered ,
147+ instrument_id ,
148+ ) = self ._is_instrument_registered (
117149 name , ObservableCounter , unit , description
118- ):
150+ )
151+
152+ if is_instrument_registered :
119153 # FIXME #2558 go through all views here and check if this
120154 # instrument registration conflict can be fixed. If it can be, do
121155 # not log the following warning.
@@ -127,6 +161,9 @@ def create_observable_counter(
127161 unit ,
128162 description ,
129163 )
164+ with self ._instrument_id_instrument_lock :
165+ return self ._instrument_id_instrument [instrument_id ]
166+
130167 instrument = ObservableCounter (
131168 name ,
132169 self ._instrumentation_scope ,
@@ -138,10 +175,18 @@ def create_observable_counter(
138175
139176 self ._measurement_consumer .register_asynchronous_instrument (instrument )
140177
141- return instrument
178+ with self ._instrument_id_instrument_lock :
179+ self ._instrument_id_instrument [instrument_id ] = instrument
180+ return instrument
142181
143182 def create_histogram (self , name , unit = "" , description = "" ) -> APIHistogram :
144- if self ._check_instrument_id (name , Histogram , unit , description ):
183+
184+ (
185+ is_instrument_registered ,
186+ instrument_id ,
187+ ) = self ._is_instrument_registered (name , Histogram , unit , description )
188+
189+ if is_instrument_registered :
145190 # FIXME #2558 go through all views here and check if this
146191 # instrument registration conflict can be fixed. If it can be, do
147192 # not log the following warning.
@@ -153,18 +198,32 @@ def create_histogram(self, name, unit="", description="") -> APIHistogram:
153198 unit ,
154199 description ,
155200 )
156- return Histogram (
201+ with self ._instrument_id_instrument_lock :
202+ return self ._instrument_id_instrument [instrument_id ]
203+
204+ instrument = Histogram (
157205 name ,
158206 self ._instrumentation_scope ,
159207 self ._measurement_consumer ,
160208 unit ,
161209 description ,
162210 )
211+ with self ._instrument_id_instrument_lock :
212+ self ._instrument_id_instrument [instrument_id ] = instrument
213+ return instrument
163214
164215 def create_observable_gauge (
165216 self , name , callbacks = None , unit = "" , description = ""
166217 ) -> APIObservableGauge :
167- if self ._check_instrument_id (name , ObservableGauge , unit , description ):
218+
219+ (
220+ is_instrument_registered ,
221+ instrument_id ,
222+ ) = self ._is_instrument_registered (
223+ name , ObservableGauge , unit , description
224+ )
225+
226+ if is_instrument_registered :
168227 # FIXME #2558 go through all views here and check if this
169228 # instrument registration conflict can be fixed. If it can be, do
170229 # not log the following warning.
@@ -176,6 +235,8 @@ def create_observable_gauge(
176235 unit ,
177236 description ,
178237 )
238+ with self ._instrument_id_instrument_lock :
239+ return self ._instrument_id_instrument [instrument_id ]
179240
180241 instrument = ObservableGauge (
181242 name ,
@@ -188,14 +249,20 @@ def create_observable_gauge(
188249
189250 self ._measurement_consumer .register_asynchronous_instrument (instrument )
190251
191- return instrument
252+ with self ._instrument_id_instrument_lock :
253+ self ._instrument_id_instrument [instrument_id ] = instrument
254+ return instrument
192255
193256 def create_observable_up_down_counter (
194257 self , name , callbacks = None , unit = "" , description = ""
195258 ) -> APIObservableUpDownCounter :
196- if self ._check_instrument_id (
197- name , ObservableUpDownCounter , unit , description
198- ):
259+
260+ (
261+ is_instrument_registered ,
262+ instrument_id ,
263+ ) = self ._is_instrument_registered (name , Counter , unit , description )
264+
265+ if is_instrument_registered :
199266 # FIXME #2558 go through all views here and check if this
200267 # instrument registration conflict can be fixed. If it can be, do
201268 # not log the following warning.
@@ -207,6 +274,8 @@ def create_observable_up_down_counter(
207274 unit ,
208275 description ,
209276 )
277+ with self ._instrument_id_instrument_lock :
278+ return self ._instrument_id_instrument [instrument_id ]
210279
211280 instrument = ObservableUpDownCounter (
212281 name ,
@@ -219,7 +288,9 @@ def create_observable_up_down_counter(
219288
220289 self ._measurement_consumer .register_asynchronous_instrument (instrument )
221290
222- return instrument
291+ with self ._instrument_id_instrument_lock :
292+ self ._instrument_id_instrument [instrument_id ] = instrument
293+ return instrument
223294
224295
225296class MeterProvider (APIMeterProvider ):
0 commit comments