Skip to content

Commit

Permalink
Fix json repeated string parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuereth committed Nov 27, 2024
1 parent 6e82b27 commit 0ed5fe2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ protected void writeTo(Serializer output) throws IOException {
output.writeString(ResourceEntityRefExperimental.SCHEMA_URL, schemaUrlUtf8);
}
output.writeString(ResourceEntityRefExperimental.TYPE, typeUtf8);
for (byte[] keyUtf8 : identityAttributeKeysUtf8) {
output.serializeString(ResourceEntityRefExperimental.IDENTITY_ATTRIBUTES, keyUtf8);
}
for (byte[] keyUtf8 : descriptiveAttributeKeysUtf8) {
output.serializeString(ResourceEntityRefExperimental.DESCRIPTION_ATTRIBUTES, keyUtf8);
}
output.writeRepeatedString(
ResourceEntityRefExperimental.IDENTITY_ATTRIBUTES, identityAttributeKeysUtf8);
output.writeRepeatedString(
ResourceEntityRefExperimental.DESCRIPTION_ATTRIBUTES, descriptiveAttributeKeysUtf8);
}

public static ResourceEntityRefMarshaler createForEntity(Entity e) {
Expand Down Expand Up @@ -81,6 +79,7 @@ private static int calculateSize(
size += MarshalerUtil.sizeBytes(ResourceEntityRefExperimental.SCHEMA_URL, schemaUrlUtf8);
}
size += MarshalerUtil.sizeBytes(ResourceEntityRefExperimental.TYPE, typeUtf8);
// TODO - we need repeated string support.
for (byte[] keyUtf8 : identityAttributeKeysUtf8) {
size += MarshalerUtil.sizeBytes(ResourceEntityRefExperimental.IDENTITY_ATTRIBUTES, keyUtf8);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.internal.otlp;

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

import io.opentelemetry.sdk.resources.Entity;
import io.opentelemetry.sdk.resources.Resource;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;

public class ResourceEntityTest {
@Test
void toJsonResourceWithEntity() throws Exception {
Resource resource =
Resource.builder()
.add(
Entity.builder()
.setSchemaUrl("http://example.com/1.0")
.setEntityType("test")
.withIdentifying(attr -> attr.put("test.id", 1))
.withDescriptive(attr -> attr.put("test.name", "one"))
.build())
.build();

ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ResourceMarshaler.create(resource).writeJsonTo(out);
} finally {
out.close();
}

String json = new String(out.toByteArray(), StandardCharsets.UTF_8);
assertThat(json)
.isEqualTo(
"{\"attributes\":[{\"key\":\"test.id\",\"value\":{\"intValue\":\"1\"}},{\"key\":\"test.name\",\"value\":{\"stringValue\":\"one\"}}],"
+ "\"entityRefs\":[{\"schemaUrl\":\"http://example.com/1.0\",\"type\":\"test\",\"idAttrKeys\":[\"test.id\"],\"descrAttrKeys\":[\"test.name\"]}]}");
}
}

0 comments on commit 0ed5fe2

Please sign in to comment.