Skip to content

Commit 7b076c6

Browse files
[otlp] Change serialization of null protobuf values (#6149)
Co-authored-by: Rajkumar Rangaraj <[email protected]>
1 parent e5f65ca commit 7b076c6

File tree

7 files changed

+36
-2
lines changed

7 files changed

+36
-2
lines changed

src/OpenTelemetry.Exporter.Console/Implementation/ConsoleTagWriter.cs

+7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ protected override void OnUnsupportedTagDropped(
7373
this.onUnsupportedTagDropped(tagKey, tagValueTypeFullName);
7474
}
7575

76+
protected override bool TryWriteEmptyTag(ref ConsoleTag consoleTag, string key, object? value)
77+
{
78+
consoleTag.Key = key;
79+
consoleTag.Value = null;
80+
return true;
81+
}
82+
7683
internal struct ConsoleTag
7784
{
7885
public string? Key;

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ Notes](../../RELEASENOTES.md).
1616
`Activity.StatusDescription` exceeds 127 bytes.
1717
([#6119](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6119))
1818

19+
* Fixed incorrect log serialization of attributes with null values, causing
20+
some backends to reject logs.
21+
some backends to reject logs when using OTLP exporter to output protobuf.
22+
([#6149](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6149))
23+
1924
## 1.11.1
2025

2126
Released 2025-Jan-22

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpTagWriter.cs

+7
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ protected override void OnUnsupportedTagDropped(
8181
tagValueTypeFullName,
8282
tagKey);
8383

84+
protected override bool TryWriteEmptyTag(ref OtlpTagWriterState state, string key, object? value)
85+
{
86+
state.WritePosition = ProtobufSerializer.WriteStringWithTag(state.Buffer, state.WritePosition, ProtobufOtlpCommonFieldNumberConstants.KeyValue_Key, key);
87+
state.WritePosition = ProtobufSerializer.WriteTagAndLength(state.Buffer, state.WritePosition, 0, ProtobufOtlpCommonFieldNumberConstants.KeyValue_Value, ProtobufWireType.LEN);
88+
return true;
89+
}
90+
8491
internal struct OtlpTagWriterState
8592
{
8693
public byte[] Buffer;

src/OpenTelemetry.Exporter.Zipkin/Implementation/ZipkinTagWriter.cs

+2
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,6 @@ protected override void OnUnsupportedTagDropped(
6464
tagValueTypeFullName,
6565
tagKey);
6666
}
67+
68+
protected override bool TryWriteEmptyTag(ref Utf8JsonWriter state, string key, object? value) => false;
6769
}

src/Shared/TagWriter/TagWriter.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public bool TryWriteTag(
3636
{
3737
if (value == null)
3838
{
39-
return false;
39+
return this.TryWriteEmptyTag(ref state, key, value);
4040
}
4141

4242
switch (value)
@@ -117,6 +117,8 @@ public bool TryWriteTag(
117117
return true;
118118
}
119119

120+
protected abstract bool TryWriteEmptyTag(ref TTagState state, string key, object? value);
121+
120122
protected abstract void WriteIntegralTag(ref TTagState state, string key, long value);
121123

122124
protected abstract void WriteFloatingPointTag(ref TTagState state, string key, double value);

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpAttributeTests.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ public class OtlpAttributeTests
1414
public void NullValueAttribute()
1515
{
1616
var kvp = new KeyValuePair<string, object?>("key", null);
17-
Assert.False(TryTransformTag(kvp, out _));
17+
Assert.True(TryTransformTag(kvp, out var attribute));
18+
Assert.Equal(OtlpCommon.AnyValue.ValueOneofCase.None, attribute.Value.ValueCase);
19+
Assert.False(attribute.Value.HasBoolValue);
20+
Assert.False(attribute.Value.HasBytesValue);
21+
Assert.False(attribute.Value.HasDoubleValue);
22+
Assert.False(attribute.Value.HasIntValue);
23+
Assert.False(attribute.Value.HasStringValue);
1824
}
1925

2026
[Fact]

test/OpenTelemetry.Tests/Internal/JsonStringArrayTagWriterTests.cs

+5
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ protected override void OnUnsupportedTagDropped(string tagKey, string tagValueTy
193193
{
194194
}
195195

196+
protected override bool TryWriteEmptyTag(ref Tag state, string key, object? value)
197+
{
198+
throw new NotImplementedException();
199+
}
200+
196201
public struct Tag
197202
{
198203
public string? Key;

0 commit comments

Comments
 (0)