Search in sources :

Example 21 with LogData

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());
}
Also used : LogData(io.opentelemetry.sdk.logs.data.LogData) ResourceLogs(io.opentelemetry.proto.logs.v1.ResourceLogs) ExportLogsServiceRequest(io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest) InstrumentationLibraryLogs(io.opentelemetry.proto.logs.v1.InstrumentationLibraryLogs)

Example 22 with LogData

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

Example 23 with LogData

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

Example 24 with LogData

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

Example 25 with LogData

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