DataDog / dd-trace-dotnet

.NET Client Library for Datadog APM

Home Page:https://docs.datadoghq.com/tracing/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

.NET runtime metrics reporting stale data since version 2.31.0 of Tracer

marcovr opened this issue · comments

Describe the bug
Since the Update from 2.30.0 to 2.31.0 several runtime metrics including runtime.dotnet.mem.committed and runtime.dotnet.threads.count are reporting incorrect/stale data.
This seems to be caused by a change in Datadog.Trace.Util.ProcessHelpers.cs where the reference to the current process is now stored and old values are read.

To Reproduce
Steps to reproduce the behavior:

  1. Start any .NET application with runtime metrics enabled
  2. Observe the reported values for runtime metrics such as runtime.dotnet.mem.committed and runtime.dotnet.threads.count
  3. The values never change from the initial correct value

Also, see the code snippet below.

Expected behavior
The runtime metrics including runtime.dotnet.mem.committed and runtime.dotnet.threads.count are reporting correct/current values

Screenshots
We noticed this by seeing a very drastic drop in memory usage which was obviously caused by wrong data:
image

Runtime environment (please complete the following information):

  • Instrumentation mode: Automatic with NuGet package Datadog.Trace.Bundle
  • Tracer version: > 2.31.0
  • OS: tested on Ubuntu 22.04 and Windows 11. Both are affected
  • CLR: .NET 7.0

Additional context
To understand the issue, see the following Program:

using System.Diagnostics;

var process = Process.GetCurrentProcess();
Console.WriteLine($"Reported memory usage: {process.PrivateMemorySize64}");

Console.WriteLine("Allocating a big buffer filled with random data");
var bigBuffer = new byte[1_000_000_000];
Random.Shared.NextBytes(bigBuffer);

Console.WriteLine($"Actual memory usage: {Process.GetCurrentProcess().PrivateMemorySize64}");
Console.WriteLine($"Reported memory usage: {process.PrivateMemorySize64}");

Console.WriteLine("Refreshing process data");
process.Refresh();
Console.WriteLine($"Reported memory usage: {process.PrivateMemorySize64}");

On my machine it produced the following output:

Reported memory usage: 12689408
Allocating a big buffer filled with random data
Actual memory usage: 1017778176
Reported memory usage: 12689408
Refreshing process data
Reported memory usage: 1017798656

It's clear that to get the correct values, instances of Process should either not be stored or it's Refresh method must be called.

Thanks for the report @marcovr! You're absolutely right, this was a regression introduced while working on startup performance. The fix is in #4531 - we hope to have that released this week. Sorry for the inconvenience

Thanks for the quick fix 👍