Skip to content

Commit 3b2380f

Browse files
committed
only serialize empty strings for attributes (StringAnyValue)
1 parent 2a8bf41 commit 3b2380f

File tree

5 files changed

+13
-6
lines changed

5 files changed

+13
-6
lines changed

exporters/common/src/main/java/io/opentelemetry/exporter/internal/marshal/Serializer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,19 @@ public void serializeRepeatedString(ProtoFieldInfo field, byte[][] utf8Bytes) th
254254
*/
255255
public void serializeStringWithContext(
256256
ProtoFieldInfo field, @Nullable String string, MarshalerContext context) throws IOException {
257+
serializeStringWithContext(field, string, context, /* allowEmpty= */ false);
258+
}
259+
260+
public void serializeStringWithContext(
261+
ProtoFieldInfo field, @Nullable String string, MarshalerContext context, boolean allowEmpty)
262+
throws IOException {
257263
if (string == null) {
258264
return;
259265
}
260266
if (string.isEmpty()) {
261-
writeString(field, string, 0, context);
267+
if (allowEmpty) {
268+
writeString(field, string, 0, context);
269+
}
262270
return;
263271
}
264272
if (context.marshalStringNoAllocation()) {

exporters/common/src/test/java/io/opentelemetry/exporter/internal/marshal/ProtoSerializerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void testSerializeEmptyStringWithContext() throws Exception {
4444
MarshalerContext context = new MarshalerContext();
4545
context.setSize(0, 0);
4646

47-
serializer.serializeStringWithContext(field, value, context);
47+
serializer.serializeStringWithContext(field, value, context, true);
4848
serializer.close();
4949

5050
byte[] result = out.toByteArray();

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/StringAnyValueMarshaler.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ static MarshalerWithSize create(String value) {
3333

3434
@Override
3535
public void writeTo(Serializer output) throws IOException {
36-
if (valueUtf8.length == 0) {
37-
return;
38-
}
3936
output.writeString(AnyValue.STRING_VALUE, valueUtf8);
4037
}
4138

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/StringAnyValueStatelessMarshaler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ private StringAnyValueStatelessMarshaler() {}
2626
@Override
2727
public void writeTo(Serializer output, String value, MarshalerContext context)
2828
throws IOException {
29-
output.serializeStringWithContext(AnyValue.STRING_VALUE, value, context);
29+
output.serializeStringWithContext(
30+
AnyValue.STRING_VALUE, value, context, /* allowEmpty= */ true);
3031
}
3132

3233
@Override

exporters/otlp/common/src/test/java/io/opentelemetry/exporter/internal/otlp/traces/LowAllocationTraceRequestMarshalerTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ private static SpanData createSpanData() {
8888
Attributes.builder()
8989
.put(KEY_BOOL, true)
9090
.put(KEY_STRING, "string")
91+
.put("empty", "")
9192
.put(KEY_INT, 100L)
9293
.put(KEY_DOUBLE, 100.3)
9394
.build())

0 commit comments

Comments
 (0)