use of io.opentelemetry.sdk.logs.SdkLogEmitterProvider in project opentelemetry-java by open-telemetry.
the class AutoConfiguredOpenTelemetrySdkBuilder method build.
/**
* Returns a new {@link AutoConfiguredOpenTelemetrySdk} holding components auto-configured using
* the settings of this {@link AutoConfiguredOpenTelemetrySdkBuilder}.
*/
public AutoConfiguredOpenTelemetrySdk build() {
if (!customized) {
customized = true;
mergeSdkTracerProviderConfigurer();
for (AutoConfigurationCustomizerProvider customizer : ServiceLoader.load(AutoConfigurationCustomizerProvider.class, serviceClassLoader)) {
customizer.customize(this);
}
}
ConfigProperties config = getConfig();
Resource resource = ResourceConfiguration.configureResource(config, serviceClassLoader, resourceCustomizer);
SdkMeterProviderBuilder meterProviderBuilder = SdkMeterProvider.builder();
meterProviderBuilder.setResource(resource);
MeterProviderConfiguration.configureMeterProvider(meterProviderBuilder, config, serviceClassLoader, metricExporterCustomizer);
meterProviderBuilder = meterProviderCustomizer.apply(meterProviderBuilder, config);
SdkMeterProvider meterProvider = meterProviderBuilder.build();
SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder();
tracerProviderBuilder.setResource(resource);
TracerProviderConfiguration.configureTracerProvider(tracerProviderBuilder, config, serviceClassLoader, meterProvider, spanExporterCustomizer, samplerCustomizer);
tracerProviderBuilder = tracerProviderCustomizer.apply(tracerProviderBuilder, config);
SdkTracerProvider tracerProvider = tracerProviderBuilder.build();
SdkLogEmitterProviderBuilder logEmitterProviderBuilder = SdkLogEmitterProvider.builder();
logEmitterProviderBuilder.setResource(resource);
LogEmitterProviderConfiguration.configureLogEmitterProvider(logEmitterProviderBuilder, config, meterProvider, logExporterCustomizer);
logEmitterProviderBuilder = logEmitterProviderCustomizer.apply(logEmitterProviderBuilder, config);
SdkLogEmitterProvider logEmitterProvider = logEmitterProviderBuilder.build();
if (registerShutdownHook) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
List<CompletableResultCode> shutdown = new ArrayList<>();
shutdown.add(tracerProvider.shutdown());
shutdown.add(meterProvider.shutdown());
shutdown.add(logEmitterProvider.shutdown());
CompletableResultCode.ofAll(shutdown).join(10, TimeUnit.SECONDS);
}));
}
ContextPropagators propagators = PropagatorConfiguration.configurePropagators(config, serviceClassLoader, propagatorCustomizer);
OpenTelemetrySdkBuilder sdkBuilder = OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).setLogEmitterProvider(logEmitterProvider).setMeterProvider(meterProvider).setPropagators(propagators);
OpenTelemetrySdk openTelemetrySdk = sdkBuilder.build();
if (setResultAsGlobal) {
GlobalOpenTelemetry.set(openTelemetrySdk);
logger.log(Level.FINE, "Global OpenTelemetrySdk set to {0} by autoconfiguration", openTelemetrySdk);
}
return AutoConfiguredOpenTelemetrySdk.create(openTelemetrySdk, resource, config);
}
use of io.opentelemetry.sdk.logs.SdkLogEmitterProvider in project opentelemetry-java by open-telemetry.
the class BatchLogProcessorTest method forceEmit.
@Test
void forceEmit() {
WaitingLogExporter waitingLogExporter = new WaitingLogExporter(100, CompletableResultCode.ofSuccess(), 1);
BatchLogProcessor batchLogProcessor = BatchLogProcessor.builder(waitingLogExporter).setMaxQueueSize(10_000).setMaxExportBatchSize(49).setScheduleDelay(10, TimeUnit.SECONDS).build();
SdkLogEmitterProvider sdkLogEmitterProvider = SdkLogEmitterProvider.builder().addLogProcessor(batchLogProcessor).build();
for (int i = 0; i < 50; i++) {
emitLog(sdkLogEmitterProvider, "notExported");
}
List<LogData> exported = waitingLogExporter.waitForExport();
assertThat(exported).isNotNull();
assertThat(exported.size()).isEqualTo(49);
for (int i = 0; i < 50; i++) {
emitLog(sdkLogEmitterProvider, "notExported");
}
exported = waitingLogExporter.waitForExport();
assertThat(exported).isNotNull();
assertThat(exported.size()).isEqualTo(49);
batchLogProcessor.forceFlush().join(10, TimeUnit.SECONDS);
exported = waitingLogExporter.getExported();
assertThat(exported).isNotNull();
assertThat(exported.size()).isEqualTo(2);
}
use of io.opentelemetry.sdk.logs.SdkLogEmitterProvider in project opentelemetry-java by open-telemetry.
the class BatchLogProcessorTest method emitMultipleLogs.
@Test
void emitMultipleLogs() {
WaitingLogExporter waitingLogExporter = new WaitingLogExporter(2, CompletableResultCode.ofSuccess());
SdkLogEmitterProvider logEmitterProvider = SdkLogEmitterProvider.builder().addLogProcessor(BatchLogProcessor.builder(waitingLogExporter).setScheduleDelay(MAX_SCHEDULE_DELAY_MILLIS, TimeUnit.MILLISECONDS).build()).build();
emitLog(logEmitterProvider, LOG_MESSAGE_1);
emitLog(logEmitterProvider, LOG_MESSAGE_2);
List<LogData> exported = waitingLogExporter.waitForExport();
assertThat(exported).satisfiesExactly(logData -> assertThat(logData).hasBody(LOG_MESSAGE_1), logData -> assertThat(logData).hasBody(LOG_MESSAGE_2));
}
use of io.opentelemetry.sdk.logs.SdkLogEmitterProvider in project opentelemetry-java by open-telemetry.
the class BatchLogProcessorTest method continuesIfExporterTimesOut.
@Test
@Timeout(5)
public void continuesIfExporterTimesOut() throws InterruptedException {
int exporterTimeoutMillis = 10;
BatchLogProcessor blp = BatchLogProcessor.builder(mockLogExporter).setExporterTimeout(exporterTimeoutMillis, TimeUnit.MILLISECONDS).setScheduleDelay(1, TimeUnit.MILLISECONDS).setMaxQueueSize(1).build();
SdkLogEmitterProvider sdkLogEmitterProvider = SdkLogEmitterProvider.builder().addLogProcessor(blp).build();
CountDownLatch exported = new CountDownLatch(1);
// We return a result we never complete, meaning it will timeout.
when(mockLogExporter.export(argThat(logs -> {
assertThat(logs).anySatisfy(log -> LogAssertions.assertThat(log).hasBody(LOG_MESSAGE_1));
exported.countDown();
return true;
}))).thenReturn(new CompletableResultCode());
emitLog(sdkLogEmitterProvider, LOG_MESSAGE_1);
exported.await();
// Timed out so the log was dropped.
await().untilAsserted(() -> assertThat(blp.getBatch()).isEmpty());
// Still processing new logs.
CountDownLatch exportedAgain = new CountDownLatch(1);
reset(mockLogExporter);
when(mockLogExporter.shutdown()).thenReturn(CompletableResultCode.ofSuccess());
when(mockLogExporter.export(argThat(logs -> {
assertThat(logs).anySatisfy(log -> LogAssertions.assertThat(log).hasBody(LOG_MESSAGE_2));
exportedAgain.countDown();
return true;
}))).thenReturn(CompletableResultCode.ofSuccess());
emitLog(sdkLogEmitterProvider, LOG_MESSAGE_2);
exported.await();
await().untilAsserted(() -> assertThat(blp.getBatch()).isEmpty());
}
use of io.opentelemetry.sdk.logs.SdkLogEmitterProvider in project opentelemetry-java by open-telemetry.
the class BatchLogProcessorTest method emitMoreLogsThanTheMaximumLimit.
@Test
void emitMoreLogsThanTheMaximumLimit() {
int maxQueuedLogs = 8;
BlockingLogExporter blockingLogExporter = new BlockingLogExporter();
WaitingLogExporter waitingLogExporter = new WaitingLogExporter(maxQueuedLogs, CompletableResultCode.ofSuccess());
SdkLogEmitterProvider sdkTracerProvider = SdkLogEmitterProvider.builder().addLogProcessor(BatchLogProcessor.builder(LogExporter.composite(Arrays.asList(blockingLogExporter, waitingLogExporter))).setScheduleDelay(MAX_SCHEDULE_DELAY_MILLIS, TimeUnit.MILLISECONDS).setMaxQueueSize(maxQueuedLogs).setMaxExportBatchSize(maxQueuedLogs / 2).build()).build();
// Wait to block the worker thread in the BatchLogProcessor. This ensures that no items
// can be removed from the queue. Need to add a log to trigger the export otherwise the
// pipeline is never called.
emitLog(sdkTracerProvider, "blocking log");
blockingLogExporter.waitUntilIsBlocked();
for (int i = 0; i < maxQueuedLogs; i++) {
// First export maxQueuedLogs, the worker thread is blocked so all items should be queued.
emitLog(sdkTracerProvider, "log_1_" + 1);
}
// Now we should start dropping.
for (int i = 0; i < 7; i++) {
emitLog(sdkTracerProvider, "log_2_" + i);
}
// Release the blocking exporter
blockingLogExporter.unblock();
// While we wait for maxQueuedLogs we ensure that the queue is also empty after this.
List<LogData> exported = waitingLogExporter.waitForExport();
assertThat(exported).isNotNull();
assertThat(exported).hasSize(maxQueuedLogs + 1);
// Clear, reset, add another batch of logs, and confirm they are exported
exported.clear();
waitingLogExporter.reset();
for (int i = 0; i < maxQueuedLogs; i++) {
emitLog(sdkTracerProvider, "log_3_" + i);
}
exported = waitingLogExporter.waitForExport();
assertThat(exported).isNotNull();
assertThat(exported).hasSize(maxQueuedLogs);
}
Aggregations