Skip to content

Commit

Permalink
Add integration test for OtlpGrpcSpanExporter.
Browse files Browse the repository at this point in the history
  • Loading branch information
timpeeters committed Jun 26, 2024
1 parent 633e121 commit bf4da7d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ dependencies {
testImplementation(project(":spring-boot-project:spring-boot-test"))
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
testImplementation("io.micrometer:micrometer-observation-test")
testImplementation("io.opentelemetry.proto:opentelemetry-proto:1.2.0-alpha")
testImplementation("io.projectreactor:reactor-test")
testImplementation("io.prometheus:prometheus-metrics-exposition-formats")
testImplementation("io.r2dbc:r2dbc-h2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import io.micrometer.tracing.Tracer;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceResponse;
import io.opentelemetry.proto.collector.trace.v1.TraceServiceGrpc;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import okhttp3.mockwebserver.MockResponse;
Expand Down Expand Up @@ -57,16 +67,29 @@ class OtlpAutoConfigurationIntegrationTests {

private final MockWebServer mockWebServer = new MockWebServer();

private final MockGrpcService mockGrpcService = new MockGrpcService();
private final Server mockGrpcServer = ServerBuilder.forPort(0).addService(this.mockGrpcService).build();

@BeforeEach
void setUp() throws IOException {
void startMockWebServer() throws IOException {
this.mockWebServer.start();
}

@BeforeEach
void startMockGrpcServer() throws IOException {
this.mockGrpcServer.start();
}

@AfterEach
void tearDown() throws IOException {
void stopMockWebServer() throws IOException {
this.mockWebServer.close();
}

@AfterEach
void stopMockGrpcServer() throws InterruptedException {
this.mockGrpcServer.shutdown().awaitTermination(10, TimeUnit.SECONDS);
}

@Test
void httpSpanExporterShouldUseProtobufAndNoCompressionByDefault() {
this.mockWebServer.enqueue(new MockResponse());
Expand Down Expand Up @@ -113,4 +136,38 @@ void httpSpanExporterCanBeConfiguredToUseGzipCompression() {
});
}

@Test
void grpcSpanExporter() {
this.contextRunner
.withPropertyValues(
"management.otlp.tracing.endpoint=http://localhost:%d".formatted(this.mockGrpcServer.getPort()),
"management.otlp.tracing.transport=grpc")
.run((context) -> {
context.getBean(Tracer.class).nextSpan().name("test").end();
assertThat(context.getBean(OtlpGrpcSpanExporter.class).flush())
.isSameAs(CompletableResultCode.ofSuccess());
ExportTraceServiceRequest request = this.mockGrpcService.takeRequest(10, TimeUnit.SECONDS);
assertThat(request).isNotNull();
assertThat(request.getResourceSpansCount()).isEqualTo(1);
});
}

private static final class MockGrpcService extends TraceServiceGrpc.TraceServiceImplBase {

private final BlockingQueue<ExportTraceServiceRequest> recordedRequests = new LinkedBlockingQueue<>();

@Override
public void export(ExportTraceServiceRequest request,
StreamObserver<ExportTraceServiceResponse> responseObserver) {
this.recordedRequests.add(request);
responseObserver.onNext(ExportTraceServiceResponse.getDefaultInstance());
responseObserver.onCompleted();
}

private ExportTraceServiceRequest takeRequest(int timeout, TimeUnit unit) throws InterruptedException {
return this.recordedRequests.poll(timeout, unit);
}

}

}

0 comments on commit bf4da7d

Please sign in to comment.