Skip to content
Draft
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 @@ -43,6 +43,13 @@ public AttributesBuilder toBuilder() {
return new ArrayBackedAttributesBuilder(new ArrayList<>(data()));
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
JsonEncoding.appendAttributes(sb, data());
return sb.toString();
}

@SuppressWarnings("unchecked") // safe cast: values are stored internally keyed by AttributeKey<T>
@Override
@Nullable
Expand Down Expand Up @@ -111,8 +118,7 @@ private Value<?> getAsValue(String keyName) {
}

@SuppressWarnings("unchecked")
@Nullable
private static Value<?> asValue(AttributeType type, Object value) {
static Value<?> asValue(AttributeType type, Object value) {
switch (type) {
case STRING:
return Value.of((String) value);
Expand Down Expand Up @@ -154,8 +160,8 @@ private static Value<?> asValue(AttributeType type, Object value) {
// Already a Value
return (Value<?>) value;
}
// Should not reach here
return null;
// Every AttributeType must have a non-null Value representation.
throw new IllegalStateException("Unknown attribute type: " + type);
}

static Attributes sortAndFilterToAttributes(Object... data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ static void append(StringBuilder sb, Value<?> value) {
}
}

static void appendAttributes(StringBuilder sb, List<Object> data) {
sb.append('{');
for (int i = 0; i < data.size(); i += 2) {
if (i > 0) {
sb.append(',');
}
AttributeKey<?> key = (AttributeKey<?>) data.get(i);
appendString(sb, key.getKey());
sb.append(':');
append(sb, ArrayBackedAttributes.asValue(key.getType(), data.get(i + 1)));
}
sb.append('}');
}

private static void appendString(StringBuilder sb, String value) {
sb.append('"');
for (int i = 0; i < value.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.entry;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -815,8 +816,38 @@ void attributesToString() {

assertThat(attributes.toString())
.isEqualTo(
"{error=true, http.response_size=100, "
+ "otel.status_code=\"OK\", process.cpu_consumed=33.44, success=\"true\"}");
"{\"error\":true,\"http.response_size\":100,"
+ "\"otel.status_code\":\"OK\",\"process.cpu_consumed\":33.44,"
+ "\"success\":\"true\"}");
}

@Test
void attributesToStringComplexValues() {
Attributes attributes =
Attributes.builder()
.put(valueKey("bytes"), Value.of("hello world".getBytes(StandardCharsets.UTF_8)))
.put("colors", "red", "blue")
.put(valueKey("empty"), Value.empty())
.put(doubleKey("infinity"), Double.POSITIVE_INFINITY)
.put(
valueKey("nested"),
Value.of(
KeyValue.of("array", Value.of(Value.of("red"), Value.of("blue"))),
KeyValue.of("boolean", Value.of(true))))
.build();

assertThat(attributes.toString())
.isEqualTo(
"{\"bytes\":\"aGVsbG8gd29ybGQ=\",\"colors\":[\"red\",\"blue\"],"
+ "\"empty\":null,\"infinity\":\"Infinity\","
+ "\"nested\":{\"array\":[\"red\",\"blue\"],\"boolean\":true}}");
}

@Test
void attributesToStringEscapesKeysAndValues() {
Attributes attributes = Attributes.of(stringKey("a\"key\n"), "a \\ value\t");

assertThat(attributes.toString()).isEqualTo("{\"a\\\"key\\n\":\"a \\\\ value\\t\"}");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ void export() {
.isEqualTo(
"'testSpan1' : 12345678876543211234567887654321 8765432112345678 "
+ "INTERNAL [tracer: tracer1:] "
+ "{animal=\"cat\", bytes=ValueBytes{AQID}, empty=ValueEmpty{}, "
+ "heterogeneousArray=ValueArray{[\"string\",123]}, lives=9, "
+ "map=KeyValueList{{\"nested\":\"value\"}}}");
+ "{\"animal\":\"cat\",\"bytes\":\"AQID\",\"empty\":null,"
+ "\"heterogeneousArray\":[\"string\",123],\"lives\":9,"
+ "\"map\":{\"nested\":\"value\"}}");
assertThat(logs.getEvents().get(1).getMessage())
.isEqualTo(
"'testSpan2' : 12340000000043211234000000004321 8765000000005678 "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ void format() {
assertThat(output.toString())
.isEqualTo(
"1970-08-07T10:00:00Z ERROR3 'message' : 00000000000000010000000000000002 0000000000000003 "
+ "[scopeInfo: logTest:1.0] {amount=1, bytes=ValueBytes{AQID}, cheese=\"cheddar\", "
+ "empty=ValueEmpty{}, heterogeneousArray=ValueArray{[\"string\",123]}, "
+ "map=KeyValueList{{\"nested\":\"value\"}}}");
+ "[scopeInfo: logTest:1.0] {\"amount\":1,\"bytes\":\"AQID\","
+ "\"cheese\":\"cheddar\",\"empty\":null,"
+ "\"heterogeneousArray\":[\"string\",123],"
+ "\"map\":{\"nested\":\"value\"}}");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ void create_ModelCustomizer() {
assertThat(sdk.toString())
.contains(
"resource=Resource{schemaUrl=null, attributes={"
+ "color=\"blue\", "
+ "foo=\"bar\", "
+ "service.name=\"unknown_service:java\", "
+ "telemetry.sdk.language=\"java\", "
+ "telemetry.sdk.name=\"opentelemetry\", "
+ "telemetry.sdk.version=\"");
+ "\"color\":\"blue\","
+ "\"foo\":\"bar\","
+ "\"service.name\":\"unknown_service:java\","
+ "\"telemetry.sdk.language\":\"java\","
+ "\"telemetry.sdk.name\":\"opentelemetry\","
+ "\"telemetry.sdk.version\":\"");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void testDescription() {
assertThat(
ComposableSampler.annotating(ComposableSampler.alwaysOn(), ATTRIBUTES).getDescription())
.isEqualTo(
"ComposableAnnotatingSampler{ComposableAlwaysOnSampler,{http.route=\"/bear\", size=100}}");
"ComposableAnnotatingSampler{ComposableAlwaysOnSampler,{\"http.route\":\"/bear\",\"size\":100}}");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ void toString_Valid() {
.isEqualTo(
"SdkLoggerProvider{"
+ "clock=SystemClock{}, "
+ "resource=Resource{schemaUrl=null, attributes={key=\"value\"}}, "
+ "resource=Resource{schemaUrl=null, attributes={\"key\":\"value\"}}, "
+ "logLimits=LogLimits{maxNumberOfAttributes=128, maxAttributeValueLength=2147483647}, "
+ "logRecordProcessor=MockLogRecordProcessor, "
+ "loggerConfigurator=ScopeConfiguratorImpl{conditions=[]}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testSanity() {
assertThat(pointData.toString())
.isEqualTo(
"MutableExponentialHistogramPointData{startEpochNanos=10, epochNanos=20, "
+ "attributes={foo=\"bar\"}, scale=1, sum=2.0, count=43, zeroCount=10, hasMin=true, "
+ "attributes={\"foo\":\"bar\"}, scale=1, sum=2.0, count=43, zeroCount=10, hasMin=true, "
+ "min=100.0, hasMax=true, max=1000.0, "
+ "positiveBuckets=MutableExponentialHistogramBuckets{scale=1, offset=2, "
+ "bucketCounts=[1, 2, 3], totalCount=3}, "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void testSanity() {
.isEqualTo(
"MutableHistogramPointData{startEpochNanos=10, "
+ "epochNanos=20, "
+ "attributes={foo=\"bar\"}, "
+ "attributes={\"foo\":\"bar\"}, "
+ "sum=2.0, "
+ "count=550, "
+ "hasMin=true, "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void append_toString() {
AttributesProcessor.append(Attributes.builder().put("key", "value").build());

assertThat(processor.toString())
.isEqualTo("AppendingAttributesProcessor{additionalAttributes={key=\"value\"}}");
.isEqualTo("AppendingAttributesProcessor{additionalAttributes={\"key\":\"value\"}}");
}

@Test
Expand Down Expand Up @@ -141,7 +141,7 @@ void joinedAttributes_toString() {
.isEqualTo(
"JoinedAttributesProcessor{processors=["
+ "BaggageAppendingAttributesProcessor{nameFilter=IncludeExcludePredicate{globMatchingEnabled=false, included=[keep]}}, "
+ "AppendingAttributesProcessor{additionalAttributes={key=\"value\"}}"
+ "AppendingAttributesProcessor{additionalAttributes={\"key\":\"value\"}}"
+ "]}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1110,9 +1110,9 @@ void spanDataToString() {
+ "traceFlags=00, "
+ "traceState=ArrayBasedTraceState\\{entries=\\[]}, remote=false, valid=false}, "
+ "resource=Resource\\{schemaUrl=null, "
+ "attributes=\\{service.name=\"unknown_service:java\", "
+ "telemetry.sdk.language=\"java\", telemetry.sdk.name=\"opentelemetry\", "
+ "telemetry.sdk.version=\"\\d+.\\d+.\\d+(-rc.\\d+)?(-SNAPSHOT)?\"}}, "
+ "attributes=\\{\"service.name\":\"unknown_service:java\","
+ "\"telemetry.sdk.language\":\"java\",\"telemetry.sdk.name\":\"opentelemetry\","
+ "\"telemetry.sdk.version\":\"\\d+.\\d+.\\d+(-rc.\\d+)?(-SNAPSHOT)?\"}}, "
+ "instrumentationScopeInfo=InstrumentationScopeInfo\\{"
+ "name=SpanBuilderSdkTest, version=null, schemaUrl=null, attributes=\\{}}, "
+ "name=span_name, "
Expand Down
Loading