Skip to content

Commit 8c1c149

Browse files
authored
Convert StateValues (when present) to Attributes. (#2554)
1 parent 74b01fd commit 8c1c149

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
* Changed `OtlpLogExporter` to convert `ILogger` structured log inputs to
4+
`Attributes` in OpenTelemetry (only active when `ParseStateValues` is `true`
5+
on `OpenTelemetryLoggerOptions`)
6+
37
## Unreleased
48

59
* Added validation that insecure channel is configured correctly when using

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/LogRecordExtensions.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,25 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord)
7676
otlpLogRecord.Body = new OtlpCommon.AnyValue { StringValue = logRecord.FormattedMessage };
7777
}
7878

79+
if (logRecord.StateValues != null)
80+
{
81+
foreach (var stateValue in logRecord.StateValues)
82+
{
83+
var otlpAttribute = stateValue.ToOtlpAttribute();
84+
otlpLogRecord.Attributes.Add(otlpAttribute);
85+
}
86+
}
87+
7988
if (logRecord.EventId != 0)
8089
{
81-
otlpLogRecord.Attributes.AddAttribute(nameof(logRecord.EventId), logRecord.EventId.ToString());
90+
otlpLogRecord.Attributes.AddStringAttribute(nameof(logRecord.EventId), logRecord.EventId.ToString());
8291
}
8392

8493
if (logRecord.Exception != null)
8594
{
86-
otlpLogRecord.Attributes.AddAttribute(SemanticConventions.AttributeExceptionType, logRecord.Exception.GetType().Name);
87-
otlpLogRecord.Attributes.AddAttribute(SemanticConventions.AttributeExceptionMessage, logRecord.Exception.Message);
88-
otlpLogRecord.Attributes.AddAttribute(SemanticConventions.AttributeExceptionStacktrace, logRecord.Exception.ToInvariantString());
95+
otlpLogRecord.Attributes.AddStringAttribute(SemanticConventions.AttributeExceptionType, logRecord.Exception.GetType().Name);
96+
otlpLogRecord.Attributes.AddStringAttribute(SemanticConventions.AttributeExceptionMessage, logRecord.Exception.Message);
97+
otlpLogRecord.Attributes.AddStringAttribute(SemanticConventions.AttributeExceptionStacktrace, logRecord.Exception.ToInvariantString());
8998
}
9099

91100
if (logRecord.TraceId != default && logRecord.SpanId != default)
@@ -107,7 +116,7 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord)
107116
return otlpLogRecord;
108117
}
109118

110-
private static void AddAttribute(this RepeatedField<OtlpCommon.KeyValue> repeatedField, string key, string value)
119+
private static void AddStringAttribute(this RepeatedField<OtlpCommon.KeyValue> repeatedField, string key, string value)
111120
{
112121
repeatedField.Add(new OtlpCommon.KeyValue
113122
{

0 commit comments

Comments
 (0)