-
Notifications
You must be signed in to change notification settings - Fork 849
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Zipkin exporter: Serialize EventData attributes as JSON (#4934)
* serialize EventData attributes to json * remove import * fix test * address code review comments. * safety first
- Loading branch information
1 parent
f004af9
commit e61d7c8
Showing
5 changed files
with
105 additions
and
8 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/EventDataToAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.zipkin; | ||
|
||
import static java.util.stream.Collectors.joining; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.sdk.trace.data.EventData; | ||
import java.util.List; | ||
|
||
/** | ||
* Converts an EventData instance to a String representation of that data, with attributes converted | ||
* to JSON. | ||
* | ||
* <p>See <a | ||
* href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk_exporters/zipkin.md#events">the | ||
* zipkin exporter spec</a> for details. | ||
*/ | ||
final class EventDataToAnnotation { | ||
|
||
private EventDataToAnnotation() {} | ||
|
||
static String apply(EventData eventData) { | ||
String name = eventData.getName(); | ||
String value = toJson(eventData.getAttributes()); | ||
return "\"" + name + "\":" + value; | ||
} | ||
|
||
private static String toJson(Attributes attributes) { | ||
return attributes.asMap().entrySet().stream() | ||
.map(entry -> "\"" + entry.getKey() + "\":" + toValue(entry.getValue())) | ||
.collect(joining(",", "{", "}")); | ||
} | ||
|
||
private static String toValue(Object o) { | ||
if (o instanceof String) { | ||
return "\"" + o + "\""; | ||
} | ||
if (o instanceof List) { | ||
return ((List<?>) o) | ||
.stream().map(EventDataToAnnotation::toValue).collect(joining(",", "[", "]")); | ||
} | ||
return String.valueOf(o); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...ters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/EventDataToAnnotationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.zipkin; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.sdk.trace.data.EventData; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class EventDataToAnnotationTest { | ||
|
||
@Test | ||
void basicConversion() { | ||
|
||
Attributes attrs = | ||
Attributes.builder() | ||
.put("v1", "v1") | ||
.put("v2", 12L) | ||
.put("v3", 123.45) | ||
.put("v4", false) | ||
.put("v5", "foo", "bar", "baz") | ||
.put("v6", 1, 2, 3) | ||
.put("v7", 1.23, 3.45) | ||
.put("v8", true, false, true) | ||
.build(); | ||
String expected = | ||
"\"cat\":{\"v1\":\"v1\",\"v2\":12,\"v3\":123.45,\"v4\":false,\"v5\":[\"foo\",\"bar\",\"baz\"],\"v6\":[1,2,3],\"v7\":[1.23,3.45],\"v8\":[true,false,true]}"; | ||
EventData eventData = EventData.create(0, "cat", attrs); | ||
|
||
String result = EventDataToAnnotation.apply(eventData); | ||
|
||
assertThat(result).isEqualTo(expected); | ||
} | ||
|
||
@Test | ||
void empty() { | ||
Attributes attrs = Attributes.empty(); | ||
String expected = "\"dog\":{}"; | ||
EventData eventData = EventData.create(0, "dog", attrs); | ||
|
||
String result = EventDataToAnnotation.apply(eventData); | ||
|
||
assertThat(result).isEqualTo(expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters