Skip to content

Commit 7cb92d3

Browse files
[integration test] otlp log exporter (#4854)
1 parent 224a4cc commit 7cb92d3

File tree

4 files changed

+99
-27
lines changed

4 files changed

+99
-27
lines changed

examples/Console/TestLogs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ internal static object Run(LogsOptions options)
7979
{
8080
processorType = ExportProcessorType.Batch;
8181
}
82-
else if (options.Protocol.Trim().ToLower().Equals("simple"))
82+
else if (options.ProcessorType.Trim().ToLower().Equals("simple"))
8383
{
8484
processorType = ExportProcessorType.Simple;
8585
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporterHelperExtensions.cs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,49 +59,46 @@ public static OpenTelemetryLoggerOptions AddOtlpExporter(
5959

6060
configureExporterAndProcessor?.Invoke(exporterOptions, processorOptions);
6161

62-
return AddOtlpLogExporterInternal(
63-
loggerOptions,
64-
exporterOptions: exporterOptions,
65-
processorOptions: processorOptions);
62+
return loggerOptions.AddProcessor(BuildOtlpLogExporter(exporterOptions, processorOptions));
6663
}
6764

68-
private static OpenTelemetryLoggerOptions AddOtlpExporterInternal(
69-
OpenTelemetryLoggerOptions loggerOptions,
70-
Action<OtlpExporterOptions> configure)
71-
{
72-
var exporterOptions = new OtlpExporterOptions();
73-
74-
configure?.Invoke(exporterOptions);
75-
76-
return AddOtlpLogExporterInternal(
77-
loggerOptions,
78-
exporterOptions: exporterOptions,
79-
processorOptions: new());
80-
}
81-
82-
private static OpenTelemetryLoggerOptions AddOtlpLogExporterInternal(
83-
OpenTelemetryLoggerOptions loggerOptions,
65+
internal static BaseProcessor<LogRecord> BuildOtlpLogExporter(
8466
OtlpExporterOptions exporterOptions,
85-
LogRecordExportProcessorOptions processorOptions)
67+
LogRecordExportProcessorOptions processorOptions,
68+
Func<BaseExporter<LogRecord>, BaseExporter<LogRecord>> configureExporterInstance = null)
8669
{
87-
var otlpExporter = new OtlpLogExporter(exporterOptions);
70+
BaseExporter<LogRecord> otlpExporter = new OtlpLogExporter(exporterOptions);
71+
72+
if (configureExporterInstance != null)
73+
{
74+
otlpExporter = configureExporterInstance(otlpExporter);
75+
}
8876

8977
if (processorOptions.ExportProcessorType == ExportProcessorType.Simple)
9078
{
91-
loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(otlpExporter));
79+
return new SimpleLogRecordExportProcessor(otlpExporter);
9280
}
9381
else
9482
{
9583
var batchOptions = processorOptions.BatchExportProcessorOptions;
9684

97-
loggerOptions.AddProcessor(new BatchLogRecordExportProcessor(
85+
return new BatchLogRecordExportProcessor(
9886
otlpExporter,
9987
batchOptions.MaxQueueSize,
10088
batchOptions.ScheduledDelayMilliseconds,
10189
batchOptions.ExporterTimeoutMilliseconds,
102-
batchOptions.MaxExportBatchSize));
90+
batchOptions.MaxExportBatchSize);
10391
}
92+
}
93+
94+
private static OpenTelemetryLoggerOptions AddOtlpExporterInternal(
95+
OpenTelemetryLoggerOptions loggerOptions,
96+
Action<OtlpExporterOptions> configure)
97+
{
98+
var exporterOptions = new OtlpExporterOptions();
99+
100+
configure?.Invoke(exporterOptions);
104101

105-
return loggerOptions;
102+
return loggerOptions.AddProcessor(BuildOtlpLogExporter(exporterOptions, new()));
106103
}
107104
}

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/IntegrationTests.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
using System.Diagnostics;
1818
using System.Diagnostics.Metrics;
1919
using System.Diagnostics.Tracing;
20+
using Microsoft.Extensions.DependencyInjection;
21+
using Microsoft.Extensions.Logging;
2022
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
23+
using OpenTelemetry.Logs;
2124
using OpenTelemetry.Metrics;
2225
using OpenTelemetry.Tests;
2326
using OpenTelemetry.Trace;
@@ -210,6 +213,75 @@ public void MetricExportResultIsSuccess(OtlpExportProtocol protocol, string endp
210213
}
211214
}
212215

216+
[InlineData(OtlpExportProtocol.Grpc, ":4317", ExportProcessorType.Batch)]
217+
[InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/logs", ExportProcessorType.Batch)]
218+
[InlineData(OtlpExportProtocol.Grpc, ":4317", ExportProcessorType.Simple)]
219+
[InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/logs", ExportProcessorType.Simple)]
220+
[InlineData(OtlpExportProtocol.Grpc, ":5317", ExportProcessorType.Simple, "https")]
221+
[InlineData(OtlpExportProtocol.HttpProtobuf, ":5318/v1/logs", ExportProcessorType.Simple, "https")]
222+
[Trait("CategoryName", "CollectorIntegrationTests")]
223+
[SkipUnlessEnvVarFoundTheory(CollectorHostnameEnvVarName)]
224+
public void LogExportResultIsSuccess(OtlpExportProtocol protocol, string endpoint, ExportProcessorType exportProcessorType, string scheme = "http")
225+
{
226+
using EventWaitHandle handle = new ManualResetEvent(false);
227+
228+
var exporterOptions = new OtlpExporterOptions
229+
{
230+
Endpoint = new Uri($"{scheme}://{CollectorHostname}{endpoint}"),
231+
Protocol = protocol,
232+
};
233+
234+
DelegatingExporter<LogRecord> delegatingExporter = null;
235+
var exportResults = new List<ExportResult>();
236+
var processorOptions = new LogRecordExportProcessorOptions();
237+
processorOptions.ExportProcessorType = exportProcessorType;
238+
processorOptions.BatchExportProcessorOptions = new()
239+
{
240+
ScheduledDelayMilliseconds = ExportIntervalMilliseconds,
241+
};
242+
243+
using var loggerFactory = LoggerFactory.Create(builder =>
244+
{
245+
builder
246+
.AddOpenTelemetry(options => options
247+
.AddProcessor(OtlpLogExporterHelperExtensions.BuildOtlpLogExporter(
248+
exporterOptions,
249+
processorOptions,
250+
configureExporterInstance: otlpExporter =>
251+
{
252+
delegatingExporter = new DelegatingExporter<LogRecord>
253+
{
254+
OnExportFunc = (batch) =>
255+
{
256+
var result = otlpExporter.Export(batch);
257+
exportResults.Add(result);
258+
handle.Set();
259+
return result;
260+
},
261+
};
262+
return delegatingExporter;
263+
})));
264+
});
265+
266+
var logger = loggerFactory.CreateLogger("OtlpLogExporterTests");
267+
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
268+
269+
switch (processorOptions.ExportProcessorType)
270+
{
271+
case ExportProcessorType.Batch:
272+
Assert.True(handle.WaitOne(ExportIntervalMilliseconds * 2));
273+
Assert.Single(exportResults);
274+
Assert.Equal(ExportResult.Success, exportResults[0]);
275+
break;
276+
case ExportProcessorType.Simple:
277+
Assert.Single(exportResults);
278+
Assert.Equal(ExportResult.Success, exportResults[0]);
279+
break;
280+
default:
281+
throw new NotSupportedException("Unexpected processor type encountered.");
282+
}
283+
}
284+
213285
[Trait("CategoryName", "CollectorIntegrationTests")]
214286
[SkipUnlessEnvVarFoundFact(CollectorHostnameEnvVarName)]
215287
public void ConstructingGrpcExporterFailsWhenHttp2UnencryptedSupportIsDisabledForNetcoreapp31()

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/otel-collector-config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ service:
3636
metrics:
3737
receivers: [otlp, otlp/tls]
3838
exporters: [logging]
39+
logs:
40+
receivers: [otlp, otlp/tls]
41+
exporters: [logging]

0 commit comments

Comments
 (0)