Skip to content

Commit

Permalink
code cleanups, mostly to reduce imports
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
Adrian Cole committed Feb 8, 2024
1 parent 6b9ea28 commit 35bfac3
Show file tree
Hide file tree
Showing 118 changed files with 554 additions and 633 deletions.
4 changes: 2 additions & 2 deletions benchmarks/src/main/java/zipkin2/codec/MoshiSpanDecoder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2019 The OpenZipkin Authors
* Copyright 2015-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 @@ -64,7 +64,7 @@ public List<Span> decodeList(ByteBuffer spans) {
}
}

final class ByteBufferSource implements okio.Source {
static final class ByteBufferSource implements okio.Source {
final ByteBuffer source;

final Buffer.UnsafeCursor cursor = new Buffer.UnsafeCursor();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2019 The OpenZipkin Authors
* Copyright 2015-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,7 +14,6 @@
package zipkin2.internal;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
Expand All @@ -31,6 +30,8 @@
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import static java.nio.charset.StandardCharsets.UTF_8;

@Measurement(iterations = 5, time = 1)
@Warmup(iterations = 10, time = 1)
@Fork(3)
Expand All @@ -39,7 +40,6 @@
@State(Scope.Thread)
@Threads(1)
public class WriteBufferBenchmarks {
static final Charset UTF_8 = Charset.forName("UTF-8");
// Order id = d07c4daa-0fa9-4c03-90b1-e06c4edae250 doesn't exist
static final String CHINESE_UTF8 = "订单d07c4daa-0fa9-4c03-90b1-e06c4edae250不存在";
static final int CHINESE_UTF8_SIZE = UTF_8.encode(CHINESE_UTF8).remaining();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2023 The OpenZipkin Authors
* Copyright 2015-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 @@ -15,7 +15,6 @@

import com.squareup.wire.ProtoWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import okio.ByteString;
import org.assertj.core.data.MapEntry;
Expand All @@ -28,7 +27,6 @@
import zipkin2.proto3.ListOfSpans;
import zipkin2.proto3.Span;

import static java.util.Collections.singletonMap;
import static okio.ByteString.decodeHex;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.MapEntry.entry;
Expand Down Expand Up @@ -68,7 +66,7 @@ public class Proto3CodecInteropTest {
.putTag("error", "此用户没有操作权限")
.shared(true)
.build();
static final List<zipkin2.Span> ZIPKIN_SPANS = Arrays.asList(ZIPKIN_SPAN, ZIPKIN_SPAN);
static final List<zipkin2.Span> ZIPKIN_SPANS = List.of(ZIPKIN_SPAN, ZIPKIN_SPAN);

static final Span PROTO_SPAN = new Span.Builder()
.trace_id(decodeHex(ZIPKIN_SPAN.traceId()))
Expand All @@ -87,15 +85,15 @@ public class Proto3CodecInteropTest {
.ipv4(ByteString.of(PROFILE.ipv4Bytes()))
.port(PROFILE.portAsInt()).build()
)
.annotations(Arrays.asList(new Annotation.Builder()
.annotations(List.of(new Annotation.Builder()
.timestamp(ZIPKIN_SPAN.annotations().get(0).timestamp())
.value(ZIPKIN_SPAN.annotations().get(0).value())
.build()))
.tags(ZIPKIN_SPAN.tags())
.shared(true)
.build();
ListOfSpans PROTO_SPANS = new ListOfSpans.Builder()
.spans(Arrays.asList(PROTO_SPAN, PROTO_SPAN)).build();
.spans(List.of(PROTO_SPAN, PROTO_SPAN)).build();

@Test void encodeIsCompatible() throws IOException {
okio.Buffer buffer = new okio.Buffer();
Expand Down Expand Up @@ -204,7 +202,7 @@ public class Proto3CodecInteropTest {

@Test void tag_sizeInBytes_matchesWire() {
MapEntry<String, String> entry = entry("clnt/finagle.version", "6.45.0");
Span wireSpan = new Span.Builder().tags(singletonMap(entry.key, entry.value)).build();
Span wireSpan = new Span.Builder().tags(Map.of(entry.key, entry.value)).build();

assertThat(new TagField(TAG_KEY).sizeInBytes(entry))
.isEqualTo(Span.ADAPTER.encodedSize(wireSpan));
Expand All @@ -216,7 +214,7 @@ public class Proto3CodecInteropTest {
byte[] zipkinBytes = new byte[field.sizeInBytes(entry)];
field.write(WriteBuffer.wrap(zipkinBytes, 0), entry);

Span oneField = new Span.Builder().tags(singletonMap(entry.key, entry.value)).build();
Span oneField = new Span.Builder().tags(Map.of(entry.key, entry.value)).build();
assertThat(zipkinBytes)
.containsExactly(oneField.encode());
}
Expand All @@ -227,7 +225,7 @@ public class Proto3CodecInteropTest {
byte[] zipkinBytes = new byte[field.sizeInBytes(entry)];
field.write(WriteBuffer.wrap(zipkinBytes, 0), entry);

Span oneField = new Span.Builder().tags(singletonMap(entry.key, entry.value)).build();
Span oneField = new Span.Builder().tags(Map.of(entry.key, entry.value)).build();
assertThat(zipkinBytes)
.containsExactly(oneField.encode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,21 @@ void runBenchmark(@Nullable GenericContainer<?> storage, GenericContainer<?> zip
WebClient prometheusClient = WebClient.of(
"h1c://" + prometheus.getContainerIpAddress() + ":" + prometheus.getFirstMappedPort());

System.out.println("Messages received: %s".formatted(prometheusValue(
prometheusClient, "sum(zipkin_collector_messages_total)")));
System.out.println("Spans received: %s".formatted(prometheusValue(
prometheusClient, "sum(zipkin_collector_spans_total)")));
System.out.println("Spans dropped: %s".formatted(prometheusValue(
prometheusClient, "sum(zipkin_collector_spans_dropped_total)")));
System.out.printf("Messages received: %s%n", prometheusValue(
prometheusClient, "sum(zipkin_collector_messages_total)"));
System.out.printf("Spans received: %s%n", prometheusValue(
prometheusClient, "sum(zipkin_collector_spans_total)"));
System.out.printf("Spans dropped: %s%n", prometheusValue(
prometheusClient, "sum(zipkin_collector_spans_dropped_total)"));

System.out.println("Memory quantiles:");
printQuartiles(prometheusClient, "jvm_memory_used_bytes{area=\"heap\"}");
printQuartiles(prometheusClient, "jvm_memory_used_bytes{area=\"nonheap\"}");

System.out.println("Total GC time (s): %s".formatted(
prometheusValue(prometheusClient, "sum(jvm_gc_pause_seconds_sum)")));
System.out.println("Number of GCs: %s".formatted(
prometheusValue(prometheusClient, "sum(jvm_gc_pause_seconds_count)")));
System.out.printf(
"Total GC time (s): %s%n", prometheusValue(prometheusClient, "sum(jvm_gc_pause_seconds_sum)"));
System.out.printf(
"Number of GCs: %s%n", prometheusValue(prometheusClient, "sum(jvm_gc_pause_seconds_count)"));

System.out.println("POST Spans latency (s)");
printHistogram(prometheusClient, """
Expand Down Expand Up @@ -331,27 +331,26 @@ GenericContainer<?> createZipkinContainer(@Nullable GenericContainer<?> storage)
}

static void printContainerMapping(GenericContainer<?> container) {
System.out.println(
"Container %s ports exposed at %s".formatted(
container.getDockerImageName(),
container.getExposedPorts().stream()
.map(port -> new SimpleImmutableEntry<>(port,
"http://" + container.getContainerIpAddress() + ":" + container.getMappedPort(port)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))));
System.out.printf(
"Container %s ports exposed at %s%n", container.getDockerImageName(),
container.getExposedPorts().stream()
.map(port -> new SimpleImmutableEntry<>(port,
"http://" + container.getContainerIpAddress() + ":" + container.getMappedPort(port)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
}

static void printQuartiles(WebClient prometheus, String metric) throws Exception {
for (double quantile : Arrays.asList(0.0, 0.25, 0.5, 0.75, 1.0)) {
String value = prometheusValue(prometheus, "quantile(" + quantile + ", " + metric + ")");
System.out.println("%s[%s] = %s".formatted(metric, quantile, value));
System.out.printf("%s[%s] = %s%n", metric, quantile, value);
}
}

static void printHistogram(WebClient prometheus, String metric) throws Exception {
for (double quantile : Arrays.asList(0.5, 0.9, 0.99)) {
String value =
prometheusValue(prometheus, "histogram_quantile(" + quantile + ", " + metric + ")");
System.out.println("%s[%s] = %s".formatted(metric, quantile, value));
System.out.printf("%s[%s] = %s%n", metric, quantile, value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.opentest4j.TestAbortedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -61,8 +60,8 @@
@Timeout(60)
@Tag("docker")
class ITActiveMQCollector {
@RegisterExtension ActiveMQExtension activemq = new ActiveMQExtension();
List<Span> spans = Arrays.asList(LOTS_OF_SPANS[0], LOTS_OF_SPANS[1]);
@RegisterExtension static ActiveMQExtension activemq = new ActiveMQExtension();
List<Span> spans = List.of(LOTS_OF_SPANS[0], LOTS_OF_SPANS[1]);

public String testName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package zipkin2.collector;

import java.util.List;
import java.util.concurrent.RejectedExecutionException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -28,9 +29,7 @@
import zipkin2.storage.InMemoryStorage;
import zipkin2.storage.StorageComponent;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
Expand Down Expand Up @@ -154,7 +153,7 @@ public class CollectorTest {
Span span2 = CLIENT_SPAN.toBuilder().id("3").build();
when(collector.idString(span2)).thenReturn("3");

assertThat(collector.new StoreSpans(asList(CLIENT_SPAN, span2)))
assertThat(collector.new StoreSpans(List.of(CLIENT_SPAN, span2)))
.hasToString("StoreSpans([1, 3])");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2020 The OpenZipkin Authors
* Copyright 2015-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 @@ -266,9 +266,10 @@ Runnable guardFailures(final Runnable delegate) {
} catch (InterruptException e) {
// Interrupts are normal on shutdown, intentionally swallow
} catch (KafkaException e) {
if (e.getCause() instanceof ConfigException) e = (KafkaException) e.getCause();
LOG.error("Kafka worker exited with exception", e);
failure.set(CheckResult.failed(e));
KafkaException kafkaException = e;
if (kafkaException.getCause() instanceof ConfigException) kafkaException = (KafkaException) kafkaException.getCause();
LOG.error("Kafka worker exited with exception", kafkaException);
failure.set(CheckResult.failed(kafkaException));
} catch (RuntimeException e) {
LOG.error("Kafka worker exited with exception", e);
failure.set(CheckResult.failed(e));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2020 The OpenZipkin Authors
* Copyright 2015-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 @@ -54,7 +54,7 @@ public void onError(Throwable t) {}
final CollectorMetrics metrics;
// added for integration tests only, see ITKafkaCollector
final AtomicReference<List<TopicPartition>> assignedPartitions =
new AtomicReference<>(Collections.emptyList());
new AtomicReference<>(List.of());
final AtomicBoolean running = new AtomicBoolean(true);

KafkaCollectorWorker(KafkaCollector.Builder builder) {
Expand All @@ -75,7 +75,7 @@ public void run() {
public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
// technically we should remove only the revoked partitions but for test purposes it
// does not matter
assignedPartitions.set(Collections.emptyList());
assignedPartitions.set(List.of());
}

@Override
Expand Down Expand Up @@ -106,7 +106,7 @@ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
metrics.incrementMessagesDropped();
continue;
}
collector.accept(Collections.singletonList(span), NOOP);
collector.accept(List.of(span), NOOP);
} else {
collector.acceptSpans(bytes, NOOP);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package zipkin2.collector.kafka;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CopyOnWriteArraySet;
Expand Down Expand Up @@ -54,9 +53,9 @@
@Timeout(60)
@Tag("docker")
class ITKafkaCollector {
@RegisterExtension KafkaExtension kafka = new KafkaExtension();
@RegisterExtension static KafkaExtension kafka = new KafkaExtension();

List<Span> spans = Arrays.asList(LOTS_OF_SPANS[0], LOTS_OF_SPANS[1]);
List<Span> spans = List.of(LOTS_OF_SPANS[0], LOTS_OF_SPANS[1]);

InMemoryCollectorMetrics metrics = new InMemoryCollectorMetrics();
InMemoryCollectorMetrics kafkaMetrics = metrics.forTransport("kafka");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import zipkin2.storage.SpanConsumer;
import zipkin2.storage.StorageComponent;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static zipkin2.TestObjects.LOTS_OF_SPANS;
import static zipkin2.TestObjects.UTF_8;
Expand All @@ -47,9 +46,9 @@
@Timeout(60)
@Tag("docker")
class ITRabbitMQCollector {
@RegisterExtension RabbitMQExtension rabbit = new RabbitMQExtension();
@RegisterExtension static RabbitMQExtension rabbit = new RabbitMQExtension();

List<Span> spans = asList(LOTS_OF_SPANS[0], LOTS_OF_SPANS[1]);
List<Span> spans = List.of(LOTS_OF_SPANS[0], LOTS_OF_SPANS[1]);

InMemoryCollectorMetrics metrics = new InMemoryCollectorMetrics();
InMemoryCollectorMetrics rabbitmqMetrics = metrics.forTransport("rabbitmq");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2023 The OpenZipkin Authors
* Copyright 2015-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 @@ -15,12 +15,12 @@

import com.rabbitmq.client.ConnectionFactory;
import java.io.UncheckedIOException;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import zipkin2.CheckResult;
import zipkin2.Component;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

Expand All @@ -33,7 +33,7 @@ class RabbitMQCollectorTest {
connectionFactory.setConnectionTimeout(100);
// We can be pretty certain RabbitMQ isn't running on localhost port 80
collector = RabbitMQCollector.builder()
.connectionFactory(connectionFactory).addresses(asList("localhost:80")).build();
.connectionFactory(connectionFactory).addresses(List.of("localhost:80")).build();
}

@Test void checkFalseWhenRabbitMQIsDown() {
Expand Down
Loading

0 comments on commit 35bfac3

Please sign in to comment.