Convert StateValues (when present) to Attributes. (#2554)

This commit is contained in:
ndrwrbgs 2022-01-07 09:35:06 -08:00 committed by GitHub
parent 74b01fdf80
commit 8c1c1495d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 5 deletions

View File

@ -1,5 +1,9 @@
# Changelog
* Changed `OtlpLogExporter` to convert `ILogger` structured log inputs to
`Attributes` in OpenTelemetry (only active when `ParseStateValues` is `true`
on `OpenTelemetryLoggerOptions`)
## Unreleased
* Added validation that insecure channel is configured correctly when using

View File

@ -76,16 +76,25 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation
otlpLogRecord.Body = new OtlpCommon.AnyValue { StringValue = logRecord.FormattedMessage };
}
if (logRecord.StateValues != null)
{
foreach (var stateValue in logRecord.StateValues)
{
var otlpAttribute = stateValue.ToOtlpAttribute();
otlpLogRecord.Attributes.Add(otlpAttribute);
}
}
if (logRecord.EventId != 0)
{
otlpLogRecord.Attributes.AddAttribute(nameof(logRecord.EventId), logRecord.EventId.ToString());
otlpLogRecord.Attributes.AddStringAttribute(nameof(logRecord.EventId), logRecord.EventId.ToString());
}
if (logRecord.Exception != null)
{
otlpLogRecord.Attributes.AddAttribute(SemanticConventions.AttributeExceptionType, logRecord.Exception.GetType().Name);
otlpLogRecord.Attributes.AddAttribute(SemanticConventions.AttributeExceptionMessage, logRecord.Exception.Message);
otlpLogRecord.Attributes.AddAttribute(SemanticConventions.AttributeExceptionStacktrace, logRecord.Exception.ToInvariantString());
otlpLogRecord.Attributes.AddStringAttribute(SemanticConventions.AttributeExceptionType, logRecord.Exception.GetType().Name);
otlpLogRecord.Attributes.AddStringAttribute(SemanticConventions.AttributeExceptionMessage, logRecord.Exception.Message);
otlpLogRecord.Attributes.AddStringAttribute(SemanticConventions.AttributeExceptionStacktrace, logRecord.Exception.ToInvariantString());
}
if (logRecord.TraceId != default && logRecord.SpanId != default)
@ -107,7 +116,7 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation
return otlpLogRecord;
}
private static void AddAttribute(this RepeatedField<OtlpCommon.KeyValue> repeatedField, string key, string value)
private static void AddStringAttribute(this RepeatedField<OtlpCommon.KeyValue> repeatedField, string key, string value)
{
repeatedField.Add(new OtlpCommon.KeyValue
{