Skip to content

Commit

Permalink
removes IpLiteral dependency from ZipkinV2JsonWriter (#1414)
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
codefromthecrypt authored Feb 13, 2024
1 parent 710e7c3 commit 665e3e8
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2023 The OpenZipkin Authors
* Copyright 2013-2024 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
Expand All @@ -14,13 +14,19 @@
package brave.propagation;

import brave.Tracing;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static brave.test.util.ClassLoaders.assertRunIsUnloadable;
import static org.junit.jupiter.api.Assertions.assertThrows;

class ThreadLocalSpanClassLoaderTest {

@BeforeEach void ensureNothingCurrent(){
Tracing current = Tracing.current();
if (current != null) current.close();
}

@Test void noop_unloadable() {
assertRunIsUnloadable(CurrentTracerUnassigned.class, getClass().getClassLoader());
}
Expand All @@ -32,13 +38,13 @@ static class CurrentTracerUnassigned implements Runnable {
}

@Test void currentTracer_basicUsage_unloadable() {
assertRunIsUnloadable(ExplicitTracerBasicUsage.class, getClass().getClassLoader());
assertRunIsUnloadable(CurrentTracerBasicUsage.class, getClass().getClassLoader());
}

static class ExplicitTracerBasicUsage implements Runnable {
static class CurrentTracerBasicUsage implements Runnable {
@Override public void run() {
try (Tracing tracing = Tracing.newBuilder().build()) {
ThreadLocalSpan tlSpan = ThreadLocalSpan.create(tracing.tracer());
ThreadLocalSpan tlSpan = ThreadLocalSpan.CURRENT_TRACER;

tlSpan.next();
tlSpan.remove().finish();
Expand All @@ -47,13 +53,13 @@ static class ExplicitTracerBasicUsage implements Runnable {
}

@Test void explicitTracer_basicUsage_unloadable() {
assertRunIsUnloadable(CurrentTracerBasicUsage.class, getClass().getClassLoader());
assertRunIsUnloadable(ExplicitTracerBasicUsage.class, getClass().getClassLoader());
}

static class CurrentTracerBasicUsage implements Runnable {
static class ExplicitTracerBasicUsage implements Runnable {
@Override public void run() {
try (Tracing tracing = Tracing.newBuilder().build()) {
ThreadLocalSpan tlSpan = ThreadLocalSpan.CURRENT_TRACER;
ThreadLocalSpan tlSpan = ThreadLocalSpan.create(tracing.tracer());

tlSpan.next();
tlSpan.remove().finish();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 The OpenZipkin Authors
* Copyright 2013-2024 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
Expand Down Expand Up @@ -263,7 +263,8 @@ static void writeEndpoint(WriteBuffer b,
}
if (ip != null) {
if (wroteField) b.writeByte(',');
if (IpLiteral.detectFamily(ip) == IpLiteral.IpFamily.IPv4) {
// MutableSpan unwraps any Ipv4 from a mapped or compatability mode IPv6.
if (ip.indexOf('.') != -1) {
b.writeAscii("\"ipv4\":\"");
} else {
b.writeAscii("\"ipv6\":\"");
Expand Down
9 changes: 9 additions & 0 deletions brave/src/test/java/brave/handler/MutableSpanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,15 @@ class Tag {
.isEqualTo(new MutableSpan(context, null));
}

@Test void unwrapsIpv4() {
MutableSpan span = new MutableSpan();
span.localIp("::FFFF:43.0.192.2"); // mapped
span.remoteIp("::0000:43.0.192.2"); // compat

assertThat(span.localIp()).isEqualTo("43.0.192.2");
assertThat(span.remoteIp()).isEqualTo("43.0.192.2");
}

@Test void tags() {
MutableSpan span = new MutableSpan();
assertThat(span.tagCount()).isZero();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2013-2024 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.codec;

import brave.Tags;
import brave.handler.MutableSpan;
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.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
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;

import static brave.handler.MutableSpanBenchmarks.newBigClientMutableSpan;
import static brave.handler.MutableSpanBenchmarks.newServerMutableSpan;

@Measurement(iterations = 5, time = 1)
@Warmup(iterations = 10, time = 1)
@Fork(3)
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Thread)
@Threads(1)
public class ZipkinV2JsonWriterBenchmarks {

static final ZipkinV2JsonWriter writer = new ZipkinV2JsonWriter(Tags.ERROR);
static final MutableSpan serverSpan = newServerMutableSpan();
static final MutableSpan bigClientSpan = newBigClientMutableSpan();
static final byte[] buffer = new byte[1024];

@Benchmark public int sizeInBytes_serverSpan() {
return writer.sizeInBytes(serverSpan);
}

@Benchmark public void write_serverSpan() {
writer.write(serverSpan, new WriteBuffer(buffer, 0));
}

@Benchmark public int sizeInBytes_bigClientSpan() {
return writer.sizeInBytes(bigClientSpan);
}

@Benchmark public void write_bigClientSpan() {
writer.write(bigClientSpan, new WriteBuffer(buffer, 0));
}

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

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

0 comments on commit 665e3e8

Please sign in to comment.