15
15
from logging import getLogger
16
16
from threading import RLock
17
17
from time import time_ns
18
- from typing import Dict , List
18
+ from typing import Dict , List , Optional
19
19
20
20
from opentelemetry .metrics import (
21
21
Asynchronous ,
@@ -119,7 +119,7 @@ def consume_measurement(self, measurement: Measurement) -> None:
119
119
):
120
120
view_instrument_match .consume_measurement (measurement )
121
121
122
- def collect (self ) -> MetricsData :
122
+ def collect (self ) -> Optional [ MetricsData ] :
123
123
# Use a list instead of yielding to prevent a slow reader from holding
124
124
# SDK locks
125
125
@@ -152,16 +152,21 @@ def collect(self) -> MetricsData:
152
152
153
153
for view_instrument_match in view_instrument_matches :
154
154
155
+ data_points = view_instrument_match .collect (
156
+ aggregation_temporality , collection_start_nanos
157
+ )
158
+
159
+ if data_points is None :
160
+ continue
161
+
155
162
if isinstance (
156
163
# pylint: disable=protected-access
157
164
view_instrument_match ._aggregation ,
158
165
_SumAggregation ,
159
166
):
160
167
data = Sum (
161
168
aggregation_temporality = aggregation_temporality ,
162
- data_points = view_instrument_match .collect (
163
- aggregation_temporality , collection_start_nanos
164
- ),
169
+ data_points = data_points ,
165
170
is_monotonic = isinstance (
166
171
instrument , (Counter , ObservableCounter )
167
172
),
@@ -171,20 +176,14 @@ def collect(self) -> MetricsData:
171
176
view_instrument_match ._aggregation ,
172
177
_LastValueAggregation ,
173
178
):
174
- data = Gauge (
175
- data_points = view_instrument_match .collect (
176
- aggregation_temporality , collection_start_nanos
177
- )
178
- )
179
+ data = Gauge (data_points = data_points )
179
180
elif isinstance (
180
181
# pylint: disable=protected-access
181
182
view_instrument_match ._aggregation ,
182
183
_ExplicitBucketHistogramAggregation ,
183
184
):
184
185
data = Histogram (
185
- data_points = view_instrument_match .collect (
186
- aggregation_temporality , collection_start_nanos
187
- ),
186
+ data_points = data_points ,
188
187
aggregation_temporality = aggregation_temporality ,
189
188
)
190
189
elif isinstance (
@@ -200,9 +199,7 @@ def collect(self) -> MetricsData:
200
199
_ExponentialBucketHistogramAggregation ,
201
200
):
202
201
data = ExponentialHistogram (
203
- data_points = view_instrument_match .collect (
204
- aggregation_temporality , collection_start_nanos
205
- ),
202
+ data_points = data_points ,
206
203
aggregation_temporality = aggregation_temporality ,
207
204
)
208
205
@@ -216,32 +213,38 @@ def collect(self) -> MetricsData:
216
213
)
217
214
)
218
215
219
- if instrument .instrumentation_scope not in (
220
- instrumentation_scope_scope_metrics
221
- ):
222
- instrumentation_scope_scope_metrics [
223
- instrument .instrumentation_scope
224
- ] = ScopeMetrics (
225
- scope = instrument .instrumentation_scope ,
226
- metrics = metrics ,
227
- schema_url = instrument .instrumentation_scope .schema_url ,
228
- )
229
- else :
230
- instrumentation_scope_scope_metrics [
231
- instrument .instrumentation_scope
232
- ].metrics .extend (metrics )
233
-
234
- return MetricsData (
235
- resource_metrics = [
236
- ResourceMetrics (
237
- resource = self ._sdk_config .resource ,
238
- scope_metrics = list (
239
- instrumentation_scope_scope_metrics .values ()
240
- ),
241
- schema_url = self ._sdk_config .resource .schema_url ,
216
+ if metrics :
217
+
218
+ if instrument .instrumentation_scope not in (
219
+ instrumentation_scope_scope_metrics
220
+ ):
221
+ instrumentation_scope_scope_metrics [
222
+ instrument .instrumentation_scope
223
+ ] = ScopeMetrics (
224
+ scope = instrument .instrumentation_scope ,
225
+ metrics = metrics ,
226
+ schema_url = instrument .instrumentation_scope .schema_url ,
227
+ )
228
+ else :
229
+ instrumentation_scope_scope_metrics [
230
+ instrument .instrumentation_scope
231
+ ].metrics .extend (metrics )
232
+
233
+ if instrumentation_scope_scope_metrics :
234
+
235
+ return MetricsData (
236
+ resource_metrics = [
237
+ ResourceMetrics (
238
+ resource = self ._sdk_config .resource ,
239
+ scope_metrics = list (
240
+ instrumentation_scope_scope_metrics .values ()
241
+ ),
242
+ schema_url = self ._sdk_config .resource .schema_url ,
243
+ )
244
+ ]
242
245
)
243
- ]
244
- )
246
+
247
+ return None
245
248
246
249
def _handle_view_instrument_match (
247
250
self ,
0 commit comments