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);
}
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);
}
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);
}
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);
}
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();
}
Aggregations