Skip to content

Commit 0d74b57

Browse files
authored
Add traceStateValue parameter to DistributedTracingData.TryDeserializ… (#2607)
We pass the 'traceparent' value in our asynchronous message communication. We follow the instructions at https://www.elastic.co/guide/en/apm/get-started/7.10/distributed-tracing.html#_manual_distributed_tracing. However, it is not possible to restore the 'tracestate' as it is for the Go agent implementation. In pair 'restart_external' TraceContinuationStrategy, it is not possible to search the logs across services using the original traceid, as each message handling generates anew traceid due to the absence of a tracestate. The PR suggests a possible solution.
1 parent 913b635 commit 0d74b57

1 file changed

Lines changed: 27 additions & 5 deletions

File tree

src/Elastic.Apm/Api/DistributedTracingData.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,25 @@ internal DistributedTracingData(string traceId, string parentId, bool flagRecord
3333
internal TraceState TraceState { get; }
3434

3535
/// <summary>
36-
/// Serializes this instance to a string.
37-
/// This method should be used at the caller side and the return value should be passed to the (possibly remote) callee
38-
/// side.
39-
/// <see cref="TryDeserializeFromString" /> should be used to deserialize the instance at the callee side.
36+
/// Serializes this instance to a traceparent string.
37+
/// This method should be used at the caller side in pairs with <see cref="SerializeTraceStateToString" />,
38+
/// and the return value should be passed to the (possibly remote) callee side.
39+
/// <see cref="Create" /> should be used to deserialize the instance at the callee side.
4040
/// </summary>
4141
/// <returns>
42-
/// String containing the instance in serialized form.
42+
/// String containing the traceparent data in serialized form.
4343
/// </returns>
4444
public string SerializeToString() => TraceContext.BuildTraceparent(this);
4545

46+
/// <summary>
47+
/// Serializes this instance to a tracestate string.
48+
/// This method should be used at the caller side in pairs with <see cref="SerializeToString" />,
49+
/// and the return value should be passed to the (possibly remote) callee side.
50+
/// <see cref="Create" /> should be used to deserialize the instance at the callee side.
51+
/// </summary>
52+
/// <returns>The string representation of the tracestate header, or null if there is no tracestate.</returns>
53+
public string SerializeTraceStateToString() => TraceState?.ToTextHeader();
54+
4655
/// <summary>
4756
/// Deserializes an instance from a string.
4857
/// This method should be used at the callee side and the deserialized instance can be passed to
@@ -54,6 +63,19 @@ internal DistributedTracingData(string traceId, string parentId, bool flagRecord
5463
/// </returns>
5564
public static DistributedTracingData TryDeserializeFromString(string serialized) => TraceContext.TryExtractTracingData(serialized);
5665

66+
/// <summary>
67+
/// Creates an instance from a treceparent and tracestate strings.
68+
/// This method should be used at the callee side, and the created instance can be passed to
69+
/// <see cref="ITracer.StartTransaction" />.
70+
/// </summary>
71+
/// <param name="traceParent">should be a return value from a call to <see cref="SerializeToString" />.</param>
72+
/// <param name="traceState">should be a return value from a call to <see cref="SerializeTraceStateToString"/>.</param>
73+
/// <returns>
74+
/// Instance deserialized from <paramref name="traceParent" /> and <paramref name="traceState" /> .
75+
/// </returns>
76+
public static DistributedTracingData Create(string traceParent, string traceState) =>
77+
TraceContext.TryExtractTracingData(traceParent, traceState);
78+
5779
public override string ToString() => new ToStringBuilder(nameof(DistributedTracingData))
5880
{
5981
{ nameof(TraceId), TraceId },

0 commit comments

Comments
 (0)