Skip to content

Commit c674996

Browse files
committed
Apply patch to fix instrument type in system metrics
1 parent 1bb3a1a commit c674996

File tree

2 files changed

+251
-0
lines changed

2 files changed

+251
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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
1112

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

7072

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

0 commit comments

Comments
 (0)