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

org.apache.cxf.tracing.opentracing.OpenTracingContext Maven / Gradle / Ivy

There is a newer version: 4.1.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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 org.apache.cxf.tracing.opentracing;

import java.util.concurrent.Callable;

import org.apache.cxf.tracing.Traceable;
import org.apache.cxf.tracing.TracerContext;

import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;

public class OpenTracingContext implements TracerContext {
    private final Tracer tracer;
    private final Span continuation;

    public OpenTracingContext(final Tracer tracer) {
        this(tracer, null);
    }

    public OpenTracingContext(final Tracer tracer, final Span continuation) {
        this.tracer = tracer;
        this.continuation = continuation;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Scope startSpan(final String description) {
        return newOrChildSpan(description, null);
    }

    @Override
    public  T continueSpan(final Traceable traceable) throws Exception {
        Scope scope = null;
        
        if (tracer.activeSpan() == null && continuation != null) {
            scope = tracer.scopeManager().activate(continuation);
        }

        try { //NOPMD
            return traceable.call(new OpenTracingContext(tracer));
        } finally {
            if (continuation != null && scope != null) {
                scope.close();
            }
        }
    }

    @Override
    public  Callable wrap(final String description, final Traceable traceable) {
        final Callable callable = new Callable() {
            @Override
            public T call() throws Exception {
                return traceable.call(new OpenTracingContext(tracer));
            }
        };

        // Carry over parent from the current thread
        final Span parent = tracer.activeSpan();
        return () -> {
            try (Scope scope = newOrChildSpan(description, parent)) {
                return callable.call();
            }
        };
    }

    @Override
    public void annotate(String key, String value) {
        final Span current = tracer.activeSpan();
        if (current != null) {
            current.setTag(key, value);
        }
    }

    @Override
    public void timeline(String message) {
        final Span current = tracer.activeSpan();
        if (current != null) {
            current.log(message);
        }
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public  T unwrap(final Class clazz) {
        if (Tracer.class.equals(clazz)) {
            return (T)tracer;
        } else {
            throw new IllegalArgumentException("The class is '" + clazz
                + "'not supported and cannot be unwrapped");
        }
    }
    
    private Scope newOrChildSpan(final String description, final Span parent) {
        final Span span;
        
        if (parent == null) {
            span = tracer.buildSpan(description).start(); 
        } else {
            span = tracer.buildSpan(description).asChildOf(parent).start();
        }
        
        return new ScopedSpan(span, tracer.scopeManager().activate(span));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy