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();
}
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");
}
}));
}
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);
}
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);
}
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);
}
Aggregations