Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e49c028

Browse files
committedJun 13, 2022
Check keys in config before creating instruments
Fixes #1125
1 parent 51ba801 commit e49c028

File tree

2 files changed

+140
-119
lines changed
  • instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics

2 files changed

+140
-119
lines changed
 

‎CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.12.0rc1-0.31b0...HEAD)
99

1010
### Fixed
11+
- Fix bug in system metrics by checking their configuration
12+
([#1129](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1129))
1113
- Adding escape call to fix [auto-instrumentation not producing spans on Windows](https://github.com/open-telemetry/opentelemetry-python/issues/2703).
1214
([#1100](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1100))
1315
- `opentelemetry-instrumentation-grpc` narrow protobuf dependency to exclude protobuf >= 4

‎instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py

+138-119
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
"system.network.connections": ["family", "type"],
102102
"runtime.memory": ["rss", "vms"],
103103
"runtime.cpu.time": ["user", "system"],
104+
"runtime.gc_count": None,
104105
}
105106

106107

@@ -156,47 +157,53 @@ def _instrument(self, **kwargs):
156157
meter_provider,
157158
)
158159

159-
self._meter.create_observable_counter(
160-
name="system.cpu.time",
161-
callbacks=[self._get_system_cpu_time],
162-
description="System CPU time",
163-
unit="seconds",
164-
)
160+
if "system.cpu.time" in self._config:
161+
self._meter.create_observable_counter(
162+
name="system.cpu.time",
163+
callbacks=[self._get_system_cpu_time],
164+
description="System CPU time",
165+
unit="seconds",
166+
)
165167

166-
self._meter.create_observable_gauge(
167-
name="system.cpu.utilization",
168-
callbacks=[self._get_system_cpu_utilization],
169-
description="System CPU utilization",
170-
unit="1",
171-
)
168+
if "system.cpu.utilization" in self._config:
169+
self._meter.create_observable_gauge(
170+
name="system.cpu.utilization",
171+
callbacks=[self._get_system_cpu_utilization],
172+
description="System CPU utilization",
173+
unit="1",
174+
)
172175

173-
self._meter.create_observable_gauge(
174-
name="system.memory.usage",
175-
callbacks=[self._get_system_memory_usage],
176-
description="System memory usage",
177-
unit="bytes",
178-
)
176+
if "system.memory.usage" in self._config:
177+
self._meter.create_observable_gauge(
178+
name="system.memory.usage",
179+
callbacks=[self._get_system_memory_usage],
180+
description="System memory usage",
181+
unit="bytes",
182+
)
179183

180-
self._meter.create_observable_gauge(
181-
name="system.memory.utilization",
182-
callbacks=[self._get_system_memory_utilization],
183-
description="System memory utilization",
184-
unit="1",
185-
)
184+
if "system.memory.utilization" in self._config:
185+
self._meter.create_observable_gauge(
186+
name="system.memory.utilization",
187+
callbacks=[self._get_system_memory_utilization],
188+
description="System memory utilization",
189+
unit="1",
190+
)
186191

187-
self._meter.create_observable_gauge(
188-
name="system.swap.usage",
189-
callbacks=[self._get_system_swap_usage],
190-
description="System swap usage",
191-
unit="pages",
192-
)
192+
if "system.swap.usage" in self._config:
193+
self._meter.create_observable_gauge(
194+
name="system.swap.usage",
195+
callbacks=[self._get_system_swap_usage],
196+
description="System swap usage",
197+
unit="pages",
198+
)
193199

194-
self._meter.create_observable_gauge(
195-
name="system.swap.utilization",
196-
callbacks=[self._get_system_swap_utilization],
197-
description="System swap utilization",
198-
unit="1",
199-
)
200+
if "system.swap.utilization" in self._config:
201+
self._meter.create_observable_gauge(
202+
name="system.swap.utilization",
203+
callbacks=[self._get_system_swap_utilization],
204+
description="System swap utilization",
205+
unit="1",
206+
)
200207

201208
# TODO Add _get_system_swap_page_faults
202209

@@ -217,26 +224,29 @@ def _instrument(self, **kwargs):
217224
# value_type=int,
218225
# )
219226

220-
self._meter.create_observable_counter(
221-
name="system.disk.io",
222-
callbacks=[self._get_system_disk_io],
223-
description="System disk IO",
224-
unit="bytes",
225-
)
227+
if "system.disk.io" in self._config:
228+
self._meter.create_observable_counter(
229+
name="system.disk.io",
230+
callbacks=[self._get_system_disk_io],
231+
description="System disk IO",
232+
unit="bytes",
233+
)
226234

227-
self._meter.create_observable_counter(
228-
name="system.disk.operations",
229-
callbacks=[self._get_system_disk_operations],
230-
description="System disk operations",
231-
unit="operations",
232-
)
235+
if "system.disk.operations" in self._config:
236+
self._meter.create_observable_counter(
237+
name="system.disk.operations",
238+
callbacks=[self._get_system_disk_operations],
239+
description="System disk operations",
240+
unit="operations",
241+
)
233242

234-
self._meter.create_observable_counter(
235-
name="system.disk.time",
236-
callbacks=[self._get_system_disk_time],
237-
description="System disk time",
238-
unit="seconds",
239-
)
243+
if "system.disk.time" in self._config:
244+
self._meter.create_observable_counter(
245+
name="system.disk.time",
246+
callbacks=[self._get_system_disk_time],
247+
description="System disk time",
248+
unit="seconds",
249+
)
240250

241251
# TODO Add _get_system_filesystem_usage
242252

@@ -260,61 +270,69 @@ def _instrument(self, **kwargs):
260270
# TODO Filesystem information can be obtained with os.statvfs in Unix-like
261271
# OSs, how to do the same in Windows?
262272

263-
self._meter.create_observable_counter(
264-
name="system.network.dropped_packets",
265-
callbacks=[self._get_system_network_dropped_packets],
266-
description="System network dropped_packets",
267-
unit="packets",
268-
)
273+
if "system.network.dropped.packets" in self._config:
274+
self._meter.create_observable_counter(
275+
name="system.network.dropped_packets",
276+
callbacks=[self._get_system_network_dropped_packets],
277+
description="System network dropped_packets",
278+
unit="packets",
279+
)
269280

270-
self._meter.create_observable_counter(
271-
name="system.network.packets",
272-
callbacks=[self._get_system_network_packets],
273-
description="System network packets",
274-
unit="packets",
275-
)
281+
if "system.network.packets" in self._config:
282+
self._meter.create_observable_counter(
283+
name="system.network.packets",
284+
callbacks=[self._get_system_network_packets],
285+
description="System network packets",
286+
unit="packets",
287+
)
276288

277-
self._meter.create_observable_counter(
278-
name="system.network.errors",
279-
callbacks=[self._get_system_network_errors],
280-
description="System network errors",
281-
unit="errors",
282-
)
289+
if "system.network.errors" in self._config:
290+
self._meter.create_observable_counter(
291+
name="system.network.errors",
292+
callbacks=[self._get_system_network_errors],
293+
description="System network errors",
294+
unit="errors",
295+
)
283296

284-
self._meter.create_observable_counter(
285-
name="system.network.io",
286-
callbacks=[self._get_system_network_io],
287-
description="System network io",
288-
unit="bytes",
289-
)
297+
if "system.network.io" in self._config:
298+
self._meter.create_observable_counter(
299+
name="system.network.io",
300+
callbacks=[self._get_system_network_io],
301+
description="System network io",
302+
unit="bytes",
303+
)
290304

291-
self._meter.create_observable_up_down_counter(
292-
name="system.network.connections",
293-
callbacks=[self._get_system_network_connections],
294-
description="System network connections",
295-
unit="connections",
296-
)
305+
if "system.network.connections" in self._config:
306+
self._meter.create_observable_up_down_counter(
307+
name="system.network.connections",
308+
callbacks=[self._get_system_network_connections],
309+
description="System network connections",
310+
unit="connections",
311+
)
297312

298-
self._meter.create_observable_counter(
299-
name=f"runtime.{self._python_implementation}.memory",
300-
callbacks=[self._get_runtime_memory],
301-
description=f"Runtime {self._python_implementation} memory",
302-
unit="bytes",
303-
)
313+
if "runtime.memory" in self._config:
314+
self._meter.create_observable_counter(
315+
name=f"runtime.{self._python_implementation}.memory",
316+
callbacks=[self._get_runtime_memory],
317+
description=f"Runtime {self._python_implementation} memory",
318+
unit="bytes",
319+
)
304320

305-
self._meter.create_observable_counter(
306-
name=f"runtime.{self._python_implementation}.cpu_time",
307-
callbacks=[self._get_runtime_cpu_time],
308-
description=f"Runtime {self._python_implementation} CPU time",
309-
unit="seconds",
310-
)
321+
if "runtime.cpu.time" in self._config:
322+
self._meter.create_observable_counter(
323+
name=f"runtime.{self._python_implementation}.cpu_time",
324+
callbacks=[self._get_runtime_cpu_time],
325+
description=f"Runtime {self._python_implementation} CPU time",
326+
unit="seconds",
327+
)
311328

312-
self._meter.create_observable_counter(
313-
name=f"runtime.{self._python_implementation}.gc_count",
314-
callbacks=[self._get_runtime_gc_count],
315-
description=f"Runtime {self._python_implementation} GC count",
316-
unit="bytes",
317-
)
329+
if "runtime.gc_count" in self._config:
330+
self._meter.create_observable_counter(
331+
name=f"runtime.{self._python_implementation}.gc_count",
332+
callbacks=[self._get_runtime_gc_count],
333+
description=f"Runtime {self._python_implementation} GC count",
334+
unit="bytes",
335+
)
318336

319337
def _uninstrument(self, **__):
320338
pass
@@ -329,7 +347,8 @@ def _get_system_cpu_time(
329347
self._system_cpu_time_labels["state"] = metric
330348
self._system_cpu_time_labels["cpu"] = cpu + 1
331349
yield Observation(
332-
getattr(times, metric), self._system_cpu_time_labels
350+
getattr(times, metric),
351+
self._system_cpu_time_labels.copy(),
333352
)
334353

335354
def _get_system_cpu_utilization(
@@ -346,7 +365,7 @@ def _get_system_cpu_utilization(
346365
self._system_cpu_utilization_labels["cpu"] = cpu + 1
347366
yield Observation(
348367
getattr(times_percent, metric) / 100,
349-
self._system_cpu_utilization_labels,
368+
self._system_cpu_utilization_labels.copy(),
350369
)
351370

352371
def _get_system_memory_usage(
@@ -359,7 +378,7 @@ def _get_system_memory_usage(
359378
if hasattr(virtual_memory, metric):
360379
yield Observation(
361380
getattr(virtual_memory, metric),
362-
self._system_memory_usage_labels,
381+
self._system_memory_usage_labels.copy(),
363382
)
364383

365384
def _get_system_memory_utilization(
@@ -373,7 +392,7 @@ def _get_system_memory_utilization(
373392
if hasattr(system_memory, metric):
374393
yield Observation(
375394
getattr(system_memory, metric) / system_memory.total,
376-
self._system_memory_utilization_labels,
395+
self._system_memory_utilization_labels.copy(),
377396
)
378397

379398
def _get_system_swap_usage(
@@ -387,7 +406,7 @@ def _get_system_swap_usage(
387406
if hasattr(system_swap, metric):
388407
yield Observation(
389408
getattr(system_swap, metric),
390-
self._system_swap_usage_labels,
409+
self._system_swap_usage_labels.copy(),
391410
)
392411

393412
def _get_system_swap_utilization(
@@ -401,7 +420,7 @@ def _get_system_swap_utilization(
401420
self._system_swap_utilization_labels["state"] = metric
402421
yield Observation(
403422
getattr(system_swap, metric) / system_swap.total,
404-
self._system_swap_utilization_labels,
423+
self._system_swap_utilization_labels.copy(),
405424
)
406425

407426
def _get_system_disk_io(
@@ -415,7 +434,7 @@ def _get_system_disk_io(
415434
self._system_disk_io_labels["direction"] = metric
416435
yield Observation(
417436
getattr(counters, f"{metric}_bytes"),
418-
self._system_disk_io_labels,
437+
self._system_disk_io_labels.copy(),
419438
)
420439

421440
def _get_system_disk_operations(
@@ -429,7 +448,7 @@ def _get_system_disk_operations(
429448
self._system_disk_operations_labels["direction"] = metric
430449
yield Observation(
431450
getattr(counters, f"{metric}_count"),
432-
self._system_disk_operations_labels,
451+
self._system_disk_operations_labels.copy(),
433452
)
434453

435454
def _get_system_disk_time(
@@ -443,7 +462,7 @@ def _get_system_disk_time(
443462
self._system_disk_time_labels["direction"] = metric
444463
yield Observation(
445464
getattr(counters, f"{metric}_time") / 1000,
446-
self._system_disk_time_labels,
465+
self._system_disk_time_labels.copy(),
447466
)
448467

449468
def _get_system_disk_merged(
@@ -461,7 +480,7 @@ def _get_system_disk_merged(
461480
self._system_disk_merged_labels["direction"] = metric
462481
yield Observation(
463482
getattr(counters, f"{metric}_merged_count"),
464-
self._system_disk_merged_labels,
483+
self._system_disk_merged_labels.copy(),
465484
)
466485

467486
def _get_system_network_dropped_packets(
@@ -481,7 +500,7 @@ def _get_system_network_dropped_packets(
481500
] = metric
482501
yield Observation(
483502
getattr(counters, f"drop{in_out}"),
484-
self._system_network_dropped_packets_labels,
503+
self._system_network_dropped_packets_labels.copy(),
485504
)
486505

487506
def _get_system_network_packets(
@@ -497,7 +516,7 @@ def _get_system_network_packets(
497516
self._system_network_packets_labels["direction"] = metric
498517
yield Observation(
499518
getattr(counters, f"packets_{recv_sent}"),
500-
self._system_network_packets_labels,
519+
self._system_network_packets_labels.copy(),
501520
)
502521

503522
def _get_system_network_errors(
@@ -512,7 +531,7 @@ def _get_system_network_errors(
512531
self._system_network_errors_labels["direction"] = metric
513532
yield Observation(
514533
getattr(counters, f"err{in_out}"),
515-
self._system_network_errors_labels,
534+
self._system_network_errors_labels.copy(),
516535
)
517536

518537
def _get_system_network_io(
@@ -528,7 +547,7 @@ def _get_system_network_io(
528547
self._system_network_io_labels["direction"] = metric
529548
yield Observation(
530549
getattr(counters, f"bytes_{recv_sent}"),
531-
self._system_network_io_labels,
550+
self._system_network_io_labels.copy(),
532551
)
533552

534553
def _get_system_network_connections(
@@ -581,7 +600,7 @@ def _get_runtime_memory(
581600
self._runtime_memory_labels["type"] = metric
582601
yield Observation(
583602
getattr(proc_memory, metric),
584-
self._runtime_memory_labels,
603+
self._runtime_memory_labels.copy(),
585604
)
586605

587606
def _get_runtime_cpu_time(
@@ -594,7 +613,7 @@ def _get_runtime_cpu_time(
594613
self._runtime_cpu_time_labels["type"] = metric
595614
yield Observation(
596615
getattr(proc_cpu, metric),
597-
self._runtime_cpu_time_labels,
616+
self._runtime_cpu_time_labels.copy(),
598617
)
599618

600619
def _get_runtime_gc_count(
@@ -603,4 +622,4 @@ def _get_runtime_gc_count(
603622
"""Observer callback for garbage collection"""
604623
for index, count in enumerate(gc.get_count()):
605624
self._runtime_gc_count_labels["count"] = str(index)
606-
yield Observation(count, self._runtime_gc_count_labels)
625+
yield Observation(count, self._runtime_gc_count_labels.copy())

0 commit comments

Comments
 (0)
Please sign in to comment.