public bool TryExtract <TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGetter carrierGetter, out SpanContext?spanContext) where TCarrierGetter : struct, ICarrierGetter <TCarrier> { spanContext = null; var rawTraceId = ParseUtility.ParseString(carrier, carrierGetter, TraceId)?.Trim(); var rawSpanId = ParseUtility.ParseString(carrier, carrierGetter, SpanId)?.Trim(); var samplingPriority = ParseUtility.ParseInt32(carrier, carrierGetter, Sampled); if (IsValidTraceId(rawTraceId) && IsValidSpanId(rawSpanId)) { #if NETCOREAPP var traceId = rawTraceId.Length == 32 ? ParseUtility.ParseFromHexOrDefault(rawTraceId.AsSpan(16)) : ParseUtility.ParseFromHexOrDefault(rawTraceId); #else var traceId = rawTraceId.Length == 32 ? ParseUtility.ParseFromHexOrDefault(rawTraceId.Substring(16)) : ParseUtility.ParseFromHexOrDefault(rawTraceId); #endif if (traceId == 0) { return(false); } var parentId = ParseUtility.ParseFromHexOrDefault(rawSpanId); spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId, rawSpanId); return(true); } return(false); }
public bool TryExtract <TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGetter carrierGetter, out SpanContext?spanContext) where TCarrierGetter : struct, ICarrierGetter <TCarrier> { spanContext = null; var rawTraceId = ParseUtility.ParseString(carrier, carrierGetter, TraceId)?.Trim(); var rawSpanId = ParseUtility.ParseString(carrier, carrierGetter, SpanId)?.Trim(); var samplingPriority = ParseUtility.ParseInt32(carrier, carrierGetter, Sampled); if (IsValidTraceId(rawTraceId, out var traceId) && IsValidSpanId(rawSpanId)) { var parentId = ParseUtility.ParseFromHexOrDefault(rawSpanId); spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId, rawSpanId); return(true); } return(false); }
public bool TryExtract <TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGetter carrierGetter, out SpanContext?spanContext) where TCarrierGetter : struct, ICarrierGetter <TCarrier> { spanContext = null; var brValue = ParseUtility.ParseString(carrier, carrierGetter, B3)?.Trim(); if (!string.IsNullOrEmpty(brValue)) { // We found a trace parent (we are reading from the Http Headers) // 80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1-05e3ac9a4f6e3b90 // 80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1 // e457b5a2e4d86bd1-e457b5a2e4d86bd1-1-05e3ac9a4f6e3b90 // e457b5a2e4d86bd1-e457b5a2e4d86bd1-1 if (brValue !.Length != 68 && brValue !.Length != 51 && brValue !.Length != 52 && brValue !.Length != 35) { return(false); } #if NETCOREAPP ReadOnlySpan <char> rawTraceId = null; ReadOnlySpan <char> rawSpanId = null; char rawSampled = '0'; if (brValue.Length > 50 && brValue[32] == '-' && brValue[49] == '-') { // 128 bits trace id rawTraceId = brValue.AsSpan(0, 32); rawSpanId = brValue.AsSpan(33, 16); rawSampled = brValue[50]; } else if (brValue.Length > 34 && brValue[16] == '-' && brValue[33] == '-') { // 64 bits trace id rawTraceId = brValue.AsSpan(0, 16); rawSpanId = brValue.AsSpan(17, 16); rawSampled = brValue[34]; } else { return(false); } var traceId = rawTraceId.Length == 32 ? ParseUtility.ParseFromHexOrDefault(rawTraceId.Slice(16)) : ParseUtility.ParseFromHexOrDefault(rawTraceId); var parentId = ParseUtility.ParseFromHexOrDefault(rawSpanId); var samplingPriority = rawSampled == '1' ? 1 : 0; spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId.ToString(), rawSpanId.ToString()); #else string?rawTraceId = null; string?rawSpanId = null; char rawSampled = '0'; if (brValue.Length > 50 && brValue[32] == '-' && brValue[49] == '-') { // 128 bits trace id rawTraceId = brValue.Substring(0, 32); rawSpanId = brValue.Substring(33, 16); rawSampled = brValue[50]; } else if (brValue.Length > 34 && brValue[16] == '-' && brValue[33] == '-') { // 64 bits trace id rawTraceId = brValue.Substring(0, 16); rawSpanId = brValue.Substring(17, 16); rawSampled = brValue[34]; } else { return(false); } var traceId = rawTraceId.Length == 32 ? ParseUtility.ParseFromHexOrDefault(rawTraceId.Substring(16)) : ParseUtility.ParseFromHexOrDefault(rawTraceId); var parentId = ParseUtility.ParseFromHexOrDefault(rawSpanId); var samplingPriority = rawSampled == '1' ? 1 : 0; spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId, rawSpanId); #endif return(true); } return(false); }
public bool TryExtract <TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGetter carrierGetter, out SpanContext?spanContext) where TCarrierGetter : struct, ICarrierGetter <TCarrier> { spanContext = null; var traceParent = ParseUtility.ParseString(carrier, carrierGetter, TraceParent)?.Trim(); if (!string.IsNullOrEmpty(traceParent)) { // We found a trace parent (we are reading from the Http Headers) /* (https://www.w3.org/TR/trace-context/) * Valid traceparent when caller sampled this request: * * Value = 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01 * base16(version) = 00 * base16(trace-id) = 4bf92f3577b34da6a3ce929d0e0e4736 * base16(parent-id) = 00f067aa0ba902b7 * base16(trace-flags) = 01 // sampled * Valid traceparent when caller didn’t sample this request: * * Value = 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00 * base16(version) = 00 * base16(trace-id) = 4bf92f3577b34da6a3ce929d0e0e4736 * base16(parent-id) = 00f067aa0ba902b7 * base16(trace-flags) = 00 // not sampled */ if (traceParent !.Length != 55 || traceParent[2] != '-' || traceParent[35] != '-' || traceParent[52] != '-') { return(false); } char w3cSampled = traceParent[54]; if (traceParent[53] != '0' || (w3cSampled != '0' && w3cSampled != '1')) { return(false); } var samplingPriority = w3cSampled == '0' ? 0 : 1; #if NETCOREAPP var w3cTraceId = traceParent.AsSpan(3, 32); var w3cSpanId = traceParent.AsSpan(36, 16); var traceId = ParseUtility.ParseFromHexOrDefault(w3cTraceId.Slice(16)); if (traceId == 0) { return(false); } var parentId = ParseUtility.ParseFromHexOrDefault(w3cSpanId); spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, w3cTraceId.ToString(), w3cSpanId.ToString()); #else var w3cTraceId = traceParent.Substring(3, 32); var w3cSpanId = traceParent.Substring(36, 16); var traceId = ParseUtility.ParseFromHexOrDefault(w3cTraceId.Substring(16)); if (traceId == 0) { return(false); } var parentId = ParseUtility.ParseFromHexOrDefault(w3cSpanId); spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, w3cTraceId, w3cSpanId); #endif return(true); } return(false); }