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 8 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 @@ -15,6 +15,8 @@
// </copyright>

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

Expand All @@ -37,6 +39,133 @@ public void RuntimeMetricsAreCaptured()
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.True(exportedItems.Count > 1);
Assert.StartsWith(MetricPrefix, exportedItems[0].Name);

var gcCountMetric = exportedItems.First(i => i.Name == "process.runtime.dotnet.gc.collections.count");
xiang17 marked this conversation as resolved.
Show resolved Hide resolved
var sumReceived = GetLongSum(gcCountMetric);
Assert.True(sumReceived >= 0);

#if NETCOREAPP3_1_OR_GREATER
var gcAllocationSizeMetric = exportedItems.First(i => i.Name == "process.runtime.dotnet.gc.allocations.size");
Assert.True(GetLongSum(gcAllocationSizeMetric) > 0);
#endif

#if NET6_0_OR_GREATER
// Supposedly to pass if no garbage collection occurred before. However one occurred, which is out of control of the code.
xiang17 marked this conversation as resolved.
Show resolved Hide resolved
// Assert.False(exportedItems.Exists(i => i.Name == "process.runtime.dotnet.gc.committed_memory.size"));
// Assert.False(exportedItems.Exists(i => i.Name == "process.runtime.dotnet.gc.heap.size"));
#endif

var assembliesCountMetric = exportedItems.First(i => i.Name == "process.runtime.dotnet.assemblies.count");
Assert.True(GetLongSum(assembliesCountMetric) > 0);
}

#if NET6_0_OR_GREATER
[Fact]
public void RuntimeMetrics_GcAvailableAfterFirst()
xiang17 marked this conversation as resolved.
Show resolved Hide resolved
{
var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddRuntimeInstrumentation()
.AddInMemoryExporter(exportedItems)
.Build();

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

System.GC.Collect(1);

var gcCommittedMemorySizeMetric = exportedItems.First(i => i.Name == "process.runtime.dotnet.gc.committed_memory.size");
Assert.True(GetLongSum(gcCommittedMemorySizeMetric) > 0);

var gcHeapSizeMetric = exportedItems.First(i => i.Name == "process.runtime.dotnet.gc.heap.size");
Assert.True(GetLongSum(gcHeapSizeMetric) > 0);
}
#endif

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

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

var jitCompiledSizeMetric = exportedItems.First(i => i.Name == "process.runtime.dotnet.jit.il_compiled.size");
Assert.True(GetLongSum(jitCompiledSizeMetric) > 0);

var jitMethodsCompiledCountMetric = exportedItems.First(i => i.Name == "process.runtime.dotnet.jit.methods_compiled.count");
Assert.True(GetLongSum(jitMethodsCompiledCountMetric) > 0);

var jitCompilationTimeMetric = exportedItems.First(i => i.Name == "process.runtime.dotnet.jit.compilation_time");
Assert.True(GetLongSum(jitCompilationTimeMetric) > 0);
}
#endif

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

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

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

var threadCountMetric = exportedItems.First(i => i.Name == "process.runtime.dotnet.thread_pool.threads.count");
Assert.True(GetLongSum(threadCountMetric) > 0);

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

var queueLengthMetric = exportedItems.First(i => i.Name == "process.runtime.dotnet.thread_pool.queue.length");
Assert.True(GetLongSum(queueLengthMetric) == 0);
xiang17 marked this conversation as resolved.
Show resolved Hide resolved

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

meterProvider.ForceFlush(MaxTimeToAllowForFlush);

xiang17 marked this conversation as resolved.
Show resolved Hide resolved
var timerCountMetric = exportedItems.First(i => i.Name == "process.runtime.dotnet.timer.count");
Assert.True(GetLongSum(timerCountMetric) >= timerCount);
for (int i = 0; i < timers.Count; i++)
{
timers[i].Dispose();
}
xiang17 marked this conversation as resolved.
Show resolved Hide resolved
}
#endif

private static double GetLongSum(Metric metric)
xiang17 marked this conversation as resolved.
Show resolved Hide resolved
{
double sum = 0;

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

return sum;
}
}
}