Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Removing cached sender and tracer from Configuration so that new inst…
Browse files Browse the repository at this point in the history
…ances will be created from the configuration.

This allows the new instance to be up-to-date with of configuration changes.

Resolves #684
     * Adding a closed attribute on sender
     * Sender configuration is resolving again is sender is closed.
Signed-off-by: Emmanuel Hugonnet <ehugonne@redhat.com>
  • Loading branch information
ehsavoie committed Feb 12, 2020
1 parent 74adce4 commit b64183a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 44 deletions.
30 changes: 3 additions & 27 deletions jaeger-core/src/main/java/io/jaegertracing/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ public enum Propagation {
private Map<String, String> tracerTags;
private boolean useTraceId128Bit;

/**
* lazy singleton JaegerTracer initialized in getTracer() method.
*/
private JaegerTracer tracer;

public Configuration(String serviceName) {
this.serviceName = JaegerTracer.Builder.checkValidServiceName(serviceName);
Expand Down Expand Up @@ -240,22 +236,11 @@ protected JaegerTracer.Builder createTracerBuilder(String serviceName) {
}

public synchronized JaegerTracer getTracer() {
if (tracer != null) {
return tracer;
}

tracer = getTracerBuilder().build();
JaegerTracer tracer = getTracerBuilder().build();
log.info("Initialized tracer={}", tracer);

return tracer;
}

public synchronized void closeTracer() {
if (tracer != null) {
tracer.close();
}
}

private MetricsFactory loadMetricsFactory() {
ServiceLoader<MetricsFactory> loader = ServiceLoader.load(MetricsFactory.class);

Expand Down Expand Up @@ -615,11 +600,6 @@ public SenderConfiguration getSenderConfiguration() {
*/
@Getter
public static class SenderConfiguration {
/**
* A custom sender set by our consumers. If set, nothing else has effect. Optional.
*/
private Sender sender;

/**
* The Agent Host. Has no effect if the sender is set. Optional.
*/
Expand Down Expand Up @@ -684,15 +664,11 @@ public SenderConfiguration withAuthPassword(String password) {
}

/**
* Returns a sender if one was given when creating the configuration, or attempts to create a sender based on the
* configuration's state.
* Returns a sender based on the configuration's state.
* @return the sender passed via the constructor or a properly configured sender
*/
public Sender getSender() {
if (sender == null) {
sender = SenderResolver.resolve(this);
}
return sender;
return SenderResolver.resolve(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,12 @@ public void testOverrideServiceName() {
@Test
public void testDefaultTracer() {
Configuration configuration = new Configuration("name");
assertNotNull(configuration.getTracer());
assertNotNull(configuration.getTracer());
configuration.closeTracer();
JaegerTracer tracer = configuration.getTracer();
assertNotNull(tracer);
tracer.close();
tracer = configuration.getTracer();
assertNotNull(tracer);
tracer.close();
}

@Test(expected = IllegalStateException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,6 @@ public void testTracer() {
Assert.assertTrue(tracer.scopeManager().activeSpan() instanceof CustomSpan);
Assert.assertTrue(tracer.scopeManager().activeSpan().context() instanceof CustomSpanContext);
}
config.closeTracer();
tracer.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.jaegertracing.crossdock.resources.behavior.http.EndToEndBehaviorResource;
import io.jaegertracing.crossdock.resources.behavior.http.TraceBehaviorResource;
import io.jaegertracing.crossdock.resources.health.HealthResource;
import io.jaegertracing.internal.JaegerTracer;
import io.jaegertracing.internal.samplers.ConstSampler;
import io.jaegertracing.spi.Sender;
import io.jaegertracing.thrift.internal.senders.HttpSender;
Expand Down Expand Up @@ -59,17 +60,17 @@ public class JerseyServer {

private final HttpServer server;

private final Configuration config;
private final JaegerTracer tracer;

public JerseyServer(String host, int port, Configuration configuration, List<Object> resources)
public JerseyServer(String host, int port, JaegerTracer tracer, List<Object> resources)
throws IOException {
this.config = configuration;
this.tracer = tracer;

// create a resource config that scans for JAX-RS resources and providers
final ResourceConfig rc = new ResourceConfig();

resources.forEach(rc::register);
rc.register(new ServerTracingDynamicFeature.Builder(config.getTracer())
rc.register(new ServerTracingDynamicFeature.Builder(tracer)
.withTraceSerialization(false)
.build())
.register(LoggingFilter.class)
Expand All @@ -84,18 +85,18 @@ public JerseyServer(String host, int port, Configuration configuration, List<Obj

server = HttpServer.createSimpleServer(".", host, port);
context.deploy(server);
client = initializeClient(config);
client = initializeClient(tracer);
server.start();
}

public void addNetworkListener(NetworkListener networkListener) {
server.addListener(networkListener);
}

private static Client initializeClient(final Configuration config) {
private static Client initializeClient(final JaegerTracer tracer) {
return ClientBuilder.newClient()
.register(ExceptionMapper.class)
.register(new ClientTracingFeature.Builder(config.getTracer()).build())
.register(new ClientTracingFeature.Builder(tracer).build())
.register(JacksonFeature.class);
}

Expand All @@ -109,7 +110,7 @@ public void shutdown() throws ExecutionException, InterruptedException {
}

public Tracer getTracer() {
return config.getTracer();
return tracer;
}

public static void main(String[] args) throws Exception {
Expand All @@ -119,9 +120,9 @@ public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration(serviceName)
.withSampler(new SamplerConfiguration().withType(ConstSampler.TYPE).withParam(0))
.withReporter(new ReporterConfiguration().withLogSpans(true));

JerseyServer server = new JerseyServer("0.0.0.0", 8081, configuration,
Arrays.asList(new TraceBehaviorResource(configuration.getTracer()),
JaegerTracer tracer = configuration.getTracer();
JerseyServer server = new JerseyServer("0.0.0.0", 8081, tracer,
Arrays.asList(new TraceBehaviorResource(tracer),
new EndToEndBehaviorResource(new EndToEndBehavior(getEvn(SAMPLING_HOST_PORT, "jaeger-agent:5778"),
"crossdock-" + serviceName,
senderFromEnv(getEvn(COLLECTOR_HOST_PORT, "jaeger-collector:14268"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.jaegertracing.crossdock.api.TraceResponse;
import io.jaegertracing.crossdock.resources.behavior.TraceBehavior;
import io.jaegertracing.internal.JaegerSpanContext;
import io.jaegertracing.internal.JaegerTracer;
import io.jaegertracing.internal.samplers.ConstSampler;
import io.opentracing.Scope;
import io.opentracing.Span;
Expand Down Expand Up @@ -87,8 +88,9 @@ public void setUp() throws Exception {
Configuration configuration = new Configuration(SERVICE_NAME).withSampler(
new SamplerConfiguration().withType(ConstSampler.TYPE).withParam(0))
.withReporter(new ReporterConfiguration().withLogSpans(true));
server = new JerseyServer("127.0.0.1", port, configuration,
Collections.singletonList(new TraceBehaviorResource(configuration.getTracer())));
JaegerTracer tracer = configuration.getTracer();
server = new JerseyServer("127.0.0.1", port, tracer,
Collections.singletonList(new TraceBehaviorResource(tracer)));
hostPort = String.format("127.0.0.1:%d", port);
behavior = new TraceBehavior(server.getTracer());
}
Expand Down

0 comments on commit b64183a

Please sign in to comment.