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

[integration test] otlp log exporter #4854

2 changes: 1 addition & 1 deletion examples/Console/TestLogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ internal static object Run(LogsOptions options)
{
processorType = ExportProcessorType.Batch;
}
else if (options.Protocol.Trim().ToLower().Equals("simple"))
else if (options.ProcessorType.Trim().ToLower().Equals("simple"))
{
processorType = ExportProcessorType.Simple;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,49 +59,46 @@ public static OpenTelemetryLoggerOptions AddOtlpExporter(

configureExporterAndProcessor?.Invoke(exporterOptions, processorOptions);

return AddOtlpLogExporterInternal(
loggerOptions,
exporterOptions: exporterOptions,
processorOptions: processorOptions);
return loggerOptions.AddProcessor(BuildOtlpLogExporter(exporterOptions, processorOptions));
}

private static OpenTelemetryLoggerOptions AddOtlpExporterInternal(
internal static OpenTelemetryLoggerOptions AddOtlpExporterInternal(
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
OpenTelemetryLoggerOptions loggerOptions,
Action<OtlpExporterOptions> configure)
{
var exporterOptions = new OtlpExporterOptions();

configure?.Invoke(exporterOptions);

return AddOtlpLogExporterInternal(
loggerOptions,
exporterOptions: exporterOptions,
processorOptions: new());
return loggerOptions.AddProcessor(BuildOtlpLogExporter(exporterOptions, new()));
}

private static OpenTelemetryLoggerOptions AddOtlpLogExporterInternal(
OpenTelemetryLoggerOptions loggerOptions,
internal static BaseProcessor<LogRecord> BuildOtlpLogExporter(
OtlpExporterOptions exporterOptions,
LogRecordExportProcessorOptions processorOptions)
LogRecordExportProcessorOptions processorOptions,
Func<BaseExporter<LogRecord>, BaseExporter<LogRecord>> configureExporterInstance = null)
{
var otlpExporter = new OtlpLogExporter(exporterOptions);
BaseExporter<LogRecord> otlpExporter = new OtlpLogExporter(exporterOptions);

if (configureExporterInstance != null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Simplify this by using configureExporterInstance?.Invoke(otlpExporter).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment that this option is only used by tests.

{
otlpExporter = configureExporterInstance(otlpExporter);
}

if (processorOptions.ExportProcessorType == ExportProcessorType.Simple)
{
loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(otlpExporter));
return new SimpleLogRecordExportProcessor(otlpExporter);
}
else
{
var batchOptions = processorOptions.BatchExportProcessorOptions;

loggerOptions.AddProcessor(new BatchLogRecordExportProcessor(
return new BatchLogRecordExportProcessor(
otlpExporter,
batchOptions.MaxQueueSize,
batchOptions.ScheduledDelayMilliseconds,
batchOptions.ExporterTimeoutMilliseconds,
batchOptions.MaxExportBatchSize));
batchOptions.MaxExportBatchSize);
}

return loggerOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Diagnostics.Tracing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
Expand Down Expand Up @@ -210,6 +213,73 @@ public void MetricExportResultIsSuccess(OtlpExportProtocol protocol, string endp
}
}

[InlineData(OtlpExportProtocol.Grpc, ":4317", ExportProcessorType.Batch)]
[InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/logs", ExportProcessorType.Batch)]
[InlineData(OtlpExportProtocol.Grpc, ":4317", ExportProcessorType.Simple)]
[InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/logs", ExportProcessorType.Simple)]
[InlineData(OtlpExportProtocol.Grpc, ":5317", ExportProcessorType.Simple, "https")]
[InlineData(OtlpExportProtocol.HttpProtobuf, ":5318/v1/logs", ExportProcessorType.Simple, "https")]
[Trait("CategoryName", "CollectorIntegrationTests")]
[SkipUnlessEnvVarFoundTheory(CollectorHostnameEnvVarName)]
public void LogExportResultIsSuccess(OtlpExportProtocol protocol, string endpoint, ExportProcessorType exportProcessorType, string scheme = "http")
{
using EventWaitHandle handle = new ManualResetEvent(false);

var exporterOptions = new OtlpExporterOptions
{
Endpoint = new Uri($"{scheme}://{CollectorHostname}{endpoint}"),
Protocol = protocol,
};

DelegatingExporter<LogRecord> delegatingExporter = null;
var exportResults = new List<ExportResult>();
var processorOptions = new LogRecordExportProcessorOptions();
processorOptions.ExportProcessorType = exportProcessorType;
processorOptions.BatchExportProcessorOptions = new()
{
ScheduledDelayMilliseconds = ExportIntervalMilliseconds,
};

using var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddOpenTelemetry(options => options
.AddProcessor(OtlpLogExporterHelperExtensions.BuildOtlpLogExporter(
exporterOptions,
processorOptions,
configureExporterInstance: otlpExporter =>
{
delegatingExporter = new DelegatingExporter<LogRecord>
{
OnExportFunc = (batch) =>
{
var result = otlpExporter.Export(batch);
exportResults.Add(result);
handle.Set();
return result;
},
};
return delegatingExporter;
})));
});

var logger = loggerFactory.CreateLogger("OtlpLogExporterTests");
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);

if (exporterOptions.ExportProcessorType == ExportProcessorType.Batch)
{
Assert.True(handle.WaitOne(ExportIntervalMilliseconds * 2));
Assert.Single(exportResults);
Assert.Equal(ExportResult.Success, exportResults[0]);
}

if (exportProcessorType == ExportProcessorType.Simple)
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
{
Assert.Single(exportResults);
Assert.Equal(ExportResult.Success, exportResults[0]);
}
}

[Trait("CategoryName", "CollectorIntegrationTests")]
[SkipUnlessEnvVarFoundFact(CollectorHostnameEnvVarName)]
public void ConstructingGrpcExporterFailsWhenHttp2UnencryptedSupportIsDisabledForNetcoreapp31()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ service:
metrics:
receivers: [otlp, otlp/tls]
exporters: [logging]
logs:
receivers: [otlp, otlp/tls]
exporters: [logging]