Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
iNikem committed Jul 30, 2020
1 parent 99de8ea commit c996970
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
7 changes: 5 additions & 2 deletions api/src/main/java/io/opentelemetry/internal/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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"))
Expand Down

0 comments on commit c996970

Please sign in to comment.