Search in sources :

Example 26 with LogData

use of io.opentelemetry.sdk.logs.data.LogData 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)

Example 27 with LogData

use of io.opentelemetry.sdk.logs.data.LogData in project opentelemetry-java by open-telemetry.

the class SdkLogBuilderTest method buildAndEmit.

@Test
// test deprecated setName method
@SuppressWarnings("deprecation")
void buildAndEmit() {
    Instant now = Instant.now();
    String name = "skippy";
    String bodyStr = "body";
    String sevText = "sevText";
    Severity severity = Severity.DEBUG3;
    Attributes attrs = Attributes.empty();
    AtomicReference<LogData> seenLog = new AtomicReference<>();
    LogProcessor logProcessor = seenLog::set;
    LogEmitterSharedState state = mock(LogEmitterSharedState.class);
    LogDataBuilder delegate = mock(LogDataBuilder.class);
    LogData logData = mock(LogData.class);
    Context context = mock(Context.class);
    when(state.getLogLimits()).thenReturn(LogLimits.getDefault());
    when(state.getLogProcessor()).thenReturn(logProcessor);
    when(delegate.build()).thenReturn(logData);
    SdkLogBuilder builder = new SdkLogBuilder(state, delegate);
    builder.setBody(bodyStr);
    verify(delegate).setBody(bodyStr);
    builder.setEpoch(123, TimeUnit.SECONDS);
    verify(delegate).setEpoch(123, TimeUnit.SECONDS);
    builder.setEpoch(now);
    verify(delegate).setEpoch(now);
    builder.setAttributes(attrs);
    verify(delegate).setAttributes(attrs);
    builder.setContext(context);
    verify(delegate).setContext(context);
    builder.setName(name);
    verify(delegate).setName(name);
    builder.setSeverity(severity);
    verify(delegate).setSeverity(severity);
    builder.setSeverityText(sevText);
    verify(delegate).setSeverityText(sevText);
    builder.emit();
    assertThat(seenLog.get()).isSameAs(logData);
}
Also used : Context(io.opentelemetry.context.Context) LogData(io.opentelemetry.sdk.logs.data.LogData) Instant(java.time.Instant) Attributes(io.opentelemetry.api.common.Attributes) LogDataBuilder(io.opentelemetry.sdk.logs.data.LogDataBuilder) Severity(io.opentelemetry.sdk.logs.data.Severity) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 28 with LogData

use of io.opentelemetry.sdk.logs.data.LogData in project opentelemetry-java by open-telemetry.

the class SdkLogEmitterTest method logBuilder_maxAttributes.

@Test
void logBuilder_maxAttributes() {
    int maxNumberOfAttrs = 8;
    AtomicReference<LogData> seenLog = new AtomicReference<>();
    SdkLogEmitterProvider logEmitterProvider = SdkLogEmitterProvider.builder().addLogProcessor(seenLog::set).setLogLimits(() -> LogLimits.builder().setMaxNumberOfAttributes(maxNumberOfAttrs).build()).build();
    AttributesBuilder attributesBuilder = Attributes.builder();
    for (int i = 0; i < 2 * maxNumberOfAttrs; i++) {
        attributesBuilder.put("key" + i, i);
    }
    logEmitterProvider.get("test").logBuilder().setAttributes(attributesBuilder.build()).emit();
    // NOTE: cannot guarantee which attributes are retained, only that there are no more that the
    // max
    assertThat(seenLog.get().getAttributes()).hasSize(maxNumberOfAttrs);
}
Also used : LogData(io.opentelemetry.sdk.logs.data.LogData) AtomicReference(java.util.concurrent.atomic.AtomicReference) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 29 with LogData

use of io.opentelemetry.sdk.logs.data.LogData in project opentelemetry-java by open-telemetry.

the class SdkLogEmitterTest method logBuilder.

@Test
void logBuilder() {
    LogEmitterSharedState state = mock(LogEmitterSharedState.class);
    InstrumentationScopeInfo info = InstrumentationScopeInfo.create("foo");
    AtomicReference<LogData> seenLog = new AtomicReference<>();
    LogProcessor logProcessor = seenLog::set;
    Clock clock = mock(Clock.class);
    when(clock.now()).thenReturn(5L);
    when(state.getResource()).thenReturn(Resource.getDefault());
    when(state.getLogProcessor()).thenReturn(logProcessor);
    when(state.getClock()).thenReturn(clock);
    SdkLogEmitter emitter = new SdkLogEmitter(state, info);
    LogBuilder logBuilder = emitter.logBuilder();
    logBuilder.setBody("foo");
    // Have to test through the builder
    logBuilder.emit();
    assertThat(seenLog.get()).hasBody("foo").hasEpochNanos(5);
}
Also used : InstrumentationScopeInfo(io.opentelemetry.sdk.common.InstrumentationScopeInfo) LogData(io.opentelemetry.sdk.logs.data.LogData) AtomicReference(java.util.concurrent.atomic.AtomicReference) Clock(io.opentelemetry.sdk.common.Clock) Test(org.junit.jupiter.api.Test)

Example 30 with LogData

use of io.opentelemetry.sdk.logs.data.LogData in project opentelemetry-java by open-telemetry.

the class SimpleLogProcessorTest method forceFlush.

@Test
void forceFlush() {
    CompletableResultCode export1 = new CompletableResultCode();
    CompletableResultCode export2 = new CompletableResultCode();
    when(logExporter.export(any())).thenReturn(export1, export2);
    LogData logData = TestUtil.createLogData(DEBUG, "Log message");
    logProcessor.emit(logData);
    logProcessor.emit(logData);
    verify(logExporter, times(2)).export(Collections.singletonList(logData));
    CompletableResultCode flush = logProcessor.forceFlush();
    assertThat(flush.isDone()).isFalse();
    export1.succeed();
    assertThat(flush.isDone()).isFalse();
    export2.succeed();
    assertThat(flush.isDone()).isTrue();
    assertThat(flush.isSuccess()).isTrue();
}
Also used : LogData(io.opentelemetry.sdk.logs.data.LogData) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) Test(org.junit.jupiter.api.Test)

Aggregations

LogData (io.opentelemetry.sdk.logs.data.LogData)40 Test (org.junit.jupiter.api.Test)34 Instant (java.time.Instant)8 SdkLogEmitterProvider (io.opentelemetry.sdk.logs.SdkLogEmitterProvider)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Attributes (io.opentelemetry.api.common.Attributes)5 SpanContext (io.opentelemetry.api.trace.SpanContext)4 SuppressLogger (io.opentelemetry.internal.testing.slf4j.SuppressLogger)4 Clock (io.opentelemetry.sdk.common.Clock)4 CompletableResultCode (io.opentelemetry.sdk.common.CompletableResultCode)4 Resource (io.opentelemetry.sdk.resources.Resource)4 TimeUnit (java.util.concurrent.TimeUnit)4 SpanLinkage (com.splunk.opentelemetry.profiler.context.SpanLinkage)3 EventPeriods (com.splunk.opentelemetry.profiler.events.EventPeriods)3 TraceFlags (io.opentelemetry.api.trace.TraceFlags)3 TraceState (io.opentelemetry.api.trace.TraceState)3 LogDataBuilder (io.opentelemetry.sdk.logs.data.LogDataBuilder)3 Mockito.when (org.mockito.Mockito.when)3 CONFIG_KEY_MEMORY_SAMPLER_INTERVAL (com.splunk.opentelemetry.profiler.Configuration.CONFIG_KEY_MEMORY_SAMPLER_INTERVAL)2 CONFIG_KEY_TLAB_ENABLED (com.splunk.opentelemetry.profiler.Configuration.CONFIG_KEY_TLAB_ENABLED)2