Skip to content

Commit

Permalink
Merge pull request #292 from tmancill/faster-uuid
Browse files Browse the repository at this point in the history
Replace UUID.randomUUID() with a faster implementation
  • Loading branch information
jasonjkeller authored Jul 12, 2022
2 parents 46d5134 + 86cdd6c commit 223b4f6
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/** Represents a collection of {@link Telemetry} instances and some common attributes */
public abstract class TelemetryBatch<T extends Telemetry> {

private final UUID uuid = UUID.randomUUID();
private final UUID uuid = Utils.generateUUID();
private Collection<T> telemetry;

private Attributes commonAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -25,4 +28,8 @@ public static String verifyNonBlank(String input, String message)
public static <E> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,7 +53,7 @@ public static void main(String[] args) throws Exception {
List<Log> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,13 +47,13 @@ public static void main(String[] args) throws Exception {
.build());

List<Span> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,12 +42,12 @@ public static void main(String[] args) throws ResponseException, MalformedURLExc
.build());

List<Span> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 223b4f6

Please sign in to comment.