Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Yun-Ting committed Aug 26, 2022
1 parent 9c6d2b0 commit 57f2183
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
OpenTelemetry.Instrumentation.Process.ProcessInstrumentationOptions
OpenTelemetry.Instrumentation.Process.ProcessInstrumentationOptions.ProcessInstrumentationOptions() -> void
OpenTelemetry.Metrics.MeterProviderBuilderExtensions
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddProcessInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.Process.ProcessInstrumentationOptions> configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder
OpenTelemetry.Instrumentation.Process.ProcessInstrumentationOptions
OpenTelemetry.Instrumentation.Process.ProcessInstrumentationOptions.CpuStatesEnabled.get -> bool?
OpenTelemetry.Instrumentation.Process.ProcessInstrumentationOptions.ProcessInstrumentationOptions() -> void
OpenTelemetry.Metrics.MeterProviderBuilderExtensions
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddProcessInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.Process.ProcessInstrumentationOptions> configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ namespace OpenTelemetry.Instrumentation.Process;
/// Options to define the process metrics.
/// </summary>
public class ProcessInstrumentationOptions
{
public bool? ExpandOnCpuStates { get; set; }
{
/// <summary>
/// Gets the flag indicating whether Cpu time should be further broken down by its states.
/// The Cpu state could be one of the following type: system, user, wait.
/// </summary>
public bool? CpuStatesEnabled { get; }
}
44 changes: 44 additions & 0 deletions src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Reflection;
using Diagnostics = System.Diagnostics;
Expand Down Expand Up @@ -46,6 +47,23 @@ public ProcessMetrics(ProcessInstrumentationOptions? options)
() => InstrumentsValues.GetVirtualMemoryUsage(),
unit: "By",
description: "The amount of committed virtual memory.");

if (options.CpuStatesEnabled == false)
{
MeterInstance.CreateObservableCounter(
$"process.cpu.time",
() => InstrumentsValues.GetProcessorCpuTime(),
unit: "s",
description: "Total CPU seconds.");
}
else
{
MeterInstance.CreateObservableCounter(
$"process.cpu.time",
() => InstrumentsValues.GetProcessorCpuTimeWithBreakdown(),
unit: "s",
description: "Total CPU seconds broken down by different states.");
}
}

private class InstrumentsValues
Expand All @@ -57,6 +75,13 @@ public InstrumentsValues()
CurrentProcess.Refresh();
}

private enum CPUState
{
System,
User,
Wait,
}

public static long GetMemoryUsage()
{
return CurrentProcess.WorkingSet64;
Expand All @@ -66,5 +91,24 @@ public static long GetVirtualMemoryUsage()
{
return CurrentProcess.VirtualMemorySize64;
}

public static long GetProcessorCpuTime()
{
return CurrentProcess.TotalProcessorTime.Seconds;
}

// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/process-metrics.md#metric-instruments
public static Measurement<long>[] GetProcessorCpuTimeWithBreakdown()
{
Measurement<long>[] measurements = new Measurement<long>[3];
var priviledgedCpuTime = CurrentProcess.PrivilegedProcessorTime.Seconds;
var userCpuTime = CurrentProcess.UserProcessorTime.Seconds;

measurements[(int)CPUState.System] = new(priviledgedCpuTime, new KeyValuePair<string, object>("state", CPUState.System.ToString()));
measurements[(int)CPUState.User] = new(userCpuTime, new KeyValuePair<string, object>("state", CPUState.User.ToString()));
measurements[(int)CPUState.Wait] = new(CurrentProcess.TotalProcessorTime.Seconds - priviledgedCpuTime - userCpuTime, new KeyValuePair<string, object>("state", CPUState.Wait.ToString()));

return measurements;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public void ProcessMetricsAreCaptured()

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

Assert.True(exportedItems.Count == 2);
Assert.True(exportedItems.Count == 3);
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 processCpuTimeMetric = exportedItems.FirstOrDefault(i => i.Name == "process.cpu.time");
Assert.NotNull(processCpuTimeMetric);
}
}

0 comments on commit 57f2183

Please sign in to comment.