From dbd75f9aecb53d538c376fa7861e17dbfa4ecfec Mon Sep 17 00:00:00 2001 From: Oleg Temnov Date: Wed, 4 Jun 2025 15:12:04 +0200 Subject: [PATCH 1/2] Add traceStateValue parameter to DistributedTracingData.TryDeserializeFromString method --- src/Elastic.Apm/Api/DistributedTracingData.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Elastic.Apm/Api/DistributedTracingData.cs b/src/Elastic.Apm/Api/DistributedTracingData.cs index 14d4c6a36..ba174d489 100644 --- a/src/Elastic.Apm/Api/DistributedTracingData.cs +++ b/src/Elastic.Apm/Api/DistributedTracingData.cs @@ -49,10 +49,11 @@ internal DistributedTracingData(string traceId, string parentId, bool flagRecord /// . /// /// should be a return value from a call to . + /// should be a return value from a call to . /// /// Instance deserialized from . /// - public static DistributedTracingData TryDeserializeFromString(string serialized) => TraceContext.TryExtractTracingData(serialized); + public static DistributedTracingData TryDeserializeFromString(string serialized, string traceStateValue = null) => TraceContext.TryExtractTracingData(serialized, traceStateValue); public override string ToString() => new ToStringBuilder(nameof(DistributedTracingData)) { From aa3c6582cc9562c336c0b4e5d0c9d4fe043801e0 Mon Sep 17 00:00:00 2001 From: Oleg Temnov Date: Wed, 11 Jun 2025 14:32:36 +0200 Subject: [PATCH 2/2] Refactor serialization methods in DistributedTracingData for clarity and consistency --- src/Elastic.Apm/Api/DistributedTracingData.cs | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Elastic.Apm/Api/DistributedTracingData.cs b/src/Elastic.Apm/Api/DistributedTracingData.cs index ba174d489..71069c979 100644 --- a/src/Elastic.Apm/Api/DistributedTracingData.cs +++ b/src/Elastic.Apm/Api/DistributedTracingData.cs @@ -33,27 +33,48 @@ internal DistributedTracingData(string traceId, string parentId, bool flagRecord internal TraceState TraceState { get; } /// - /// Serializes this instance to a string. - /// This method should be used at the caller side and the return value should be passed to the (possibly remote) callee - /// side. - /// should be used to deserialize the instance at the callee side. + /// Serializes this instance to a traceparent string. + /// This method should be used at the caller side in pairs with , + /// and the return value should be passed to the (possibly remote) callee side. + /// should be used to deserialize the instance at the callee side. /// /// - /// String containing the instance in serialized form. + /// String containing the traceparent data in serialized form. /// public string SerializeToString() => TraceContext.BuildTraceparent(this); + /// + /// Serializes this instance to a tracestate string. + /// This method should be used at the caller side in pairs with , + /// and the return value should be passed to the (possibly remote) callee side. + /// should be used to deserialize the instance at the callee side. + /// + /// The string representation of the tracestate header, or null if there is no tracestate. + public string SerializeTraceStateToString() => TraceState?.ToTextHeader(); + /// /// Deserializes an instance from a string. /// This method should be used at the callee side and the deserialized instance can be passed to /// . /// /// should be a return value from a call to . - /// should be a return value from a call to . /// /// Instance deserialized from . /// - public static DistributedTracingData TryDeserializeFromString(string serialized, string traceStateValue = null) => TraceContext.TryExtractTracingData(serialized, traceStateValue); + public static DistributedTracingData TryDeserializeFromString(string serialized) => TraceContext.TryExtractTracingData(serialized); + + /// + /// Creates an instance from a treceparent and tracestate strings. + /// This method should be used at the callee side, and the created instance can be passed to + /// . + /// + /// should be a return value from a call to . + /// should be a return value from a call to . + /// + /// Instance deserialized from and . + /// + public static DistributedTracingData Create(string traceParent, string traceState) => + TraceContext.TryExtractTracingData(traceParent, traceState); public override string ToString() => new ToStringBuilder(nameof(DistributedTracingData)) {