Skip to content

Commit 05e4475

Browse files
committed
Refactor serialization methods in DistributedTracingData for clarity and consistency
1 parent 6ca0563 commit 05e4475

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/Elastic.Apm/Api/DistributedTracingData.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,48 @@ 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
4958
/// <see cref="ITracer.StartTransaction" />.
5059
/// </summary>
5160
/// <param name="serialized">should be a return value from a call to <see cref="SerializeToString" />.</param>
52-
/// <param name="traceStateValue">should be a return value from a call to <see cref="DistributedTracing.TraceState.ToTextHeader"/>.</param>
5361
/// <returns>
5462
/// Instance deserialized from <paramref name="serialized" />.
5563
/// </returns>
56-
public static DistributedTracingData TryDeserializeFromString(string serialized, string traceStateValue = null) => TraceContext.TryExtractTracingData(serialized, traceStateValue);
64+
public static DistributedTracingData TryDeserializeFromString(string serialized) => TraceContext.TryExtractTracingData(serialized);
65+
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);
5778

5879
public override string ToString() => new ToStringBuilder(nameof(DistributedTracingData))
5980
{

0 commit comments

Comments
 (0)