From 4db1075be34b35bbe03d98bb5cff43a8139d0e1d Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Fri, 10 Jan 2025 18:37:24 -0800 Subject: [PATCH] feedback --- .../core/http/models/PagedIterable.java | 2 +- .../fallback/FallbackContextPropagator.java | 35 +++++++++---------- .../fallback/FallbackSpanContext.java | 11 ++---- .../fallback/RandomIdUtils.java | 6 ++-- .../instrumentation/otel/OTelInitializer.java | 2 +- 5 files changed, 23 insertions(+), 33 deletions(-) diff --git a/sdk/clientcore/core/src/main/java/io/clientcore/core/http/models/PagedIterable.java b/sdk/clientcore/core/src/main/java/io/clientcore/core/http/models/PagedIterable.java index 8e0b8d29f4eb6..3425ad51289de 100644 --- a/sdk/clientcore/core/src/main/java/io/clientcore/core/http/models/PagedIterable.java +++ b/sdk/clientcore/core/src/main/java/io/clientcore/core/http/models/PagedIterable.java @@ -3,7 +3,7 @@ package io.clientcore.core.http.models; -import io.clientcore.core.util.ClientLogger; +import io.clientcore.core.instrumentation.logging.ClientLogger; import java.util.Iterator; import java.util.NoSuchElementException; diff --git a/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/fallback/FallbackContextPropagator.java b/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/fallback/FallbackContextPropagator.java index d168deea15c26..c61837b0138a3 100644 --- a/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/fallback/FallbackContextPropagator.java +++ b/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/fallback/FallbackContextPropagator.java @@ -51,23 +51,28 @@ public InstrumentationContext extract(InstrumentationContext context, C carr return context == null ? FallbackSpanContext.INVALID : context; } + /** + * Validates the traceparent header according to W3C Trace Context + * + * @param traceparent the traceparent header value + * @return true if the traceparent header is valid, false otherwise + */ private static boolean isValidTraceparent(String traceparent) { if (traceparent == null || traceparent.length() != 55) { return false; } - // version - for (int i = 0; i < 2; i++) { - if (traceparent.charAt(i) != '0') { - return false; - } - } - - if (traceparent.charAt(2) != '-') { + // valid traceparent format: --- + // version - only 00 is supported + if (traceparent.charAt(0) != '0' + || traceparent.charAt(1) != '0' + || traceparent.charAt(2) != '-' + || traceparent.charAt(35) != '-' + || traceparent.charAt(52) != '-') { return false; } - // trace-id + // trace-id - 32 lower case hex characters, all 0 is invalid boolean isAllZero = true; for (int i = 3; i < 35; i++) { char c = traceparent.charAt(i); @@ -82,11 +87,7 @@ private static boolean isValidTraceparent(String traceparent) { return false; } - if (traceparent.charAt(35) != '-') { - return false; - } - - // span-id + // span-id - 16 lower case hex characters, all 0 is invalid isAllZero = true; for (int i = 36; i < 52; i++) { char c = traceparent.charAt(i); @@ -102,11 +103,7 @@ private static boolean isValidTraceparent(String traceparent) { return false; } - if (traceparent.charAt(52) != '-') { - return false; - } - - // trace-flags + // trace-flags - 2 lower case hex characters for (int i = 53; i < 55; i++) { char c = traceparent.charAt(i); if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) { diff --git a/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/fallback/FallbackSpanContext.java b/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/fallback/FallbackSpanContext.java index 50a2999c573ce..f5a588451d05d 100644 --- a/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/fallback/FallbackSpanContext.java +++ b/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/fallback/FallbackSpanContext.java @@ -12,7 +12,8 @@ import static io.clientcore.core.implementation.instrumentation.fallback.RandomIdUtils.generateTraceId; final class FallbackSpanContext implements InstrumentationContext { - static final FallbackSpanContext INVALID = new FallbackSpanContext(); + static final FallbackSpanContext INVALID + = new FallbackSpanContext(INVALID_TRACE_ID, INVALID_SPAN_ID, "00", false, Span.noop()); private final String traceId; private final String spanId; private final String traceFlags; @@ -59,14 +60,6 @@ public String getTraceFlags() { return traceFlags; } - private FallbackSpanContext() { - this.traceId = INVALID_TRACE_ID; - this.spanId = INVALID_SPAN_ID; - this.traceFlags = "00"; - this.isValid = false; - this.span = Span.noop(); - } - FallbackSpanContext(String traceId, String spanId, String traceFlags, boolean isValid, Span span) { this.traceId = traceId; this.spanId = spanId; diff --git a/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/fallback/RandomIdUtils.java b/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/fallback/RandomIdUtils.java index c5390e2f60fad..205f9b77be48f 100644 --- a/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/fallback/RandomIdUtils.java +++ b/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/fallback/RandomIdUtils.java @@ -23,7 +23,6 @@ class RandomIdUtils { private static final int TRACE_ID_HEX_LENGTH = 32; private static final int SPAN_ID_HEX_LENGTH = 16; private static final char[] ENCODING = buildEncodingArray(); - private static final String ALPHABET = "0123456789abcdef"; public static String generateSpanId() { long id; @@ -80,10 +79,11 @@ private static void byteToBase16(byte value, char[] dest, int destOffset) { } private static char[] buildEncodingArray() { + String alphabet = "0123456789abcdef"; char[] encoding = new char[512]; for (int i = 0; i < 256; ++i) { - encoding[i] = ALPHABET.charAt(i >>> 4); - encoding[i | 0x100] = ALPHABET.charAt(i & 0xF); + encoding[i] = alphabet.charAt(i >>> 4); + encoding[i | 0x100] = alphabet.charAt(i & 0xF); } return encoding; } diff --git a/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/otel/OTelInitializer.java b/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/otel/OTelInitializer.java index f6af8ae1ad380..3e9b02871bd68 100644 --- a/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/otel/OTelInitializer.java +++ b/sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/otel/OTelInitializer.java @@ -184,7 +184,7 @@ public static void runtimeError(ClientLogger logger, Throwable t) { * @return true if OTel is initialized successfully, false otherwise */ public static boolean isInitialized() { - return INSTANCE == null || INSTANCE.initialized; + return INSTANCE.initialized; } }