Skip to content

Commit f963f9d

Browse files
Jonathan WilhelmCodeBlanch
andauthored
[Logs] UnitTest: LogRecord attribute limits (#3758)
* Unittest for LogRecord attribute limits * Remove maxValueLength from LogRecordExtensions.AddIntAttribute. Change requested from #3684 (comment) * Pr commits addressed Co-authored-by: Mikel Blanchard <[email protected]>
1 parent 6fae370 commit f963f9d

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord, SdkLimitO
9292

9393
if (logRecord.EventId.Id != default)
9494
{
95-
otlpLogRecord.AddIntAttribute(nameof(logRecord.EventId.Id), logRecord.EventId.Id, attributeValueLengthLimit, attributeCountLimit);
95+
otlpLogRecord.AddIntAttribute(nameof(logRecord.EventId.Id), logRecord.EventId.Id, attributeCountLimit);
9696
}
9797

9898
if (!string.IsNullOrEmpty(logRecord.EventId.Name))
@@ -193,10 +193,10 @@ private static void AddStringAttribute(this OtlpLogs.LogRecord logRecord, string
193193
}
194194

195195
[MethodImpl(MethodImplOptions.AggressiveInlining)]
196-
private static void AddIntAttribute(this OtlpLogs.LogRecord logRecord, string key, int value, int? maxValueLength, int maxAttributeCount)
196+
private static void AddIntAttribute(this OtlpLogs.LogRecord logRecord, string key, int value, int maxAttributeCount)
197197
{
198198
var attributeItem = new KeyValuePair<string, object>(key, value);
199-
if (OtlpKeyValueTransformer.Instance.TryTransformTag(attributeItem, out var result, maxValueLength))
199+
if (OtlpKeyValueTransformer.Instance.TryTransformTag(attributeItem, out var result))
200200
{
201201
logRecord.AddAttribute(result, maxAttributeCount);
202202
}

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System;
1818
using System.Collections.Generic;
1919
using System.Diagnostics;
20+
using System.Linq;
2021
using Microsoft.Extensions.DependencyInjection;
2122
using Microsoft.Extensions.Hosting;
2223
using Microsoft.Extensions.Logging;
@@ -26,6 +27,7 @@
2627
using OpenTelemetry.Tests;
2728
using OpenTelemetry.Trace;
2829
using Xunit;
30+
using OtlpCommon = OpenTelemetry.Proto.Common.V1;
2931
using OtlpLogs = OpenTelemetry.Proto.Logs.V1;
3032

3133
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests
@@ -458,5 +460,52 @@ public void CheckToOtlpLogRecordExceptionAttributes()
458460
Assert.Contains(SemanticConventions.AttributeExceptionStacktrace, otlpLogRecordAttributes);
459461
Assert.Contains(logRecord.Exception.ToInvariantString(), otlpLogRecordAttributes);
460462
}
463+
464+
[Fact]
465+
public void CheckToOtlpLogRecordRespectsAttributeLimits()
466+
{
467+
var sdkLimitOptions = new SdkLimitOptions
468+
{
469+
AttributeCountLimit = 3, // 3 => LogCategory, exception.type and exception.message
470+
AttributeValueLengthLimit = 8,
471+
};
472+
473+
var logRecords = new List<LogRecord>();
474+
using var loggerFactory = LoggerFactory.Create(builder =>
475+
{
476+
builder.AddOpenTelemetry(options =>
477+
{
478+
options.AddInMemoryExporter(logRecords);
479+
});
480+
});
481+
482+
var logger = loggerFactory.CreateLogger("OtlpLogExporterTests");
483+
logger.LogInformation(new NotSupportedException("I'm the exception message."), "Exception Occurred");
484+
485+
var logRecord = logRecords[0];
486+
var otlpLogRecord = logRecord.ToOtlpLog(sdkLimitOptions);
487+
488+
Assert.NotNull(otlpLogRecord);
489+
Assert.Equal(1u, otlpLogRecord.DroppedAttributesCount);
490+
491+
var exceptionTypeAtt = TryGetAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionType);
492+
Assert.NotNull(exceptionTypeAtt);
493+
494+
// "NotSuppo" == first 8 chars from the exception typename "NotSupportedException"
495+
Assert.Equal("NotSuppo", exceptionTypeAtt.Value.StringValue);
496+
var exceptionMessageAtt = TryGetAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionMessage);
497+
Assert.NotNull(exceptionMessageAtt);
498+
499+
// "I'm the " == first 8 chars from the exception message
500+
Assert.Equal("I'm the ", exceptionMessageAtt.Value.StringValue);
501+
502+
var exceptionStackTraceAtt = TryGetAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionStacktrace);
503+
Assert.Null(exceptionStackTraceAtt);
504+
}
505+
506+
private static OtlpCommon.KeyValue TryGetAttribute(OtlpLogs.LogRecord record, string key)
507+
{
508+
return record.Attributes.FirstOrDefault(att => att.Key == key);
509+
}
461510
}
462511
}

0 commit comments

Comments
 (0)