Skip to content

Commit

Permalink
[shared] Add TagWriter implementation (#5476)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Apr 9, 2024
1 parent b78a369 commit a4e5cf5
Show file tree
Hide file tree
Showing 4 changed files with 429 additions and 0 deletions.
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
{
threadStream.SetLength(0);
threadWriter!.Reset(threadStream);
return new(threadStream, threadWriter);
}
}
}
}
Loading

0 comments on commit a4e5cf5

Please sign in to comment.