Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
lmolkova committed Jan 11, 2025
1 parent d00c417 commit 4db1075
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,28 @@ public <C> InstrumentationContext extract(InstrumentationContext context, C carr
return context == null ? FallbackSpanContext.INVALID : context;
}

/**
* Validates the traceparent header according to <a href="https://www.w3.org/TR/trace-context/#traceparent-header-field-values">W3C Trace Context</a>
*
* @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>-<trace-id>-<span-id>-<trace-flags>
// 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);
Expand All @@ -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);
Expand All @@ -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'))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

0 comments on commit 4db1075

Please sign in to comment.