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

OTLP exporter: Fix TypeInitializationException thrown by exporter #1873

Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion build/Common.nonprod.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<BenchmarkDotNetPkgVer>[0.12.1,0.13)</BenchmarkDotNetPkgVer>
<CommandLineParserPkgVer>[2.3.0,3.0)</CommandLineParserPkgVer>
<DotNetXUnitCliVer>[2.3.1,3.0)</DotNetXUnitCliVer>
<GoogleProtobufPkgVer>[3.13.0,4.0)</GoogleProtobufPkgVer>
<GoogleProtobufPkgVer>[3.15.5,4.0)</GoogleProtobufPkgVer>
<GrpcAspNetCorePkgVer>[2.27.0,3.0)</GrpcAspNetCorePkgVer>
<GrpcAspNetCoreServerPkgVer>[2.30.0, 3.0)</GrpcAspNetCoreServerPkgVer>
<GrpcToolsPkgVer>[2.25.0,3.0)</GrpcToolsPkgVer>
Expand Down
2 changes: 1 addition & 1 deletion build/Common.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Refer to https://docs.microsoft.com/en-us/nuget/concepts/package-versioning for semver syntax.
-->
<MinVerPkgVer>[2.3.0,3.0)</MinVerPkgVer>
<GoogleProtobufPkgVer>[3.6.1,4.0)</GoogleProtobufPkgVer>
<GoogleProtobufPkgVer>[3.15.5,4.0)</GoogleProtobufPkgVer>
<GrpcPkgVer>[2.23.0,3.0)</GrpcPkgVer>
<GrpcNetClientPkgVer>[2.32.0,3.0)</GrpcNetClientPkgVer>
<GrpcToolsPkgVer>[2.25.0,3.0)</GrpcToolsPkgVer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ please check the latest changes

## Unreleased

* Resolves `System.TypeInitializationException` exception when using the
exporter with an application that references Google.Protobuf 3.15. The OTLP
exporter now depends on Google.Protobuf 3.15.5 enabling the use of the new
`UnsafeByteOperations.UnsafeWrap` to avoid unnecessary allocations.
([#TBD](https://github.com/open-telemetry/opentelemetry-dotnet/pull/TBD))
alanwest marked this conversation as resolved.
Show resolved Hide resolved

* Null values in primitive arrays are preserved through otlp.
[spec](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/common/common.md).
([#1919](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1919))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ internal static class ActivityExtensions
{
private static readonly ConcurrentBag<OtlpTrace.InstrumentationLibrarySpans> SpanListPool = new ConcurrentBag<OtlpTrace.InstrumentationLibrarySpans>();
private static readonly Action<RepeatedField<OtlpTrace.Span>, int> RepeatedFieldOfSpanSetCountAction = CreateRepeatedFieldOfSpanSetCountAction();
private static readonly Func<byte[], ByteString> ByteStringCtorFunc = CreateByteStringCtorFunc();

internal static void AddBatch(
this OtlpCollector.ExportTraceServiceRequest request,
Expand Down Expand Up @@ -135,7 +134,7 @@ internal static OtlpTrace.Span ToOtlpSpan(this Activity activity)
{
byte[] parentSpanIdBytes = new byte[8];
activity.ParentSpanId.CopyTo(parentSpanIdBytes);
parentSpanIdString = ByteStringCtorFunc(parentSpanIdBytes);
parentSpanIdString = UnsafeByteOperations.UnsafeWrap(parentSpanIdBytes);
}

var startTimeUnixNano = activity.StartTimeUtc.ToUnixTimeNanoseconds();
Expand All @@ -146,8 +145,8 @@ internal static OtlpTrace.Span ToOtlpSpan(this Activity activity)
// There is an offset of 1 on the OTLP enum.
Kind = (OtlpTrace.Span.Types.SpanKind)(activity.Kind + 1),

TraceId = ByteStringCtorFunc(traceIdBytes),
SpanId = ByteStringCtorFunc(spanIdBytes),
TraceId = UnsafeByteOperations.UnsafeWrap(traceIdBytes),
SpanId = UnsafeByteOperations.UnsafeWrap(spanIdBytes),
ParentSpanId = parentSpanIdString,

StartTimeUnixNano = (ulong)startTimeUnixNano,
Expand Down Expand Up @@ -285,8 +284,8 @@ private static OtlpTrace.Span.Types.Link ToOtlpLink(ActivityLink activityLink)

var otlpLink = new OtlpTrace.Span.Types.Link
{
TraceId = ByteStringCtorFunc(traceIdBytes),
SpanId = ByteStringCtorFunc(spanIdBytes),
TraceId = UnsafeByteOperations.UnsafeWrap(traceIdBytes),
SpanId = UnsafeByteOperations.UnsafeWrap(spanIdBytes),
};

TagEnumerationState otlpTags = default;
Expand Down Expand Up @@ -341,26 +340,6 @@ private static OtlpTrace.Span.Types.Event ToOtlpEvent(ActivityEvent activityEven
return (Action<RepeatedField<OtlpTrace.Span>, int>)dynamicMethod.CreateDelegate(typeof(Action<RepeatedField<OtlpTrace.Span>, int>));
}

private static Func<byte[], ByteString> CreateByteStringCtorFunc()
{
ConstructorInfo byteStringCtor = typeof(ByteString).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(byte[]) }, null);

DynamicMethod dynamicMethod = new DynamicMethod(
"ByteStringCtor",
typeof(ByteString),
new[] { typeof(byte[]) },
typeof(ActivityExtensions).Module,
skipVisibility: true);

var generator = dynamicMethod.GetILGenerator();

generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Newobj, byteStringCtor);
generator.Emit(OpCodes.Ret);

return (Func<byte[], ByteString>)dynamicMethod.CreateDelegate(typeof(Func<byte[], ByteString>));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static OtlpCommon.KeyValue CreateOtlpKeyValue(string key, OtlpCommon.AnyValue value)
{
Expand Down