Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.api.internal;
package io.opentelemetry.api.impl;

/**
* {@link ThreadLocal} buffers for use when creating new derived objects such as {@link String}s.
Expand All @@ -12,8 +12,8 @@
* should get a temporary buffer, fill it with data, and finish by converting into the derived
* object within the same method to avoid multiple usages of the same buffer.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
* <p>This class is not intended for use by application developers. Its API is stable and will not
* be changed or removed in a backwards-incompatible manner.
*/
public final class TemporaryBuffers {

Expand All @@ -24,6 +24,9 @@ public final class TemporaryBuffers {
* {@code len} as this buffer will remain for the lifetime of the thread. The returned buffer will
* not be zeroed and may be larger than the requested size, you must make sure to fill the entire
* content to the desired value and set the length explicitly when converting to a {@link String}.
*
* @param len the minimum buffer capacity
* @return a reusable thread-local character buffer with capacity of at least {@code len}
*/
public static char[] chars(int len) {
char[] buffer = CHAR_ARRAY.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package io.opentelemetry.api.internal;

import io.opentelemetry.api.impl.TemporaryBuffers;
import javax.annotation.CheckForNull;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

package io.opentelemetry.api.trace;

import io.opentelemetry.api.impl.TemporaryBuffers;
import io.opentelemetry.api.internal.OtelEncodingUtils;
import io.opentelemetry.api.internal.TemporaryBuffers;
import io.opentelemetry.common.impl.ApiUsageLogger;
import javax.annotation.concurrent.Immutable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

package io.opentelemetry.api.trace;

import io.opentelemetry.api.impl.TemporaryBuffers;
import io.opentelemetry.api.internal.OtelEncodingUtils;
import io.opentelemetry.api.internal.TemporaryBuffers;
import io.opentelemetry.common.impl.ApiUsageLogger;
import javax.annotation.concurrent.Immutable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import static io.opentelemetry.api.trace.propagation.internal.W3CTraceContextEncoding.decodeTraceState;
import static io.opentelemetry.api.trace.propagation.internal.W3CTraceContextEncoding.encodeTraceState;

import io.opentelemetry.api.impl.TemporaryBuffers;
import io.opentelemetry.api.internal.OtelEncodingUtils;
import io.opentelemetry.api.internal.TemporaryBuffers;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.api.internal;
package io.opentelemetry.api.impl;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down
5 changes: 4 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-api.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Comparing source compatibility of opentelemetry-api-1.68.0-SNAPSHOT.jar against opentelemetry-api-1.67.0.jar
No changes.
+++ NEW CLASS: PUBLIC(+) FINAL(+) io.opentelemetry.api.impl.TemporaryBuffers (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW SUPERCLASS: java.lang.Object
+++ NEW METHOD: PUBLIC(+) STATIC(+) char[] chars(int)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package io.opentelemetry.extension.trace.propagation;

import io.opentelemetry.api.internal.StringUtils;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapGetter;
Expand Down Expand Up @@ -34,7 +33,7 @@ public <C> Optional<Context> extract(
private static <C> Optional<Context> extractSpanContextFromSingleHeader(
Context context, @Nullable C carrier, TextMapGetter<C> getter) {
String value = getter.get(carrier, B3Propagator.COMBINED_HEADER);
if (StringUtils.isNullOrEmpty(value)) {
if (value == null || value.isEmpty()) {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import static io.opentelemetry.extension.trace.propagation.B3Propagator.COMBINED_HEADER;

import io.opentelemetry.api.internal.TemporaryBuffers;
import io.opentelemetry.api.impl.TemporaryBuffers;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package io.opentelemetry.extension.trace.propagation;

import io.opentelemetry.api.internal.StringUtils;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanId;
import io.opentelemetry.api.trace.TraceFlags;
Expand Down Expand Up @@ -56,12 +55,13 @@ static SpanContext buildSpanContext(
}

static boolean isTraceIdValid(@Nullable String value) {
return !(StringUtils.isNullOrEmpty(value)
|| (value.length() != MIN_TRACE_ID_LENGTH && value.length() != MAX_TRACE_ID_LENGTH)
|| !TraceId.isValid(StringUtils.padLeft(value, TraceId.getLength())));
return value != null
&& !value.isEmpty()
&& (value.length() == MIN_TRACE_ID_LENGTH || value.length() == MAX_TRACE_ID_LENGTH)
&& TraceId.isValid(StringUtils.padLeft(value, TraceId.getLength()));
}

static boolean isSpanIdValid(@Nullable String value) {
return !StringUtils.isNullOrEmpty(value) && SpanId.isValid(value);
return value != null && !value.isEmpty() && SpanId.isValid(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.baggage.BaggageBuilder;
import io.opentelemetry.api.internal.StringUtils;
import io.opentelemetry.api.internal.TemporaryBuffers;
import io.opentelemetry.api.impl.TemporaryBuffers;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanId;
Expand Down Expand Up @@ -184,7 +183,7 @@ public String toString() {
private static <C> SpanContext getSpanContextFromHeader(
@Nullable C carrier, TextMapGetter<C> getter) {
String value = getter.get(carrier, PROPAGATION_HEADER);
if (StringUtils.isNullOrEmpty(value)) {
if (value == null || value.isEmpty()) {
return SpanContext.getInvalid();
}

Expand Down Expand Up @@ -419,14 +418,14 @@ private static SpanContext buildSpanContext(String traceId, String spanId, Strin
}

private static boolean isTraceIdValid(String value) {
return !(StringUtils.isNullOrEmpty(value) || value.length() > MAX_TRACE_ID_LENGTH);
return !value.isEmpty() && value.length() <= MAX_TRACE_ID_LENGTH;
}

private static boolean isSpanIdValid(String value) {
return !(StringUtils.isNullOrEmpty(value) || value.length() > MAX_SPAN_ID_LENGTH);
return !value.isEmpty() && value.length() <= MAX_SPAN_ID_LENGTH;
}

private static boolean isFlagsValid(String value) {
return !(StringUtils.isNullOrEmpty(value) || value.length() > MAX_FLAGS_LENGTH);
return !value.isEmpty() && value.length() <= MAX_FLAGS_LENGTH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.baggage.BaggageBuilder;
import io.opentelemetry.api.internal.StringUtils;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.extension.trace.propagation;

import java.util.Objects;
import javax.annotation.concurrent.Immutable;

/**
* Utilities for working with strings.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
@Immutable
final class StringUtils {

/**
* Returns {@code value} left-padded with zeroes to at least {@code minLength} characters.
*
* <p>If {@code value} is already at least {@code minLength} characters long, it is returned
* unchanged.
*
* @param value the string to pad
* @param minLength the minimum length of the returned string
* @return the padded string, or {@code value} if it already meets the minimum length
*/
static String padLeft(String value, int minLength) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is only used in the opentelemetry-extension-trace-propagators module and tests, so let's go ahead and remove it from io.opentelemetry.api.internal.StringUtils.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is also used here in SdkLoggerTest.java, Line 79

return padStart(value, minLength, '0');
}

/**
* Returns a string, of length at least {@code minLength}, consisting of {@code string} prepended
* with as many copies of {@code padChar} as are necessary to reach that length. For example,
*
* <ul>
* <li>{@code padStart("7", 3, '0')} returns {@code "007"}
* <li>{@code padStart("2010", 3, '0')} returns {@code "2010"}
* </ul>
*
* <p>See {@link java.util.Formatter} for a richer set of formatting capabilities.
*
* <p>This method was copied almost verbatim from Guava library method
* com.google.common.base.Strings#padStart(java.lang.String, int, char).
*
* @param string the string which should appear at the end of the result
* @param minLength the minimum length the resulting string must have. Can be zero or negative, in
* which case the input string is always returned.
* @param padChar the character to insert at the beginning of the result until the minimum length
* is reached
* @return the padded string
*/
private static String padStart(String string, int minLength, char padChar) {
Objects.requireNonNull(string);
if (string.length() >= minLength) {
return string;
}
StringBuilder sb = new StringBuilder(minLength);
for (int i = string.length(); i < minLength; i++) {
sb.append(padChar);
}
sb.append(string);
return sb.toString();
}

private StringUtils() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static io.opentelemetry.extension.trace.propagation.B3Propagator.DEBUG_CONTEXT_KEY;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.internal.StringUtils;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanId;
Expand Down Expand Up @@ -236,6 +235,15 @@ void extract_InvalidTraceId_NotHex() {
verifyInvalidBehavior(invalidHeaders);
}

@Test
void extract_EmptyTraceId() {
Map<String, String> invalidHeaders = new LinkedHashMap<>();
invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, "");
invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID);
invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT);
verifyInvalidBehavior(invalidHeaders);
}

@Test
void extract_InvalidTraceId_TooShort() {
Map<String, String> invalidHeaders = new LinkedHashMap<>();
Expand Down Expand Up @@ -272,6 +280,15 @@ void extract_InvalidSpanId_NotHex() {
verifyInvalidBehavior(invalidHeaders);
}

@Test
void extract_EmptySpanId() {
Map<String, String> invalidHeaders = new LinkedHashMap<>();
invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID);
invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, "");
invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT);
verifyInvalidBehavior(invalidHeaders);
}

@Test
void extract_InvalidSpanId_TooShort() {
Map<String, String> invalidHeaders = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.extension.trace.propagation;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class CommonTest {

@Test
void isTraceIdValid() {
assertThat(Common.isTraceIdValid(null)).isFalse();
assertThat(Common.isTraceIdValid("")).isFalse();
assertThat(Common.isTraceIdValid("0000000000000001")).isTrue();
assertThat(Common.isTraceIdValid("00000000000000000000000000000001")).isTrue();
assertThat(Common.isTraceIdValid("000000000000000")).isFalse();
assertThat(Common.isTraceIdValid("0000000000000000")).isFalse();
}

@Test
void isSpanIdValid() {
assertThat(Common.isSpanIdValid(null)).isFalse();
assertThat(Common.isSpanIdValid("")).isFalse();
assertThat(Common.isSpanIdValid("0000000000000001")).isTrue();
assertThat(Common.isSpanIdValid("0000000000000000")).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ void extract_InvalidTraceId() {
verifyInvalidBehavior(invalidHeaders);
}

@Test
void extract_EmptyTraceId() {
Map<String, String> invalidHeaders = new LinkedHashMap<>();
invalidHeaders.put(
PROPAGATION_HEADER, generateTraceIdHeaderValue("", SPAN_ID, DEPRECATED_PARENT_SPAN, "0"));

verifyInvalidBehavior(invalidHeaders);
}

@Test
void extract_InvalidTraceId_Size() {
Map<String, String> invalidHeaders = new LinkedHashMap<>();
Expand All @@ -294,6 +303,15 @@ void extract_InvalidSpanId() {
verifyInvalidBehavior(invalidHeaders);
}

@Test
void extract_EmptySpanId() {
Map<String, String> invalidHeaders = new LinkedHashMap<>();
invalidHeaders.put(
PROPAGATION_HEADER, generateTraceIdHeaderValue(TRACE_ID, "", DEPRECATED_PARENT_SPAN, "0"));

verifyInvalidBehavior(invalidHeaders);
}

@Test
void extract_InvalidSpanId_Size() {
Map<String, String> invalidHeaders = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.extension.trace.propagation;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;

class StringUtilsTest {

@Test
void padLeft() {
assertThat(StringUtils.padLeft("value", 10)).isEqualTo("00000value");
}

@Test
void padLeft_length_does_not_exceed_minimum() {
assertThat(StringUtils.padLeft("value", 3)).isEqualTo("value");
assertThat(StringUtils.padLeft("value", -10)).isEqualTo("value");
assertThat(StringUtils.padLeft("value", 0)).isEqualTo("value");
}

@Test
void padLeft_throws_for_null_value() {
assertThatThrownBy(() -> StringUtils.padLeft(null, 10))
.isInstanceOf(NullPointerException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import static io.opentelemetry.sdk.extension.incubator.trace.samplers.ImmutableSamplingIntent.isValidRandomValue;
import static io.opentelemetry.sdk.extension.incubator.trace.samplers.ImmutableSamplingIntent.isValidThreshold;

import io.opentelemetry.api.impl.TemporaryBuffers;
import io.opentelemetry.api.internal.OtelEncodingUtils;
import io.opentelemetry.api.internal.TemporaryBuffers;
import io.opentelemetry.api.trace.TraceState;
import java.util.ArrayList;
import java.util.Collections;
Expand Down
Loading