-
Notifications
You must be signed in to change notification settings - Fork 322
add W3C baggage propagator
#8203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,6 +125,8 @@ private static Map<TracePropagationStyle, Injector> createInjectors( | |
| case TRACECONTEXT: | ||
| result.put(style, W3CHttpCodec.newInjector(reverseBaggageMapping)); | ||
| break; | ||
| case BAGGAGE: | ||
| result.put(style, W3CBaggageHttpCodec.newInjector(reverseBaggageMapping)); | ||
| default: | ||
| log.debug("No implementation found to inject propagation style: {}", style); | ||
| break; | ||
|
|
@@ -159,6 +161,9 @@ public static Extractor createExtractor( | |
| case TRACECONTEXT: | ||
| extractors.add(W3CHttpCodec.newExtractor(config, traceConfigSupplier)); | ||
| break; | ||
| case BAGGAGE: | ||
| extractors.add(W3CBaggageHttpCodec.newExtractor(config, traceConfigSupplier)); | ||
| break; | ||
| default: | ||
| log.debug("No implementation found to extract propagation style: {}", style); | ||
| break; | ||
|
|
@@ -228,6 +233,7 @@ public <C> TagContext extract( | |
| context = extractedContext; | ||
| // Stop extraction if only extracting first valid context and drop everything else | ||
| if (this.extractFirst) { | ||
| // TODO: change this logic to always extract baggage | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a due to the fact it is not a tracing propagator. |
||
| break; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,169 @@ | ||||
| package datadog.trace.core.propagation; | ||||
|
|
||||
| import static datadog.trace.api.TracePropagationStyle.BAGGAGE; | ||||
| import static datadog.trace.api.sampling.PrioritySampling.SAMPLER_DROP; | ||||
| import static datadog.trace.api.sampling.PrioritySampling.SAMPLER_KEEP; | ||||
| import static datadog.trace.core.propagation.HttpCodec.firstHeaderValue; | ||||
| import static datadog.trace.core.propagation.PropagationTags.HeaderType.W3C; | ||||
|
|
||||
| import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||||
| import static java.util.concurrent.TimeUnit.NANOSECONDS; | ||||
|
|
||||
| import datadog.trace.api.Config; | ||||
| import datadog.trace.api.DD128bTraceId; | ||||
| import datadog.trace.api.DDSpanId; | ||||
| import datadog.trace.api.DDTags; | ||||
| import datadog.trace.api.DDTraceId; | ||||
| import datadog.trace.api.TraceConfig; | ||||
| import datadog.trace.api.TracePropagationStyle; | ||||
| import datadog.trace.api.internal.util.LongStringUtils; | ||||
| import datadog.trace.api.sampling.PrioritySampling; | ||||
| import datadog.trace.api.sampling.SamplingMechanism; | ||||
| import datadog.trace.bootstrap.instrumentation.api.AgentPropagation; | ||||
| import datadog.trace.bootstrap.instrumentation.api.TagContext; | ||||
| import datadog.trace.core.DDSpanContext; | ||||
|
|
||||
| import java.util.Map; | ||||
| import java.util.TreeMap; | ||||
| import java.util.function.Supplier; | ||||
|
|
||||
| import java.net.URLEncoder; | ||||
| import java.nio.charset.StandardCharsets; | ||||
|
|
||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|
|
||||
| /** A codec designed for HTTP transport via headers using W3C "baggage" header */ | ||||
| class W3CBaggageHttpCodec { | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overall, how do we expect to handle properties? Should they be considered as part of the value?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean these properties from the W3C spec? OpenTelemetry doesn't support them (they simplified a few things from the W3C spec). A user can set
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Not yet 😇 But according to the doc:
No, I was thinking:
|
||||
| private static final Logger log = LoggerFactory.getLogger(W3CHttpCodec.class); | ||||
|
|
||||
| static final String BAGGAGE_KEY = "baggage"; | ||||
|
|
||||
| private W3CBaggageHttpCodec() { | ||||
| // This class should not be created. This also makes code coverage checks happy. | ||||
| } | ||||
|
|
||||
| public static HttpCodec.Injector newInjector(Map<String, String> invertedBaggageMapping) { | ||||
| return new Injector(invertedBaggageMapping); | ||||
| } | ||||
|
|
||||
| private static class Injector implements HttpCodec.Injector { | ||||
|
|
||||
| private final Map<String, String> invertedBaggageMapping; | ||||
|
|
||||
| public Injector(Map<String, String> invertedBaggageMapping) { | ||||
| assert invertedBaggageMapping != null; | ||||
| this.invertedBaggageMapping = invertedBaggageMapping; | ||||
| } | ||||
|
|
||||
| @Override | ||||
| public <C> void inject( | ||||
| final DDSpanContext context, final C carrier, final AgentPropagation.Setter<C> setter) { | ||||
| StringBuilder baggageHeader = new StringBuilder(); | ||||
|
|
||||
| for (final Map.Entry<String, String> entry : context.baggageItems()) { | ||||
| if (baggageHeader.length() > 0) { | ||||
| baggageHeader.append(","); | ||||
| } | ||||
|
|
||||
| String header = invertedBaggageMapping.get(entry.getKey()); | ||||
| header = header != null ? header : entry.getKey(); | ||||
| String value = HttpCodec.encodeBaggage(entry.getValue()); | ||||
|
|
||||
| baggageHeader.append(header).append("=").append(value); | ||||
| } | ||||
|
|
||||
| setter.set(carrier, BAGGAGE_KEY, baggageHeader.toString()); | ||||
| } | ||||
| } | ||||
|
|
||||
| public static HttpCodec.Extractor newExtractor( | ||||
| Config config, Supplier<TraceConfig> traceConfigSupplier) { | ||||
| return new TagContextExtractor(traceConfigSupplier, () -> new W3CBaggageContextInterpreter(config)); | ||||
| } | ||||
|
|
||||
| private static class W3CBaggageContextInterpreter extends ContextInterpreter { | ||||
|
|
||||
| private W3CBaggageContextInterpreter(Config config) { | ||||
| super(config); | ||||
| } | ||||
|
|
||||
| @Override | ||||
| public TracePropagationStyle style() { | ||||
| return BAGGAGE; | ||||
| } | ||||
|
|
||||
| @Override | ||||
| public boolean accept(String key, String value) { | ||||
| if (null == key || key.isEmpty()) { | ||||
| return true; | ||||
| } | ||||
| if (LOG_EXTRACT_HEADER_NAMES) { | ||||
| log.debug("Header: {}", key); | ||||
| } | ||||
|
|
||||
| char first = Character.toLowerCase(key.charAt(0)); | ||||
|
|
||||
| if (first == 'b' && BAGGAGE_KEY.equalsIgnoreCase(key)) { | ||||
| try { | ||||
| if (baggage.isEmpty()) { | ||||
| baggage = new TreeMap<>(); | ||||
| } | ||||
|
|
||||
| for (String entry : value.split(",")) { | ||||
| String[] keyValue = entry.split("=", 2); | ||||
| if (keyValue.length == 2) { | ||||
| baggage.put( | ||||
| HttpCodec.decode(keyValue[0].trim()), | ||||
| HttpCodec.decode(keyValue[1].trim()) | ||||
| ); | ||||
| } | ||||
| } | ||||
| } catch (RuntimeException e) { | ||||
| invalidateContext(); | ||||
| log.debug("Exception when extracting baggage", e); | ||||
| return false; | ||||
| } | ||||
| } | ||||
| return true; | ||||
| } | ||||
|
|
||||
| private long extractEndToEndStartTime(String value) { | ||||
| try { | ||||
| return MILLISECONDS.toNanos(Long.parseLong(value)); | ||||
| } catch (RuntimeException e) { | ||||
| log.debug("Ignoring invalid end-to-end start time {}", value, e); | ||||
| return 0; | ||||
| } | ||||
| } | ||||
|
|
||||
| private static String trim(String input) { | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a benefit compared to
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Happy to change it. I just copied this from dd-trace-java/dd-trace-core/src/main/java/datadog/trace/core/propagation/W3CHttpCodec.java Line 356 in 0b1e6ff
|
||||
| if (input == null) { | ||||
| return ""; | ||||
| } | ||||
| final int last = input.length() - 1; | ||||
| if (last == 0) { | ||||
| return input; | ||||
| } | ||||
| int start; | ||||
| for (start = 0; start <= last; start++) { | ||||
| char c = input.charAt(start); | ||||
| if (c != '\t' && c != ' ') { | ||||
| break; | ||||
| } | ||||
| } | ||||
| int end; | ||||
| for (end = last; end > start; end--) { | ||||
| char c = input.charAt(end); | ||||
| if (c != '\t' && c != ' ') { | ||||
| break; | ||||
| } | ||||
| } | ||||
| if (start == 0 && end == last) { | ||||
| return input; | ||||
| } else { | ||||
| return input.substring(start, end + 1); | ||||
| } | ||||
| } | ||||
| } | ||||
| } | ||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like DSM, Baggage is not a trace propagation style.
I'm introducing a new API for non tracing propagator.