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

com.vmware.xenon.common.opentracing.TracerFactory Maven / Gradle / Ivy

There is a newer version: 1.6.18
Show newest version
/*
 * Copyright (c) 2017 VMware, Inc. All Rights Reserved.
 *
 * 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 com.vmware.xenon.common.opentracing;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import io.opentracing.NoopTracerFactory;
import io.opentracing.Tracer;

import com.vmware.xenon.common.ServiceHost;

public class TracerFactory {
    /**
     * Singleton: may be replaced to customise implicit tracer creation - e.g. to add support for
     * a different OpenTracing implementation.
     */
    @edu.umd.cs.findbugs.annotations.SuppressFBWarnings("MS_SHOULD_BE_FINAL")
    public static TracerFactory factory = new TracerFactory();

    private boolean enabled;
    private boolean enableChecked;

    /**
     * Create a {@link io.opentracing.Tracer} for use by a {@link com.vmware.xenon.common.ServiceHost}.
     *
     * See README.md for the configuration variables for this factory. The default implementation does
     * not perform any host-specific customisations.
     *
     * @return A {@link io.opentracing.Tracer} instance for tracing the given {@link com.vmware.xenon.common.ServiceHost}
     * If tracing is not enabled, them a {@link io.opentracing.NoopTracer} is returned.
     */
    @SuppressWarnings("unchecked")
    public synchronized Tracer create(ServiceHost host) {
        Logger logger = Logger.getLogger(getClass().getName());
        Map env = System.getenv();
        String implementation = env.get("XENON_TRACER_FACTORY_PROVIDER");
        if (implementation == null) {
            implementation = "";
        }
        implementation = implementation.toLowerCase();
        if (!this.enabled()) {
            logger.info(String.format("Opentracing not enabled."));
            return NoopTracerFactory.create();
        }
        Class factoryClass = null;
        try {
            if (implementation.equals("jaeger")) {
                factoryClass = (Class) Class.forName("com.vmware.xenon.common.opentracing.Jaeger");
            } else if (implementation.equals("zipkin")) {
                factoryClass = (Class) Class.forName("com.vmware.xenon.common.opentracing.Zipkin");
            } else {
                throw new RuntimeException(String.format("Bad tracer type %s", implementation));
            }
            if (factoryClass == null) {
                throw new RuntimeException(String.format(
                        "Failed to cast implementation '%s' to Class", implementation));
            }
        } catch (ClassNotFoundException e) {
            logger.log(Level.SEVERE, "Failed to load implementation class", e);
            throw new RuntimeException(String.format("Could not load implementation for %s", implementation), e);
        }
        assert factoryClass != null;
        TracerFactoryInterface factory;
        try {
            factory = factoryClass.getConstructor().newInstance();
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            logger.log(Level.SEVERE, "Failed to instantiate tracer factory", e);
            throw new RuntimeException(String.format("Could not instantiate factory for %s", implementation), e);
        }
        return factory.create(host);
    }

    /**
     * Is Tracing enabled? {@link TracerFactory#create(ServiceHost)} will return a {@link io.opentracing.NoopTracer}
     *
     * @return
     */
    public boolean enabled() {
        if (!this.enableChecked) {
            Map env = System.getenv();
            String implementation = env.get("XENON_TRACER_FACTORY_PROVIDER");
            this.enabled = (implementation != null) && (implementation.length() > 0);
            this.enableChecked = true;
        }
        return this.enabled;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy