Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit Tests for Logs #1613

Merged
4 changes: 2 additions & 2 deletions src/OpenTelemetry/Trace/SamplingDecision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public enum SamplingDecision
/// <summary>
/// The activity will be created and recorded, but sampling flag will not be set.
/// Activity.IsAllDataRequested will return true.
/// Activity.IsRecorded will return false.
/// Activity.Recorded will return false.
/// </summary>
RecordOnly,

/// <summary>
/// The activity will be created, recorded, and sampling flag will be set.
/// Activity.IsAllDataRequested will return true.
/// Activity.IsRecorded will return true.
/// Activity.Recorded will return true.
/// </summary>
RecordAndSample,
}
Expand Down
205 changes: 205 additions & 0 deletions test/OpenTelemetry.Tests/Logs/BatchExportLogRecordProcessorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
// <copyright file="BatchExportLogRecordProcessorTest.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

#if !NET452 && !NET46
#if NETCOREAPP2_1
using Microsoft.Extensions.DependencyInjection;
#endif
using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using Xunit;

namespace OpenTelemetry.Tests.Logs
{
public class BatchExportLogRecordProcessorTest
utpilla marked this conversation as resolved.
Show resolved Hide resolved
{
[Fact]
public void CheckNullExporter()
{
Assert.Throws<ArgumentNullException>(() => new BatchExportProcessor<LogRecord>(null));
}

[Fact]
public void CheckConstructorWithInvalidValues()
{
var exportedItems = new List<LogRecord>();
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<LogRecord>(new InMemoryExporter<LogRecord>(exportedItems), maxQueueSize: 0));
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<LogRecord>(new InMemoryExporter<LogRecord>(exportedItems), maxExportBatchSize: 0));
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<LogRecord>(new InMemoryExporter<LogRecord>(exportedItems), maxQueueSize: 1, maxExportBatchSize: 2049));
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<LogRecord>(new InMemoryExporter<LogRecord>(exportedItems), scheduledDelayMilliseconds: 0));
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<LogRecord>(new InMemoryExporter<LogRecord>(exportedItems), exporterTimeoutMilliseconds: -1));
}

[Fact]
public void CheckIfBatchIsExportingOnQueueLimit()
{
var exportedItems = new List<LogRecord>();
using var exporter = new InMemoryExporter<LogRecord>(exportedItems);
using var processor = new BatchExportProcessor<LogRecord>(
exporter,
maxQueueSize: 1,
maxExportBatchSize: 1,
scheduledDelayMilliseconds: 100_000);

#if NETCOREAPP2_1
var serviceCollection = new ServiceCollection().AddLogging(builder =>
#else
var loggerFactory = LoggerFactory.Create(builder =>
#endif
{
builder.AddOpenTelemetry(options => options
.AddProcessor(processor));
});

#if NETCOREAPP2_1
var serviceProvider = serviceCollection.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<BatchExportLogRecordProcessorTest>>();
#else
var logger = loggerFactory.CreateLogger<SimpleExportLogRecordProcessorTest>();
#endif

logger.LogInformation("Log");

for (int i = 0; i < 10 && exportedItems.Count == 0; i++)
{
Thread.Sleep(500);
}

Assert.Single(exportedItems);

Assert.Equal(1, processor.ProcessedCount);
Assert.Equal(1, processor.ReceivedCount);
Assert.Equal(0, processor.DroppedCount);
}

[Fact]
public void CheckForceFlushWithInvalidTimeout()
{
var exportedItems = new List<LogRecord>();
using var exporter = new InMemoryExporter<LogRecord>(exportedItems);
using var processor = new BatchExportProcessor<LogRecord>(exporter, maxQueueSize: 2, maxExportBatchSize: 1);
Assert.Throws<ArgumentOutOfRangeException>(() => processor.ForceFlush(-2));
}

[Theory]
[InlineData(Timeout.Infinite)]
[InlineData(0)]
[InlineData(1)]
public void CheckForceFlushExport(int timeout)
{
var exportedItems = new List<LogRecord>();
using var exporter = new InMemoryExporter<LogRecord>(exportedItems);
using var processor = new BatchExportProcessor<LogRecord>(
exporter,
maxQueueSize: 3,
maxExportBatchSize: 3,
exporterTimeoutMilliseconds: 30000);

#if NETCOREAPP2_1
var serviceCollection = new ServiceCollection().AddLogging(builder =>
#else
var loggerFactory = LoggerFactory.Create(builder =>
#endif
{
builder.AddOpenTelemetry(options => options
.AddProcessor(processor));
});

#if NETCOREAPP2_1
var serviceProvider = serviceCollection.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<BatchExportLogRecordProcessorTest>>();
#else
var logger = loggerFactory.CreateLogger<SimpleExportLogRecordProcessorTest>();
#endif

logger.LogInformation("Log1");
logger.LogInformation("Log2");

Assert.Equal(0, processor.ProcessedCount);

// waiting to see if time is triggering the exporter
Thread.Sleep(1_000);
Assert.Empty(exportedItems);

// forcing flush
processor.ForceFlush(timeout);

if (timeout == 0)
{
// ForceFlush(0) will trigger flush and return immediately, so let's sleep for a while
Thread.Sleep(1_000);
}

Assert.Equal(2, exportedItems.Count);

Assert.Equal(2, processor.ProcessedCount);
Assert.Equal(2, processor.ReceivedCount);
Assert.Equal(0, processor.DroppedCount);
}

[Theory]
[InlineData(Timeout.Infinite)]
[InlineData(0)]
[InlineData(1)]
public void CheckShutdownExport(int timeout)
{
var exportedItems = new List<LogRecord>();
using var exporter = new InMemoryExporter<LogRecord>(exportedItems);
using var processor = new BatchExportProcessor<LogRecord>(
exporter,
maxQueueSize: 3,
maxExportBatchSize: 3,
exporterTimeoutMilliseconds: 30000);

#if NETCOREAPP2_1
var serviceCollection = new ServiceCollection().AddLogging(builder =>
#else
var loggerFactory = LoggerFactory.Create(builder =>
#endif
{
builder.AddOpenTelemetry(options => options
.AddProcessor(processor));
});

#if NETCOREAPP2_1
var serviceProvider = serviceCollection.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<BatchExportLogRecordProcessorTest>>();
#else
var logger = loggerFactory.CreateLogger<SimpleExportLogRecordProcessorTest>();
#endif

logger.LogInformation("Log");
processor.Shutdown(timeout);

if (timeout == 0)
{
// Shutdown(0) will trigger flush and return immediately, so let's sleep for a while
Thread.Sleep(1_000);
}

Assert.Single(exportedItems);

Assert.Equal(1, processor.ProcessedCount);
Assert.Equal(1, processor.ReceivedCount);
Assert.Equal(0, processor.DroppedCount);
}
}
}
#endif
Loading