Skip to content

Commit

Permalink
Makes it possible to specify a recorder (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriancole committed Jan 2, 2017
1 parent 4dc3de5 commit 850279d
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions brave-core/src/main/java/com/github/kristofa/brave/Brave.java
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.kristofa.brave; package com.github.kristofa.brave;


import com.github.kristofa.brave.AnnotationSubmitter.Clock;
import com.github.kristofa.brave.AnnotationSubmitter.DefaultClock;
import com.github.kristofa.brave.internal.Internal; import com.github.kristofa.brave.internal.Internal;
import com.github.kristofa.brave.internal.InternalSpan; import com.github.kristofa.brave.internal.InternalSpan;
import com.github.kristofa.brave.internal.Nullable; import com.github.kristofa.brave.internal.Nullable;
Expand Down Expand Up @@ -43,9 +45,10 @@ public class Brave {
public static class Builder { public static class Builder {
final SpanFactory.Default.Builder spanFactoryBuilder = SpanFactory.Default.builder(); final SpanFactory.Default.Builder spanFactoryBuilder = SpanFactory.Default.builder();
private final ServerClientAndLocalSpanState state; private final ServerClientAndLocalSpanState state;
private Reporter reporter = new LoggingReporter(); private final Endpoint localEndpoint;
private boolean allowNestedLocalSpans = false; private boolean allowNestedLocalSpans = false;
private AnnotationSubmitter.Clock clock; private Clock clock;
private Recorder recorder;


/** /**
* Builder which initializes with serviceName = "unknown". * Builder which initializes with serviceName = "unknown".
Expand Down Expand Up @@ -73,6 +76,7 @@ public Builder(String serviceName) {
try { try {
int ip = toInt(getLocalHostLANAddress()); int ip = toInt(getLocalHostLANAddress());
state = new ThreadLocalServerClientAndLocalSpanState(ip, 0, serviceName); state = new ThreadLocalServerClientAndLocalSpanState(ip, 0, serviceName);
localEndpoint = state.endpoint();
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
throw new IllegalStateException("Unable to get Inet address", e); throw new IllegalStateException("Unable to get Inet address", e);
} }
Expand Down Expand Up @@ -101,6 +105,7 @@ public Builder(Endpoint endpoint) {
*/ */
public Builder(ServerClientAndLocalSpanState state) { public Builder(ServerClientAndLocalSpanState state) {
this.state = Util.checkNotNull(state, "state must be specified."); this.state = Util.checkNotNull(state, "state must be specified.");
this.localEndpoint = state.endpoint();


// the legacy span state doesn't support nested spans per (#166). Only permit nesting on the span // the legacy span state doesn't support nested spans per (#166). Only permit nesting on the span
// state that has instructions on how to use it properly // state that has instructions on how to use it properly
Expand Down Expand Up @@ -139,7 +144,8 @@ public Builder traceSampler(Sampler sampler) {
* <p>See https://github.com/openzipkin/zipkin-reporter-java * <p>See https://github.com/openzipkin/zipkin-reporter-java
*/ */
public Builder reporter(Reporter<zipkin.Span> reporter) { public Builder reporter(Reporter<zipkin.Span> reporter) {
this.reporter = checkNotNull(reporter, "reporter"); checkNotNull(reporter, "reporter");
this.recorder = new AutoValue_Recorder_Default(localEndpoint, reporter);
return this; return this;
} }


Expand All @@ -151,7 +157,7 @@ public Builder spanCollector(SpanCollector spanCollector) {
return reporter(new SpanCollectorReporterAdapter(spanCollector)); return reporter(new SpanCollectorReporterAdapter(spanCollector));
} }


public Builder clock(AnnotationSubmitter.Clock clock) { public Builder clock(Clock clock) {
this.clock = checkNotNull(clock, "clock"); this.clock = checkNotNull(clock, "clock");
return this; return this;
} }
Expand All @@ -163,6 +169,9 @@ public Builder traceId128Bit(boolean traceId128Bit) {
} }


public Brave build() { public Brave build() {
if (recorder == null) {
recorder = new AutoValue_Recorder_Default(localEndpoint, new LoggingReporter());
}
return new Brave(this); return new Brave(this);
} }


Expand Down Expand Up @@ -257,27 +266,23 @@ public AnnotationSubmitter serverSpanAnnotationSubmitter() {
} }


private Brave(Builder builder) { private Brave(Builder builder) {
SpanFactory spanFactory = builder.spanFactoryBuilder.build();
AnnotationSubmitter.Clock clock = builder.clock != null
? builder.clock
: new AnnotationSubmitter.DefaultClock();

serverSpanThreadBinder = new ServerSpanThreadBinder(builder.state); serverSpanThreadBinder = new ServerSpanThreadBinder(builder.state);
clientSpanThreadBinder = new ClientSpanThreadBinder(builder.state); clientSpanThreadBinder = new ClientSpanThreadBinder(builder.state);
localSpanThreadBinder = new LocalSpanThreadBinder(builder.state); localSpanThreadBinder = new LocalSpanThreadBinder(builder.state);


Recorder recorder = SpanFactory spanFactory = builder.spanFactoryBuilder.build();
new AutoValue_Recorder_Default(builder.state.endpoint(), builder.reporter); Clock clock = builder.clock != null ? builder.clock : new DefaultClock();

serverTracer = new AutoValue_ServerTracer( serverTracer = new AutoValue_ServerTracer(
clock, clock,
recorder, builder.recorder,
serverSpanThreadBinder, serverSpanThreadBinder,
spanFactory spanFactory
); );


clientTracer = new AutoValue_ClientTracer( clientTracer = new AutoValue_ClientTracer(
clock, clock,
recorder, builder.recorder,
localSpanThreadBinder, localSpanThreadBinder,
serverSpanThreadBinder, serverSpanThreadBinder,
clientSpanThreadBinder, clientSpanThreadBinder,
Expand All @@ -286,18 +291,23 @@ private Brave(Builder builder) {


localTracer = new AutoValue_LocalTracer.Builder() localTracer = new AutoValue_LocalTracer.Builder()
.spanFactory(spanFactory) .spanFactory(spanFactory)
.recorder(recorder) .recorder(builder.recorder)
.allowNestedLocalSpans(builder.allowNestedLocalSpans) .allowNestedLocalSpans(builder.allowNestedLocalSpans)
.currentServerSpan(serverSpanThreadBinder) .currentServerSpan(serverSpanThreadBinder)
.currentSpan(localSpanThreadBinder) .currentSpan(localSpanThreadBinder)
.clock(clock) .clock(clock)
.build(); .build();


serverSpanAnnotationSubmitter = AnnotationSubmitter.create(
serverSpanThreadBinder,
clock,
builder.recorder
);

serverRequestInterceptor = new ServerRequestInterceptor(serverTracer); serverRequestInterceptor = new ServerRequestInterceptor(serverTracer);
serverResponseInterceptor = new ServerResponseInterceptor(serverTracer); serverResponseInterceptor = new ServerResponseInterceptor(serverTracer);
clientRequestInterceptor = new ClientRequestInterceptor(clientTracer); clientRequestInterceptor = new ClientRequestInterceptor(clientTracer);
clientResponseInterceptor = new ClientResponseInterceptor(clientTracer); clientResponseInterceptor = new ClientResponseInterceptor(clientTracer);
serverSpanAnnotationSubmitter = AnnotationSubmitter.create(serverSpanThreadBinder, clock, recorder);
} }


static { static {
Expand Down

0 comments on commit 850279d

Please sign in to comment.