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

[Instrumentation.Process] Added the metrics for CpuTime and CpuUtilization. #625

Closed
wants to merge 1 commit into from

Conversation

Yun-Ting
Copy link
Contributor

@Yun-Ting Yun-Ting commented Sep 7, 2022

Changes

Working towards: #447
Background: #335
Following OTel spec.

For the process.cpu.time metric, Process.TotalProcessorTime was used to get the value.

For the process.cpu.utilization metric, it was defined as the "difference in process.cpu.time since the last measurement, divided by the elapsed time and number of CPUs available to the process" in the spec.
In this PR, the last measurement was defined as the last time GetCpuUtilization() callback function was invoked.
At the collection phase of the metricReader of the Exporter, GetCpuUtilization() will be triggered.
The lastCollectionTimestamp and lastCollectionCpuTime will be updated before the current CPU utilization result was returned.

The unit defined in the spec for the above two mentioning CPU metrics are "s" (second). In this PR, the values were being calculated by converting TimeSpan.Ticks
to seconds for more granularity.

Please note that currently, broken down by different states , i.e. user, system, or wait for CPU related metrics is not implemented in this PR because of the following reasons.

  1. There is no easy way to get the wait state from the existing Process releated APIs from .net.
  2. I am unsure how beneficial to the user given this broken down by states information.

The plan is to release an alpha version of the OpenTelemetry.Instrumentation.Process package and collect customer feedback. And based on the feedback, we decide whether the division by states feature should be implemented for CPU-related metrics or not.

@codecov
Copy link

codecov bot commented Sep 7, 2022

Codecov Report

Merging #625 (6d30337) into main (0adee5a) will increase coverage by 0.10%.
The diff coverage is 94.11%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #625      +/-   ##
==========================================
+ Coverage   77.76%   77.86%   +0.10%     
==========================================
  Files         170      170              
  Lines        5163     5201      +38     
==========================================
+ Hits         4015     4050      +35     
- Misses       1148     1151       +3     
Impacted Files Coverage Δ
...elemetry.Instrumentation.Process/ProcessMetrics.cs 95.16% <94.11%> (-4.84%) ⬇️

@utpilla utpilla added the comp:instrumentation.process Things related to OpenTelemetry.Instrumentation.Process label Sep 8, 2022

private class InstrumentsValues
{
private const int TicksPerSecond = 10 * 7;
Copy link

@tarekgh tarekgh Sep 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private double? cpuTime;
private double? cpuUtlization;

private DateTime lastCollectionTimestamp = DateTime.Now;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use DateTime.UtcNow instead of DateTime.Now. It is much faster.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, these are not monotonic, I think for duration we should use something that measures elapsed time rather than wall clock.

var currentCpuTime = this.currentProcess.TotalProcessorTime.Ticks * TicksPerSecond;
var deltaInCpuTime = currentCpuTime - this.lastCollectionCpuTime;

this.lastCollectionTimestamp = DateTime.Now;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be DateTime.UtcNow too.

(this.memoryUsage, this.virtualMemoryUsage, this.cpuTime, this.cpuUtlization) = this.UpdateValues();
}

var elapsedTime = (DateTime.Now - this.lastCollectionTimestamp).Ticks * TicksPerSecond;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To convert ticks to seconds, you need to divide the ticks by the TicksPerSecond.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, this is embarrassing, thanks for pointing it out.

$"process.cpu.time",
() => values.GetCpuTime(),
unit: "s",
description: "The amount of time that the current process has actively used a CPU to perform computations.");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be good to specify the measurement time unit in the description

$"process.cpu.utilization",
() => values.GetCpuUtilization(),
unit: "s",
description: "Difference in process.cpu.time since the last collection of observerble instruments from the MetricReader, divided by the elapsed time multiply by the number of CPUs available to the current process.");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CPU utilization percentage since last collection would be better?

The unit should be a percentage here?

@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 7 days.

@github-actions github-actions bot added the Stale label Sep 17, 2022
@github-actions
Copy link
Contributor

Closed as inactive. Feel free to reopen if this PR is still being worked on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:instrumentation.process Things related to OpenTelemetry.Instrumentation.Process Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants