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

SystemUsageCollectorHostedService initializes timestamp to local time but performs calculations in UTC #656

Open
foxthedream opened this issue Mar 17, 2021 · 2 comments
Labels

Comments

@foxthedream
Copy link

The constructor for SystemUsageCollectorHostedService sets a timestamp to the current process' start time. In the CollectData method we calculate the cpuTimeElapsed as DateTime.UtcNow - _lastTimeStamp, but they are in different timezones. The resulting first reading is incorrect. This is particularly evident for system clocks that are ahead of UTC and the resulting calculation is negative.

public SystemUsageCollectorHostedService(IMetrics metrics, MetricsSystemUsageCollectorOptions options)
{
    _metrics = metrics;
    _options = options;
    _lastTimeStamp = _process.StartTime;
}
private void CollectData(object state)
{
    // Lines omitted

    var cpuTimeElapsed = (DateTime.UtcNow - _lastTimeStamp).TotalMilliseconds * Environment.ProcessorCount;
    _lastTimeStamp = DateTime.UtcNow;

    // lines omitted
}
@foxthedream
Copy link
Author

I am happy to fix the issue. I have made a fork but needed an issue number. If there is an agreement that this is a bug then I will submit a PR for it.

@alhardy alhardy added the bug label Mar 18, 2021
@alhardy
Copy link
Collaborator

alhardy commented Mar 18, 2021

Agree this is a bug. A PR would be great

@alhardy alhardy added this to TODO in 4.2.0 Mar 18, 2021
@alhardy alhardy removed this from TODO in 4.2.0 Apr 13, 2021
foxthedream pushed a commit to foxthedream/AppMetrics that referenced this issue Jun 15, 2021
…erformed using UTC

Convert the Process StartTime to UTC during initialization.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants