-
Notifications
You must be signed in to change notification settings - Fork 290
/
ProcessMetrics.cs
77 lines (66 loc) · 2.58 KB
/
ProcessMetrics.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Reflection;
using Diagnostics = System.Diagnostics;
namespace OpenTelemetry.Instrumentation.Process;
internal sealed class ProcessMetrics
{
internal static readonly AssemblyName AssemblyName = typeof(ProcessMetrics).Assembly.GetName();
internal static readonly string MeterName = AssemblyName.Name;
private static readonly Meter MeterInstance = new(MeterName, AssemblyName.Version.ToString());
static ProcessMetrics()
{
MeterInstance.CreateObservableUpDownCounter(
"process.memory.usage",
() =>
{
return Diagnostics.Process.GetCurrentProcess().WorkingSet64;
},
unit: "By",
description: "The amount of physical memory allocated for this process.");
MeterInstance.CreateObservableUpDownCounter(
"process.memory.virtual",
() =>
{
return Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;
},
unit: "By",
description: "The amount of committed virtual memory for this process.");
MeterInstance.CreateObservableCounter(
"process.cpu.time",
() =>
{
var process = Diagnostics.Process.GetCurrentProcess();
return new[]
{
new Measurement<double>(process.UserProcessorTime.TotalSeconds, new KeyValuePair<string, object?>("state", "user")),
new Measurement<double>(process.PrivilegedProcessorTime.TotalSeconds, new KeyValuePair<string, object?>("state", "system")),
};
},
unit: "s",
description: "Total CPU seconds broken down by different states.");
MeterInstance.CreateObservableUpDownCounter(
"process.cpu.count",
() =>
{
return Environment.ProcessorCount;
},
unit: "{processors}",
description: "The number of processors (CPU cores) available to the current process.");
MeterInstance.CreateObservableUpDownCounter(
"process.threads",
() =>
{
return Diagnostics.Process.GetCurrentProcess().Threads.Count;
},
unit: "{threads}",
description: "Process threads count.");
}
public ProcessMetrics(ProcessInstrumentationOptions options)
{
}
}