-
Notifications
You must be signed in to change notification settings - Fork 773
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3462ac9
Add TagWriter implementation.
CodeBlanch 54bce1f
Merge remote-tracking branch 'upstream/main' into shared-tagwriter
CodeBlanch 26ae16f
Code review.
CodeBlanch cb4055c
Tweaked names and pass things by ref.
CodeBlanch 3554b31
Fix missing changes from code review.
CodeBlanch b9a69c2
Merge branch 'main' into shared-tagwriter
CodeBlanch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. Theelse
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.