Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Yun-Ting committed Oct 5, 2022
1 parent b429d16 commit 13434f5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static MeterProviderBuilder AddProcessInstrumentation(
configure?.Invoke(options);

var instrumentation = new ProcessMetrics(options);
builder.AddMeter(ProcessMetrics.MeterInstance.Name);
builder.AddMeter(instrumentation.MeterInstance.Name);
return builder.AddInstrumentation(() => instrumentation);
}
}
37 changes: 16 additions & 21 deletions src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,50 @@

using System.Diagnostics.Metrics;
using System.Reflection;
using System.Threading;
using Diagnostics = System.Diagnostics;

namespace OpenTelemetry.Instrumentation.Process;

internal sealed class ProcessMetrics
{
internal static readonly AssemblyName AssemblyName = typeof(ProcessMetrics).Assembly.GetName();
internal static readonly Meter MeterInstance = new(AssemblyName.Name, AssemblyName.Version.ToString());
internal readonly Meter MeterInstance = new(AssemblyName.Name, AssemblyName.Version.ToString());

private static readonly ThreadLocal<InstrumentsValues> CurrentThreadInstrumentsValues = new(() => new InstrumentsValues());
private readonly InstrumentsValues instrumentsValues;

public ProcessMetrics(ProcessInstrumentationOptions options)
{
this.instrumentsValues = new InstrumentsValues();

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
this.MeterInstance.CreateObservableGauge(
"process.memory.usage",
() => CurrentThreadInstrumentsValues.Value.GetMemoryUsage(),
() => this.instrumentsValues.GetMemoryUsage(),
unit: "By",
description: "The amount of physical memory in use.");
description: "The amount of physical memory allocated for this process.");

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
this.MeterInstance.CreateObservableGauge(
"process.memory.virtual",
() => CurrentThreadInstrumentsValues.Value.GetVirtualMemoryUsage(),
() => this.instrumentsValues.GetVirtualMemoryUsage(),
unit: "By",
description: "The amount of committed virtual memory.");
description: "The amount of virtual memory allocated for this process that cannot be shared with other processes.");
}

private sealed class InstrumentsValues
{
private static readonly Diagnostics.Process CurrentProcess = Diagnostics.Process.GetCurrentProcess();
private readonly Diagnostics.Process currentProcess = Diagnostics.Process.GetCurrentProcess();
private double? memoryUsage;
private double? virtualMemoryUsage;

internal InstrumentsValues()
{
this.memoryUsage = null;
this.virtualMemoryUsage = null;
}

internal double GetMemoryUsage()
{
if (!this.memoryUsage.HasValue)
{
this.Snapshot();
}

var value = (double)this.memoryUsage;
var value = this.memoryUsage.Value;
this.memoryUsage = null;
return value;
}
Expand All @@ -76,16 +71,16 @@ internal double GetVirtualMemoryUsage()
this.Snapshot();
}

var value = (double)this.virtualMemoryUsage;
var value = this.virtualMemoryUsage.Value;
this.virtualMemoryUsage = null;
return value;
}

private void Snapshot()
{
CurrentProcess.Refresh();
this.memoryUsage = CurrentProcess.WorkingSet64;
this.virtualMemoryUsage = CurrentProcess.PagedMemorySize64;
this.currentProcess.Refresh();
this.memoryUsage = this.currentProcess.WorkingSet64;
this.virtualMemoryUsage = this.currentProcess.PrivateMemorySize64;
}
}
}

0 comments on commit 13434f5

Please sign in to comment.