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] Enable nullable #893

Merged
merged 6 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
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
@@ -0,0 +1,4 @@
#nullable enable
*REMOVED*static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddRuntimeInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.Runtime.RuntimeInstrumentationOptions> configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddRuntimeInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder! builder) -> OpenTelemetry.Metrics.MeterProviderBuilder!
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddRuntimeInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder! builder, System.Action<OpenTelemetry.Instrumentation.Runtime.RuntimeInstrumentationOptions!>! configure) -> OpenTelemetry.Metrics.MeterProviderBuilder!
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#nullable enable
*REMOVED*static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddRuntimeInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.Runtime.RuntimeInstrumentationOptions> configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddRuntimeInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder! builder) -> OpenTelemetry.Metrics.MeterProviderBuilder!
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddRuntimeInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder! builder, System.Action<OpenTelemetry.Instrumentation.Runtime.RuntimeInstrumentationOptions!>! configure) -> OpenTelemetry.Metrics.MeterProviderBuilder!

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#nullable enable
*REMOVED*static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddRuntimeInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.Runtime.RuntimeInstrumentationOptions> configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddRuntimeInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder! builder) -> OpenTelemetry.Metrics.MeterProviderBuilder!
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddRuntimeInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder! builder, System.Action<OpenTelemetry.Instrumentation.Runtime.RuntimeInstrumentationOptions!>! configure) -> OpenTelemetry.Metrics.MeterProviderBuilder!
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ namespace OpenTelemetry.Metrics;
/// </summary>
public static class MeterProviderBuilderExtensions
{
/// <summary>
/// Enables runtime instrumentation.
/// </summary>
/// <param name="builder"><see cref="MeterProviderBuilder"/> being configured.</param>
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddRuntimeInstrumentation(
this MeterProviderBuilder builder) =>
AddRuntimeInstrumentationImplementation(builder, configure: null);

/// <summary>
/// Enables runtime instrumentation.
/// </summary>
Expand All @@ -33,7 +42,12 @@ public static class MeterProviderBuilderExtensions
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddRuntimeInstrumentation(
this MeterProviderBuilder builder,
Action<RuntimeInstrumentationOptions> configure = null)
Action<RuntimeInstrumentationOptions> configure) =>
AddRuntimeInstrumentationImplementation(builder, configure);

private static MeterProviderBuilder AddRuntimeInstrumentationImplementation(
MeterProviderBuilder builder,
Action<RuntimeInstrumentationOptions>? configure)
{
Guard.ThrowIfNull(builder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<PackageTags>$(PackageTags);runtime</PackageTags>
<MinVerTagPrefix>Instrumentation.Runtime-</MinVerTagPrefix>
<EnableAnalysis>true</EnableAnalysis>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
14 changes: 7 additions & 7 deletions src/OpenTelemetry.Instrumentation.Runtime/RuntimeMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace OpenTelemetry.Instrumentation.Runtime;
internal class RuntimeMetrics
{
internal static readonly AssemblyName AssemblyName = typeof(RuntimeMetrics).Assembly.GetName();
internal static readonly Meter MeterInstance = new(AssemblyName.Name, AssemblyName.Version.ToString());
internal static readonly Meter MeterInstance = new(AssemblyName.Name!, AssemblyName.Version?.ToString());

#if NET6_0_OR_GREATER
private const long NanosecondsPerTick = 100;
Expand Down Expand Up @@ -76,11 +76,11 @@ static RuntimeMetrics()
description: "The amount of committed virtual memory for the managed GC heap, as observed during the latest garbage collection. Committed virtual memory may be larger than the heap size because it includes both memory for storing existing objects (the heap size) and some extra memory that is ready to handle newly allocated objects in the future. The value will be unavailable until at least one garbage collection has occurred.");

// GC.GetGCMemoryInfo().GenerationInfo[i].SizeAfterBytes is better but it has a bug in .NET 6. See context in https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/496
Func<int, ulong> getGenerationSize = null;
Func<int, ulong>? getGenerationSize = null;
bool isCodeRunningOnBuggyRuntimeVersion = Environment.Version.Major == 6;
if (isCodeRunningOnBuggyRuntimeVersion)
{
MethodInfo mi = typeof(GC).GetMethod("GetGenerationSize", BindingFlags.NonPublic | BindingFlags.Static);
var mi = typeof(GC).GetMethod("GetGenerationSize", BindingFlags.NonPublic | BindingFlags.Static);
if (mi != null)
{
getGenerationSize = mi.CreateDelegate<Func<int, ulong>>();
Expand All @@ -106,11 +106,11 @@ static RuntimeMetrics()
{
if (isCodeRunningOnBuggyRuntimeVersion)
{
measurements[i] = new((long)getGenerationSize(i), new KeyValuePair<string, object>("generation", GenNames[i]));
measurements[i] = new((long)getGenerationSize!(i), new KeyValuePair<string, object?>("generation", GenNames[i]));
}
else
{
measurements[i] = new(generationInfo[i].SizeAfterBytes, new KeyValuePair<string, object>("generation", GenNames[i]));
measurements[i] = new(generationInfo[i].SizeAfterBytes, new KeyValuePair<string, object?>("generation", GenNames[i]));
}
}

Expand All @@ -137,7 +137,7 @@ static RuntimeMetrics()
int maxSupportedLength = Math.Min(generationInfo.Length, GenNames.Length);
for (int i = 0; i < maxSupportedLength; ++i)
{
measurements[i] = new(generationInfo[i].FragmentationAfterBytes, new KeyValuePair<string, object>("generation", GenNames[i]));
measurements[i] = new(generationInfo[i].FragmentationAfterBytes, new KeyValuePair<string, object?>("generation", GenNames[i]));
}

return measurements;
Expand Down Expand Up @@ -238,7 +238,7 @@ private static IEnumerable<Measurement<long>> GetGarbageCollectionCounts()
{
long collectionsFromThisGeneration = GC.CollectionCount(gen);

yield return new(collectionsFromThisGeneration - collectionsFromHigherGeneration, new KeyValuePair<string, object>("generation", GenNames[gen]));
yield return new(collectionsFromThisGeneration - collectionsFromHigherGeneration, new KeyValuePair<string, object?>("generation", GenNames[gen]));

collectionsFromHigherGeneration = collectionsFromThisGeneration;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. -->
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
</PropertyGroup>
<PropertyGroup>
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. -->
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="$(CoverletCollectorPkgVer)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPkgVer)" />
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="$(OpenTelemetryExporterInMemoryLatestPreReleasePkgVer)" />
<PackageReference Include="xunit" Version="$(XUnitPkgVer)" />
<PackageReference Condition="$([MSBuild]::IsOsPlatform('Windows'))" Include="xunit.runner.visualstudio" Version="$(XUnitRunnerVisualStudioPkgVer)">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<DotNetCliToolReference Include="dotnet-xunit" Version="$(DotNetXUnitCliVer)" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="$(CoverletCollectorPkgVer)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPkgVer)" />
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="$(OpenTelemetryExporterInMemoryLatestPreReleasePkgVer)" />
<PackageReference Include="xunit" Version="$(XUnitPkgVer)" />
<PackageReference Condition="$([MSBuild]::IsOsPlatform('Windows'))" Include="xunit.runner.visualstudio" Version="$(XUnitRunnerVisualStudioPkgVer)">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<DotNetCliToolReference Include="dotnet-xunit" Version="$(DotNetXUnitCliVer)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.Runtime\OpenTelemetry.Instrumentation.Runtime.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.Runtime\OpenTelemetry.Instrumentation.Runtime.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void RuntimeMetricsAreCaptured()
Assert.NotNull(assembliesCountMetric);

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

Expand Down Expand Up @@ -147,6 +148,7 @@ public void ThreadingRelatedMetricsTest()
Assert.NotNull(threadCountMetric);

var completedItemsCountMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.thread_pool.completed_items.count");
Assert.NotNull(completedItemsCountMetric);
Assert.True(GetValue(completedItemsCountMetric) >= taskCount);

var queueLengthMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.thread_pool.queue.length");
Expand All @@ -167,6 +169,7 @@ public void ThreadingRelatedMetricsTest()
meterProvider.ForceFlush(MaxTimeToAllowForFlush);

var timerCountMetric = exportedItems.FirstOrDefault(i => i.Name == "process.runtime.dotnet.timer.count");
Assert.NotNull(timerCountMetric);
Assert.True(GetValue(timerCountMetric) >= timerCount);
}
finally
Expand All @@ -181,7 +184,6 @@ public void ThreadingRelatedMetricsTest()

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

foreach (ref readonly var metricPoint in metric.GetMetricPoints())
Expand Down