diff --git a/api/src/main/java/io/opentelemetry/internal/StringUtils.java b/api/src/main/java/io/opentelemetry/internal/StringUtils.java index ace694400e6..aae059cde11 100644 --- a/api/src/main/java/io/opentelemetry/internal/StringUtils.java +++ b/api/src/main/java/io/opentelemetry/internal/StringUtils.java @@ -18,6 +18,7 @@ import io.opentelemetry.common.AttributeValue; import java.util.List; +import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; /** Internal utility methods for working with attribute keys, attribute values, and metric names. */ @@ -88,18 +89,20 @@ public static AttributeValue cutIfNeeded(AttributeValue value, int limit) { String[] newStrings = new String[strings.size()]; for (int i = 0; i < strings.size(); i++) { String string = strings.get(i); - newStrings[i] = string == null ? null : cutIfNeeded(string, limit); + newStrings[i] = cutIfNeeded(string, limit); } return AttributeValue.arrayAttributeValue(newStrings); } String string = value.getStringValue(); - return string.length() <= limit + // Don't allocate new AttributeValue if not needed + return (string == null || string.length() <= limit) ? value : AttributeValue.stringAttributeValue(string.substring(0, limit)); } + @Nullable private static String cutIfNeeded(String s, int limit) { if (s == null || s.length() <= limit) { return s; diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/config/TraceConfig.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/config/TraceConfig.java index c80da554f52..df40961921b 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/config/TraceConfig.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/config/TraceConfig.java @@ -150,7 +150,8 @@ public static TraceConfig getDefault() { public abstract int getMaxNumberOfAttributesPerLink(); /** - * Returns the global default max length of string attribute value in characters. + * Returns the global default max length of string attribute value in characters. Zero means + * unlimited. * * @return the global default max length of string attribute value in characters. */ diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index 265e53821f4..c7d01d72538 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -424,6 +424,7 @@ public void tooLargeAttributeValuesAreTruncated() { .build(); tracerSdkFactory.updateActiveTraceConfig(traceConfig); Span.Builder spanBuilder = tracerSdk.spanBuilder(SPAN_NAME); + spanBuilder.setAttribute("builderStringNull", (String) null); spanBuilder.setAttribute("builderStringSmall", "small"); spanBuilder.setAttribute("builderStringLarge", "very large string that we have to cut"); spanBuilder.setAttribute("builderLong", 42L); @@ -447,6 +448,7 @@ public void tooLargeAttributeValuesAreTruncated() { try { ReadableAttributes attrs = span.toSpanData().getAttributes(); + assertThat(attrs.get("builderStringNull")).isEqualTo(null); assertThat(attrs.get("builderStringSmall")) .isEqualTo(AttributeValue.stringAttributeValue("small")); assertThat(attrs.get("builderStringLarge"))