Search in sources :

Example 1 with CompletableResultCode

use of io.opentelemetry.sdk.common.CompletableResultCode in project opentelemetry-java by open-telemetry.

the class SystemOutLogExporterTest method returnCodes.

@Test
void returnCodes() {
    SystemOutLogExporter exporter = SystemOutLogExporter.create();
    CompletableResultCode resultCode = exporter.export(singletonList(sampleLog(System.currentTimeMillis())));
    assertThat(resultCode).isSameAs(CompletableResultCode.ofSuccess());
    assertThat(exporter.shutdown()).isSameAs(CompletableResultCode.ofSuccess());
}
Also used : CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) Test(org.junit.jupiter.api.Test)

Example 2 with CompletableResultCode

use of io.opentelemetry.sdk.common.CompletableResultCode 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 3 with CompletableResultCode

use of io.opentelemetry.sdk.common.CompletableResultCode in project opentelemetry-java by open-telemetry.

the class JaegerGrpcSpanExporterTest method testExport.

@Test
void testExport() throws Exception {
    // ms
    long duration = 900;
    long startMs = System.currentTimeMillis();
    long endMs = startMs + duration;
    SpanData span = TestSpanData.builder().setHasEnded(true).setSpanContext(SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getSampled(), TraceState.getDefault())).setName("GET /api/endpoint").setStartEpochNanos(TimeUnit.MILLISECONDS.toNanos(startMs)).setEndEpochNanos(TimeUnit.MILLISECONDS.toNanos(endMs)).setStatus(StatusData.ok()).setKind(SpanKind.CONSUMER).setLinks(Collections.emptyList()).setTotalRecordedLinks(0).setTotalRecordedEvents(0).setInstrumentationLibraryInfo(InstrumentationLibraryInfo.create("io.opentelemetry.auto", "1.0.0")).setResource(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, "myServiceName", AttributeKey.stringKey("resource-attr-key"), "resource-attr-value"))).build();
    // test
    CompletableResultCode result = exporter.export(Collections.singletonList(span));
    result.join(10, TimeUnit.SECONDS);
    assertThat(result.isSuccess()).isEqualTo(true);
    // verify
    assertThat(postedRequests).hasSize(1);
    Model.Batch batch = postedRequests.poll().getBatch();
    assertThat(batch.getSpans(0).getOperationName()).isEqualTo("GET /api/endpoint");
    assertThat(SpanId.fromBytes(batch.getSpans(0).getSpanId().toByteArray())).isEqualTo(SPAN_ID);
    assertThat(getTagValue(batch.getProcess().getTagsList(), "resource-attr-key").orElseThrow(() -> new AssertionError("resource-attr-key not found")).getVStr()).isEqualTo("resource-attr-value");
    verifyBatch(batch);
    assertThat(batch.getProcess().getServiceName()).isEqualTo("myServiceName");
}
Also used : SpanData(io.opentelemetry.sdk.trace.data.SpanData) TestSpanData(io.opentelemetry.sdk.testing.trace.TestSpanData) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) Model(io.opentelemetry.exporter.jaeger.proto.api_v2.Model) Test(org.junit.jupiter.api.Test)

Example 4 with CompletableResultCode

use of io.opentelemetry.sdk.common.CompletableResultCode in project opentelemetry-java by open-telemetry.

the class JaegerThriftSpanExporter method export.

/**
 * Submits all the given spans in a single batch to the Jaeger collector.
 *
 * @param spans the list of sampled Spans to be exported.
 * @return the result of the operation
 */
@Override
public CompletableResultCode export(Collection<SpanData> spans) {
    Map<Process, List<Span>> batches = spans.stream().collect(Collectors.groupingBy(SpanData::getResource)).entrySet().stream().collect(Collectors.toMap(entry -> createProcess(entry.getKey()), entry -> Adapter.toJaeger(entry.getValue())));
    List<CompletableResultCode> batchResults = new ArrayList<>(batches.size());
    batches.forEach((process, jaegerSpans) -> {
        CompletableResultCode batchResult = new CompletableResultCode();
        batchResults.add(batchResult);
        try {
            // todo: consider making truly async
            thriftSender.send(process, jaegerSpans);
            batchResult.succeed();
        } catch (SenderException e) {
            logger.log(Level.WARNING, "Failed to export spans", e);
            batchResult.fail();
        }
    });
    return CompletableResultCode.ofAll(batchResults);
}
Also used : ThrottlingLogger(io.opentelemetry.sdk.internal.ThrottlingLogger) Resource(io.opentelemetry.sdk.resources.Resource) Span(io.jaegertracing.thriftjava.Span) Collection(java.util.Collection) SpanExporter(io.opentelemetry.sdk.trace.export.SpanExporter) ThreadSafe(javax.annotation.concurrent.ThreadSafe) Logger(java.util.logging.Logger) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) ResourceAttributes(io.opentelemetry.semconv.resource.attributes.ResourceAttributes) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) InetAddress(java.net.InetAddress) TagType(io.jaegertracing.thriftjava.TagType) List(java.util.List) ThriftSender(io.jaegertracing.thrift.internal.senders.ThriftSender) Map(java.util.Map) SpanData(io.opentelemetry.sdk.trace.data.SpanData) Process(io.jaegertracing.thriftjava.Process) SenderException(io.jaegertracing.internal.exceptions.SenderException) Tag(io.jaegertracing.thriftjava.Tag) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) SpanData(io.opentelemetry.sdk.trace.data.SpanData) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) ArrayList(java.util.ArrayList) Process(io.jaegertracing.thriftjava.Process) ArrayList(java.util.ArrayList) List(java.util.List) SenderException(io.jaegertracing.internal.exceptions.SenderException)

Example 5 with CompletableResultCode

use of io.opentelemetry.sdk.common.CompletableResultCode in project opentelemetry-java by open-telemetry.

the class OtlpGrpcRetryTest method testRetryableStatusCodes.

private static <T> void testRetryableStatusCodes(Supplier<T> dataSupplier, Function<T, CompletableResultCode> exporter, Supplier<Integer> serverRequestCountSupplier) {
    for (Status.Code code : Status.Code.values()) {
        server.reset();
        server.responseStatuses.add(Status.fromCode(code));
        server.responseStatuses.add(Status.OK);
        CompletableResultCode resultCode = exporter.apply(dataSupplier.get()).join(10, TimeUnit.SECONDS);
        boolean retryable = RetryUtil.retryableGrpcStatusCodes().contains(String.valueOf(code.value()));
        boolean expectedResult = retryable || code == Status.Code.OK;
        assertThat(resultCode.isSuccess()).as("status code %s should export %s", code, expectedResult ? "successfully" : "unsuccessfully").isEqualTo(expectedResult);
        int expectedRequests = retryable ? 2 : 1;
        assertThat(serverRequestCountSupplier.get()).as("status code %s should make %s requests", code, expectedRequests).isEqualTo(expectedRequests);
    }
}
Also used : Status(io.grpc.Status) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode)

Aggregations

CompletableResultCode (io.opentelemetry.sdk.common.CompletableResultCode)59 Test (org.junit.jupiter.api.Test)28 SpanData (io.opentelemetry.sdk.trace.data.SpanData)15 ArrayList (java.util.ArrayList)11 TestSpanData (io.opentelemetry.sdk.testing.trace.TestSpanData)8 List (java.util.List)7 TimeUnit (java.util.concurrent.TimeUnit)7 IOException (java.io.IOException)6 Arrays (java.util.Arrays)6 SuppressLogger (io.opentelemetry.internal.testing.slf4j.SuppressLogger)5 LogData (io.opentelemetry.sdk.logs.data.LogData)5 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Attributes (io.opentelemetry.api.common.Attributes)4 Collectors (java.util.stream.Collectors)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3 Message (com.google.protobuf.Message)3 Status (com.google.rpc.Status)3 AggregatedHttpRequest (com.linecorp.armeria.common.AggregatedHttpRequest)3 HttpMethod (com.linecorp.armeria.common.HttpMethod)3