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

[shared] Add TagWriter implementation #5476

Merged
merged 6 commits into from
Apr 9, 2024
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
8 changes: 8 additions & 0 deletions OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Configuration", "Configurat
src\Shared\Configuration\OpenTelemetryConfigurationExtensions.cs = src\Shared\Configuration\OpenTelemetryConfigurationExtensions.cs
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TagWriter", "TagWriter", "{993E65E5-E71B-40FD-871C-60A9EBD59724}"
ProjectSection(SolutionItems) = preProject
src\Shared\TagWriter\ArrayTagWriter.cs = src\Shared\TagWriter\ArrayTagWriter.cs
src\Shared\TagWriter\JsonStringArrayTagWriter.cs = src\Shared\TagWriter\JsonStringArrayTagWriter.cs
src\Shared\TagWriter\TagWriter.cs = src\Shared\TagWriter\TagWriter.cs
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -694,6 +701,7 @@ Global
{7BE494FC-4B0D-4340-A62A-9C9F3E7389FE} = {A115CE4C-71A8-4B95-96A5-C1DF46FD94C2}
{19545B37-8518-4BDD-AD49-00C031FB3C2A} = {3862190B-E2C5-418E-AFDC-DB281FB5C705}
{87A20A76-D524-4AAC-AF92-8725BFED0415} = {A49299FB-C5CD-4E0E-B7E1-B7867BBD67CC}
{993E65E5-E71B-40FD-871C-60A9EBD59724} = {A49299FB-C5CD-4E0E-B7E1-B7867BBD67CC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {55639B5C-0770-4A22-AB56-859604650521}
Expand Down
24 changes: 24 additions & 0 deletions src/Shared/TagWriter/ArrayTagWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable enable

namespace OpenTelemetry.Internal;

internal abstract class ArrayTagWriter<TArrayState>
where TArrayState : notnull
{
public abstract TArrayState BeginWriteArray();

public abstract void WriteNullValue(ref TArrayState state);

public abstract void WriteIntegralValue(ref TArrayState state, long value);

public abstract void WriteFloatingPointValue(ref TArrayState state, double value);

public abstract void WriteBooleanValue(ref TArrayState state, bool value);

public abstract void WriteStringValue(ref TArrayState state, string value);

public abstract void EndWriteArray(ref TArrayState state);
}
99 changes: 99 additions & 0 deletions src/Shared/TagWriter/JsonStringArrayTagWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable enable

using System.Diagnostics;
using System.Text.Json;

namespace OpenTelemetry.Internal;

internal abstract class JsonStringArrayTagWriter<TTagState> : TagWriter<TTagState, JsonStringArrayTagWriter<TTagState>.JsonArrayTagWriterState>
where TTagState : notnull
{
protected JsonStringArrayTagWriter()
: base(new JsonArrayTagWriter())
{
}

protected sealed override void WriteArrayTag(ref TTagState writer, string key, ref JsonArrayTagWriterState array)
{
var result = array.Stream.TryGetBuffer(out var buffer);

Debug.Assert(result, "result was false");

this.WriteArrayTag(ref writer, key, buffer);
}

protected abstract void WriteArrayTag(ref TTagState writer, string key, ArraySegment<byte> arrayUtf8JsonBytes);

internal readonly struct JsonArrayTagWriterState(MemoryStream stream, Utf8JsonWriter writer)
{
public MemoryStream Stream { get; } = stream;

public Utf8JsonWriter Writer { get; } = writer;
}

internal sealed class JsonArrayTagWriter : ArrayTagWriter<JsonArrayTagWriterState>
{
[ThreadStatic]
private static MemoryStream? threadStream;

[ThreadStatic]
private static Utf8JsonWriter? threadWriter;

public override JsonArrayTagWriterState BeginWriteArray()
{
var state = EnsureWriter();
state.Writer.WriteStartArray();
return state;
}

public override void EndWriteArray(ref JsonArrayTagWriterState state)
{
state.Writer.WriteEndArray();
state.Writer.Flush();
}

public override void WriteBooleanValue(ref JsonArrayTagWriterState state, bool value)
{
state.Writer.WriteBooleanValue(value);
}

public override void WriteFloatingPointValue(ref JsonArrayTagWriterState state, double value)
{
state.Writer.WriteNumberValue(value);
}

public override void WriteIntegralValue(ref JsonArrayTagWriterState state, long value)
{
state.Writer.WriteNumberValue(value);
}

public override void WriteNullValue(ref JsonArrayTagWriterState state)
{
state.Writer.WriteNullValue();
}

public override void WriteStringValue(ref JsonArrayTagWriterState state, string value)
{
state.Writer.WriteStringValue(value);
}

private static JsonArrayTagWriterState EnsureWriter()
{
if (threadStream == null)
{
threadStream = new MemoryStream();
threadWriter = new Utf8JsonWriter(threadStream);
return new(threadStream, threadWriter);
}
else
Copy link
Contributor

@rajkumar-rangaraj rajkumar-rangaraj Mar 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is this else clause executed, if the tags collection contains more than one array type? I'm trying to understand the scope of a thread and this struct allocation on the stack.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first time something is serialized with a tag value containing an array we create the storage. We use [ThreadStatic] so it will happen per thread. For exporters using the batch export processor there is a dedicated thread so that will happen once. For exporters using simple or something like reentrant, each thread can potentially have the storage. The else clause happens for all the calls after the first one. For that case we reset the state of storage so that it can be reused.

{
threadStream.SetLength(0);
threadWriter!.Reset(threadStream);
return new(threadStream, threadWriter);
}
}
}
}
Loading