Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
OpenTelemetryLoggerOptions loggerOptions,
Action<OtlpExporterOptions> configure)
{
var exporterOptions = new OtlpExporterOptions();

configure?.Invoke(exporterOptions);

return AddOtlpLogExporterInternal(
loggerOptions,
exporterOptions: exporterOptions,
processorOptions: 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);
}
}

private static OpenTelemetryLoggerOptions AddOtlpExporterInternal(
OpenTelemetryLoggerOptions loggerOptions,
Action<OtlpExporterOptions> configure)
{
var exporterOptions = new OtlpExporterOptions();

configure?.Invoke(exporterOptions);

return loggerOptions;
return loggerOptions.AddProcessor(BuildOtlpLogExporter(exporterOptions, new()));
}
}
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,75 @@ 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);

switch (processorOptions.ExportProcessorType)
{
case ExportProcessorType.Batch:
Assert.True(handle.WaitOne(ExportIntervalMilliseconds * 2));
Assert.Single(exportResults);
Assert.Equal(ExportResult.Success, exportResults[0]);
break;
case ExportProcessorType.Simple:
Assert.Single(exportResults);
Assert.Equal(ExportResult.Success, exportResults[0]);
break;
default:
throw new NotSupportedException("Unexpected processor type encountered.");
}
}

[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]