Skip to content
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
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

using System.Buffers;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using OpenTelemetry.Resources;

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Serializer;
Expand All @@ -12,7 +12,7 @@ internal static class ProtobufOtlpResourceSerializer
private const int ReserveSizeForLength = 4;
private const int InitialBufferSize = 2048;

private static readonly ConcurrentDictionary<Resource, byte[]> CachedResourceBytes = new();
private static readonly ConditionalWeakTable<Resource, byte[]> CachedResourceBytes = new();

private static ReadOnlySpan<byte> EmptyResourceBytes => [0x0A, 0x80, 0x80, 0x80, 0x00];

Expand All @@ -24,16 +24,21 @@ internal static int WriteResource(byte[] buffer, int writePosition, Resource? re
return writePosition + EmptyResourceBytes.Length;
}

var cached = CachedResourceBytes.GetOrAdd(resource, static r => SerializeResourceToBytes(r));
#if NET10_0_OR_GREATER
var cached = CachedResourceBytes.GetOrAdd(resource, SerializeResourceToBytes);
#else
var cached = CachedResourceBytes.GetValue(resource, SerializeResourceToBytes);
#endif
Comment thread
martincostello marked this conversation as resolved.

Buffer.BlockCopy(cached, 0, buffer, writePosition, cached.Length);
return writePosition + cached.Length;
}

private static byte[] SerializeResourceToBytes(Resource resource)
{
var pool = ArrayPool<byte>.Shared;

var buffer = pool.Rent(InitialBufferSize);

try
{
while (true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Runtime.CompilerServices;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Serializer;
using OpenTelemetry.Proto.Trace.V1;
using OpenTelemetry.Resources;
Expand Down Expand Up @@ -65,4 +66,30 @@ public void ToOtlpResourceTest(bool includeServiceNameInResource)
Assert.DoesNotContain(otlpResource.Attributes, kvp => kvp.Key == ResourceSemanticConventions.AttributeServiceName);
}
}

[Fact]
public void WriteResourceDoesNotKeepResourceAlive()
{
var reference = CreateSerializedResourceWeakReference();

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Assert.False(reference.TryGetTarget(out _), "Resource should not be kept alive after serialization.");
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static WeakReference<Resource> CreateSerializedResourceWeakReference()
{
var resource = ResourceBuilder.CreateEmpty()
.AddAttributes([new("key", "value")])
.Build();

var buffer = new byte[1024];

_ = ProtobufOtlpResourceSerializer.WriteResource(buffer, 0, resource);

return new WeakReference<Resource>(resource);
}
}
Loading