com.uber.jaeger.propagation.TextMapCodec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jaeger-core Show documentation
Show all versions of jaeger-core Show documentation
Jaeger Java bindings for OpenTracing API
/*
* Copyright (c) 2016, Uber Technologies, Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.uber.jaeger.propagation;
import com.uber.jaeger.Constants;
import com.uber.jaeger.SpanContext;
import io.opentracing.propagation.TextMap;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class TextMapCodec implements Injector, Extractor {
/** Key used to store serialized span context representation */
private static final String SPAN_CONTEXT_KEY = "uber-trace-id";
/** Key prefix used for baggage items */
private static final String BAGGAGE_KEY_PREFIX = "uberctx-";
private static final PrefixedKeys keys = new PrefixedKeys();
private final String contextKey = SPAN_CONTEXT_KEY;
private final String baggagePrefix = BAGGAGE_KEY_PREFIX;
private final boolean urlEncoding;
public TextMapCodec(boolean urlEncoding) {
this.urlEncoding = urlEncoding;
}
@Override
public void inject(SpanContext spanContext, TextMap carrier) {
carrier.put(contextKey, encodedValue(spanContext.contextAsString()));
for (Map.Entry entry: spanContext.baggageItems()) {
carrier.put(
keys.prefixedKey(entry.getKey(), baggagePrefix),
encodedValue(entry.getValue()));
}
}
@Override
public SpanContext extract(TextMap carrier) {
SpanContext context = null;
Map baggage = null;
String debugID = null;
for (Map.Entry entry : carrier) {
// TODO there should be no lower-case here
String key = entry.getKey().toLowerCase();
if (key.equals(contextKey)) {
context = SpanContext.contextFromString(
decodedValue(entry.getValue()));
} else if (key.equals(Constants.DEBUG_ID_HEADER_KEY)) {
debugID = decodedValue(entry.getValue());
} else if (key.startsWith(baggagePrefix)) {
if (baggage == null) {
baggage = new HashMap<>();
}
baggage.put(
keys.unprefixedKey(key, baggagePrefix),
decodedValue(entry.getValue()));
}
}
if (context == null) {
if (debugID != null) {
return SpanContext.withDebugID(debugID);
}
return null;
}
if (baggage == null) {
return context;
}
return context.withBaggage(baggage);
}
private String encodedValue(String value) {
if (!urlEncoding) {
return value;
}
try {
return URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
// not much we can do, try raw value
return value;
}
}
private String decodedValue(String value) {
if (!urlEncoding) {
return value;
}
try {
return URLDecoder.decode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
// not much we can do, try raw value
return value;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy