Skip to content

Commit

Permalink
Removes bytecode so we can add more
Browse files Browse the repository at this point in the history
This removes the auto-value dependency and related for inlining the few
cases where it was used. It also trims the class hierarchy of sampling
flags. Finally, it moves out the in-flight localEndpoint as it is
constant.

Besides fixing a few bugs, these changes reduced the size of the jar by
12KiB and scraps 2 dependencies.

```bash
du -k /Users/acole/.m2/repository/io/zipkin/brave/brave/5.1.2/brave-5.1.2.jar /Users/acole/.m2/repository/io/zipkin/brave/brave/5.1.3-SNAPSHOT/brave-5.1.3-SNAPSHOT.jar
116	/Users/acole/.m2/repository/io/zipkin/brave/brave/5.1.2/brave-5.1.2.jar
128	/Users/acole/.m2/repository/io/zipkin/brave/brave/5.1.3-SNAPSHOT/brave-5.1.3-SNAPSHOT.jar
```
  • Loading branch information
Adrian Cole committed Jul 21, 2018
1 parent af055a6 commit fed9d28
Show file tree
Hide file tree
Showing 25 changed files with 427 additions and 317 deletions.
12 changes: 0 additions & 12 deletions brave/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@
<!-- annotations are not runtime retention, so don't need a runtime dep -->
<scope>provided</scope>
</dependency>
<!-- for value types... don't worry. this dependency is compile only! -->
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<scope>provided</scope>
</dependency>
<!-- generated code includes Generated annotations! -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- to mock Platform calls -->
<dependency>
<groupId>org.powermock</groupId>
Expand Down
16 changes: 16 additions & 0 deletions brave/src/main/java/brave/NoopScopedSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,20 @@ final class NoopScopedSpan extends ScopedSpan {
@Override public String toString() {
return "NoopScopedSpan(" + context + ")";
}

@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof NoopScopedSpan)) return false;
NoopScopedSpan that = (NoopScopedSpan) o;
return context.equals(that.context) && scope.equals(that.scope);
}

@Override public int hashCode() {
int h = 1;
h *= 1000003;
h ^= context.hashCode();
h *= 1000003;
h ^= scope.hashCode();
return h;
}
}
28 changes: 23 additions & 5 deletions brave/src/main/java/brave/NoopSpan.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package brave;

import brave.propagation.TraceContext;
import com.google.auto.value.AutoValue;
import zipkin2.Endpoint;

@AutoValue
abstract class NoopSpan extends Span {
final class NoopSpan extends Span {

static NoopSpan create(TraceContext context) {
return new AutoValue_NoopSpan(context);
final TraceContext context;

NoopSpan(TraceContext context) {
this.context = context;
}

@Override public SpanCustomizer customizer() {
Expand All @@ -19,6 +19,10 @@ static NoopSpan create(TraceContext context) {
return true;
}

@Override public TraceContext context() {
return context;
}

@Override public Span start() {
return this;
}
Expand Down Expand Up @@ -66,4 +70,18 @@ static NoopSpan create(TraceContext context) {

@Override public void flush() {
}

@Override public String toString() {
return "NoopSpan(" + context + ")";
}

@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof NoopSpan)) return false;
return context.equals(((NoopSpan) o).context);
}

@Override public int hashCode() {
return context.hashCode();
}
}
16 changes: 14 additions & 2 deletions brave/src/main/java/brave/RealScopedSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,19 @@ final class RealScopedSpan extends ScopedSpan {
recorder.finish(context);
}

@Override public String toString() {
return "RealScopedSpan(" + context + ")";
@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof RealScopedSpan)) return false;
RealScopedSpan that = (RealScopedSpan) o;
return context.equals(that.context) && scope.equals(that.scope);
}

@Override public int hashCode() {
int h = 1;
h *= 1000003;
h ^= context.hashCode();
h *= 1000003;
h ^= scope.hashCode();
return h;
}
}
67 changes: 43 additions & 24 deletions brave/src/main/java/brave/RealSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,107 @@

import brave.internal.recorder.Recorder;
import brave.propagation.TraceContext;
import com.google.auto.value.AutoValue;
import zipkin2.Endpoint;

/** This wraps the public api and guards access to a mutable span. */
@AutoValue
abstract class RealSpan extends Span {
final class RealSpan extends Span {

abstract Recorder recorder();
abstract ErrorParser errorParser();
final TraceContext context;
final Recorder recorder;
final ErrorParser errorParser;
final RealSpanCustomizer customizer;

static RealSpan create(TraceContext context, Recorder recorder, ErrorParser errorParser) {
return new AutoValue_RealSpan(context, RealSpanCustomizer.create(context, recorder), recorder,
errorParser);
RealSpan(TraceContext context, Recorder recorder, ErrorParser errorParser) {
this.context = context;
this.recorder = recorder;
this.customizer = new RealSpanCustomizer(context, recorder);
this.errorParser = errorParser;
}

@Override public boolean isNoop() {
return false;
}

@Override public TraceContext context() {
return context;
}

@Override public SpanCustomizer customizer() {
return new RealSpanCustomizer(context, recorder);
}

@Override public Span start() {
recorder().start(context());
recorder.start(context());
return this;
}

@Override public Span start(long timestamp) {
recorder().start(context(), timestamp);
recorder.start(context(), timestamp);
return this;
}

@Override public Span name(String name) {
recorder().name(context(), name);
recorder.name(context(), name);
return this;
}

@Override public Span kind(Kind kind) {
recorder().kind(context(), kind);
recorder.kind(context(), kind);
return this;
}

@Override public Span annotate(String value) {
recorder().annotate(context(), value);
recorder.annotate(context(), value);
return this;
}

@Override public Span annotate(long timestamp, String value) {
recorder().annotate(context(), timestamp, value);
recorder.annotate(context(), timestamp, value);
return this;
}

@Override public Span tag(String key, String value) {
recorder().tag(context(), key, value);
recorder.tag(context(), key, value);
return this;
}

@Override public Span error(Throwable throwable) {
errorParser().error(throwable, customizer());
errorParser.error(throwable, customizer());
return this;
}

@Override public Span remoteEndpoint(Endpoint remoteEndpoint) {
recorder().remoteEndpoint(context(), remoteEndpoint);
recorder.remoteEndpoint(context(), remoteEndpoint);
return this;
}

@Override public void finish() {
recorder().finish(context());
recorder.finish(context());
}

@Override public void finish(long timestamp) {
recorder().finish(context(), timestamp);
recorder.finish(context(), timestamp);
}

@Override public void abandon() {
recorder().abandon(context());
recorder.abandon(context());
}

@Override public void flush() {
recorder().flush(context());
recorder.flush(context());
}

@Override public String toString() {
return "RealSpan(" + context + ")";
}

@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof RealSpan)) return false;
return context.equals(((RealSpan) o).context);
}

@Override
public String toString() {
return "RealSpan(" + context() + ")";
@Override public int hashCode() {
return context.hashCode();
}
}
21 changes: 10 additions & 11 deletions brave/src/main/java/brave/RealSpanCustomizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,35 @@

import brave.internal.recorder.Recorder;
import brave.propagation.TraceContext;
import com.google.auto.value.AutoValue;

/** This wraps the public api and guards access to a mutable span. */
@AutoValue
abstract class RealSpanCustomizer implements SpanCustomizer {
final class RealSpanCustomizer implements SpanCustomizer {

abstract TraceContext context();
abstract Recorder recorder();
final TraceContext context;
final Recorder recorder;

static RealSpanCustomizer create(TraceContext context, Recorder recorder) {
return new AutoValue_RealSpanCustomizer(context, recorder);
RealSpanCustomizer(TraceContext context, Recorder recorder) {
this.context = context;
this.recorder = recorder;
}

@Override public SpanCustomizer name(String name) {
recorder().name(context(), name);
recorder.name(context, name);
return this;
}

@Override public SpanCustomizer annotate(String value) {
recorder().annotate(context(), value);
recorder.annotate(context, value);
return this;
}

@Override public SpanCustomizer tag(String key, String value) {
recorder().tag(context(), key, value);
recorder.tag(context, key, value);
return this;
}

@Override
public String toString() {
return "RealSpanCustomizer(" + context() + ")";
return "RealSpanCustomizer(" + context + ")";
}
}
28 changes: 13 additions & 15 deletions brave/src/main/java/brave/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,8 @@ public Span nextSpan(TraceContextOrSamplingFlags extracted) {
public Span toSpan(TraceContext context) {
if (context == null) throw new NullPointerException("context == null");
TraceContext decorated = propagationFactory.decorate(context);
if (!noop.get() && Boolean.TRUE.equals(decorated.sampled())) {
return RealSpan.create(decorated, recorder, errorParser);
}
return NoopSpan.create(decorated);
if (isNoop(context)) return new NoopSpan(decorated);
return new RealSpan(decorated, recorder, errorParser);
}

/**
Expand Down Expand Up @@ -308,9 +306,9 @@ public SpanInScope withSpanInScope(@Nullable Span span) {
* as a method-local variable vs repeated calls.
*/
public SpanCustomizer currentSpanCustomizer() {
TraceContext currentContext = currentTraceContext.get();
return currentContext != null && Boolean.TRUE.equals(currentContext.sampled())
? RealSpanCustomizer.create(currentContext, recorder) : NoopSpanCustomizer.INSTANCE;
TraceContext context = currentTraceContext.get();
if (context == null || isNoop(context)) return NoopSpanCustomizer.INSTANCE;
return new RealSpanCustomizer(context, recorder);
}

/**
Expand Down Expand Up @@ -367,14 +365,10 @@ public ScopedSpan startScopedSpanWithParent(String name, @Nullable TraceContext
if (name == null) throw new NullPointerException("name == null");
TraceContext context = propagationFactory.decorate(newContextBuilder(parent, sampler).build());
CurrentTraceContext.Scope scope = currentTraceContext.newScope(context);
ScopedSpan result;
if (!noop.get() && Boolean.TRUE.equals(context.sampled())) {
result = new RealScopedSpan(context, scope, recorder, errorParser);
recorder.name(context, name);
recorder.start(context);
} else {
result = new NoopScopedSpan(context, scope);
}
if (isNoop(context)) return new NoopScopedSpan(context, scope);
ScopedSpan result = new RealScopedSpan(context, scope, recorder, errorParser);
recorder.name(context, name);
recorder.start(context);
return result;
}

Expand Down Expand Up @@ -425,6 +419,10 @@ TraceContext.Builder newContextBuilder(@Nullable TraceContext parent, Sampler sa
.spanId(nextId);
}

boolean isNoop(TraceContext context) {
return noop.get() || !Boolean.TRUE.equals(context.sampled());
}

TraceContext.Builder newContextBuilder(TraceContext parent, SamplingFlags samplingFlags) {
return newContextBuilder(parent, samplerOverride(samplingFlags)).debug(samplingFlags.debug());
}
Expand Down
Loading

0 comments on commit fed9d28

Please sign in to comment.