Search in sources :

Example 11 with SdkLogEmitterProvider

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);
}
Also used : SdkTracerProviderBuilder(io.opentelemetry.sdk.trace.SdkTracerProviderBuilder) ConfigProperties(io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties) SdkMeterProvider(io.opentelemetry.sdk.metrics.SdkMeterProvider) ContextPropagators(io.opentelemetry.context.propagation.ContextPropagators) Resource(io.opentelemetry.sdk.resources.Resource) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) OpenTelemetrySdkBuilder(io.opentelemetry.sdk.OpenTelemetrySdkBuilder) ArrayList(java.util.ArrayList) List(java.util.List) AutoConfigurationCustomizerProvider(io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider) SdkMeterProviderBuilder(io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder) SdkLogEmitterProvider(io.opentelemetry.sdk.logs.SdkLogEmitterProvider) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) SdkLogEmitterProviderBuilder(io.opentelemetry.sdk.logs.SdkLogEmitterProviderBuilder)

Example 12 with SdkLogEmitterProvider

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);
}
Also used : LogData(io.opentelemetry.sdk.logs.data.LogData) SdkLogEmitterProvider(io.opentelemetry.sdk.logs.SdkLogEmitterProvider) Test(org.junit.jupiter.api.Test)

Example 13 with SdkLogEmitterProvider

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));
}
Also used : LogData(io.opentelemetry.sdk.logs.data.LogData) SdkLogEmitterProvider(io.opentelemetry.sdk.logs.SdkLogEmitterProvider) Test(org.junit.jupiter.api.Test)

Example 14 with SdkLogEmitterProvider

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());
}
Also used : Strictness(org.mockito.quality.Strictness) LogAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.LogAssertions.assertThat) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) AssertionsForClassTypes.assertThatCode(org.assertj.core.api.AssertionsForClassTypes.assertThatCode) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) Mockito.doThrow(org.mockito.Mockito.doThrow) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) LogData(io.opentelemetry.sdk.logs.data.LogData) Nullable(javax.annotation.Nullable) Awaitility.await(org.awaitility.Awaitility.await) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) GuardedBy(io.opentelemetry.api.internal.GuardedBy) SdkLogEmitterProvider(io.opentelemetry.sdk.logs.SdkLogEmitterProvider) Collection(java.util.Collection) LogAssertions(io.opentelemetry.sdk.testing.assertj.LogAssertions) Mockito.when(org.mockito.Mockito.when) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) SuppressLogger(io.opentelemetry.internal.testing.slf4j.SuppressLogger) Mockito.reset(org.mockito.Mockito.reset) Timeout(org.junit.jupiter.api.Timeout) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) SdkLogEmitterProvider(io.opentelemetry.sdk.logs.SdkLogEmitterProvider) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 15 with SdkLogEmitterProvider

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);
}
Also used : LogData(io.opentelemetry.sdk.logs.data.LogData) SdkLogEmitterProvider(io.opentelemetry.sdk.logs.SdkLogEmitterProvider) Test(org.junit.jupiter.api.Test)

Aggregations

SdkLogEmitterProvider (io.opentelemetry.sdk.logs.SdkLogEmitterProvider)16 Test (org.junit.jupiter.api.Test)10 LogData (io.opentelemetry.sdk.logs.data.LogData)8 CompletableResultCode (io.opentelemetry.sdk.common.CompletableResultCode)4 List (java.util.List)4 TimeUnit (java.util.concurrent.TimeUnit)4 SuppressLogger (io.opentelemetry.internal.testing.slf4j.SuppressLogger)3 OpenTelemetrySdk (io.opentelemetry.sdk.OpenTelemetrySdk)3 SdkMeterProvider (io.opentelemetry.sdk.metrics.SdkMeterProvider)3 SdkTracerProvider (io.opentelemetry.sdk.trace.SdkTracerProvider)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 Timeout (org.junit.jupiter.api.Timeout)3 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)3 Mock (org.mockito.Mock)3 Mockito.when (org.mockito.Mockito.when)3 GuardedBy (io.opentelemetry.api.internal.GuardedBy)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 Map (java.util.Map)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2