From 9cf398b9c47de2389bab341f11affc41ade060dd Mon Sep 17 00:00:00 2001 From: Jonathan Wilhelm Date: Wed, 12 Oct 2022 16:26:10 +0200 Subject: [PATCH 1/3] Unittest for LogRecord attribute limits --- .../OtlpLogExporterTests.cs | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs index 3ac1e8e7199..1b95d0321bf 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs @@ -17,6 +17,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -26,6 +27,7 @@ using OpenTelemetry.Tests; using OpenTelemetry.Trace; using Xunit; +using OtlpCommon = OpenTelemetry.Proto.Common.V1; using OtlpLogs = OpenTelemetry.Proto.Logs.V1; namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests @@ -458,5 +460,48 @@ public void CheckToOtlpLogRecordExceptionAttributes() Assert.Contains(SemanticConventions.AttributeExceptionStacktrace, otlpLogRecordAttributes); Assert.Contains(logRecord.Exception.ToInvariantString(), otlpLogRecordAttributes); } + + [Fact] + public void CheckToOtlpLogRecordRespectsAttributeLimits() + { + var sdkLimitOptions = new SdkLimitOptions + { + AttributeCountLimit = 3, // 3 => LogCategory, exception.type and exception.message + AttributeValueLengthLimit = 8, + }; + + var logRecords = new List(); + using var loggerFactory = LoggerFactory.Create(builder => + { + builder.AddOpenTelemetry(options => + { + options.AddInMemoryExporter(logRecords); + }); + }); + + var logger = loggerFactory.CreateLogger("OtlpLogExporterTests"); + logger.LogInformation(new Exception("I'm the exception message."), "Exception Occurred"); + + var logRecord = logRecords[0]; + var otlpLogRecord = logRecord.ToOtlpLog(sdkLimitOptions); + + Assert.NotNull(otlpLogRecord); + Assert.True(otlpLogRecord.DroppedAttributesCount > 0, "Attributes dropped count is unset"); + + var exceptionTypeAtt = TryGetAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionType); + Assert.NotNull(exceptionTypeAtt); + Assert.Equal("Exceptio", exceptionTypeAtt.Value.StringValue); + var exceptionMessageAtt = TryGetAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionMessage); + Assert.NotNull(exceptionMessageAtt); + Assert.Equal("I'm the ", exceptionMessageAtt.Value.StringValue); + + var exceptionStackTraceAtt = TryGetAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionStacktrace); + Assert.Null(exceptionStackTraceAtt); + } + + private static OtlpCommon.KeyValue TryGetAttribute(OtlpLogs.LogRecord record, string key) + { + return record.Attributes.FirstOrDefault(att => att.Key == key); + } } } From ecc330f1c9901b789cd2dd22edc7a87df7eb9c3f Mon Sep 17 00:00:00 2001 From: Jonathan Wilhelm Date: Fri, 14 Oct 2022 08:41:04 +0200 Subject: [PATCH 2/3] Remove maxValueLength from LogRecordExtensions.AddIntAttribute. Change requested from https://github.com/open-telemetry/opentelemetry-dotnet/pull/3684#discussion_r995164277 --- .../Implementation/LogRecordExtensions.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/LogRecordExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/LogRecordExtensions.cs index b7d289f8f28..71811bded0f 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/LogRecordExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/LogRecordExtensions.cs @@ -92,7 +92,7 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord, SdkLimitO if (logRecord.EventId.Id != default) { - otlpLogRecord.AddIntAttribute(nameof(logRecord.EventId.Id), logRecord.EventId.Id, attributeValueLengthLimit, attributeCountLimit); + otlpLogRecord.AddIntAttribute(nameof(logRecord.EventId.Id), logRecord.EventId.Id, attributeCountLimit); } if (!string.IsNullOrEmpty(logRecord.EventId.Name)) @@ -193,10 +193,10 @@ private static void AddStringAttribute(this OtlpLogs.LogRecord logRecord, string } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void AddIntAttribute(this OtlpLogs.LogRecord logRecord, string key, int value, int? maxValueLength, int maxAttributeCount) + private static void AddIntAttribute(this OtlpLogs.LogRecord logRecord, string key, int value, int maxAttributeCount) { var attributeItem = new KeyValuePair(key, value); - if (OtlpKeyValueTransformer.Instance.TryTransformTag(attributeItem, out var result, maxValueLength)) + if (OtlpKeyValueTransformer.Instance.TryTransformTag(attributeItem, out var result)) { logRecord.AddAttribute(result, maxAttributeCount); } From 7b91cc67e5235555153480bde86e492972b4417f Mon Sep 17 00:00:00 2001 From: Jonathan Wilhelm Date: Wed, 19 Oct 2022 08:21:16 +0200 Subject: [PATCH 3/3] Pr commits addressed --- .../OtlpLogExporterTests.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs index 1b95d0321bf..eeb91476970 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs @@ -480,19 +480,23 @@ public void CheckToOtlpLogRecordRespectsAttributeLimits() }); var logger = loggerFactory.CreateLogger("OtlpLogExporterTests"); - logger.LogInformation(new Exception("I'm the exception message."), "Exception Occurred"); + logger.LogInformation(new NotSupportedException("I'm the exception message."), "Exception Occurred"); var logRecord = logRecords[0]; var otlpLogRecord = logRecord.ToOtlpLog(sdkLimitOptions); Assert.NotNull(otlpLogRecord); - Assert.True(otlpLogRecord.DroppedAttributesCount > 0, "Attributes dropped count is unset"); + Assert.Equal(1u, otlpLogRecord.DroppedAttributesCount); var exceptionTypeAtt = TryGetAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionType); Assert.NotNull(exceptionTypeAtt); - Assert.Equal("Exceptio", exceptionTypeAtt.Value.StringValue); + + // "NotSuppo" == first 8 chars from the exception typename "NotSupportedException" + Assert.Equal("NotSuppo", exceptionTypeAtt.Value.StringValue); var exceptionMessageAtt = TryGetAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionMessage); Assert.NotNull(exceptionMessageAtt); + + // "I'm the " == first 8 chars from the exception message Assert.Equal("I'm the ", exceptionMessageAtt.Value.StringValue); var exceptionStackTraceAtt = TryGetAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionStacktrace);