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); } diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs index 3ac1e8e7199..eeb91476970 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,52 @@ 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 NotSupportedException("I'm the exception message."), "Exception Occurred"); + + var logRecord = logRecords[0]; + var otlpLogRecord = logRecord.ToOtlpLog(sdkLimitOptions); + + Assert.NotNull(otlpLogRecord); + Assert.Equal(1u, otlpLogRecord.DroppedAttributesCount); + + var exceptionTypeAtt = TryGetAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionType); + Assert.NotNull(exceptionTypeAtt); + + // "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); + Assert.Null(exceptionStackTraceAtt); + } + + private static OtlpCommon.KeyValue TryGetAttribute(OtlpLogs.LogRecord record, string key) + { + return record.Attributes.FirstOrDefault(att => att.Key == key); + } } }