Skip to content

Commit

Permalink
set TraceState field in OTLP Exporter (#4331)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yun-Ting authored Mar 27, 2023
1 parent 094124e commit 27560f6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

* Added support to set `TraceState` when converting the
System.Diagnostics.Activity object to its corresponding
OpenTelemetry.Proto.Trace.V1.Span object.
([#4331](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4331))

## 1.5.0-alpha.1

Released 2023-Mar-07
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ internal static Span ToOtlpSpan(this Activity activity, SdkLimitOptions sdkLimit
TraceId = UnsafeByteOperations.UnsafeWrap(traceIdBytes),
SpanId = UnsafeByteOperations.UnsafeWrap(spanIdBytes),
ParentSpanId = parentSpanIdString,
TraceState = activity.TraceStateString ?? string.Empty,

StartTimeUnixNano = (ulong)startTimeUnixNano,
EndTimeUnixNano = (ulong)(startTimeUnixNano + activity.Duration.ToNanoseconds()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,32 @@ public void ToOtlpSpanActivityStatusTakesPrecedenceOverStatusTagsWhenActivitySta
Assert.Equal(StatusDescriptionOnError, otlpSpan.Status.Message);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void ToOtlpSpanTraceStateTest(bool traceStateWasSet)
{
using var activitySource = new ActivitySource(nameof(this.ToOtlpSpanTest));
using var activity = activitySource.StartActivity("Name");
string tracestate = "a=b;c=d";
if (traceStateWasSet)
{
activity.TraceStateString = tracestate;
}

var otlpSpan = activity.ToOtlpSpan(DefaultSdkLimitOptions);

if (traceStateWasSet)
{
Assert.NotNull(otlpSpan.TraceState);
Assert.Equal(tracestate, otlpSpan.TraceState);
}
else
{
Assert.Equal(string.Empty, otlpSpan.TraceState);
}
}

[Fact]
public void ToOtlpSpanPeerServiceTest()
{
Expand Down

0 comments on commit 27560f6

Please sign in to comment.