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.runtime.dotnet.";
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved

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

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
$"{MetricPrefix}memory.virtual",
() => Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64,
unit: "By",
description: "The amount of committed virtual memory.");
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,33 @@
// 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;

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

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

Assert.True(exportedItems.Count == 2);
var physicalMemoryMetric = exportedItems.FirstOrDefault(i => i.Name == $"{metricPrefix}memory.usage");
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
Assert.NotNull(physicalMemoryMetric);
var virtualMemoryMetric = exportedItems.FirstOrDefault(i => i.Name == $"{metricPrefix}memory.virtual");
Assert.NotNull(virtualMemoryMetric);
}
}