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

[Exporter.Geneva] Add TraceState support #1850

Merged
Merged
Show file tree
Hide file tree
Changes from 17 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
@@ -0,0 +1,2 @@
OpenTelemetry.Exporter.Geneva.GenevaExporterOptions.IncludeTraceStateForSpan.get -> bool
OpenTelemetry.Exporter.Geneva.GenevaExporterOptions.IncludeTraceStateForSpan.set -> void
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Update GenevaTraceExporter to export `activity.TraceStateString` as the value
xiang17 marked this conversation as resolved.
Show resolved Hide resolved
for Part B `traceState` field when the `IncludeTraceState` option is set to `true`.
([#1850](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1850))

## 1.9.0-alpha.1

Released 2024-May-22
Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Exporter.Geneva/GenevaExporterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class GenevaExporterOptions

public EventNameExportMode EventNameExportMode { get; set; }

public bool IncludeTraceStateForSpan { get; set; }

public IReadOnlyDictionary<string, string> TableNameMappings
{
get => this._tableNameMappings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public MsgPackTraceExporter(GenevaExporterOptions options)
#endif
}

this.m_shouldIncludeTraceState = options.IncludeTraceStateForSpan;

var buffer = new byte[BUFFER_SIZE];

var cursor = 0;
Expand Down Expand Up @@ -243,6 +245,17 @@ internal int SerializeActivity(Activity activity)
cntFields += 1;
}

if (this.m_shouldIncludeTraceState)
{
var traceStateString = activity.TraceStateString;
if (!string.IsNullOrEmpty(traceStateString))
{
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "traceState");
cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, traceStateString);
cntFields += 1;
}
}

var linkEnumerator = activity.EnumerateLinks();
if (linkEnumerator.MoveNext())
{
Expand Down Expand Up @@ -453,5 +466,7 @@ public void Dispose()
private readonly HashSet<string> m_dedicatedFields;
#endif

private readonly bool m_shouldIncludeTraceState;

private bool isDisposed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ internal sealed class TldTraceExporter : TldExporter, IDisposable
private readonly byte partAFieldsCount = 3; // At least three fields: time, ext_dt_traceId, ext_dt_spanId
private readonly HashSet<string> m_customFields;
private readonly Tuple<byte[], byte[]> repeatedPartAFields;
private readonly bool m_shouldIncludeTraceState;

private readonly EventProvider eventProvider;

Expand Down Expand Up @@ -114,6 +115,8 @@ public TldTraceExporter(GenevaExporterOptions options)
this.repeatedPartAFields = eb.GetRawFields();
}
}

this.m_shouldIncludeTraceState = options.IncludeTraceStateForSpan;
}

public ExportResult Export(in Batch<Activity> batch)
Expand Down Expand Up @@ -205,6 +208,16 @@ internal void SerializeActivity(Activity activity)
partBFieldsCount++;
}

if (this.m_shouldIncludeTraceState)
{
var traceStateString = activity.TraceStateString;
if (!string.IsNullOrEmpty(traceStateString))
{
eb.AddCountedAnsiString("traceState", traceStateString, Encoding.UTF8);
partBFieldsCount++;
}
}

var linkEnumerator = activity.EnumerateLinks();
if (linkEnumerator.MoveNext())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,15 @@ public void GenevaTraceExporter_Success_Windows()
}

[Theory]
[InlineData(false, false)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(true, true)]
public void GenevaTraceExporter_Serialization_Success(bool hasTableNameMapping, bool hasCustomFields)
[InlineData(false, false, false)]
[InlineData(false, true, false)]
[InlineData(true, false, false)]
[InlineData(true, true, false)]
[InlineData(false, false, true)]
[InlineData(false, true, true)]
[InlineData(true, false, true)]
[InlineData(true, true, true)]
public void GenevaTraceExporter_Serialization_Success(bool hasTableNameMapping, bool hasCustomFields, bool includeTraceState)
{
string path = string.Empty;
Socket server = null;
Expand Down Expand Up @@ -241,6 +245,11 @@ public void GenevaTraceExporter_Serialization_Success(bool hasTableNameMapping,
exporterOptions.CustomFields = new string[] { "clientRequestId" };
}

if (includeTraceState)
{
exporterOptions.IncludeTraceStateForSpan = true;
}

using var exporter = new MsgPackTraceExporter(exporterOptions);
#if NET8_0_OR_GREATER
var dedicatedFields = typeof(MsgPackTraceExporter).GetField("m_dedicatedFields", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(exporter) as FrozenSet<string>;
Expand Down Expand Up @@ -278,6 +287,11 @@ public void GenevaTraceExporter_Serialization_Success(bool hasTableNameMapping,
var linkedtraceId2 = ActivityTraceId.CreateFromString("e8ea7e9ac72de94e91fabc613f9686a2".AsSpan());
var linkedSpanId2 = ActivitySpanId.CreateFromString("888915b6286b9c02".AsSpan());

if (includeTraceState)
{
parentActivity.TraceStateString = "some=state";
}

var links = new[]
{
new ActivityLink(new ActivityContext(
Expand Down Expand Up @@ -659,6 +673,15 @@ private void AssertFluentdForwardModeForActivity(GenevaExporterOptions exporterO
Assert.Equal(activity.ParentSpanId.ToHexString(), mapping["parentId"]);
}

if (!exporterOptions.IncludeTraceStateForSpan || string.IsNullOrEmpty(activity.TraceStateString))
{
Assert.False(mapping.ContainsKey("traceState"));
}
else
{
Assert.Equal(activity.TraceStateString, mapping["traceState"]);
}

#region Assert Activity Links
if (activity.Links.Any())
{
Expand Down
Loading