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 Cpu related metrics and addressed comments. #612

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
1a4d561
initial
Yun-Ting Aug 19, 2022
654601a
AssemblyInfo
Yun-Ting Aug 19, 2022
512dd21
Merge branch 'main' into yunl/pInst2
Yun-Ting Aug 19, 2022
23a6d35
nit
Yun-Ting Aug 19, 2022
dedd949
nit
Yun-Ting Aug 19, 2022
aa9a818
CI
Yun-Ting Aug 19, 2022
8a3e283
merge main
Yun-Ting Aug 23, 2022
6a95d5b
initial
Yun-Ting Aug 23, 2022
58f9dac
update
Yun-Ting Aug 24, 2022
0b111bb
Merge branch 'main' into yunl/pInst2
Yun-Ting Aug 24, 2022
a1279b2
adding a new metric
Yun-Ting Aug 24, 2022
db31dac
convention
Yun-Ting Aug 24, 2022
6bdd043
Merge branch 'yunl/pInst2' of https://github.com/Yun-Ting/opentelemet…
Yun-Ting Aug 25, 2022
05d4478
semantics
Yun-Ting Aug 25, 2022
f1d267f
comments
Yun-Ting Aug 25, 2022
12e12ba
Merge branch 'main' into yunl/pInst2
Yun-Ting Aug 25, 2022
6f9d879
comment
Yun-Ting Aug 25, 2022
07450d5
comment
Yun-Ting Aug 25, 2022
1a63dc7
merge main
Yun-Ting Aug 25, 2022
b19a2f6
cpuTime
Yun-Ting Aug 26, 2022
d18d8c0
Merge branch 'main' into yunl/pInst4
cijothomas Aug 26, 2022
371dbe3
nit
Yun-Ting Aug 26, 2022
3a6c115
refresh once
Yun-Ting Aug 26, 2022
dc81215
ctor
Yun-Ting Aug 26, 2022
9c6d2b0
param
Yun-Ting Aug 26, 2022
57f2183
comments
Yun-Ting Aug 26, 2022
ef37cce
update
Yun-Ting Aug 26, 2022
550966c
sanity
Yun-Ting Aug 26, 2022
879bb9d
Oops
Yun-Ting Aug 26, 2022
35c6186
comment
Yun-Ting Aug 26, 2022
ce98e93
unused
Yun-Ting Aug 26, 2022
8e38272
update
Yun-Ting Aug 26, 2022
10b5773
removed state option
Yun-Ting Aug 29, 2022
893fbac
api
Yun-Ting Aug 29, 2022
553efa4
added cpuUtil
Yun-Ting Aug 29, 2022
a24055f
update
Yun-Ting Sep 7, 2022
7e9198b
Merge branch 'main' into yunl/pInst4
Yun-Ting Sep 7, 2022
e498b06
update
Yun-Ting Sep 7, 2022
5167722
Merge branch 'yunl/pInst4' of https://github.com/Yun-Ting/opentelemet…
Yun-Ting Sep 7, 2022
e3fd84a
fix refresh
Yun-Ting Sep 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 64 additions & 13 deletions src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
// limitations under the License.
// </copyright>

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

namespace OpenTelemetry.Instrumentation.Process;
Expand All @@ -24,31 +27,41 @@ internal class ProcessMetrics
{
internal static readonly AssemblyName AssemblyName = typeof(ProcessMetrics).Assembly.GetName();
internal static readonly Meter MeterInstance = new(AssemblyName.Name, AssemblyName.Version.ToString());
private static readonly Diagnostics.Process CurrentProcess = Diagnostics.Process.GetCurrentProcess();
private const int MeasurementWindowInSeconds = 1;

static ProcessMetrics()
{
InstrumentsValues values = new InstrumentsValues();

// Refer to the spec for instrument details:
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/process-metrics.md#metric-instruments

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
"process.memory.usage",
() =>
{
CurrentProcess.Refresh();
return CurrentProcess.WorkingSet64;
},
() => values.GetMemoryUsage(),
unit: "By",
description: "The amount of physical memory in use.");
description: "The amount of physical memory in use by the current process.");

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
"process.memory.virtual",
() =>
{
CurrentProcess.Refresh();
return CurrentProcess.VirtualMemorySize64;
},
() => values.GetVirtualMemoryUsage(),
unit: "By",
description: "The amount of committed virtual memory.");
description: "The amount of committed virtual memory by the current process.");

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

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
$"process.cpu.utilization",
() => values.GetCpuUtilization(MeasurementWindowInSeconds).Result,
unit: "s",
description: "Difference in process.cpu.time since the last measurement, divided by the elapsed time and number of CPUs available to the current process.");
}

/// <summary>
Expand All @@ -58,4 +71,42 @@ static ProcessMetrics()
public ProcessMetrics(ProcessInstrumentationOptions options)
{
}

private class InstrumentsValues
{
private readonly Diagnostics.Process currentProcess = Diagnostics.Process.GetCurrentProcess();

public InstrumentsValues()
{
this.currentProcess.Refresh();
}

public long GetMemoryUsage()
{
return this.currentProcess.WorkingSet64;
}

public long GetVirtualMemoryUsage()
{
return this.currentProcess.VirtualMemorySize64;
}

public double GetCpuTime()
{
return this.currentProcess.TotalProcessorTime.TotalSeconds;
}

public async Task<double> GetCpuUtilization(int measurementWindowInSeconds)
{
var startCpuTime = this.currentProcess.TotalProcessorTime.TotalSeconds;
Stopwatch timer = Stopwatch.StartNew();

await Task.Delay(measurementWindowInSeconds * 1000).ConfigureAwait(false);

var endCpuTime = this.currentProcess.TotalProcessorTime.TotalSeconds;
timer.Stop();

return (endCpuTime - startCpuTime) / (Environment.ProcessorCount * (timer.ElapsedMilliseconds / 1000));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ public void ProcessMetricsAreCaptured()

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

Assert.True(exportedItems.Count == 2);
Assert.True(exportedItems.Count == 4);
var physicalMemoryMetric = exportedItems.FirstOrDefault(i => i.Name == "process.memory.usage");
Assert.NotNull(physicalMemoryMetric);
var virtualMemoryMetric = exportedItems.FirstOrDefault(i => i.Name == "process.memory.virtual");
Assert.NotNull(virtualMemoryMetric);
var cpuTimeMetric = exportedItems.FirstOrDefault(i => i.Name == "process.cpu.time");
Assert.NotNull(cpuTimeMetric);
var cpuUtilizationMetric = exportedItems.FirstOrDefault(i => i.Name == "process.cpu.utilization");
Assert.NotNull(cpuUtilizationMetric);
}
}