Search in sources :

Example 1 with ExportLogsServiceRequest

use of io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest in project opentelemetry-java by open-telemetry.

the class OtlpHttpLogExporterTest method exportAndAssertResult.

private static ExportLogsServiceRequest exportAndAssertResult(OtlpHttpLogExporter otlpHttpLogExporter, boolean expectedResult) {
    List<LogData> logs = Collections.singletonList(generateFakeLog());
    CompletableResultCode resultCode = otlpHttpLogExporter.export(logs);
    resultCode.join(10, TimeUnit.SECONDS);
    assertThat(resultCode.isSuccess()).isEqualTo(expectedResult);
    List<ResourceLogs> resourceLogs = Arrays.stream(ResourceLogsMarshaler.create(logs)).map(marshaler -> {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            marshaler.writeBinaryTo(bos);
            return ResourceLogs.parseFrom(bos.toByteArray());
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }).collect(Collectors.toList());
    return ExportLogsServiceRequest.newBuilder().addAllResourceLogs(resourceLogs).build();
}
Also used : RecordedRequest(com.linecorp.armeria.testing.junit5.server.mock.RecordedRequest) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ExportLogsServiceResponse(io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Attributes(io.opentelemetry.api.common.Attributes) InetAddress(java.net.InetAddress) ResourceLogs(io.opentelemetry.proto.logs.v1.ResourceLogs) Duration(java.time.Duration) Level(org.slf4j.event.Level) ResourceLogsMarshaler(io.opentelemetry.exporter.internal.otlp.logs.ResourceLogsMarshaler) LogDataBuilder(io.opentelemetry.sdk.logs.data.LogDataBuilder) GzipSource(okio.GzipSource) ExportLogsServiceRequest(io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) HttpResponse(com.linecorp.armeria.common.HttpResponse) MockWebServerExtension(com.linecorp.armeria.testing.junit5.server.mock.MockWebServerExtension) Status(com.google.rpc.Status) HttpMethod(com.linecorp.armeria.common.HttpMethod) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) OkHttpExporter(io.opentelemetry.exporter.internal.okhttp.OkHttpExporter) UncheckedIOException(java.io.UncheckedIOException) Test(org.junit.jupiter.api.Test) List(java.util.List) LogCapturer(io.github.netmikey.logunit.api.LogCapturer) SuppressLogger(io.opentelemetry.internal.testing.slf4j.SuppressLogger) HeldCertificate(okhttp3.tls.HeldCertificate) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) ServerBuilder(com.linecorp.armeria.server.ServerBuilder) AggregatedHttpRequest(com.linecorp.armeria.common.AggregatedHttpRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Resource(io.opentelemetry.sdk.resources.Resource) MediaType(com.linecorp.armeria.common.MediaType) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) RetryPolicy(io.opentelemetry.exporter.internal.retry.RetryPolicy) HttpStatus(com.linecorp.armeria.common.HttpStatus) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) LogData(io.opentelemetry.sdk.logs.data.LogData) LoggingEvent(org.slf4j.event.LoggingEvent) Severity(io.opentelemetry.sdk.logs.data.Severity) Buffer(okio.Buffer) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) TimeUnit(java.util.concurrent.TimeUnit) RetryUtil(io.opentelemetry.exporter.internal.retry.RetryUtil) Message(com.google.protobuf.Message) Collections(java.util.Collections) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) LogData(io.opentelemetry.sdk.logs.data.LogData) ResourceLogs(io.opentelemetry.proto.logs.v1.ResourceLogs) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) UncheckedIOException(java.io.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Example 2 with ExportLogsServiceRequest

use of io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest in project opentelemetry-java by open-telemetry.

the class OtlpHttpLogExporterTest method testExportGzipCompressed.

@Test
void testExportGzipCompressed() {
    server.enqueue(successResponse());
    OtlpHttpLogExporter exporter = builder.setCompression("gzip").build();
    ExportLogsServiceRequest payload = exportAndAssertResult(exporter, /* expectedResult= */
    true);
    AggregatedHttpRequest request = server.takeRequest().request();
    assertRequestCommon(request);
    assertThat(request.headers().get("Content-Encoding")).isEqualTo("gzip");
    assertThat(parseRequestBody(gzipDecompress(request.content().array()))).isEqualTo(payload);
}
Also used : ExportLogsServiceRequest(io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest) AggregatedHttpRequest(com.linecorp.armeria.common.AggregatedHttpRequest) Test(org.junit.jupiter.api.Test)

Example 3 with ExportLogsServiceRequest

use of io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest in project opentelemetry-java by open-telemetry.

the class OtlpHttpLogExporterTest method testExportTls.

@Test
void testExportTls() {
    server.enqueue(successResponse());
    OtlpHttpLogExporter exporter = builder.setEndpoint("https://" + canonicalHostName + ":" + server.httpsPort() + "/v1/logs").setTrustedCertificates(HELD_CERTIFICATE.certificatePem().getBytes(StandardCharsets.UTF_8)).build();
    ExportLogsServiceRequest payload = exportAndAssertResult(exporter, /* expectedResult= */
    true);
    RecordedRequest recorded = server.takeRequest();
    AggregatedHttpRequest request = recorded.request();
    assertRequestCommon(request);
    assertThat(parseRequestBody(request.content().array())).isEqualTo(payload);
    // OkHttp does support HTTP/2 upgrade on TLS.
    assertThat(recorded.context().sessionProtocol().isMultiplex()).isTrue();
}
Also used : RecordedRequest(com.linecorp.armeria.testing.junit5.server.mock.RecordedRequest) ExportLogsServiceRequest(io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest) AggregatedHttpRequest(com.linecorp.armeria.common.AggregatedHttpRequest) Test(org.junit.jupiter.api.Test)

Example 4 with ExportLogsServiceRequest

use of io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest in project opentelemetry-java by open-telemetry.

the class FullConfigTest method configures.

@Test
void configures() throws Exception {
    Collection<String> fields = GlobalOpenTelemetry.get().getPropagators().getTextMapPropagator().fields();
    List<String> keys = new ArrayList<>();
    keys.addAll(W3CTraceContextPropagator.getInstance().fields());
    keys.addAll(W3CBaggagePropagator.getInstance().fields());
    keys.addAll(B3Propagator.injectingSingleHeader().fields());
    keys.addAll(B3Propagator.injectingMultiHeaders().fields());
    keys.addAll(JaegerPropagator.getInstance().fields());
    keys.addAll(OtTracePropagator.getInstance().fields());
    keys.addAll(AwsXrayPropagator.getInstance().fields());
    // Added by TestPropagatorProvider
    keys.add("test");
    assertThat(fields).containsExactlyInAnyOrderElementsOf(keys);
    GlobalOpenTelemetry.get().getMeterProvider().get("test").counterBuilder("test").build().add(1);
    GlobalOpenTelemetry.get().getTracer("test").spanBuilder("test").startSpan().setAttribute("cat", "meow").setAttribute("dog", "bark").end();
    Meter meter = GlobalOpenTelemetry.get().getMeter("test");
    meter.counterBuilder("my-metric").build().add(1, Attributes.builder().put("allowed", "bear").put("not allowed", "dog").build());
    meter.counterBuilder("my-other-metric").build().add(1);
    LogEmitter logEmitter = logEmitterProvider.get("test");
    logEmitter.logBuilder().setBody("debug log message").setSeverity(Severity.DEBUG).emit();
    logEmitter.logBuilder().setBody("info log message").setSeverity(Severity.INFO).emit();
    await().untilAsserted(() -> assertThat(otlpTraceRequests).hasSize(1));
    ExportTraceServiceRequest traceRequest = otlpTraceRequests.take();
    assertThat(traceRequest.getResourceSpans(0).getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey("service.name").setValue(AnyValue.newBuilder().setStringValue("test").build()).build(), KeyValue.newBuilder().setKey("cat").setValue(AnyValue.newBuilder().setStringValue("meow").build()).build());
    io.opentelemetry.proto.trace.v1.Span span = traceRequest.getResourceSpans(0).getInstrumentationLibrarySpans(0).getSpans(0);
    // Dog dropped by attribute limit.
    assertThat(span.getAttributesList()).containsExactlyInAnyOrder(KeyValue.newBuilder().setKey("configured").setValue(AnyValue.newBuilder().setBoolValue(true).build()).build(), KeyValue.newBuilder().setKey("wrapped").setValue(AnyValue.newBuilder().setIntValue(1).build()).build(), KeyValue.newBuilder().setKey("cat").setValue(AnyValue.newBuilder().setStringValue("meow").build()).build());
    // await on assertions since metrics may come in different order for BatchSpanProcessor,
    // exporter, or the ones we
    // created in the test.
    await().untilAsserted(() -> {
        ExportMetricsServiceRequest metricRequest = otlpMetricsRequests.take();
        assertThat(metricRequest.getResourceMetrics(0).getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey("service.name").setValue(AnyValue.newBuilder().setStringValue("test").build()).build(), KeyValue.newBuilder().setKey("cat").setValue(AnyValue.newBuilder().setStringValue("meow").build()).build());
        for (ResourceMetrics resourceMetrics : metricRequest.getResourceMetricsList()) {
            assertThat(resourceMetrics.getInstrumentationLibraryMetricsList()).anySatisfy(ilm -> assertThat(ilm.getInstrumentationLibrary().getName()).isEqualTo("test"));
            for (InstrumentationLibraryMetrics instrumentationLibraryMetrics : resourceMetrics.getInstrumentationLibraryMetricsList()) {
                for (Metric metric : instrumentationLibraryMetrics.getMetricsList()) {
                    // SPI was loaded
                    // MetricExporterCustomizer filters metrics not named my-metric
                    assertThat(metric.getName()).isEqualTo("my-metric");
                    // TestMeterProviderConfigurer configures a view that only passes on attribute
                    // named allowed
                    // configured-test
                    assertThat(getFirstDataPointLabels(metric)).contains(KeyValue.newBuilder().setKey("allowed").setValue(AnyValue.newBuilder().setStringValue("bear").build()).build());
                }
            }
        }
    });
    await().untilAsserted(() -> assertThat(otlpLogsRequests).hasSize(1));
    ExportLogsServiceRequest logRequest = otlpLogsRequests.take();
    assertThat(logRequest.getResourceLogs(0).getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey("service.name").setValue(AnyValue.newBuilder().setStringValue("test").build()).build(), KeyValue.newBuilder().setKey("cat").setValue(AnyValue.newBuilder().setStringValue("meow").build()).build());
    // MetricExporterCustomizer filters logs not whose level is less than Severity.INFO
    LogRecord log = logRequest.getResourceLogs(0).getInstrumentationLibraryLogs(0).getLogRecords(0);
    assertThat(log.getBody().getStringValue()).isEqualTo("info log message");
    assertThat(log.getSeverityNumberValue()).isEqualTo(Severity.INFO.getSeverityNumber());
}
Also used : ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) Meter(io.opentelemetry.api.metrics.Meter) ArrayList(java.util.ArrayList) InstrumentationLibraryMetrics(io.opentelemetry.proto.metrics.v1.InstrumentationLibraryMetrics) ExportLogsServiceRequest(io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest) ResourceMetrics(io.opentelemetry.proto.metrics.v1.ResourceMetrics) LogEmitter(io.opentelemetry.sdk.logs.LogEmitter) LogRecord(io.opentelemetry.proto.logs.v1.LogRecord) Metric(io.opentelemetry.proto.metrics.v1.Metric) ExportMetricsServiceRequest(io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest) Test(org.junit.jupiter.api.Test)

Example 5 with ExportLogsServiceRequest

use of io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest 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)

Aggregations

ExportLogsServiceRequest (io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest)6 Test (org.junit.jupiter.api.Test)5 AggregatedHttpRequest (com.linecorp.armeria.common.AggregatedHttpRequest)4 RecordedRequest (com.linecorp.armeria.testing.junit5.server.mock.RecordedRequest)3 ResourceLogs (io.opentelemetry.proto.logs.v1.ResourceLogs)2 LogData (io.opentelemetry.sdk.logs.data.LogData)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Message (com.google.protobuf.Message)1 Status (com.google.rpc.Status)1 HttpMethod (com.linecorp.armeria.common.HttpMethod)1 HttpResponse (com.linecorp.armeria.common.HttpResponse)1 HttpStatus (com.linecorp.armeria.common.HttpStatus)1 MediaType (com.linecorp.armeria.common.MediaType)1 ServerBuilder (com.linecorp.armeria.server.ServerBuilder)1 MockWebServerExtension (com.linecorp.armeria.testing.junit5.server.mock.MockWebServerExtension)1 LogCapturer (io.github.netmikey.logunit.api.LogCapturer)1 Attributes (io.opentelemetry.api.common.Attributes)1 Meter (io.opentelemetry.api.metrics.Meter)1 OkHttpExporter (io.opentelemetry.exporter.internal.okhttp.OkHttpExporter)1 ResourceLogsMarshaler (io.opentelemetry.exporter.internal.otlp.logs.ResourceLogsMarshaler)1