-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[shared] Add TagWriter implementation (#5476)
- Loading branch information
1 parent
b78a369
commit a4e5cf5
Showing
4 changed files
with
429 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.