Skip to content

Commit 424e44c

Browse files
committed
Apply patch to fix instrument type in system metrics
1 parent 98e44d3 commit 424e44c

File tree

2 files changed

+246
-0
lines changed

2 files changed

+246
-0
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/patches/_instrumentation_patch.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import pkg_resources
99

1010
from amazon.opentelemetry.distro.patches._resource_detector_patches import _apply_resource_detector_patches
11+
from amazon.opentelemetry.distro.patches._system_metrics_patch import _apply_system_metrics_patches
12+
1113

1214
# Env variable for determining whether we want to monkey patch gevent modules. Possible values are 'all', 'none', and
1315
# comma separated list 'os, thread, time, sys, socket, select, ssl, subprocess, builtins, signal, queue, contextvars'
@@ -66,6 +68,7 @@ def apply_instrumentation_patches() -> None:
6668
# No need to check if library is installed as this patches opentelemetry.sdk,
6769
# which must be installed for the distro to work at all.
6870
_apply_resource_detector_patches()
71+
_apply_system_metrics_patches()
6972

7073

7174
def _is_installed(req: str) -> bool:
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
# Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License.
4+
5+
# FIXME Remove this pylint disabling line when Github issue is cleared
6+
# pylint: disable=no-name-in-module
7+
from opentelemetry.instrumentation.system_metrics.version import __version__
8+
from opentelemetry.instrumentation.system_metrics import _logger
9+
from opentelemetry.metrics import get_meter
10+
from opentelemetry.instrumentation.system_metrics import SystemMetricsInstrumentor
11+
12+
13+
# This patch can be deleted once https://github.com/open-telemetry/opentelemetry-python-contrib/issues/2861 is merged.
14+
def _apply_system_metrics_patches() -> None:
15+
16+
def _instrument_with_fixed_instrument_type(self, **kwargs):
17+
# pylint: disable=too-many-branches
18+
meter_provider = kwargs.get("meter_provider")
19+
self._meter = get_meter(
20+
"opentelemetry.instrumentation.system_metrics",
21+
__version__,
22+
meter_provider,
23+
schema_url="https://opentelemetry.io/schemas/1.11.0",
24+
)
25+
26+
if "system.cpu.time" in self._config:
27+
self._meter.create_observable_counter(
28+
name="system.cpu.time",
29+
callbacks=[self._get_system_cpu_time],
30+
description="System CPU time",
31+
unit="seconds",
32+
)
33+
34+
if "system.cpu.utilization" in self._config:
35+
self._meter.create_observable_gauge(
36+
name="system.cpu.utilization",
37+
callbacks=[self._get_system_cpu_utilization],
38+
description="System CPU utilization",
39+
unit="1",
40+
)
41+
42+
if "system.memory.usage" in self._config:
43+
self._meter.create_observable_gauge(
44+
name="system.memory.usage",
45+
callbacks=[self._get_system_memory_usage],
46+
description="System memory usage",
47+
unit="bytes",
48+
)
49+
50+
if "system.memory.utilization" in self._config:
51+
self._meter.create_observable_gauge(
52+
name="system.memory.utilization",
53+
callbacks=[self._get_system_memory_utilization],
54+
description="System memory utilization",
55+
unit="1",
56+
)
57+
58+
if "system.swap.usage" in self._config:
59+
self._meter.create_observable_gauge(
60+
name="system.swap.usage",
61+
callbacks=[self._get_system_swap_usage],
62+
description="System swap usage",
63+
unit="pages",
64+
)
65+
66+
if "system.swap.utilization" in self._config:
67+
self._meter.create_observable_gauge(
68+
name="system.swap.utilization",
69+
callbacks=[self._get_system_swap_utilization],
70+
description="System swap utilization",
71+
unit="1",
72+
)
73+
74+
# TODO Add _get_system_swap_page_faults
75+
76+
# self._meter.create_observable_counter(
77+
# name="system.swap.page_faults",
78+
# callbacks=[self._get_system_swap_page_faults],
79+
# description="System swap page faults",
80+
# unit="faults",
81+
# value_type=int,
82+
# )
83+
84+
# TODO Add _get_system_swap_page_operations
85+
# self._meter.create_observable_counter(
86+
# name="system.swap.page_operations",
87+
# callbacks=self._get_system_swap_page_operations,
88+
# description="System swap page operations",
89+
# unit="operations",
90+
# value_type=int,
91+
# )
92+
93+
if "system.disk.io" in self._config:
94+
self._meter.create_observable_counter(
95+
name="system.disk.io",
96+
callbacks=[self._get_system_disk_io],
97+
description="System disk IO",
98+
unit="bytes",
99+
)
100+
101+
if "system.disk.operations" in self._config:
102+
self._meter.create_observable_counter(
103+
name="system.disk.operations",
104+
callbacks=[self._get_system_disk_operations],
105+
description="System disk operations",
106+
unit="operations",
107+
)
108+
109+
if "system.disk.time" in self._config:
110+
self._meter.create_observable_counter(
111+
name="system.disk.time",
112+
callbacks=[self._get_system_disk_time],
113+
description="System disk time",
114+
unit="seconds",
115+
)
116+
117+
# TODO Add _get_system_filesystem_usage
118+
119+
# self.accumulator.register_valueobserver(
120+
# callback=self._get_system_filesystem_usage,
121+
# name="system.filesystem.usage",
122+
# description="System filesystem usage",
123+
# unit="bytes",
124+
# value_type=int,
125+
# )
126+
127+
# TODO Add _get_system_filesystem_utilization
128+
# self._meter.create_observable_gauge(
129+
# callback=self._get_system_filesystem_utilization,
130+
# name="system.filesystem.utilization",
131+
# description="System filesystem utilization",
132+
# unit="1",
133+
# value_type=float,
134+
# )
135+
136+
# TODO Filesystem information can be obtained with os.statvfs in Unix-like
137+
# OSs, how to do the same in Windows?
138+
139+
if "system.network.dropped.packets" in self._config:
140+
self._meter.create_observable_counter(
141+
name="system.network.dropped_packets",
142+
callbacks=[self._get_system_network_dropped_packets],
143+
description="System network dropped_packets",
144+
unit="packets",
145+
)
146+
147+
if "system.network.packets" in self._config:
148+
self._meter.create_observable_counter(
149+
name="system.network.packets",
150+
callbacks=[self._get_system_network_packets],
151+
description="System network packets",
152+
unit="packets",
153+
)
154+
155+
if "system.network.errors" in self._config:
156+
self._meter.create_observable_counter(
157+
name="system.network.errors",
158+
callbacks=[self._get_system_network_errors],
159+
description="System network errors",
160+
unit="errors",
161+
)
162+
163+
if "system.network.io" in self._config:
164+
self._meter.create_observable_counter(
165+
name="system.network.io",
166+
callbacks=[self._get_system_network_io],
167+
description="System network io",
168+
unit="bytes",
169+
)
170+
171+
if "system.network.connections" in self._config:
172+
self._meter.create_observable_gauge(
173+
name="system.network.connections",
174+
callbacks=[self._get_system_network_connections],
175+
description="System network connections",
176+
)
177+
178+
if "system.thread_count" in self._config:
179+
self._meter.create_observable_gauge(
180+
name="system.thread_count",
181+
callbacks=[self._get_system_thread_count],
182+
description="System active threads count",
183+
)
184+
185+
if "process.runtime.memory" in self._config:
186+
self._meter.create_observable_gauge(
187+
name=f"process.runtime.{self._python_implementation}.memory",
188+
callbacks=[self._get_runtime_memory],
189+
description=f"Runtime {self._python_implementation} memory",
190+
unit="bytes",
191+
)
192+
193+
if "process.runtime.cpu.time" in self._config:
194+
self._meter.create_observable_counter(
195+
name=f"process.runtime.{self._python_implementation}.cpu_time",
196+
callbacks=[self._get_runtime_cpu_time],
197+
description=f"Runtime {self._python_implementation} CPU time",
198+
unit="seconds",
199+
)
200+
201+
if "process.runtime.gc_count" in self._config:
202+
if self._python_implementation == "pypy":
203+
_logger.warning(
204+
"The process.runtime.gc_count metric won't be collected because the interpreter is PyPy"
205+
)
206+
else:
207+
self._meter.create_observable_gauge(
208+
name=f"process.runtime.{self._python_implementation}.gc_count",
209+
callbacks=[self._get_runtime_gc_count],
210+
description=f"Runtime {self._python_implementation} GC count",
211+
)
212+
213+
if "process.runtime.thread_count" in self._config:
214+
self._meter.create_observable_gauge(
215+
name=f"process.runtime.{self._python_implementation}.thread_count",
216+
callbacks=[self._get_runtime_thread_count],
217+
description="Runtime active threads count",
218+
)
219+
220+
if "process.runtime.cpu.utilization" in self._config:
221+
self._meter.create_observable_gauge(
222+
name=f"process.runtime.{self._python_implementation}.cpu.utilization",
223+
callbacks=[self._get_runtime_cpu_utilization],
224+
description="Runtime CPU utilization",
225+
unit="1",
226+
)
227+
228+
if "process.runtime.context_switches" in self._config:
229+
self._meter.create_observable_counter(
230+
name=f"process.runtime.{self._python_implementation}.context_switches",
231+
callbacks=[self._get_runtime_context_switches],
232+
description="Runtime context switches",
233+
unit="switches",
234+
)
235+
236+
if "process.open_file_descriptor.count" in self._config:
237+
self._meter.create_observable_gauge(
238+
name="process.open_file_descriptor.count",
239+
callbacks=[self._get_open_file_descriptors],
240+
description="Number of file descriptors in use by the process.",
241+
)
242+
243+
SystemMetricsInstrumentor._instrument = _instrument_with_fixed_instrument_type

0 commit comments

Comments
 (0)