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 21 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ namespace OpenTelemetry.Instrumentation.Process;
/// </summary>
public class ProcessInstrumentationOptions
{
public bool? ExpandOnCpuStates { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly suggest to use a different name, and also strongly suggest to not ask me to provide one 🏃 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would CpuStatesEnabled be better? 😁

Copy link
Member

@reyang reyang Aug 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would vote for not having the option at all (and later if folks really need that, we can add it in a non-breaking way).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what should we have as the default behavior?
Breaking down by the states or not?
The spec is not very clear about this currently.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think? (I'll hold my answer for now 😄)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe default to not having the breakdown unless people really want it.

}
70 changes: 70 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 @@ -57,5 +58,74 @@ static ProcessMetrics()
/// <param name="options">The options to define the metrics.</param>
public ProcessMetrics(ProcessInstrumentationOptions options)
{
// TODO: change to ObservableUpDownCounter
// MeterInstance.CreateObservableGauge(
// $"process.cpu.utilization",
// () => CurrentProcess.TotalProcessorTime.TotalMilliseconds / (Environment.ProcessorCount * (DateTime.Now - CurrentProcess.StartTime).Milliseconds),
// unit: "1",
// description: "Difference in process.cpu.time since the last measurement, divided by the elapsed time and number of CPUs available to the process.");

// What type should be used for the label?
// labels
// state, if specified, SHOULD be one of: system, user, wait. A process SHOULD be characterized either by data points with no state labels, or only data points with state labels.
// IEnumberable,
// List<Measurement<long>>
// wait time = totalprocessortime - Process.PrivilegedProcessorTime - Process.userProcessorTime

if (options.ExpandOnCpuStates == true)
{
MeterInstance.CreateObservableCounter(
$"process.cpu.time",
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
() =>
{
Measurement<long>[] measurements = new Measurement<long>[3];

CurrentProcess.Refresh();
var priviledgedCpuTime = CurrentProcess.PrivilegedProcessorTime.Seconds;
var userCpuTime = CurrentProcess.PrivilegedProcessorTime.Seconds;
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved

measurements[(int)CPUState.System] = new(priviledgedCpuTime, new KeyValuePair<string, object>("state", CPUState.System.ToString()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would we allocate a new string each time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah... that's the issue; maybe hardcoding the indices here is not that of a bad choice?

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;
},
unit: "s",
description: "Total CPU seconds broken down by different states.");
}
else
{
MeterInstance.CreateObservableCounter(
$"process.cpu.time",
() =>
{
CurrentProcess.Refresh();
return CurrentProcess.TotalProcessorTime.Seconds;
},
unit: "s",
description: "Total CPU seconds broken down by different states.");
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
}

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
"process.memory.usage",
() => CurrentProcess.WorkingSet64,
unit: "By",
description: "The amount of physical memory in use.");

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
"process.memory.virtual",
() => CurrentProcess.VirtualMemorySize64,
unit: "By",
description: "The amount of committed virtual memory.");

}

private enum CPUState
{
System,
User,
Wait,
}
}