-
Notifications
You must be signed in to change notification settings - Fork 228
w3c tracing integration test #522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
sdk-tests/src/test/java/io/dapr/it/tracing/OpenTelemetry.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* | ||
| * Copyright (c) Microsoft Corporation and Dapr Contributors. | ||
| * Licensed under the MIT License. | ||
| */ | ||
|
|
||
| package io.dapr.it.tracing; | ||
|
|
||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.propagation.ContextPropagators; | ||
| import io.opentelemetry.context.propagation.TextMapPropagator; | ||
| import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter; | ||
| import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
| import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
| import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
| import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
| import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.Socket; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class OpenTelemetry { | ||
|
|
||
| private static final int ZIPKIN_PORT = 9411; | ||
|
|
||
| private static final String ENDPOINT_V2_SPANS = "/api/v2/spans"; | ||
|
|
||
| /** | ||
| * Creates an opentelemetry instance. | ||
| * @param serviceName Name of the service in Zipkin | ||
| * @return OpenTelemetry. | ||
| */ | ||
| public static io.opentelemetry.api.OpenTelemetry createOpenTelemetry(String serviceName) { | ||
| // Only exports to Zipkin if it is up. Otherwise, ignore it. | ||
| // This is helpful to avoid exceptions for examples that do not require Zipkin. | ||
| if (isZipkinUp()) { | ||
| String httpUrl = String.format("http://localhost:%d", ZIPKIN_PORT); | ||
| ZipkinSpanExporter zipkinExporter = | ||
| ZipkinSpanExporter.builder() | ||
| .setEndpoint(httpUrl + ENDPOINT_V2_SPANS) | ||
| .setServiceName(serviceName) | ||
| .build(); | ||
|
|
||
| SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder() | ||
| .addSpanProcessor(SimpleSpanProcessor.create(zipkinExporter)) | ||
| .build(); | ||
|
|
||
| return OpenTelemetrySdk.builder() | ||
| .setTracerProvider(sdkTracerProvider) | ||
| .setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())) | ||
| .buildAndRegisterGlobal(); | ||
| } else { | ||
| System.out.println("WARNING: Zipkin is not available."); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Converts current OpenTelemetry's context into Reactor's context. | ||
| * @return Reactor's context. | ||
| */ | ||
| public static reactor.util.context.Context getReactorContext() { | ||
| return getReactorContext(Context.current()); | ||
| } | ||
|
|
||
| /** | ||
| * Converts given OpenTelemetry's context into Reactor's context. | ||
| * @param context OpenTelemetry's context. | ||
| * @return Reactor's context. | ||
| */ | ||
| public static reactor.util.context.Context getReactorContext(Context context) { | ||
| Map<String, String> map = new HashMap<>(); | ||
| TextMapPropagator.Setter<Map<String, String>> setter = | ||
| (carrier, key, value) -> map.put(key, value); | ||
|
|
||
| GlobalOpenTelemetry.getPropagators().getTextMapPropagator().inject(context, map, setter); | ||
| reactor.util.context.Context reactorContext = reactor.util.context.Context.empty(); | ||
| for (Map.Entry<String, String> entry : map.entrySet()) { | ||
| reactorContext = reactorContext.put(entry.getKey(), entry.getValue()); | ||
| } | ||
| return reactorContext; | ||
| } | ||
|
|
||
| private static boolean isZipkinUp() { | ||
| try (Socket ignored = new Socket("localhost", ZIPKIN_PORT)) { | ||
| return true; | ||
| } catch (IOException ignored) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
70 changes: 70 additions & 0 deletions
70
sdk-tests/src/test/java/io/dapr/it/tracing/Validation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright (c) Microsoft Corporation and Dapr Contributors. | ||
| * Licensed under the MIT License. | ||
| */ | ||
|
|
||
| package io.dapr.it.tracing; | ||
|
|
||
| import com.jayway.jsonpath.DocumentContext; | ||
| import com.jayway.jsonpath.JsonPath; | ||
| import net.minidev.json.JSONArray; | ||
| import okhttp3.HttpUrl; | ||
| import okhttp3.OkHttpClient; | ||
| import okhttp3.Request; | ||
| import okhttp3.Response; | ||
|
|
||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| /** | ||
| * Class used to verify that traces are present as expected. | ||
| * | ||
| * Checks for the main span and then checks for its child span for `sleep` API call. | ||
| */ | ||
| public final class Validation { | ||
|
|
||
| private static final OkHttpClient HTTP_CLIENT = new OkHttpClient(); | ||
|
|
||
| /** | ||
| * JSON Path for main span Id. | ||
| */ | ||
| public static final String JSONPATH_MAIN_SPAN_ID = "$..[?(@.name == \"%s\")]['id']"; | ||
|
|
||
| /** | ||
| * JSON Path for child span Id where duration is greater than 1s. | ||
| */ | ||
| public static final String JSONPATH_SLEEP_SPAN_ID = | ||
| "$..[?(@.parentId=='%s' && @.duration > 1000000 && @.name=='%s')]['id']"; | ||
|
|
||
| public static void validate(String spanName, String sleepSpanName) throws Exception { | ||
| // Must wait for some time to make sure Zipkin receives all spans. | ||
| Thread.sleep(5000); | ||
| HttpUrl.Builder urlBuilder = new HttpUrl.Builder(); | ||
| urlBuilder.scheme("http") | ||
| .host("localhost") | ||
| .port(9411) | ||
| .addPathSegments("api/v2/traces"); | ||
| Request.Builder requestBuilder = new Request.Builder() | ||
| .url(urlBuilder.build()); | ||
| requestBuilder.method("GET", null); | ||
|
|
||
| Request request = requestBuilder.build(); | ||
|
|
||
| Response response = HTTP_CLIENT.newCall(request).execute(); | ||
| DocumentContext documentContext = JsonPath.parse(response.body().string()); | ||
| String mainSpanId = readOne(documentContext, String.format(JSONPATH_MAIN_SPAN_ID, spanName)).toString(); | ||
| assertNotNull(mainSpanId); | ||
|
|
||
| String sleepSpanId = readOne(documentContext, String.format(JSONPATH_SLEEP_SPAN_ID, mainSpanId, sleepSpanName)) | ||
| .toString(); | ||
| assertNotNull(sleepSpanId); | ||
| } | ||
|
|
||
| private static Object readOne(DocumentContext documentContext, String path) { | ||
| JSONArray arr = documentContext.read(path); | ||
| assertTrue(arr.size() > 0); | ||
|
|
||
| return arr.get(0); | ||
| } | ||
|
|
||
| } |
133 changes: 133 additions & 0 deletions
133
sdk-tests/src/test/java/io/dapr/it/tracing/grpc/Service.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /* | ||
| * Copyright (c) Microsoft Corporation and Dapr Contributors. | ||
| * Licensed under the MIT License. | ||
| */ | ||
|
|
||
| package io.dapr.it.tracing.grpc; | ||
|
|
||
| import com.google.protobuf.Any; | ||
| import io.dapr.v1.AppCallbackGrpc; | ||
| import io.dapr.v1.CommonProtos; | ||
| import io.grpc.Server; | ||
| import io.grpc.ServerBuilder; | ||
| import io.grpc.stub.StreamObserver; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import static io.dapr.it.MethodInvokeServiceProtos.DeleteMessageRequest; | ||
| import static io.dapr.it.MethodInvokeServiceProtos.DeleteMessageResponse; | ||
| import static io.dapr.it.MethodInvokeServiceProtos.GetMessagesRequest; | ||
| import static io.dapr.it.MethodInvokeServiceProtos.GetMessagesResponse; | ||
| import static io.dapr.it.MethodInvokeServiceProtos.PostMessageRequest; | ||
| import static io.dapr.it.MethodInvokeServiceProtos.PostMessageResponse; | ||
| import static io.dapr.it.MethodInvokeServiceProtos.SleepRequest; | ||
| import static io.dapr.it.MethodInvokeServiceProtos.SleepResponse; | ||
|
|
||
| public class Service { | ||
|
|
||
| public static final String SUCCESS_MESSAGE = "application discovered on port "; | ||
|
|
||
| /** | ||
| * Server mode: class that encapsulates all server-side logic for Grpc. | ||
| */ | ||
| private static class MyDaprService extends AppCallbackGrpc.AppCallbackImplBase { | ||
|
|
||
| /** | ||
| * Server mode: Grpc server. | ||
| */ | ||
| private Server server; | ||
|
|
||
| /** | ||
| * Server mode: starts listening on given port. | ||
| * | ||
| * @param port Port to listen on. | ||
| * @throws IOException Errors while trying to start service. | ||
| */ | ||
| private void start(int port) throws IOException { | ||
| this.server = ServerBuilder | ||
| .forPort(port) | ||
| .addService(this) | ||
| .build() | ||
| .start(); | ||
| System.out.printf("Server: started listening on port %d\n", port); | ||
|
|
||
| // Now we handle ctrl+c (or any other JVM shutdown) | ||
| Runtime.getRuntime().addShutdownHook(new Thread(() -> { | ||
| System.out.println("Server: shutting down gracefully ..."); | ||
| MyDaprService.this.server.shutdown(); | ||
| System.out.println("Server: Bye."); | ||
| })); | ||
| } | ||
|
|
||
| /** | ||
| * Server mode: waits for shutdown trigger. | ||
| * | ||
| * @throws InterruptedException Propagated interrupted exception. | ||
| */ | ||
| private void awaitTermination() throws InterruptedException { | ||
| if (this.server != null) { | ||
| this.server.awaitTermination(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Server mode: this is the Dapr method to receive Invoke operations via Grpc. | ||
| * | ||
| * @param request Dapr envelope request, | ||
| * @param responseObserver Dapr envelope response. | ||
| */ | ||
| @Override | ||
| public void onInvoke(CommonProtos.InvokeRequest request, | ||
| StreamObserver<CommonProtos.InvokeResponse> responseObserver) { | ||
| try { | ||
| if ("sleepOverGRPC".equals(request.getMethod())) { | ||
| SleepRequest req = SleepRequest.parseFrom(request.getData().getValue().toByteArray()); | ||
|
|
||
| SleepResponse res = this.sleep(req); | ||
|
|
||
| CommonProtos.InvokeResponse.Builder responseBuilder = CommonProtos.InvokeResponse.newBuilder(); | ||
| responseBuilder.setData(Any.pack(res)); | ||
| responseObserver.onNext(responseBuilder.build()); | ||
| } | ||
| } catch (Exception e) { | ||
| responseObserver.onError(e); | ||
| } finally { | ||
| responseObserver.onCompleted(); | ||
| } | ||
| } | ||
|
|
||
| public SleepResponse sleep(SleepRequest request) { | ||
| if (request.getSeconds() < 0) { | ||
| throw new IllegalArgumentException("Sleep time cannot be negative."); | ||
| } | ||
|
|
||
| try { | ||
| Thread.sleep(request.getSeconds() * 1000); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| new RuntimeException(e); | ||
| } | ||
|
|
||
| // Now respond with current timestamp. | ||
| return SleepResponse.newBuilder().build(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * This is the main method of this app. | ||
| * @param args The port to listen on. | ||
| * @throws Exception An Exception. | ||
| */ | ||
| public static void main(String[] args) throws Exception { | ||
| int port = Integer.parseInt(args[0]); | ||
|
|
||
| System.out.printf("Service starting on port %d ...\n", port); | ||
|
|
||
| final MyDaprService service = new MyDaprService(); | ||
| service.start(port); | ||
| service.awaitTermination(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth calling any specific API here to actually check the health? Or do we not really need to worry about a scenario where the socket is open but the underlying server isn't listening?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that happens, then we need to have a smarter logic here. Zipkin is installed way before this check, so we don't run into that race condition (yet).