Search in sources :

Example 1 with SenderFactory

use of io.jaegertracing.spi.SenderFactory in project jaeger-client-java by jaegertracing.

the class SenderResolver method resolve.

/**
 * Resolves a sender by passing the given {@link Configuration.SenderConfiguration} down to the
 * {@link SenderFactory}. The factory is loaded either based on the value from the environment variable
 * {@link Configuration#JAEGER_SENDER_FACTORY} or, in its absence or failure to deliver a {@link Sender},
 * via the {@link ServiceLoader}. If no factories are found, a {@link NoopSender} is returned. If multiple factories
 * are available, the factory whose {@link SenderFactory#getType()} matches the JAEGER_SENDER_FACTORY env var is
 * selected. If none matches, {@link NoopSender} is returned.
 *
 * @param senderConfiguration the configuration to pass down to the factory
 * @return the resolved Sender, or NoopSender
 */
public static Sender resolve(Configuration.SenderConfiguration senderConfiguration) {
    Sender sender = null;
    ServiceLoader<SenderFactory> senderFactoryServiceLoader = ServiceLoader.load(SenderFactory.class, SenderFactory.class.getClassLoader());
    Iterator<SenderFactory> senderFactoryIterator = senderFactoryServiceLoader.iterator();
    if (!senderFactoryIterator.hasNext()) {
        log.warn("No sender factories available. Using NoopSender, meaning that data will not be sent anywhere!");
        return new NoopSender();
    }
    String requestedFactory = System.getProperty(Configuration.JAEGER_SENDER_FACTORY);
    boolean hasMultipleFactories = false;
    boolean isRequestedFactoryAvailable = false;
    while (senderFactoryIterator.hasNext()) {
        SenderFactory senderFactory = senderFactoryIterator.next();
        if (senderFactoryIterator.hasNext()) {
            log.debug("There are multiple factories available via the service loader.");
            hasMultipleFactories = true;
        }
        if (hasMultipleFactories) {
            // factory the user wants:
            if (senderFactory.getType().equals(requestedFactory)) {
                log.debug(String.format("Found the requested (%s) sender factory: %s", requestedFactory, senderFactory));
                isRequestedFactoryAvailable = true;
                sender = getSenderFromFactory(senderFactory, senderConfiguration);
            }
        } else {
            sender = getSenderFromFactory(senderFactory, senderConfiguration);
        }
    }
    if (null != sender) {
        log.debug(String.format("Using sender %s", sender));
        return sender;
    } else if (requestedFactory == null && hasMultipleFactories) {
        log.warn("Multiple factories available but JAEGER_SENDER_FACTORY property not specified.");
    } else if (requestedFactory != null && hasMultipleFactories && !isRequestedFactoryAvailable) {
        log.warn(String.format("%s not available, using NoopSender, hence data will not be sent anywhere!", requestedFactory));
    } else {
        log.warn("No suitable sender found. Using NoopSender, meaning that data will not be sent anywhere!");
    }
    return new NoopSender();
}
Also used : Sender(io.jaegertracing.spi.Sender) SenderFactory(io.jaegertracing.spi.SenderFactory)

Aggregations

Sender (io.jaegertracing.spi.Sender)1 SenderFactory (io.jaegertracing.spi.SenderFactory)1