|
18 | 18 | using Microsoft.Extensions.DependencyInjection; |
19 | 19 | using Microsoft.Extensions.Hosting; |
20 | 20 | using Microsoft.Extensions.Logging; |
| 21 | +using Moq; |
21 | 22 | using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation; |
| 23 | +using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient; |
22 | 24 | using OpenTelemetry.Internal; |
23 | 25 | using OpenTelemetry.Logs; |
24 | 26 | using OpenTelemetry.Tests; |
25 | 27 | using OpenTelemetry.Trace; |
26 | 28 | using Xunit; |
| 29 | +using OtlpCollector = OpenTelemetry.Proto.Collector.Logs.V1; |
27 | 30 | using OtlpCommon = OpenTelemetry.Proto.Common.V1; |
28 | 31 | using OtlpLogs = OpenTelemetry.Proto.Logs.V1; |
29 | 32 |
|
@@ -500,6 +503,69 @@ public void CheckToOtlpLogRecordRespectsAttributeLimits() |
500 | 503 | Assert.Null(exceptionStackTraceAtt); |
501 | 504 | } |
502 | 505 |
|
| 506 | + [Fact] |
| 507 | + public void Export_WhenExportClientIsProvidedInCtor_UsesProvidedExportClient() |
| 508 | + { |
| 509 | + // Arrange. |
| 510 | + var fakeExportClient = new Mock<IExportClient<OtlpCollector.ExportLogsServiceRequest>>(); |
| 511 | + var emptyLogRecords = Array.Empty<LogRecord>(); |
| 512 | + var emptyBatch = new Batch<LogRecord>(emptyLogRecords, emptyLogRecords.Length); |
| 513 | + var sut = new OtlpLogExporter( |
| 514 | + new OtlpExporterOptions(), |
| 515 | + new SdkLimitOptions(), |
| 516 | + fakeExportClient.Object); |
| 517 | + |
| 518 | + // Act. |
| 519 | + var result = sut.Export(emptyBatch); |
| 520 | + |
| 521 | + // Assert. |
| 522 | + fakeExportClient.Verify(x => x.SendExportRequest(It.IsAny<OtlpCollector.ExportLogsServiceRequest>(), default), Times.Once()); |
| 523 | + } |
| 524 | + |
| 525 | + [Fact] |
| 526 | + public void Export_WhenExportClientThrowsException_ReturnsExportResultFailure() |
| 527 | + { |
| 528 | + // Arrange. |
| 529 | + var fakeExportClient = new Mock<IExportClient<OtlpCollector.ExportLogsServiceRequest>>(); |
| 530 | + var emptyLogRecords = Array.Empty<LogRecord>(); |
| 531 | + var emptyBatch = new Batch<LogRecord>(emptyLogRecords, emptyLogRecords.Length); |
| 532 | + fakeExportClient |
| 533 | + .Setup(_ => _.SendExportRequest(It.IsAny<OtlpCollector.ExportLogsServiceRequest>(), default)) |
| 534 | + .Throws(new Exception("Test Exception")); |
| 535 | + var sut = new OtlpLogExporter( |
| 536 | + new OtlpExporterOptions(), |
| 537 | + new SdkLimitOptions(), |
| 538 | + fakeExportClient.Object); |
| 539 | + |
| 540 | + // Act. |
| 541 | + var result = sut.Export(emptyBatch); |
| 542 | + |
| 543 | + // Assert. |
| 544 | + Assert.Equal(ExportResult.Failure, result); |
| 545 | + } |
| 546 | + |
| 547 | + [Fact] |
| 548 | + public void Export_WhenExportIsSuccessful_ReturnsExportResultSuccess() |
| 549 | + { |
| 550 | + // Arrange. |
| 551 | + var fakeExportClient = new Mock<IExportClient<OtlpCollector.ExportLogsServiceRequest>>(); |
| 552 | + var emptyLogRecords = Array.Empty<LogRecord>(); |
| 553 | + var emptyBatch = new Batch<LogRecord>(emptyLogRecords, emptyLogRecords.Length); |
| 554 | + fakeExportClient |
| 555 | + .Setup(_ => _.SendExportRequest(It.IsAny<OtlpCollector.ExportLogsServiceRequest>(), default)) |
| 556 | + .Returns(true); |
| 557 | + var sut = new OtlpLogExporter( |
| 558 | + new OtlpExporterOptions(), |
| 559 | + new SdkLimitOptions(), |
| 560 | + fakeExportClient.Object); |
| 561 | + |
| 562 | + // Act. |
| 563 | + var result = sut.Export(emptyBatch); |
| 564 | + |
| 565 | + // Assert. |
| 566 | + Assert.Equal(ExportResult.Success, result); |
| 567 | + } |
| 568 | + |
503 | 569 | private static OtlpCommon.KeyValue TryGetAttribute(OtlpLogs.LogRecord record, string key) |
504 | 570 | { |
505 | 571 | return record.Attributes.FirstOrDefault(att => att.Key == key); |
|
0 commit comments