Search in sources :

Example 1 with NoopSpanContext

use of io.opentracing.noop.NoopSpanContext 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 2 with NoopSpanContext

use of io.opentracing.noop.NoopSpanContext in project hono by eclipse.

the class TracingHelper method injectSpanContext.

/**
 * Injects a {@code SpanContext} into a JSON object.
 * <p>
 * The span context will be injected into a new JSON object under key <em>span-context</em>.
 *
 * @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 jsonObject The JSON object to inject the context into.
 * @throws NullPointerException if tracer or jsonObject is {@code null}.
 */
public static void injectSpanContext(final Tracer tracer, final SpanContext spanContext, final JsonObject jsonObject) {
    Objects.requireNonNull(tracer);
    Objects.requireNonNull(jsonObject);
    if (spanContext != null && !(spanContext instanceof NoopSpanContext)) {
        final JsonObject spanContextJson = new JsonObject();
        jsonObject.put(JSON_KEY_SPAN_CONTEXT, spanContextJson);
        tracer.inject(spanContext, Format.Builtin.TEXT_MAP, new JsonObjectInjectAdapter(spanContextJson));
    }
}
Also used : NoopSpanContext(io.opentracing.noop.NoopSpanContext) JsonObject(io.vertx.core.json.JsonObject)

Example 3 with NoopSpanContext

use of io.opentracing.noop.NoopSpanContext in project hono by eclipse.

the class TracingHelper method injectSpanContext.

/**
 * Injects a {@code SpanContext} into the headers of vert.x {@code DeliveryOptions}.
 *
 * @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 deliveryOptions The delivery options to inject the context into.
 * @throws NullPointerException if tracer or deliveryOptions is {@code null}.
 */
public static void injectSpanContext(final Tracer tracer, final SpanContext spanContext, final DeliveryOptions deliveryOptions) {
    Objects.requireNonNull(tracer);
    Objects.requireNonNull(deliveryOptions);
    if (spanContext != null && !(spanContext instanceof NoopSpanContext)) {
        final MultiMap headers = Optional.of(deliveryOptions).map(options -> options.getHeaders()).orElseGet(() -> {
            final MultiMap newHeaders = MultiMap.caseInsensitiveMultiMap();
            deliveryOptions.setHeaders(newHeaders);
            return newHeaders;
        });
        tracer.inject(spanContext, Format.Builtin.TEXT_MAP, new MultiMapInjectAdapter(headers));
    }
}
Also used : NoopSpanContext(io.opentracing.noop.NoopSpanContext) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) LoggerFactory(org.slf4j.LoggerFactory) MultiMap(io.vertx.core.MultiMap) IntTag(io.opentracing.tag.IntTag) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) Tags(io.opentracing.tag.Tags) BooleanTag(io.opentracing.tag.BooleanTag) References(io.opentracing.References) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Message(org.apache.qpid.proton.message.Message) Fields(io.opentracing.log.Fields) JsonObject(io.vertx.core.json.JsonObject) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Tracer(io.opentracing.Tracer) MessageHelper(org.eclipse.hono.util.MessageHelper) StringTag(io.opentracing.tag.StringTag) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) TextMap(io.opentracing.propagation.TextMap) Format(io.opentracing.propagation.Format) NoopSpanContext(io.opentracing.noop.NoopSpanContext) Optional(java.util.Optional) Span(io.opentracing.Span) MultiMap(io.vertx.core.MultiMap)

Aggregations

NoopSpanContext (io.opentracing.noop.NoopSpanContext)3 TextMap (io.opentracing.propagation.TextMap)2 MultiMap (io.vertx.core.MultiMap)2 JsonObject (io.vertx.core.json.JsonObject)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 References (io.opentracing.References)1 Span (io.opentracing.Span)1 SpanContext (io.opentracing.SpanContext)1 Tracer (io.opentracing.Tracer)1 Fields (io.opentracing.log.Fields)1 Format (io.opentracing.propagation.Format)1 BooleanTag (io.opentracing.tag.BooleanTag)1 IntTag (io.opentracing.tag.IntTag)1 StringTag (io.opentracing.tag.StringTag)1 Tags (io.opentracing.tag.Tags)1 DeliveryOptions (io.vertx.core.eventbus.DeliveryOptions)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1