From a9b46c58fe101ddad372f3651081dc38e5f06d37 Mon Sep 17 00:00:00 2001 From: Arpit Agarwal Date: Fri, 12 Apr 2019 11:51:58 +0200 Subject: [PATCH] HDDS-1421. Avoid unnecessary object allocations in TracingUtil Closes #722 --- .../org/apache/hadoop/hdds/tracing/TracingUtil.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/tracing/TracingUtil.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/tracing/TracingUtil.java index c274625dda..8e82a375ab 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/tracing/TracingUtil.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/tracing/TracingUtil.java @@ -34,6 +34,8 @@ */ public final class TracingUtil { + private static final String NULL_SPAN_AS_STRING = ""; + private TracingUtil() { } @@ -59,12 +61,13 @@ public static void initTracing(String serviceName) { * @return encoded tracing context. */ public static String exportCurrentSpan() { - StringBuilder builder = new StringBuilder(); if (GlobalTracer.get().activeSpan() != null) { + StringBuilder builder = new StringBuilder(); GlobalTracer.get().inject(GlobalTracer.get().activeSpan().context(), StringCodec.FORMAT, builder); + return builder.toString(); } - return builder.toString(); + return NULL_SPAN_AS_STRING; } /** @@ -73,11 +76,12 @@ public static String exportCurrentSpan() { * @return encoded tracing context. */ public static String exportSpan(Span span) { - StringBuilder builder = new StringBuilder(); if (span != null) { + StringBuilder builder = new StringBuilder(); GlobalTracer.get().inject(span.context(), StringCodec.FORMAT, builder); + return builder.toString(); } - return builder.toString(); + return NULL_SPAN_AS_STRING; } /**