diff --git a/build/Common.nonprod.props b/build/Common.nonprod.props
index 58ff63eb96d..57593f6ca76 100644
--- a/build/Common.nonprod.props
+++ b/build/Common.nonprod.props
@@ -25,7 +25,7 @@
[0.12.1,0.13)
[2.3.0,3.0)
[2.3.1,3.0)
- [3.13.0,4.0)
+ [3.15.5,4.0)
[2.27.0,3.0)
[2.30.0, 3.0)
[2.25.0,3.0)
diff --git a/build/Common.props b/build/Common.props
index 2bc3c531f61..5c9505ed4e0 100644
--- a/build/Common.props
+++ b/build/Common.props
@@ -22,7 +22,7 @@
Refer to https://docs.microsoft.com/en-us/nuget/concepts/package-versioning for semver syntax.
-->
[2.3.0,3.0)
- [3.6.1,4.0)
+ [3.15.5,4.0)
[2.23.0,3.0)
[2.32.0,3.0)
[2.25.0,3.0)
diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
index dad06df3640..f45e4b7fcf3 100644
--- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
+++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
@@ -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
diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs
index dca8e21890e..135a83d91ba 100644
--- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs
+++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs
@@ -37,7 +37,6 @@ internal static class ActivityExtensions
{
private static readonly ConcurrentBag SpanListPool = new ConcurrentBag();
private static readonly Action, int> RepeatedFieldOfSpanSetCountAction = CreateRepeatedFieldOfSpanSetCountAction();
- private static readonly Func ByteStringCtorFunc = CreateByteStringCtorFunc();
internal static void AddBatch(
this OtlpCollector.ExportTraceServiceRequest request,
@@ -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();
@@ -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,
@@ -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;
@@ -341,26 +340,6 @@ private static OtlpTrace.Span.Types.Event ToOtlpEvent(ActivityEvent activityEven
return (Action, int>)dynamicMethod.CreateDelegate(typeof(Action, int>));
}
- private static Func 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)dynamicMethod.CreateDelegate(typeof(Func));
- }
-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static OtlpCommon.KeyValue CreateOtlpKeyValue(string key, OtlpCommon.AnyValue value)
{