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

[Not for merging] Mapping different kinds of memory between System.Diagnostics.Process and Process Explorer #673

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 5 additions & 6 deletions examples/runtime-instrumentation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ public class Program
public static void Main()
{
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddRuntimeInstrumentation()
.AddPrometheusExporter(options =>
{
options.StartHttpListener = true;
options.ScrapeResponseCacheDurationMilliseconds = 0;
})
.AddProcessInstrumentation()
.AddConsoleExporter((exporterOptions, metricReaderOptions) =>
{
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000;
})
.Build();

// Most of the process.runtime.dotnet.gc.* metrics are only available after the GC finished at least one collection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.4.0-alpha.2" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus" Version="1.3.0-rc.2" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.HttpListener" Version="1.4.0-alpha.2" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.Runtime\OpenTelemetry.Instrumentation.Runtime.csproj" />
<ProjectReference Include="..\..\src\OpenTelemetry.Instrumentation.Process\OpenTelemetry.Instrumentation.Process.csproj" />
</ItemGroup>
</Project>
</Project>
216 changes: 155 additions & 61 deletions src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,155 @@
// <copyright file="ProcessMetrics.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

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

namespace OpenTelemetry.Instrumentation.Process;

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();

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

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

/// <summary>
/// Initializes a new instance of the <see cref="ProcessMetrics"/> class.
/// </summary>
/// <param name="options">The options to define the metrics.</param>
public ProcessMetrics(ProcessInstrumentationOptions options)
{
}
}
// <copyright file="ProcessMetrics.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

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

namespace OpenTelemetry.Instrumentation.Process;

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 ThreadLocal<InstrumentsValues> CurrentThreadInstrumentsValues = new(() => new InstrumentsValues());

static ProcessMetrics()
{
// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
"process.memory.usage",
() => CurrentThreadInstrumentsValues.Value.GetMemoryUsage(),
unit: "By",
description: "The amount of workingSet64");

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
"process.memory.pagedMemorySize",
() => CurrentThreadInstrumentsValues.Value.GetPagedMemorySize(),
unit: "By",
description: "The amount of pagedMemorySize");

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
"process.memory.pagedSystemMemorySize",
() => CurrentThreadInstrumentsValues.Value.GetPagedSystemMemorySize(),
unit: "By",
description: "The amount of pagedSystemMemorySize");

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
"process.memory.nonPagedSystemMemorySize",
() => CurrentThreadInstrumentsValues.Value.GetNonPagedSystemMemorySize(),
unit: "By",
description: "The amount of nonPagedSystemMemorySize");

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
"process.memory.privateMemorySize",
() => CurrentThreadInstrumentsValues.Value.GetPrivateMemorySize(),
unit: "By",
description: "The amount of PrivateMemorySize");
}

public ProcessMetrics(ProcessInstrumentationOptions options)
{
}

private class InstrumentsValues
{
private static readonly Diagnostics.Process CurrentProcess = Diagnostics.Process.GetCurrentProcess();
private double? workingSet64;
private double? pagedMemorySize64;
private double? pagedSystemMemorySize64;
private double? nonpagedSystemMemorySize64;
private double? privateMemorySize64;

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

var value = this.workingSet64.Value;
this.workingSet64 = null;
return value;
}

internal double GetPagedMemorySize()
{
if (!this.pagedMemorySize64.HasValue)
{
this.Snapshot();
}

var value = this.pagedMemorySize64.Value;
this.pagedMemorySize64 = null;
return value;
}

internal double GetPagedSystemMemorySize()
{
if (!this.pagedSystemMemorySize64.HasValue)
{
this.Snapshot();
}

var value = this.pagedSystemMemorySize64.Value;
this.pagedSystemMemorySize64 = null;
return value;
}

internal double GetNonPagedSystemMemorySize()
{
if (!this.nonpagedSystemMemorySize64.HasValue)
{
this.Snapshot();
}

var value = this.nonpagedSystemMemorySize64.Value;
this.nonpagedSystemMemorySize64 = null;
return value;
}

internal double GetPrivateMemorySize()
{
if (!this.privateMemorySize64.HasValue)
{
this.Snapshot();
}

var value = this.privateMemorySize64.Value;
this.privateMemorySize64 = null;
return value;
}

private void Snapshot()
{
Console.WriteLine("Refresh()");
CurrentProcess.Refresh();

this.workingSet64 = CurrentProcess.WorkingSet64;
this.pagedMemorySize64 = CurrentProcess.PagedMemorySize64;
this.pagedSystemMemorySize64 = CurrentProcess.PagedSystemMemorySize64;
this.nonpagedSystemMemorySize64 = CurrentProcess.NonpagedSystemMemorySize64;
this.privateMemorySize64 = CurrentProcess.PrivateMemorySize64;
}
}
}