All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentracing.v_030.shim.TracerShim Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016-2018 The OpenTracing Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package io.opentracing.v_030.shim;

import io.opentracing.ScopeManager;
import io.opentracing.Scope;
import io.opentracing.v_030.ActiveSpan;
import io.opentracing.v_030.BaseSpan;
import io.opentracing.v_030.Span;
import io.opentracing.v_030.SpanContext;
import io.opentracing.v_030.Tracer;
import io.opentracing.v_030.propagation.Format;

import java.util.Map;

public class TracerShim implements Tracer {
    final io.opentracing.Tracer tracer;

    public TracerShim(io.opentracing.Tracer tracer) {
        checkArgumentNotNull(tracer, "tracer");

        this.tracer = tracer;
    }

    void checkArgumentNotNull(Object value, String argName) {
        if (value == null) {
            throw new IllegalArgumentException(argName + " cannot be null");
        }
    }

    protected ActiveSpanShim createActiveSpanShim(Scope scope) {
        return new SimpleActiveSpanShim(scope);
    }

    protected SpanShim createSpanShim(io.opentracing.Span span) {
        return new SimpleSpanShim(span);
    }

    @Override
    public ActiveSpan activeSpan() {
        if (tracer.scopeManager().active() == null) {
            return null;
        }

        return createActiveSpanShim(tracer.scopeManager().active());
    }

    @Override
    public ActiveSpan makeActive(Span span) {
        checkArgumentNotNull(span, "span");

        io.opentracing.Span wrappedSpan = ((SpanWrapper)span).span();
        return createActiveSpanShim(tracer.scopeManager().activate(wrappedSpan, true));
    }

    @Override
    public SpanBuilder buildSpan(String operationName) {
        return new SpanBuilderShim(tracer.buildSpan(operationName));
    }

    @Override
    public  void inject(SpanContext spanContext, Format format, C carrier) {
        checkArgumentNotNull(spanContext, "spanContext");

        tracer.inject(((SpanContextShim)spanContext).context(),
                FormatConverter.toUpstreamFormat(format),
                FormatConverter.toUpstreamInjectCarrier(format, carrier));
    }

    @Override
    public  SpanContext extract(Format format, C carrier) {
        io.opentracing.SpanContext context = tracer.extract(FormatConverter.toUpstreamFormat(format),
                FormatConverter.toUpstreamExtractCarrier(format, carrier));
        return new SpanContextShim(context);
    }

    private final static class SimpleActiveSpanShim extends ActiveSpanShim {
        public SimpleActiveSpanShim(io.opentracing.Scope scope) {
            super(scope);
        }
    }

    private final static class SimpleSpanShim extends SpanShim {
        public SimpleSpanShim(io.opentracing.Span span) {
            super(span);
        }
    }

    private final class SpanBuilderShim implements Tracer.SpanBuilder {
        io.opentracing.Tracer.SpanBuilder builder;

        public SpanBuilderShim(io.opentracing.Tracer.SpanBuilder builder) {
            this.builder = builder;
        }

        @Override
        public SpanBuilderShim asChildOf(SpanContext parent) {
            checkArgumentNotNull(parent, "parent");

            builder.asChildOf(((SpanContextShim)parent).context());
            return this;
        }

        @Override
        public SpanBuilderShim asChildOf(BaseSpan parent) {
            checkArgumentNotNull(parent, "parent");

            builder.asChildOf(((SpanWrapper)parent).span());
            return this;
        }

        @Override
        public SpanBuilderShim addReference(String referenceType, SpanContext referencedContext) {
            checkArgumentNotNull(referencedContext, "referencedContext");

            builder.addReference(referenceType, ((SpanContextShim)referencedContext).context());
            return this;
        }

        @Override
        public SpanBuilderShim ignoreActiveSpan() {
            builder.ignoreActiveSpan();
            return this;
        }

        @Override
        public SpanBuilderShim withTag(String key, String value) {
            builder.withTag(key, value);
            return this;
        }

        @Override
        public SpanBuilderShim withTag(String key, boolean value) {
            builder.withTag(key, value);
            return this;
        }

        @Override
        public SpanBuilderShim withTag(String key, Number value) {
            builder.withTag(key, value);
            return this;
        }

        @Override
        public SpanBuilderShim withStartTimestamp(long microseconds) {
            builder.withStartTimestamp(microseconds);
            return this;
        }

        @Override
        public ActiveSpan startActive() {
            Scope scope = builder.startActive(true);
            return createActiveSpanShim(scope);
        }

        @Override
        public Span startManual() {
            return createSpanShim(builder.startManual());
        }

        @Override
        public Span start() {
            return createSpanShim(builder.start());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy