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.Runtime] Add unit tests #510

Merged
merged 20 commits into from
Jul 21, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using OpenTelemetry.Metrics;
using Xunit;

Expand All @@ -34,9 +37,156 @@ public void RuntimeMetricsAreCaptured()
.AddInMemoryExporter(exportedItems)
.Build();

// The process.runtime.dotnet.exception.count metrics are only available after an exception has been thrown post OpenTelemetry.Instrumentation.Runtime initialization.
try
{
throw new Exception("Oops!");
}
catch (Exception)
{
// swallow the exception
}

meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.True(exportedItems.Count > 1);
Assert.StartsWith(MetricPrefix, exportedItems[0].Name);

var assembliesCountMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.assemblies.count");
Assert.NotNull(assembliesCountMetric);

var exceptionsCountMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.exceptions.count");
Assert.True(GetValue(exceptionsCountMetric) >= 1);
}

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

GC.Collect(1);

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

var gcCountMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.gc.collections.count");
Assert.NotNull(gcCountMetric);

#if NETCOREAPP3_1_OR_GREATER
xiang17 marked this conversation as resolved.
Show resolved Hide resolved
var gcAllocationSizeMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.gc.allocations.size");
Assert.NotNull(gcAllocationSizeMetric);
#endif

#if NET6_0_OR_GREATER
var gcCommittedMemorySizeMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.gc.committed_memory.size");
Assert.NotNull(gcCommittedMemorySizeMetric);

var gcHeapSizeMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.gc.heap.size");
Assert.NotNull(gcHeapSizeMetric);

if (Environment.Version.Major >= 7)
{
var gcHeapFragmentationSizeMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.gc.heap.fragmentation.size");
Assert.NotNull(gcHeapFragmentationSizeMetric);
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
}
#endif
}

#if NET6_0_OR_GREATER
[Fact]
public void JitRelatedMetricsTest()
{
var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddRuntimeInstrumentation()
.AddInMemoryExporter(exportedItems)
.Build();

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

var jitCompiledSizeMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.jit.il_compiled.size");
Assert.NotNull(jitCompiledSizeMetric);

var jitMethodsCompiledCountMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.jit.methods_compiled.count");
Assert.NotNull(jitMethodsCompiledCountMetric);

var jitCompilationTimeMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.jit.compilation_time");
Assert.NotNull(jitCompilationTimeMetric);
}
#endif

#if NETCOREAPP3_1_OR_GREATER
[Fact]
public void ThreadingRelatedMetricsTest()
{
var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddRuntimeInstrumentation()
.AddInMemoryExporter(exportedItems)
.Build();

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

xiang17 marked this conversation as resolved.
Show resolved Hide resolved
var lockContentionCountMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.monitor.lock_contention.count");
Assert.NotNull(lockContentionCountMetric);

var threadCountMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.thread_pool.threads.count");
Assert.NotNull(threadCountMetric);

var completedItemsCountMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.thread_pool.completed_items.count");
xiang17 marked this conversation as resolved.
Show resolved Hide resolved
Assert.NotNull(completedItemsCountMetric);

var queueLengthMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.thread_pool.queue.length");
Assert.NotNull(queueLengthMetric);

List<Timer> timers = new List<Timer>();
try
{
// Create 10 timers to bump timer.count metrics.
int timerCount = 10;
TimerCallback timerCallback = _ => { };
for (int i = 0; i < timerCount; i++)
{
Timer timer = new Timer(timerCallback, null, 1000, 250);
timers.Add(timer);
}

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

var timerCountMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.timer.count");
Assert.True(GetValue(timerCountMetric) >= timerCount);
}
finally
{
for (int i = 0; i < timers.Count; i++)
{
timers[i].Dispose();
}
}
}
#endif

private static double GetValue(Metric metric)
{
Assert.NotNull(metric);
double sum = 0;

foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
if (metric.MetricType.IsSum())
{
sum += metricPoint.GetSumLong();
}
else
{
sum += metricPoint.GetGaugeLastValueLong();
break;
}
}

return sum;
}
}
}