Search in sources :

Example 1 with Sender

use of io.jaegertracing.spi.Sender 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)

Example 2 with Sender

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

the class RemoteReporterTest method testCloseLogSenderException.

@Test
public void testCloseLogSenderException() throws SenderException {
    Appender<ILoggingEvent> mockAppender = mockLogger(e -> {
    });
    final Sender mockedSender = mock(Sender.class);
    when(mockedSender.close()).thenThrow(SenderException.class);
    final RemoteReporter testReporter = new RemoteReporter.Builder().withSender(mockedSender).build();
    // call the to be tested code path
    testReporter.close();
    // verify the actual log was written
    verify(mockAppender).doAppend(argThat(new ArgumentMatcher<ILoggingEvent>() {

        @Override
        public boolean matches(ILoggingEvent event) {
            return event.getFormattedMessage().startsWith("Remote reporter error");
        }
    }));
}
Also used : InMemorySender(io.jaegertracing.internal.senders.InMemorySender) Sender(io.jaegertracing.spi.Sender) Builder(io.jaegertracing.internal.reporters.RemoteReporter.Builder) ArgumentMatcher(org.mockito.ArgumentMatcher) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Test(org.junit.Test)

Example 3 with Sender

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

the class SenderResolverTest method testNoImplementations.

@Test
public void testNoImplementations() throws Exception {
    Sender sender = getSenderForServiceFileContents("", false);
    assertTrue("Expected to have sender as instance of NoopSender, but was " + sender.getClass(), sender instanceof NoopSender);
}
Also used : Sender(io.jaegertracing.spi.Sender) Test(org.junit.Test)

Example 4 with Sender

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

the class SenderResolverTest method testNoopSenderFactory.

@Test
public void testNoopSenderFactory() throws Exception {
    System.setProperty(Configuration.JAEGER_SENDER_FACTORY, "noop");
    Sender sender = getSenderForServiceFileContents("\n" + NoopSenderFactory.class.getName(), true);
    SenderResolver.resolve();
    assertTrue("Expected to have sender as instance of NoopSender, but was " + sender.getClass(), sender instanceof NoopSender);
    System.clearProperty(Configuration.JAEGER_SENDER_FACTORY);
}
Also used : Sender(io.jaegertracing.spi.Sender) Test(org.junit.Test)

Example 5 with Sender

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

the class SenderResolverTest method testMultipleImplementationsNotAmbiguous.

@Test
public void testMultipleImplementationsNotAmbiguous() throws Exception {
    System.setProperty(Configuration.JAEGER_SENDER_FACTORY, "to-be-loaded");
    CustomSender customSender = new CustomSender();
    SenderFactoryToBeLoaded.sender = customSender;
    Sender sender = getSenderForServiceFileContents("\nio.jaegertracing.internal.senders.InMemorySenderFactory", true);
    assertEquals(customSender, sender);
    System.setProperty(Configuration.JAEGER_SENDER_FACTORY, "in-memory");
    sender = getSenderForServiceFileContents("\nio.jaegertracing.internal.senders.InMemorySenderFactory", true);
    assertTrue(sender instanceof InMemorySender);
}
Also used : Sender(io.jaegertracing.spi.Sender) Test(org.junit.Test)

Aggregations

Sender (io.jaegertracing.spi.Sender)12 Test (org.junit.Test)10 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 Slf4jLogSender (demo.jaxrs.tracing.Slf4jLogSender)1 Configuration (io.jaegertracing.Configuration)1 ReporterConfiguration (io.jaegertracing.Configuration.ReporterConfiguration)1 SamplerConfiguration (io.jaegertracing.Configuration.SamplerConfiguration)1 SenderConfiguration (io.jaegertracing.Configuration.SenderConfiguration)1 Builder (io.jaegertracing.internal.reporters.RemoteReporter.Builder)1 InMemorySender (io.jaegertracing.internal.senders.InMemorySender)1 SenderFactory (io.jaegertracing.spi.SenderFactory)1 Tracer (io.opentracing.Tracer)1 Response (javax.ws.rs.core.Response)1 OpenTracingClientProvider (org.apache.cxf.tracing.opentracing.jaxrs.OpenTracingClientProvider)1 ArgumentMatcher (org.mockito.ArgumentMatcher)1