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

io.opentracing.util.GlobalTracer Maven / Gradle / Ivy

There is a newer version: 0.33.0
Show newest version
/**
 * Copyright 2016-2017 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.util;

import io.opentracing.NoopTracer;
import io.opentracing.NoopTracerFactory;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Global tracer that forwards all methods to another tracer
 * that can be configured by calling {@link #register(Tracer)}.
 * 

* The {@linkplain #register(Tracer) register} method should only be called once * during the application initialization phase.
* If the {@linkplain #register(Tracer) register} method is never called, * the default {@link NoopTracer} is used. */ public final class GlobalTracer implements Tracer { private static final Logger LOGGER = Logger.getLogger(GlobalTracer.class.getName()); /** * Singleton instance. *

* Since we cannot prevent people using {@linkplain #get() GlobalTracer.get()} as a constant, * this guarantees that references obtained before, during or after initialization * all behave as if obtained after initialization once properly initialized.
* As a minor additional benefit it makes it harder to circumvent the {@link Tracer} API. */ private static final GlobalTracer INSTANCE = new GlobalTracer(); /** * The registered {@link Tracer} delegate or the {@link NoopTracer} if none was registered yet. * Never {@code null}. */ private static Tracer tracer = NoopTracerFactory.create(); private GlobalTracer() { } /** * Returns the constant {@linkplain GlobalTracer}. *

* All methods are forwarded to the currently configured tracer.
* Until a tracer is {@link #register(Tracer) explicitly configured}, * the {@link io.opentracing.NoopTracer NoopTracer} is used. * * @return The global tracer constant. * @see #register(Tracer) */ public static Tracer get() { return INSTANCE; } /** * Register a {@link Tracer} to back the behaviour of the {@link #get() global tracer}. *

* Registration is a one-time operation, attempting to call it more often will result in a runtime exception. *

* Every application intending to use the global tracer is responsible for registering it once * during its initialization. * * @param tracer Tracer to use as global tracer. * @throws RuntimeException if there is already a current tracer registered */ public static synchronized void register(final Tracer tracer) { if (tracer == null) { throw new NullPointerException("Cannot register GlobalTracer ."); } if (tracer instanceof GlobalTracer) { LOGGER.log(Level.FINE, "Attempted to register the GlobalTracer as delegate of itself."); return; // no-op } if (isRegistered() && !GlobalTracer.tracer.equals(tracer)) { throw new IllegalStateException("There is already a current global Tracer registered."); } GlobalTracer.tracer = tracer; } /** * Identify whether a {@link Tracer} has previously been registered. *

* This check is useful in scenarios where more than one component may be responsible * for registering a tracer. For example, when using a Java Agent, it will need to determine * if the application has already registered a tracer, and if not attempt to resolve and * register one itself. * * @return Whether a tracer has been registered */ public static synchronized boolean isRegistered() { return !(GlobalTracer.tracer instanceof NoopTracer); } @Override public SpanBuilder buildSpan(String operationName) { return tracer.buildSpan(operationName); } @Override public void inject(SpanContext spanContext, Format format, C carrier) { tracer.inject(spanContext, format, carrier); } @Override public SpanContext extract(Format format, C carrier) { return tracer.extract(format, carrier); } @Override public String toString() { return GlobalTracer.class.getSimpleName() + '{' + tracer + '}'; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy