use of io.opentracing.propagation.TextMap in project Payara by payara.
the class Tracer method extract.
@Override
public <C> SpanContext extract(Format<C> format, C carrier) {
boolean traceIDRecieved = false;
if (carrier instanceof TextMap) {
TextMap map = (TextMap) carrier;
Map<String, String> baggageItems = new HashMap<>();
Iterator<Map.Entry<String, String>> allEntries = map.iterator();
UUID traceId = null;
UUID spanId = null;
if (format.equals(Format.Builtin.HTTP_HEADERS)) {
while (allEntries.hasNext()) {
Entry<String, String> entry = allEntries.next();
switch(entry.getKey()) {
case TRACEID_KEY:
traceId = UUID.fromString(decodeURLString(entry.getValue()));
traceIDRecieved = true;
break;
case SPANID_KEY:
spanId = UUID.fromString(decodeURLString(entry.getValue()));
break;
default:
baggageItems.put(decodeURLString(entry.getKey()), decodeURLString(entry.getValue()));
break;
}
}
} else if (format.equals(Format.Builtin.TEXT_MAP)) {
while (allEntries.hasNext()) {
Entry<String, String> entry = allEntries.next();
switch(entry.getKey()) {
case TRACEID_KEY:
traceId = UUID.fromString(entry.getValue());
traceIDRecieved = true;
break;
case SPANID_KEY:
spanId = UUID.fromString(entry.getValue());
break;
default:
baggageItems.put(entry.getKey(), entry.getValue());
break;
}
}
} else {
throw new InvalidCarrierFormatException(format, carrier);
}
if (traceIDRecieved) {
if (spanId == null) {
throw new IllegalArgumentException("No SpanId recieved");
}
return new RequestTraceSpanContext(traceId, spanId, baggageItems);
} else {
// Did not recieve a SpanContext
return null;
}
} else if (carrier instanceof ByteBuffer) {
ByteBuffer buffer = (ByteBuffer) carrier;
if (format.equals(Format.Builtin.BINARY)) {
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer.array());
ObjectInputStream objectStream = new ObjectInputStream(inputStream);
return (SpanContext) objectStream.readObject();
} catch (IOException | ClassNotFoundException | ClassCastException ex) {
Logger.getLogger(Tracer.class.getName()).log(Level.FINER, null, ex);
// Serialised state is invalid
throw new IllegalArgumentException(ex);
}
} else {
throw new InvalidCarrierFormatException(format, carrier);
}
}
throw new InvalidCarrierFormatException(format, carrier);
}
use of io.opentracing.propagation.TextMap in project Payara by payara.
the class Tracer method inject.
@Override
public <C> void inject(SpanContext spanContext, Format<C> format, C carrier) {
RequestTraceSpanContext payaraSpanContext = (RequestTraceSpanContext) spanContext;
Iterable<Map.Entry<String, String>> baggageItems = payaraSpanContext.baggageItems();
if (carrier instanceof TextMap) {
TextMap map = (TextMap) carrier;
if (format.equals(Format.Builtin.HTTP_HEADERS)) {
for (Map.Entry<String, String> baggage : baggageItems) {
map.put(encodeURLString(baggage.getKey()), encodeURLString(baggage.getValue()));
}
map.put(TRACEID_KEY, encodeURLString(payaraSpanContext.getTraceId().toString()));
map.put(SPANID_KEY, encodeURLString(payaraSpanContext.getSpanId().toString()));
} else if (format.equals(Format.Builtin.TEXT_MAP)) {
for (Map.Entry<String, String> baggage : baggageItems) {
map.put(baggage.getKey(), baggage.getValue());
}
map.put(TRACEID_KEY, payaraSpanContext.getTraceId().toString());
map.put(SPANID_KEY, payaraSpanContext.getSpanId().toString());
} else {
throw new InvalidCarrierFormatException(format, carrier);
}
} else if (carrier instanceof ByteBuffer) {
ByteBuffer buffer = (ByteBuffer) carrier;
if (format.equals(Format.Builtin.BINARY)) {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
try {
ObjectOutputStream objectWriter = new ObjectOutputStream(bytesOut);
objectWriter.writeObject(spanContext);
objectWriter.flush();
buffer.put(bytesOut.toByteArray());
} catch (IOException ex) {
Logger.getLogger(Tracer.class.getName()).log(Level.WARNING, null, ex);
throw new UncheckedIOException(ex);
}
} else {
throw new InvalidCarrierFormatException(format, carrier);
}
} else {
throw new InvalidCarrierFormatException(format, carrier);
}
}
use of io.opentracing.propagation.TextMap in project hono by eclipse.
the class TracingHelper method injectSpanContext.
/**
* Injects a {@code SpanContext} as key-value pairs into a given operation.
* <p>
* This provides a generic way to serialize a span context in any kind of textual data.
* See {@link #extractSpanContext(Tracer, Supplier)} for the corresponding method to deserialize the
* context from that data.
*
* @param tracer The Tracer to use for injecting the context.
* @param spanContext The context to inject or {@code null} if no context is available.
* @param keyValueConsumer The operation that will receive the key-value pairs representing the context.
* @throws NullPointerException if tracer or keyValueConsumer is {@code null}.
*/
public static void injectSpanContext(final Tracer tracer, final SpanContext spanContext, final BiConsumer<String, String> keyValueConsumer) {
Objects.requireNonNull(tracer);
Objects.requireNonNull(keyValueConsumer);
if (spanContext != null && !(spanContext instanceof NoopSpanContext)) {
tracer.inject(spanContext, Format.Builtin.TEXT_MAP, new TextMap() {
@Override
public Iterator<Map.Entry<String, String>> iterator() {
throw new UnsupportedOperationException();
}
@Override
public void put(final String key, final String value) {
keyValueConsumer.accept(key, value);
}
});
}
}
use of io.opentracing.propagation.TextMap in project jaeger-client-java by jaegertracing.
the class ConfigurationTest method testAddedCodecs.
@Test
public void testAddedCodecs() {
Codec<TextMap> codec1 = new Codec<TextMap>() {
@Override
public JaegerSpanContext extract(TextMap carrier) {
return null;
}
@Override
public void inject(JaegerSpanContext spanContext, TextMap carrier) {
}
};
Codec<TextMap> codec2 = new Codec<TextMap>() {
@Override
public JaegerSpanContext extract(TextMap carrier) {
return null;
}
@Override
public void inject(JaegerSpanContext spanContext, TextMap carrier) {
}
};
Codec<Binary> codec3 = new Codec<Binary>() {
@Override
public JaegerSpanContext extract(Binary carrier) {
return null;
}
@Override
public void inject(JaegerSpanContext spanContext, Binary carrier) {
}
};
CodecConfiguration codecConfiguration = new CodecConfiguration().withCodec(Builtin.HTTP_HEADERS, codec1).withCodec(Builtin.HTTP_HEADERS, codec2).withBinaryCodec(Builtin.BINARY, codec3);
assertEquals(2, codecConfiguration.getCodecs().get(Builtin.HTTP_HEADERS).size());
assertEquals(codec1, codecConfiguration.getCodecs().get(Builtin.HTTP_HEADERS).get(0));
assertEquals(codec2, codecConfiguration.getCodecs().get(Builtin.HTTP_HEADERS).get(1));
assertEquals(codec3, codecConfiguration.getBinaryCodecs().get(Builtin.BINARY).get(0));
Configuration configuration = new Configuration("foo").withCodec(codecConfiguration);
long traceIdLow = 2L;
long spanId = 11L;
long parentId = 22L;
JaegerSpanContext spanContext = new JaegerSpanContext(0, traceIdLow, spanId, parentId, (byte) 0);
assertInjectExtract(configuration.getTracer(), Builtin.TEXT_MAP, spanContext, false);
// added codecs above overrides the default implementation
assertInjectExtract(configuration.getTracer(), Builtin.HTTP_HEADERS, spanContext, true);
}
use of io.opentracing.propagation.TextMap in project jaeger-client-java by jaegertracing.
the class AdhocHeadersTest method testDebugCorrelationId.
@Test
public void testDebugCorrelationId() {
Map<String, String> headers = Collections.singletonMap(Constants.DEBUG_ID_HEADER_KEY, "Coraline");
TextMap carrier = new TextMapAdapter(headers);
JaegerSpanContext inboundSpanContext = tracer.extract(Format.Builtin.TEXT_MAP, carrier);
assertNotNull(inboundSpanContext);
assertFalse(inboundSpanContext.hasTrace());
assertEquals("Coraline", inboundSpanContext.getDebugId());
JaegerSpan span = tracer.buildSpan("span").asChildOf(inboundSpanContext).start();
JaegerSpanContext serverSpanContext = span.context();
assertTrue(serverSpanContext.isSampled());
assertTrue(serverSpanContext.isDebug());
assertEquals("Coraline", span.getTags().get(Constants.DEBUG_ID_HEADER_KEY));
}
Aggregations