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 memory related metrics. #595

Merged
merged 19 commits into from
Aug 26, 2022
16 changes: 16 additions & 0 deletions src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System.Diagnostics.Metrics;
using System.Reflection;
using Diagnostics = System.Diagnostics;
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved

namespace OpenTelemetry.Instrumentation.Process;

Expand All @@ -24,8 +25,23 @@ internal class ProcessMetrics
internal static readonly AssemblyName AssemblyName = typeof(ProcessMetrics).Assembly.GetName();
internal static readonly Meter MeterInstance = new(AssemblyName.Name, AssemblyName.Version.ToString());

private const string MetricPrefix = "process.dotnet.";

static ProcessMetrics()
{
// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
$"{MetricPrefix}memory.usage.physical",
() => Diagnostics.Process.GetCurrentProcess().WorkingSet64,
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
unit: "bytes",
description: "The amount of physical memory allocated for the current process.");
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
$"{MetricPrefix}memory.usage.virtual",
() => Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64,
unit: "bytes",
description: "The amount of virtual memory allocated for the current process.");
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,37 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System.Collections.Generic;
reyang marked this conversation as resolved.
Show resolved Hide resolved
using System.Linq;
using OpenTelemetry.Metrics;
using Xunit;

namespace OpenTelemetry.Instrumentation.Process.Tests;

public class ProcessMetricsTests
{
private const int MaxTimeToAllowForFlush = 10000;
private const string MetricPrefix = "process.dotnet.";

[Fact]
public void ProcessMetricsAreCaptured()
{
var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddProcessInstrumentation()
.AddInMemoryExporter(exportedItems)
.Build();

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

Assert.True(exportedItems.Count == 2);
Assert.StartsWith(MetricPrefix, exportedItems[0].Name);
Assert.StartsWith(MetricPrefix, exportedItems[1].Name);

var physicalMemorymetric = exportedItems.FirstOrDefault(i => i.Name == "process.dotnet.memory.usage.physical");
Assert.NotNull(physicalMemorymetric);

var virtualMemorymetric = exportedItems.FirstOrDefault(i => i.Name == "process.dotnet.memory.usage.virtual");
Assert.NotNull(virtualMemorymetric);
}
}