Skip to content

Commit

Permalink
Makes traceIdHigh convertable to AWS X-Ray (openzipkin#509)
Browse files Browse the repository at this point in the history
This customizes the trace ID generator to make the high bits convertable
to Amazon X-Ray trace ID format v1.
  • Loading branch information
adriancole committed Oct 5, 2017
1 parent 6437e78 commit b4b0b7f
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static Builder builder() {
long newSpanId = randomGenerator().nextLong();
if (maybeParent == null) { // new trace
return Brave.toSpan(SpanId.builder()
.traceIdHigh(traceId128Bit() ? randomGenerator().nextLong() : 0L)
.traceIdHigh(traceId128Bit() ? nextTraceIdHigh(randomGenerator()) : 0L)
.traceId(newSpanId)
.spanId(newSpanId)
.sampled(sampler().isSampled(newSpanId))
Expand Down Expand Up @@ -78,5 +78,12 @@ static Builder builder() {
}
}
}

static long nextTraceIdHigh(Random prng) {
long epochSeconds = System.currentTimeMillis() / 1000;
int random = prng.nextInt();
return (epochSeconds & 0xffffffffL) << 32
| (random & 0xffffffffL);
}
}

2 changes: 1 addition & 1 deletion brave/src/main/java/brave/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ TraceContext nextContext(@Nullable TraceContext parent, SamplingFlags samplingFl
return TraceContext.newBuilder()
.sampled(sampled)
.debug(samplingFlags.debug())
.traceIdHigh(traceId128Bit ? Platform.get().randomLong() : 0L).traceId(nextId)
.traceIdHigh(traceId128Bit ? Platform.get().nextTraceIdHigh() : 0L).traceId(nextId)
.spanId(nextId).build();
}

Expand Down
26 changes: 26 additions & 0 deletions brave/src/main/java/brave/internal/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import brave.Clock;
import brave.Tracer;
import brave.Tracing;
import com.google.auto.value.AutoValue;
import java.net.InetAddress;
import java.net.NetworkInterface;
Expand Down Expand Up @@ -112,6 +113,15 @@ static Platform findPlatform() {
*/
public abstract long randomLong();

/**
* Returns the high 8-bytes for use in {@link Tracing.Builder#traceId128Bit 128-bit trace IDs}.
*
* <p>The upper 4-bytes are epoch seconds and the lower 4-bytes are random. This makes it
* convertible to <a href="http://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-request-tracing.html"></a>Amazon
* X-Ray trace ID format v1</a>.
*/
public abstract long nextTraceIdHigh();

/** gets a timestamp based on duration since the create tick. */
@Override
public long currentTimeMicroseconds() {
Expand All @@ -136,6 +146,18 @@ static Jre7 buildIfSupported(boolean zipkinV1Present) {
@Override public long randomLong() {
return java.util.concurrent.ThreadLocalRandom.current().nextLong();
}

@IgnoreJRERequirement
@Override public long nextTraceIdHigh() {
return nextTraceIdHigh(java.util.concurrent.ThreadLocalRandom.current());
}
}

static long nextTraceIdHigh(Random prng) {
long epochSeconds = System.currentTimeMillis() / 1000;
int random = prng.nextInt();
return (epochSeconds & 0xffffffffL) << 32
| (random & 0xffffffffL);
}

@AutoValue
Expand All @@ -149,5 +171,9 @@ static Jre6 build(boolean zipkinV1Present) {
@Override public long randomLong() {
return prng().nextLong();
}

@Override public long nextTraceIdHigh() {
return nextTraceIdHigh(prng());
}
}
}
32 changes: 30 additions & 2 deletions brave/src/test/java/brave/internal/PlatformTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.Callable;
Expand All @@ -16,13 +17,13 @@
import javax.annotation.Nullable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import zipkin2.Endpoint;

import static org.assertj.core.api.Assertions.assertThat;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

Expand All @@ -47,13 +48,40 @@ public class PlatformTest {
@Override public long randomLong() {
return 1L;
}

@Override public long nextTraceIdHigh() {
return 1L;
}
};

when(System.nanoTime()).thenReturn(1000L); // 1 microsecond

assertThat(platform.currentTimeMicroseconds()).isEqualTo(1);
}

// example from X-Amzn-Trace-Id: Root=1-5759e988-bd862e3fe1be46a994272793;Sampled=1
@Test public void randomLong_epochSecondsPlusRandom() {
mockStatic(System.class);
when(System.currentTimeMillis())
.thenReturn(1465510280_000L); // Thursday, June 9, 2016 10:11:20 PM

long traceIdHigh = Platform.Jre7.buildIfSupported(true).nextTraceIdHigh();

assertThat(HexCodec.toLowerHex(traceIdHigh)).startsWith("5759e988");
}

@Test public void randomLong_whenRandomIsMostNegative() {
mockStatic(System.class);
when(System.currentTimeMillis()).thenReturn(1465510280_000L);
Random prng = mock(Random.class);
when(prng.nextInt()).thenReturn(0xffffffff);

long traceIdHigh = Platform.nextTraceIdHigh(prng);

assertThat(HexCodec.toLowerHex(traceIdHigh))
.isEqualTo("5759e988ffffffff");
}

@Test public void zipkinV1Absent() throws ClassNotFoundException {
mockStatic(Class.class);
when(Class.forName(zipkin.Endpoint.class.getName()))
Expand Down Expand Up @@ -167,7 +195,7 @@ static void nicWithAddress(@Nullable InetAddress address) throws SocketException
mockStatic(NetworkInterface.class);
Vector<InetAddress> addresses = new Vector<>();
if (address != null) addresses.add(address);
NetworkInterface nic = PowerMockito.mock(NetworkInterface.class);
NetworkInterface nic = mock(NetworkInterface.class);
Vector<NetworkInterface> nics = new Vector<>();
nics.add(nic);
when(NetworkInterface.getNetworkInterfaces()).thenReturn(nics.elements());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,22 @@ public Traced() {
}
}

public static class Traced128 extends ForwardingTracingFilter {
public Traced128() {
super(Tracing.newBuilder().traceId128Bit(true).spanReporter(Reporter.NOOP).build());
}
}

@Override protected void init(DeploymentInfo servletBuilder) {
servletBuilder.addFilter(new FilterInfo("Unsampled", Unsampled.class))
.addFilterUrlMapping("Unsampled", "/unsampled", REQUEST)
.addFilterUrlMapping("Unsampled", "/unsampled/api", REQUEST)
.addFilter(new FilterInfo("Traced", Traced.class))
.addFilterUrlMapping("Traced", "/traced", REQUEST)
.addFilterUrlMapping("Traced", "/traced/api", REQUEST)
.addFilter(new FilterInfo("Traced128", Traced128.class))
.addFilterUrlMapping("Traced128", "/traced128", REQUEST)
.addFilterUrlMapping("Traced128", "/traced128/api", REQUEST)
.addServlets(Servlets.servlet("HelloServlet", HelloServlet.class).addMapping("/*"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,20 @@ protected int initServer() throws ServletException {

@Benchmark public void tracedServer_get_resumeTrace() throws Exception {
client.newCall(new Request.Builder().url(baseUrl() + "/traced")
.header("X-B3-TraceId", "7180c278b62e8f6a216a2aea45d08fc9")
.header("X-B3-TraceId", "216a2aea45d08fc9")
.header("X-B3-SpanId", "5b4185666d50f68b")
.header("X-B3-Sampled", "1")
.build())
.execute().body().close();
}

@Benchmark public void traced128Server_get() throws Exception {
get("/traced128");
}

@Benchmark public void traced128Server_get_resumeTrace() throws Exception {
client.newCall(new Request.Builder().url(baseUrl() + "/traced128")
.header("X-B3-TraceId", "5759e988b62e8f6a216a2aea45d08fc9")
.header("X-B3-SpanId", "5b4185666d50f68b")
.header("X-B3-Sampled", "1")
.build())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Copyright 2015-2016 The OpenZipkin Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package brave.internal;

import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Group;
import org.openjdk.jmh.annotations.GroupThreads;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

@Measurement(iterations = 5, time = 1)
@Warmup(iterations = 10, time = 1)
@Fork(3)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class PlatformBenchmarks {
static final Platform jre6 = Platform.Jre6.build(false);
static final Platform jre7 = Platform.Jre7.buildIfSupported(false);

@Benchmark @Group("no_contention") @GroupThreads(1)
public long no_contention_nextTraceIdHigh_jre6() {
return jre6.nextTraceIdHigh();
}

@Benchmark @Group("mild_contention") @GroupThreads(2)
public long mild_contention_nextTraceIdHigh_jre6() {
return jre6.nextTraceIdHigh();
}

@Benchmark @Group("high_contention") @GroupThreads(8)
public long high_contention_nextTraceIdHigh_jre6() {
return jre6.nextTraceIdHigh();
}

@Benchmark @Group("no_contention") @GroupThreads(1)
public long no_contention_randomLong_jre6() {
return jre6.randomLong();
}

@Benchmark @Group("mild_contention") @GroupThreads(2)
public long mild_contention_randomLong_jre6() {
return jre6.randomLong();
}

@Benchmark @Group("high_contention") @GroupThreads(8)
public long high_contention_randomLong_jre6() {
return jre6.randomLong();
}

@Benchmark @Group("no_contention") @GroupThreads(1)
public long no_contention_nextTraceIdHigh_jre7() {
return jre7.nextTraceIdHigh();
}

@Benchmark @Group("mild_contention") @GroupThreads(2)
public long mild_contention_nextTraceIdHigh_jre7() {
return jre7.nextTraceIdHigh();
}

@Benchmark @Group("high_contention") @GroupThreads(8)
public long high_contention_nextTraceIdHigh_jre7() {
return jre7.nextTraceIdHigh();
}

@Benchmark @Group("no_contention") @GroupThreads(1)
public long no_contention_randomLong_jre7() {
return jre7.randomLong();
}

@Benchmark @Group("mild_contention") @GroupThreads(2)
public long mild_contention_randomLong_jre7() {
return jre7.randomLong();
}

@Benchmark @Group("high_contention") @GroupThreads(8)
public long high_contention_randomLong_jre7() {
return jre7.randomLong();
}

// Convenience main entry-point
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(".*" + PlatformBenchmarks.class.getSimpleName() + ".*")
.build();

new Runner(opt).run();
}
}

0 comments on commit b4b0b7f

Please sign in to comment.