From 160c117628047d685f0dd89b2e06f4fed208fc93 Mon Sep 17 00:00:00 2001 From: Tony Mancill Date: Mon, 11 Jul 2022 23:41:34 +0000 Subject: [PATCH] Replace UUID.randomUUID() with a faster implementation --- .../main/java/com/newrelic/telemetry/TelemetryBatch.java | 2 +- .../src/main/java/com/newrelic/telemetry/util/Utils.java | 7 +++++++ .../java/com/newrelic/telemetry/TelemetryClientTest.java | 4 ++-- .../java/com/newrelic/telemetry/examples/LogExample.java | 4 ++-- .../java/com/newrelic/telemetry/examples/SpanExample.java | 6 +++--- .../telemetry/examples/SpanToTraceObserverExample.java | 6 +++--- .../telemetry/examples/TelemetryClientExample.java | 8 ++++---- 7 files changed, 22 insertions(+), 15 deletions(-) diff --git a/telemetry-core/src/main/java/com/newrelic/telemetry/TelemetryBatch.java b/telemetry-core/src/main/java/com/newrelic/telemetry/TelemetryBatch.java index c98f4d46..3123d104 100644 --- a/telemetry-core/src/main/java/com/newrelic/telemetry/TelemetryBatch.java +++ b/telemetry-core/src/main/java/com/newrelic/telemetry/TelemetryBatch.java @@ -16,7 +16,7 @@ /** Represents a collection of {@link Telemetry} instances and some common attributes */ public abstract class TelemetryBatch { - private final UUID uuid = UUID.randomUUID(); + private final UUID uuid = Utils.generateUUID(); private Collection telemetry; private Attributes commonAttributes; diff --git a/telemetry-core/src/main/java/com/newrelic/telemetry/util/Utils.java b/telemetry-core/src/main/java/com/newrelic/telemetry/util/Utils.java index 8c3f8e74..cf758598 100644 --- a/telemetry-core/src/main/java/com/newrelic/telemetry/util/Utils.java +++ b/telemetry-core/src/main/java/com/newrelic/telemetry/util/Utils.java @@ -4,6 +4,9 @@ */ package com.newrelic.telemetry.util; +import java.util.UUID; +import java.util.concurrent.ThreadLocalRandom; + /** A class with helper functions */ public class Utils { @@ -25,4 +28,8 @@ public static String verifyNonBlank(String input, String message) public static E verifyNonNull(E input) throws IllegalArgumentException { return verifyNonNull(input, "input cannot be null"); } + + public static UUID generateUUID() { + return new UUID(ThreadLocalRandom.current().nextLong(), ThreadLocalRandom.current().nextLong()); + } } diff --git a/telemetry-core/src/test/java/com/newrelic/telemetry/TelemetryClientTest.java b/telemetry-core/src/test/java/com/newrelic/telemetry/TelemetryClientTest.java index dc8f1ba8..df5a453c 100644 --- a/telemetry-core/src/test/java/com/newrelic/telemetry/TelemetryClientTest.java +++ b/telemetry-core/src/test/java/com/newrelic/telemetry/TelemetryClientTest.java @@ -32,6 +32,7 @@ import com.newrelic.telemetry.spans.Span; import com.newrelic.telemetry.spans.SpanBatch; import com.newrelic.telemetry.spans.SpanBatchSender; +import com.newrelic.telemetry.util.Utils; import java.net.URI; import java.net.URL; import java.util.ArrayList; @@ -41,7 +42,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.UUID; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -352,7 +352,7 @@ private MetricBatch makeBatchOf3Metrics() { private static Metric makeMetric() { return new Count( - UUID.randomUUID().toString(), + Utils.generateUUID().toString(), 99, System.currentTimeMillis() - 100, System.currentTimeMillis(), diff --git a/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/LogExample.java b/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/LogExample.java index ed50f766..150167f3 100644 --- a/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/LogExample.java +++ b/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/LogExample.java @@ -11,11 +11,11 @@ import com.newrelic.telemetry.logs.LogBatch; import com.newrelic.telemetry.logs.LogBatchSender; import com.newrelic.telemetry.util.IngestWarnings; +import com.newrelic.telemetry.util.Utils; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; -import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,7 +53,7 @@ public static void main(String[] args) throws Exception { List logs = new ArrayList<>(); logs.add(Log.builder().level("INFO").message("Start of process").build()); for (String item : items) { - String logId = UUID.randomUUID().toString(); + String logId = Utils.generateUUID().toString(); Attributes attributes = new Attributes().put("id", logId).put("food", item); Log log = Log.builder().attributes(attributes).message("Processing " + item).level("DEBUG").build(); diff --git a/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/SpanExample.java b/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/SpanExample.java index 3e11be22..af3c59f1 100644 --- a/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/SpanExample.java +++ b/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/SpanExample.java @@ -10,10 +10,10 @@ import com.newrelic.telemetry.spans.Span; import com.newrelic.telemetry.spans.SpanBatch; import com.newrelic.telemetry.spans.SpanBatchSender; +import com.newrelic.telemetry.util.Utils; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,13 +47,13 @@ public static void main(String[] args) throws Exception { .build()); List spans = new ArrayList<>(); - String traceId = UUID.randomUUID().toString(); + String traceId = Utils.generateUUID().toString(); long spanStartTime = System.currentTimeMillis(); String parentId = null; for (String item : items) { int durationMs = random.nextInt(1000); - String spanId = UUID.randomUUID().toString(); + String spanId = Utils.generateUUID().toString(); spans.add( Span.builder(spanId) .traceId(traceId) diff --git a/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/SpanToTraceObserverExample.java b/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/SpanToTraceObserverExample.java index b2d866ba..3e636dca 100644 --- a/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/SpanToTraceObserverExample.java +++ b/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/SpanToTraceObserverExample.java @@ -7,12 +7,12 @@ import com.newrelic.telemetry.spans.Span; import com.newrelic.telemetry.spans.SpanBatch; import com.newrelic.telemetry.spans.SpanBatchSender; +import com.newrelic.telemetry.util.Utils; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,12 +42,12 @@ public static void main(String[] args) throws ResponseException, MalformedURLExc .build()); List spans = new ArrayList<>(); - String traceId = UUID.randomUUID().toString(); + String traceId = Utils.generateUUID().toString(); long spanStartTime = System.currentTimeMillis(); String parentId = null; for (String item : items) { int durationMs = random.nextInt(1000); - String spanId = UUID.randomUUID().toString(); + String spanId = Utils.generateUUID().toString(); spans.add( Span.builder(spanId) .traceId(traceId) diff --git a/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/TelemetryClientExample.java b/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/TelemetryClientExample.java index 99a41676..21911d03 100644 --- a/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/TelemetryClientExample.java +++ b/telemetry_examples/src/main/java/com/newrelic/telemetry/examples/TelemetryClientExample.java @@ -20,10 +20,10 @@ import com.newrelic.telemetry.metrics.Summary; import com.newrelic.telemetry.spans.Span; import com.newrelic.telemetry.spans.SpanBatch; +import com.newrelic.telemetry.util.Utils; import java.net.InetAddress; import java.time.Duration; import java.time.temporal.ChronoUnit; -import java.util.UUID; /** * This example shows how to use the TelemetryClient to handle standard error conditions. @@ -114,13 +114,13 @@ private static void sendSampleMetrics( private static Span sendSampleSpan(TelemetryClient telemetryClient, Attributes commonAttributes) { Span sampleSpan = - Span.builder(UUID.randomUUID().toString()) + Span.builder(Utils.generateUUID().toString()) .timestamp(System.currentTimeMillis()) - .traceId(UUID.randomUUID().toString()) + .traceId(Utils.generateUUID().toString()) .durationMs(150d) .name("testSpan") .build(); - String traceId = UUID.randomUUID().toString(); + String traceId = Utils.generateUUID().toString(); SpanBatch spanBatch = new SpanBatch(singleton(sampleSpan), commonAttributes, traceId); telemetryClient.sendBatch(spanBatch); return sampleSpan;