use of io.opentelemetry.sdk.logs.data.LogData in project opentelemetry-java by open-telemetry.
the class OtlpExporterIntegrationTest method testLogExporter.
// test deprecated setName method
@SuppressWarnings("deprecation")
private static void testLogExporter(LogExporter logExporter) {
LogData logData = LogDataBuilder.create(RESOURCE, InstrumentationLibraryInfo.create(OtlpExporterIntegrationTest.class.getName(), null)).setName("log-name").setBody("log body").setAttributes(Attributes.builder().put("key", "value").build()).setSeverity(Severity.DEBUG).setSeverityText("DEBUG").setEpoch(Instant.now()).setSpanContext(SpanContext.create(IdGenerator.random().generateTraceId(), IdGenerator.random().generateSpanId(), TraceFlags.getDefault(), TraceState.getDefault())).build();
logExporter.export(Collections.singletonList(logData));
await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> assertThat(grpcServer.logRequests).hasSize(1));
ExportLogsServiceRequest request = grpcServer.logRequests.get(0);
assertThat(request.getResourceLogsCount()).isEqualTo(1);
ResourceLogs resourceLogs = request.getResourceLogs(0);
assertThat(resourceLogs.getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey(ResourceAttributes.SERVICE_NAME.getKey()).setValue(AnyValue.newBuilder().setStringValue("integration test").build()).build());
assertThat(resourceLogs.getInstrumentationLibraryLogsCount()).isEqualTo(1);
InstrumentationLibraryLogs ilLogs = resourceLogs.getInstrumentationLibraryLogs(0);
assertThat(ilLogs.getInstrumentationLibrary().getName()).isEqualTo(OtlpExporterIntegrationTest.class.getName());
assertThat(ilLogs.getLogRecordsCount()).isEqualTo(1);
io.opentelemetry.proto.logs.v1.LogRecord protoLog = ilLogs.getLogRecords(0);
assertThat(protoLog.getName()).isEqualTo("log-name");
assertThat(protoLog.getBody().getStringValue()).isEqualTo("log body");
assertThat(protoLog.getAttributesList()).isEqualTo(Collections.singletonList(KeyValue.newBuilder().setKey("key").setValue(AnyValue.newBuilder().setStringValue("value").build()).build()));
assertThat(protoLog.getSeverityNumber().getNumber()).isEqualTo(logData.getSeverity().getSeverityNumber());
assertThat(protoLog.getSeverityText()).isEqualTo("DEBUG");
assertThat(TraceId.fromBytes(protoLog.getTraceId().toByteArray())).isEqualTo(logData.getSpanContext().getTraceId());
assertThat(SpanId.fromBytes(protoLog.getSpanId().toByteArray())).isEqualTo(logData.getSpanContext().getSpanId());
assertThat(TraceFlags.fromByte((byte) protoLog.getFlags())).isEqualTo(logData.getSpanContext().getTraceFlags());
assertThat(protoLog.getTimeUnixNano()).isEqualTo(logData.getEpochNanos());
}
use of io.opentelemetry.sdk.logs.data.LogData in project opentelemetry-java by open-telemetry.
the class SystemOutLogExporterTest method format.
@Test
void format() {
long timestamp = LocalDateTime.of(1970, Month.AUGUST, 7, 10, 0).toInstant(ZoneOffset.UTC).toEpochMilli();
LogData log = sampleLog(timestamp);
StringBuilder output = new StringBuilder();
SystemOutLogExporter.formatLog(output, log);
assertThat(output.toString()).isEqualTo("1970-08-07T10:00:00Z ERROR3 'message' : 00000000000000010000000000000002 0000000000000003 " + "[libraryInfo: logTest:1.0] {amount=1, cheese=\"cheddar\"}");
}
use of io.opentelemetry.sdk.logs.data.LogData in project opentelemetry-java by open-telemetry.
the class SdkLogEmitterProviderTest method canSetClock.
@Test
void canSetClock() {
long now = TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis());
Clock clock = mock(Clock.class);
when(clock.now()).thenReturn(now);
List<LogData> seenLogs = new ArrayList<>();
logProcessor = seenLogs::add;
sdkLogEmitterProvider = SdkLogEmitterProvider.builder().setClock(clock).addLogProcessor(logProcessor).build();
sdkLogEmitterProvider.logEmitterBuilder(null).build().logBuilder().emit();
assertThat(seenLogs.size()).isEqualTo(1);
assertThat(seenLogs.get(0).getEpochNanos()).isEqualTo(now);
}
use of io.opentelemetry.sdk.logs.data.LogData 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.data.LogData 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));
}
Aggregations