Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process.runtime.cpu.utilization values are between 0 and 100, should be 0 and 1 #2810

Closed
alexmojaki opened this issue Aug 21, 2024 · 3 comments · Fixed by #2812
Closed

process.runtime.cpu.utilization values are between 0 and 100, should be 0 and 1 #2810

alexmojaki opened this issue Aug 21, 2024 · 3 comments · Fixed by #2812
Assignees
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@alexmojaki
Copy link
Contributor

alexmojaki commented Aug 21, 2024

What happened?

The process.runtime.cpu.utilization system metric should have values between 0 and 1, based on the spec:

utilization - an instrument that measures the fraction of usage out of its limit should be called entity.utilization. For example, system.memory.utilization for the fraction of memory in use. Utilization can be with respect to a fixed limit or a soft limit. Utilization values are represented as a ratio and are typically in the range [0, 1], but may go above 1 in case of exceeding a soft limit.

(https://opentelemetry.io/docs/specs/semconv/general/metrics/#instrument-naming)

Instead the values are between 0 and 100. The values are in the correct range for system.cpu.utilization.

Steps to Reproduce

from opentelemetry.instrumentation.system_metrics import SystemMetricsInstrumentor
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import InMemoryMetricReader

reader = InMemoryMetricReader()
meter_provider = MeterProvider(metric_readers=[reader])
config = {
    'process.runtime.cpu.utilization': None,
    'system.cpu.utilization': ['user'],
}
instrumentor = SystemMetricsInstrumentor(config=config)
instrumentor.instrument(meter_provider=meter_provider)

# Take an initial reading which will always record 0 for the process metric,
# see https://github.com/open-telemetry/opentelemetry-python-contrib/issues/2797#issuecomment-2298749008
reader.collect()

# Use some CPU for some time to get a high reading.
sum(list(range(int(1e8))))

metrics_data = reader.get_metrics_data()
for resource_metric in metrics_data.resource_metrics:
    for scope_metric in resource_metric.scope_metrics:
        for metric in scope_metric.metrics:
            print(metric.name)
            print(sorted([round(data_point.value, 2) for data_point in metric.data.data_points]))

Actual Result

system.cpu.utilization
[0.0, 0.0, 0.0, 0.0, 0.0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.07, 0.1, 0.11, 0.13, 0.16, 0.26]
process.runtime.cpython.cpu.utilization
[100.0]

Expected Result

Under process.runtime.cpython.cpu.utilization it should print something like [1.0], not [100.0].

Additional context

This code:

def _get_runtime_cpu_utilization(
self, options: CallbackOptions
) -> Iterable[Observation]:
"""Observer callback for runtime CPU utilization"""
proc_cpu_percent = self._proc.cpu_percent()
yield Observation(
proc_cpu_percent,
self._runtime_cpu_utilization_labels.copy(),
)

should have a / 100, similar to this:

def _get_system_cpu_utilization(
self, options: CallbackOptions
) -> Iterable[Observation]:
"""Observer callback for system CPU utilization"""
for cpu, times_percent in enumerate(
psutil.cpu_times_percent(percpu=True)
):
for metric in self._config["system.cpu.utilization"]:
if hasattr(times_percent, metric):
self._system_cpu_utilization_labels["state"] = metric
self._system_cpu_utilization_labels["cpu"] = cpu + 1
yield Observation(
getattr(times_percent, metric) / 100,
self._system_cpu_utilization_labels.copy(),
)

This is because psutil returns values in the 0-100 range.

Would you like to implement a fix?

No

@alexmojaki alexmojaki added the bug Something isn't working label Aug 21, 2024
@emdneto emdneto added good first issue Good for newcomers help wanted Extra attention is needed labels Aug 21, 2024
@mrugeshmaster
Copy link
Contributor

I would like to work on this issue

@rissh
Copy link

rissh commented Aug 21, 2024

Hey @lzchen ,

I also want to give it a try to work on this issue. Can you please assign me this issue?
Please don't hesitate to give me any suggestions or guidance as I work on this task. I'm open to feedback and would appreciate any insights you have.

Thank you!

@lzchen
Copy link
Contributor

lzchen commented Aug 21, 2024

@rissh

I believe @mrugeshmaster commented first to want to work on this issue. You can reach out to them if you want to collaborate or wait for a PR from them so you can review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
5 participants