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 2 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,7 @@
// </copyright>

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

Expand All @@ -37,6 +38,90 @@ 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
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"));

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
xiang17 marked this conversation as resolved.
Show resolved Hide resolved
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

var timerCountMetric = exportedItems.First(i => i.Name == "process.runtime.dotnet.timer.count");
Assert.True(GetLongSum(timerCountMetric) > 0);
xiang17 marked this conversation as resolved.
Show resolved Hide resolved
#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_GcOnlyAvailableAfterFirst()
{
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
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

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;
}
}
}