Search in sources :

Example 11 with TextMap

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);
}
Also used : HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) RequestTraceSpanContext(fish.payara.notification.requesttracing.RequestTraceSpanContext) Entry(java.util.Map.Entry) ByteArrayInputStream(java.io.ByteArrayInputStream) TextMap(io.opentracing.propagation.TextMap) UUID(java.util.UUID) ObjectInputStream(java.io.ObjectInputStream)

Example 12 with TextMap

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);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ObjectOutputStream(java.io.ObjectOutputStream) ByteBuffer(java.nio.ByteBuffer) RequestTraceSpanContext(fish.payara.notification.requesttracing.RequestTraceSpanContext) Entry(java.util.Map.Entry) TextMap(io.opentracing.propagation.TextMap) HashMap(java.util.HashMap) Map(java.util.Map) TextMap(io.opentracing.propagation.TextMap)

Example 13 with TextMap

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);
            }
        });
    }
}
Also used : NoopSpanContext(io.opentracing.noop.NoopSpanContext) Iterator(java.util.Iterator) TextMap(io.opentracing.propagation.TextMap) MultiMap(io.vertx.core.MultiMap) HashMap(java.util.HashMap) Map(java.util.Map) TextMap(io.opentracing.propagation.TextMap)

Example 14 with TextMap

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);
}
Also used : TraceContextCodec(io.jaegertracing.internal.propagation.TraceContextCodec) B3TextMapCodec(io.jaegertracing.internal.propagation.B3TextMapCodec) Codec(io.jaegertracing.spi.Codec) BinaryCodec(io.jaegertracing.internal.propagation.BinaryCodec) TextMapCodec(io.jaegertracing.internal.propagation.TextMapCodec) SenderConfiguration(io.jaegertracing.Configuration.SenderConfiguration) CodecConfiguration(io.jaegertracing.Configuration.CodecConfiguration) ReporterConfiguration(io.jaegertracing.Configuration.ReporterConfiguration) SamplerConfiguration(io.jaegertracing.Configuration.SamplerConfiguration) CodecConfiguration(io.jaegertracing.Configuration.CodecConfiguration) TextMap(io.opentracing.propagation.TextMap) Binary(io.opentracing.propagation.Binary) JaegerSpanContext(io.jaegertracing.internal.JaegerSpanContext) Test(org.junit.Test)

Example 15 with TextMap

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));
}
Also used : TextMap(io.opentracing.propagation.TextMap) TextMapAdapter(io.opentracing.propagation.TextMapAdapter) Test(org.junit.Test)

Aggregations

TextMap (io.opentracing.propagation.TextMap)29 Test (org.junit.Test)16 HashMap (java.util.HashMap)14 Map (java.util.Map)7 SpanContext (com.uber.jaeger.SpanContext)6 JaegerSpanContext (io.jaegertracing.internal.JaegerSpanContext)6 ConstSampler (com.uber.jaeger.samplers.ConstSampler)3 Format (io.opentracing.propagation.Format)3 TextMapAdapter (io.opentracing.propagation.TextMapAdapter)3 SpanId (com.github.kristofa.brave.SpanId)2 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)2 Metrics (com.uber.jaeger.metrics.Metrics)2 InMemoryReporter (com.uber.jaeger.reporters.InMemoryReporter)2 RequestTraceSpanContext (fish.payara.notification.requesttracing.RequestTraceSpanContext)2 JaegerSpan (io.jaegertracing.internal.JaegerSpan)2 TextMapExtractAdapter (io.opentracing.propagation.TextMapExtractAdapter)2 MultiMap (io.vertx.core.MultiMap)2 ByteBuffer (java.nio.ByteBuffer)2 Iterator (java.util.Iterator)2 Entry (java.util.Map.Entry)2