Skip to content

Commit

Permalink
OTLP exporter: Fix TypeInitializationException thrown by exporter (#1873
Browse files Browse the repository at this point in the history
)

* Fix TypeInitializationException thrown by exporter

* Update src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md

Co-authored-by: Reiley Yang <reyang@microsoft.com>

Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
Co-authored-by: Reiley Yang <reyang@microsoft.com>
  • Loading branch information
3 people committed Mar 31, 2021
1 parent 9ebc1ce commit ec9f2d4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
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
6 changes: 6 additions & 0 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
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.
([#1873](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1873))

* Null values in string arrays are preserved according to
[spec](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/common/common.md).
([#1919](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1919)) and
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

0 comments on commit ec9f2d4

Please sign in to comment.