Skip to content

Commit

Permalink
Moves zipkin-core language level to JRE 6 (#1245)
Browse files Browse the repository at this point in the history
This is an api compatible change to allow code sharing between zipkin
server and instrumentation, and without having to publish 2 jar files.

We accidentally broke the ability to make agent-based instrumentation by
compiling our model classes against Java 7 bytecode. Particularly, this
causes a regression in Brave.

To move back to Java 6 bytecode, we had to do a few things:
* replace moshi internal dependency with gson, which is java 6 compliant
* remove okio dependency and copy its Base64 class
  * incidentally a class it also copied!
* do variable-size buffer writes w/ OutputBuffer instead of okio
* fix a few nags about diamond ops etc.
  • Loading branch information
adriancole authored Aug 18, 2016
1 parent 331e4e5 commit a17f260
Show file tree
Hide file tree
Showing 26 changed files with 655 additions and 333 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ Once you've started, browse to http://your_host:9411 to find traces!
Check out the [`zipkin-server`](/zipkin-server) documentation for configuration details, or [`docker-zipkin`](https://github.com/openzipkin/docker-zipkin) for how to use docker-compose.

## Core Library
The [core library](https://github.com/openzipkin/zipkin/tree/master/zipkin/src/main/java/io/zipkin) requires minimum language level 7. While currently only used by the server, we expect this library to be used in native instrumentation as well.
The [core library](https://github.com/openzipkin/zipkin/tree/master/zipkin/src/main/java/io/zipkin) is used by both Zipkin instrumentation and the Zipkin server. Its minimum Java language level is 6, in efforts to support those writing agent instrumentation.

This includes built-in codec for both thrift and json structs. Direct dependencies on thrift or moshi (json library) are avoided by minifying and repackaging classes used. The result is a 190k jar which won't conflict with any library you use.
This includes built-in codec for both thrift and json structs. A direct dependency on gson (json library) is avoided by minifying and repackaging classes used. The result is a 155k jar which won't conflict with any library you use.

Ex.
```java
Expand Down
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@

<brave.version>3.9.0</brave.version>
<cassandra-driver-core.version>3.1.0</cassandra-driver-core.version>
<moshi.version>1.2.0</moshi.version>
<okio.version>1.9.0</okio.version>
<jooq.version>3.8.4</jooq.version>
<spring-boot.version>1.4.0.RELEASE</spring-boot.version>
Expand Down Expand Up @@ -275,12 +274,6 @@
</dependency>
<!-- End spring-boot-dependencies overrides -->

<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>${moshi.version}</version>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
Expand Down
37 changes: 16 additions & 21 deletions zipkin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@

<properties>
<main.basedir>${project.basedir}/..</main.basedir>
<main.java.version>1.6</main.java.version>
<main.signature.artifact>java16</main.signature.artifact>
</properties>

<dependencies>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>

<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<scope>test</scope>
</dependency>

<dependency>
Expand Down Expand Up @@ -73,17 +77,13 @@
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>com.squareup.moshi:moshi</artifact>
<artifact>com.google.code.gson:gson</artifact>
<includes>
<include>com/squareup/moshi/BufferedSinkJsonWriter*</include>
<include>com/squareup/moshi/BufferedSourceJsonReader*</include>
<!-- don't get zipkin/internal/moshi/JsonAdapter$Factory.class -->
<include>com/squareup/moshi/JsonAdapter.class</include>
<include>com/squareup/moshi/JsonAdapter$?.class</include>
<include>com/squareup/moshi/JsonDataException*</include>
<include>com/squareup/moshi/JsonReader*</include>
<include>com/squareup/moshi/JsonScope*</include>
<include>com/squareup/moshi/JsonWriter*</include>
<include>com/google/gson/stream/JsonReader*.class</include>
<include>com/google/gson/stream/JsonToken.class</include>
<include>com/google/gson/stream/JsonWriter.class</include>
<include>com/google/gson/stream/MalformedJsonException.class</include>
<include>com/google/gson/internal/JsonReaderInternalAccess.class</include>
</includes>
</filter>
<filter>
Expand All @@ -96,20 +96,15 @@
</filters>
<relocations>
<relocation>
<pattern>okio</pattern>
<shadedPattern>zipkin.internal.okio</shadedPattern>
</relocation>
<relocation>
<pattern>com.squareup.moshi</pattern>
<shadedPattern>zipkin.internal.moshi</shadedPattern>
<pattern>com.google.gson</pattern>
<shadedPattern>zipkin.internal.gson</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
Expand Down
8 changes: 1 addition & 7 deletions zipkin/src/main/java/zipkin/Annotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package zipkin;

import zipkin.internal.JsonCodec;
import zipkin.internal.Nullable;

import static zipkin.internal.Util.checkNotNull;
Expand Down Expand Up @@ -100,11 +99,6 @@ public Annotation build() {
}
}

@Override
public String toString() {
return JsonCodec.ANNOTATION_ADAPTER.toJson(this);
}

@Override
public boolean equals(Object o) {
if (o == this) {
Expand Down Expand Up @@ -135,7 +129,7 @@ public int hashCode() {
@Override
public int compareTo(Annotation that) {
if (this == that) return 0;
int byTimestamp = Long.compare(timestamp, that.timestamp);
int byTimestamp = timestamp < that.timestamp ? -1 : timestamp == that.timestamp ? 0 : 1;
if (byTimestamp != 0) return byTimestamp;
return value.compareTo(that.value);
}
Expand Down
6 changes: 0 additions & 6 deletions zipkin/src/main/java/zipkin/BinaryAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package zipkin;

import java.util.Arrays;
import zipkin.internal.JsonCodec;
import zipkin.internal.Nullable;
import zipkin.internal.Util;

Expand Down Expand Up @@ -196,11 +195,6 @@ public BinaryAnnotation build() {
}
}

@Override
public String toString() {
return JsonCodec.BINARY_ANNOTATION_ADAPTER.toJson(this);
}

@Override
public boolean equals(Object o) {
if (o == this) {
Expand Down
4 changes: 2 additions & 2 deletions zipkin/src/main/java/zipkin/DependencyLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import zipkin.internal.JsonCodec;

import static zipkin.internal.Util.UTF_8;
import static zipkin.internal.Util.checkNotNull;

public final class DependencyLink implements Serializable {
Expand Down Expand Up @@ -86,7 +86,7 @@ public DependencyLink build() {

@Override
public String toString() {
return JsonCodec.DEPENDENCY_LINK_ADAPTER.toJson(this);
return new String(Codec.JSON.writeDependencyLink(this), UTF_8);
}

@Override
Expand Down
6 changes: 0 additions & 6 deletions zipkin/src/main/java/zipkin/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import java.util.Arrays;
import java.util.Locale;
import zipkin.internal.JsonCodec;
import zipkin.internal.Nullable;
import zipkin.internal.Util;

Expand Down Expand Up @@ -150,11 +149,6 @@ public Endpoint build() {
}
}

@Override
public String toString() {
return JsonCodec.ENDPOINT_ADAPTER.toJson(this);
}

@Override
public boolean equals(Object o) {
if (o == this) {
Expand Down
20 changes: 10 additions & 10 deletions zipkin/src/main/java/zipkin/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import java.util.List;
import java.util.Locale;
import java.util.Set;
import zipkin.internal.JsonCodec;
import zipkin.internal.Nullable;

import static zipkin.internal.Util.UTF_8;
import static zipkin.internal.Util.checkNotNull;
import static zipkin.internal.Util.equal;
import static zipkin.internal.Util.sortedList;
Expand Down Expand Up @@ -273,14 +273,14 @@ public Builder duration(@Nullable Long duration) {
* @see Span#annotations
*/
public Builder annotations(Collection<Annotation> annotations) {
this.annotations = new HashSet<>(annotations);
this.annotations = new HashSet<Annotation>(annotations);
return this;
}

/** @see Span#annotations */
public Builder addAnnotation(Annotation annotation) {
if (annotations == null) {
annotations = new HashSet<>();
annotations = new HashSet<Annotation>();
}
annotations.add(annotation);
return this;
Expand All @@ -292,14 +292,14 @@ public Builder addAnnotation(Annotation annotation) {
* @see Span#binaryAnnotations
*/
public Builder binaryAnnotations(Collection<BinaryAnnotation> binaryAnnotations) {
this.binaryAnnotations = new HashSet<>(binaryAnnotations);
this.binaryAnnotations = new HashSet<BinaryAnnotation>(binaryAnnotations);
return this;
}

/** @see Span#binaryAnnotations */
public Builder addBinaryAnnotation(BinaryAnnotation binaryAnnotation) {
if (binaryAnnotations == null) {
binaryAnnotations = new HashSet<>();
binaryAnnotations = new HashSet<BinaryAnnotation>();
}
binaryAnnotations.add(binaryAnnotation);
return this;
Expand All @@ -318,7 +318,7 @@ public Span build() {

@Override
public String toString() {
return JsonCodec.SPAN_ADAPTER.toJson(this);
return new String(Codec.JSON.writeSpan(this), UTF_8);
}

@Override
Expand Down Expand Up @@ -369,9 +369,9 @@ public int hashCode() {
@Override
public int compareTo(Span that) {
if (this == that) return 0;
int byTimestamp = Long.compare(
this.timestamp == null ? Long.MIN_VALUE : this.timestamp,
that.timestamp == null ? Long.MIN_VALUE : that.timestamp);
long x = this.timestamp == null ? Long.MIN_VALUE : this.timestamp;
long y = that.timestamp == null ? Long.MIN_VALUE : that.timestamp;
int byTimestamp = x < y ? -1 : x == y ? 0 : 1;
if (byTimestamp != 0) return byTimestamp;
return this.name.compareTo(that.name);
}
Expand All @@ -390,7 +390,7 @@ public String idString() {

/** Returns the distinct {@link Endpoint#serviceName service names} that logged to this span. */
public Set<String> serviceNames() {
Set<String> result = new HashSet<>();
Set<String> result = new HashSet<String>();
for (Annotation a : annotations) {
if (a.endpoint == null) continue;
if (a.endpoint.serviceName.isEmpty()) continue;
Expand Down
4 changes: 2 additions & 2 deletions zipkin/src/main/java/zipkin/collector/Collector.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void acceptSpans(byte[] serializedSpans, Codec codec, Callback<Void> call
}

public void acceptSpans(List<byte[]> serializedSpans, Codec codec, Callback<Void> callback) {
List<Span> spans = new ArrayList<>(serializedSpans.size());
List<Span> spans = new ArrayList<Span>(serializedSpans.size());
try {
int bytesRead = 0;
for (byte[] serializedSpan : serializedSpans) {
Expand Down Expand Up @@ -137,7 +137,7 @@ public void accept(List<Span> spans, Callback<Void> callback) {
}

List<Span> sample(List<Span> input) {
List<Span> sampled = new ArrayList<>(input.size());
List<Span> sampled = new ArrayList<Span>(input.size());
for (Span s : input) {
if (sampler.isSampled(s)) sampled.add(s);
}
Expand Down
Loading

0 comments on commit a17f260

Please sign in to comment.