Skip to content

Commit

Permalink
[Exporter.Geneva] Resolve AOT warnings in OpenTelemetry.Exporter.Gene…
Browse files Browse the repository at this point in the history
…va (#1666)

Co-authored-by: Vishwesh Bankwar <vishweshbankwar@users.noreply.github.com>
  • Loading branch information
eerhardt and vishweshbankwar authored Apr 22, 2024
1 parent 6d6082b commit d1a7283
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ jobs:
|| contains(needs.detect-changes.outputs.changes, 'aot')
|| contains(needs.detect-changes.outputs.changes, 'aottestapp')
|| contains(needs.detect-changes.outputs.changes, 'build')
|| contains(needs.detect-changes.outputs.changes, 'geneva')
|| contains(needs.detect-changes.outputs.changes, 'onecollector')
|| contains(needs.detect-changes.outputs.changes, 'redis')
|| contains(needs.detect-changes.outputs.changes, 'shared')
Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Native AOT compatibility.
([#1666](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1666))

* Fix a bug in `GenevaMetricExporter` where the `MetricEtwDataTransport` singleton
is disposed.
([#1537](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1537))
Expand Down
18 changes: 15 additions & 3 deletions src/OpenTelemetry.Exporter.Geneva/Metrics/GenevaMetricExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace OpenTelemetry.Exporter.Geneva;

public class GenevaMetricExporter : BaseExporter<Metric>
/// <summary>
/// An exporter for Geneva metrics.
/// </summary>
public partial class GenevaMetricExporter : BaseExporter<Metric>
{
internal const int BufferSize = 65360; // the maximum ETW payload (inclusive)

Expand All @@ -23,6 +26,8 @@ public class GenevaMetricExporter : BaseExporter<Metric>

internal const string DimensionKeyForCustomMetricsNamespace = "_microsoft_metrics_namespace";

private const string DisableRegexPattern = ".*";

private readonly IDisposable exporter;

private delegate ExportResult ExportMetricsFunc(in Batch<Metric> batch);
Expand Down Expand Up @@ -91,7 +96,7 @@ protected override void Dispose(bool disposing)

internal static PropertyInfo GetOpenTelemetryInstrumentNameRegexProperty()
{
var meterProviderBuilderSdkType = typeof(Sdk).Assembly.GetType("OpenTelemetry.Metrics.MeterProviderBuilderSdk", throwOnError: false)
var meterProviderBuilderSdkType = Type.GetType("OpenTelemetry.Metrics.MeterProviderBuilderSdk, OpenTelemetry", throwOnError: false)
?? throw new InvalidOperationException("OpenTelemetry.Metrics.MeterProviderBuilderSdk type could not be found reflectively.");

var instrumentNameRegexProperty = meterProviderBuilderSdkType.GetProperty("InstrumentNameRegex", BindingFlags.Public | BindingFlags.Static)
Expand All @@ -102,6 +107,13 @@ internal static PropertyInfo GetOpenTelemetryInstrumentNameRegexProperty()

internal static void DisableOpenTelemetrySdkMetricNameValidation()
{
GetOpenTelemetryInstrumentNameRegexProperty().SetValue(null, new Regex(".*", RegexOptions.Compiled));
GetOpenTelemetryInstrumentNameRegexProperty().SetValue(null, GetDisableRegexPattern());
}

#if NET7_0_OR_GREATER
[GeneratedRegex(DisableRegexPattern)]
private static partial Regex GetDisableRegexPattern();
#else
private static Regex GetDisableRegexPattern() => new(DisableRegexPattern, RegexOptions.Compiled);
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

using System;
#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using System.Diagnostics.Tracing;

namespace OpenTelemetry.Exporter.Geneva;
Expand All @@ -24,6 +27,9 @@ private MetricEtwDataTransport()
}

[NonEvent]
#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "WriteEventCore is safe when eventData object is a primitive type, which it is in this case.")]
#endif
public unsafe void Send(MetricEventType eventType, byte[] data, int size)
{
var eventDataPtr = stackalloc EventData[1];
Expand All @@ -36,6 +42,9 @@ public unsafe void Send(MetricEventType eventType, byte[] data, int size)
}

[NonEvent]
#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "WriteEventCore is safe when eventData object is a primitive type, which it is in this case.")]
#endif
public unsafe void SendOtlpProtobufEvent(byte[] data, int size)
{
if (this.IsEnabled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

using System;
#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using System.Diagnostics.Tracing;

namespace OpenTelemetry.Exporter.Geneva;
Expand All @@ -24,6 +27,9 @@ public void InformationalEvent()
}

[NonEvent]
#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "WriteEventCore is safe when eventData object is a primitive type, which it is in this case.")]
#endif
public unsafe void SendEvent(int eventId, byte[] data, int size)
{
EventData* dataDesc = stackalloc EventData[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<PublishAot>true</PublishAot>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
<EventSourceSupport>true</EventSourceSupport>
<SelfContained>true</SelfContained>
</PropertyGroup>

Expand All @@ -13,6 +14,7 @@
When adding projects here please also update the verify-aot-compat job in
.github\workflows\ci.yml so that it runs for the project(s) being added.
-->
<TrimmerRootAssembly Include="OpenTelemetry.Exporter.Geneva" />
<TrimmerRootAssembly Include="OpenTelemetry.Exporter.OneCollector" />
<TrimmerRootAssembly Include="OpenTelemetry.Extensions" />
<TrimmerRootAssembly Include="OpenTelemetry.Extensions.Enrichment" />
Expand Down

0 comments on commit d1a7283

Please sign in to comment.