|
17 | 17 | using System; |
18 | 18 | using System.Collections.Generic; |
19 | 19 | using System.Diagnostics; |
| 20 | +using System.Linq; |
20 | 21 | using Microsoft.Extensions.DependencyInjection; |
21 | 22 | using Microsoft.Extensions.Hosting; |
22 | 23 | using Microsoft.Extensions.Logging; |
|
26 | 27 | using OpenTelemetry.Tests; |
27 | 28 | using OpenTelemetry.Trace; |
28 | 29 | using Xunit; |
| 30 | +using OtlpCommon = OpenTelemetry.Proto.Common.V1; |
29 | 31 | using OtlpLogs = OpenTelemetry.Proto.Logs.V1; |
30 | 32 |
|
31 | 33 | namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests |
@@ -458,5 +460,52 @@ public void CheckToOtlpLogRecordExceptionAttributes() |
458 | 460 | Assert.Contains(SemanticConventions.AttributeExceptionStacktrace, otlpLogRecordAttributes); |
459 | 461 | Assert.Contains(logRecord.Exception.ToInvariantString(), otlpLogRecordAttributes); |
460 | 462 | } |
| 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 | + } |
461 | 510 | } |
462 | 511 | } |
0 commit comments