From b04424bf3f7f0a620ce0992f0df1d4f8ca234a2a Mon Sep 17 00:00:00 2001 From: Christopher Meiklejohn Date: Wed, 15 Sep 2021 13:36:38 -0400 Subject: [PATCH 01/11] Integrate Filibuster. --- .../armeria-1.3/library/build.gradle.kts | 13 ++++++++++ .../armeria/v1_3/ArmeriaTracing.java | 14 ++++++----- .../grpc-1.6/library/build.gradle.kts | 14 +++++++++++ .../grpc/v1_6/GrpcTracing.java | 24 +++++++++++-------- 4 files changed, 49 insertions(+), 16 deletions(-) diff --git a/instrumentation/armeria-1.3/library/build.gradle.kts b/instrumentation/armeria-1.3/library/build.gradle.kts index fc599f337d78..80e61359e185 100644 --- a/instrumentation/armeria-1.3/library/build.gradle.kts +++ b/instrumentation/armeria-1.3/library/build.gradle.kts @@ -5,6 +5,19 @@ plugins { dependencies { library("com.linecorp.armeria:armeria:1.3.0") + library("cloud.filibuster:instrumentation:0.2-SNAPSHOT") testImplementation(project(":instrumentation:armeria-1.3:testing")) } + +repositories { + mavenCentral() + + maven { + url = uri("https://maven.pkg.github.com/filibuster-testing/filibuster-java") + credentials { + username = project.findProperty("gpr.user") as String? ?: System.getenv("GITHUB_USERNAME") + password = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") + } + } +} \ No newline at end of file diff --git a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaTracing.java b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaTracing.java index be751aa94ad4..d6ff7b0a30de 100644 --- a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaTracing.java +++ b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaTracing.java @@ -13,6 +13,8 @@ import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; import java.util.function.Function; +import cloud.filibuster.instrumentation.libraries.armeria.http.FilibusterDecoratingHttpClient; +import cloud.filibuster.instrumentation.libraries.armeria.http.FilibusterDecoratingHttpService; /** Entrypoint for tracing Armeria services or clients. */ public final class ArmeriaTracing { @@ -26,14 +28,14 @@ public static ArmeriaTracingBuilder newBuilder(OpenTelemetry openTelemetry) { return new ArmeriaTracingBuilder(openTelemetry); } - private final Instrumenter clientInstrumenter; - private final Instrumenter serverInstrumenter; +// private final Instrumenter clientInstrumenter; +// private final Instrumenter serverInstrumenter; ArmeriaTracing( Instrumenter clientInstrumenter, Instrumenter serverInstrumenter) { - this.clientInstrumenter = clientInstrumenter; - this.serverInstrumenter = serverInstrumenter; +// this.clientInstrumenter = clientInstrumenter; +// this.serverInstrumenter = serverInstrumenter; } /** @@ -41,7 +43,7 @@ public static ArmeriaTracingBuilder newBuilder(OpenTelemetry openTelemetry) { * com.linecorp.armeria.client.ClientBuilder#decorator(Function)}. */ public Function newClientDecorator() { - return client -> new OpenTelemetryClient(client, clientInstrumenter); + return client -> new FilibusterDecoratingHttpClient(client); } /** @@ -49,6 +51,6 @@ public static ArmeriaTracingBuilder newBuilder(OpenTelemetry openTelemetry) { * HttpService#decorate(Function)}. */ public Function newServiceDecorator() { - return service -> new OpenTelemetryService(service, serverInstrumenter); + return service -> new FilibusterDecoratingHttpService(service); } } diff --git a/instrumentation/grpc-1.6/library/build.gradle.kts b/instrumentation/grpc-1.6/library/build.gradle.kts index e67788d53241..79e886769525 100644 --- a/instrumentation/grpc-1.6/library/build.gradle.kts +++ b/instrumentation/grpc-1.6/library/build.gradle.kts @@ -7,6 +7,8 @@ val grpcVersion = "1.6.0" dependencies { library("io.grpc:grpc-core:$grpcVersion") + library("cloud.filibuster:instrumentation:0.2-SNAPSHOT") + testLibrary("io.grpc:grpc-netty:$grpcVersion") testLibrary("io.grpc:grpc-protobuf:$grpcVersion") testLibrary("io.grpc:grpc-services:$grpcVersion") @@ -15,3 +17,15 @@ dependencies { testImplementation("org.assertj:assertj-core") testImplementation(project(":instrumentation:grpc-1.6:testing")) } + +repositories { + mavenCentral() + + maven { + url = uri("https://maven.pkg.github.com/filibuster-testing/filibuster-java") + credentials { + username = project.findProperty("gpr.user") as String? ?: System.getenv("GITHUB_USERNAME") + password = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") + } + } +} \ No newline at end of file diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java index 2ed8cf35b490..68421ac358dc 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java @@ -11,6 +11,8 @@ import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.context.propagation.ContextPropagators; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; +import cloud.filibuster.instrumentation.libraries.grpc.FilibusterClientInterceptor; +import cloud.filibuster.instrumentation.libraries.grpc.FilibusterServerInterceptor; /** Entrypoint for tracing gRPC servers or clients. */ public final class GrpcTracing { @@ -25,20 +27,20 @@ public static GrpcTracingBuilder newBuilder(OpenTelemetry openTelemetry) { return new GrpcTracingBuilder(openTelemetry); } - private final Instrumenter serverInstrumenter; - private final Instrumenter clientInstrumenter; - private final ContextPropagators propagators; - private final boolean captureExperimentalSpanAttributes; +// private final Instrumenter serverInstrumenter; +// private final Instrumenter clientInstrumenter; +// private final ContextPropagators propagators; +// private final boolean captureExperimentalSpanAttributes; GrpcTracing( Instrumenter serverInstrumenter, Instrumenter clientInstrumenter, ContextPropagators propagators, boolean captureExperimentalSpanAttributes) { - this.serverInstrumenter = serverInstrumenter; - this.clientInstrumenter = clientInstrumenter; - this.propagators = propagators; - this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes; +// this.serverInstrumenter = serverInstrumenter; +// this.clientInstrumenter = clientInstrumenter; +// this.propagators = propagators; +// this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes; } /** @@ -46,7 +48,8 @@ public static GrpcTracingBuilder newBuilder(OpenTelemetry openTelemetry) { * io.grpc.ManagedChannelBuilder#intercept(ClientInterceptor...)}. */ public ClientInterceptor newClientInterceptor() { - return new TracingClientInterceptor(clientInstrumenter, propagators); + return new FilibusterClientInterceptor(); +// return new TracingClientInterceptor(clientInstrumenter, propagators); } /** @@ -54,6 +57,7 @@ public ClientInterceptor newClientInterceptor() { * io.grpc.ServerBuilder#intercept(ServerInterceptor)}. */ public ServerInterceptor newServerInterceptor() { - return new TracingServerInterceptor(serverInstrumenter, captureExperimentalSpanAttributes); + return new FilibusterServerInterceptor(); +// return new TracingServerInterceptor(serverInstrumenter, captureExperimentalSpanAttributes); } } From fa1ca92973b18acaaec052fba8a1e6f1a0b44836 Mon Sep 17 00:00:00 2001 From: Christopher Meiklejohn Date: Wed, 15 Sep 2021 13:41:31 -0400 Subject: [PATCH 02/11] Don't comment code, use suppress warnings. --- .../armeria/v1_3/ArmeriaTracing.java | 9 +++++---- .../instrumentation/grpc/v1_6/GrpcTracing.java | 17 +++++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaTracing.java b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaTracing.java index d6ff7b0a30de..35badaa7058a 100644 --- a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaTracing.java +++ b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaTracing.java @@ -17,6 +17,7 @@ import cloud.filibuster.instrumentation.libraries.armeria.http.FilibusterDecoratingHttpService; /** Entrypoint for tracing Armeria services or clients. */ +@SuppressWarnings("FieldCanBeLocal") public final class ArmeriaTracing { /** Returns a new {@link ArmeriaTracing} configured with the given {@link OpenTelemetry}. */ @@ -28,14 +29,14 @@ public static ArmeriaTracingBuilder newBuilder(OpenTelemetry openTelemetry) { return new ArmeriaTracingBuilder(openTelemetry); } -// private final Instrumenter clientInstrumenter; -// private final Instrumenter serverInstrumenter; + private final Instrumenter clientInstrumenter; + private final Instrumenter serverInstrumenter; ArmeriaTracing( Instrumenter clientInstrumenter, Instrumenter serverInstrumenter) { -// this.clientInstrumenter = clientInstrumenter; -// this.serverInstrumenter = serverInstrumenter; + this.clientInstrumenter = clientInstrumenter; + this.serverInstrumenter = serverInstrumenter; } /** diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java index 68421ac358dc..1a8d689ed75e 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java @@ -15,6 +15,7 @@ import cloud.filibuster.instrumentation.libraries.grpc.FilibusterServerInterceptor; /** Entrypoint for tracing gRPC servers or clients. */ +@SuppressWarnings("FieldCanBeLocal") public final class GrpcTracing { /** Returns a new {@link GrpcTracing} configured with the given {@link OpenTelemetry}. */ @@ -27,20 +28,20 @@ public static GrpcTracingBuilder newBuilder(OpenTelemetry openTelemetry) { return new GrpcTracingBuilder(openTelemetry); } -// private final Instrumenter serverInstrumenter; -// private final Instrumenter clientInstrumenter; -// private final ContextPropagators propagators; -// private final boolean captureExperimentalSpanAttributes; + private final Instrumenter serverInstrumenter; + private final Instrumenter clientInstrumenter; + private final ContextPropagators propagators; + private final boolean captureExperimentalSpanAttributes; GrpcTracing( Instrumenter serverInstrumenter, Instrumenter clientInstrumenter, ContextPropagators propagators, boolean captureExperimentalSpanAttributes) { -// this.serverInstrumenter = serverInstrumenter; -// this.clientInstrumenter = clientInstrumenter; -// this.propagators = propagators; -// this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes; + this.serverInstrumenter = serverInstrumenter; + this.clientInstrumenter = clientInstrumenter; + this.propagators = propagators; + this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes; } /** From ca3d0c58edc16299de4955059a3742f4af0c65ab Mon Sep 17 00:00:00 2001 From: Christopher Meiklejohn Date: Wed, 15 Sep 2021 13:42:44 -0400 Subject: [PATCH 03/11] Remove lines. --- .../io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java index 1a8d689ed75e..7c8d654b1910 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java @@ -50,7 +50,6 @@ public static GrpcTracingBuilder newBuilder(OpenTelemetry openTelemetry) { */ public ClientInterceptor newClientInterceptor() { return new FilibusterClientInterceptor(); -// return new TracingClientInterceptor(clientInstrumenter, propagators); } /** @@ -59,6 +58,5 @@ public ClientInterceptor newClientInterceptor() { */ public ServerInterceptor newServerInterceptor() { return new FilibusterServerInterceptor(); -// return new TracingServerInterceptor(serverInstrumenter, captureExperimentalSpanAttributes); } } From aacdc4b279ba9ebd94f910d4471d98ed9bf1badc Mon Sep 17 00:00:00 2001 From: Christopher Meiklejohn Date: Wed, 15 Sep 2021 19:14:09 -0400 Subject: [PATCH 04/11] Bump version. --- instrumentation/armeria-1.3/library/build.gradle.kts | 3 ++- instrumentation/grpc-1.6/library/build.gradle.kts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/instrumentation/armeria-1.3/library/build.gradle.kts b/instrumentation/armeria-1.3/library/build.gradle.kts index 80e61359e185..206204c58f25 100644 --- a/instrumentation/armeria-1.3/library/build.gradle.kts +++ b/instrumentation/armeria-1.3/library/build.gradle.kts @@ -5,7 +5,8 @@ plugins { dependencies { library("com.linecorp.armeria:armeria:1.3.0") - library("cloud.filibuster:instrumentation:0.2-SNAPSHOT") + + library("cloud.filibuster:instrumentation:0.3-SNAPSHOT") testImplementation(project(":instrumentation:armeria-1.3:testing")) } diff --git a/instrumentation/grpc-1.6/library/build.gradle.kts b/instrumentation/grpc-1.6/library/build.gradle.kts index 79e886769525..d803df43f2fb 100644 --- a/instrumentation/grpc-1.6/library/build.gradle.kts +++ b/instrumentation/grpc-1.6/library/build.gradle.kts @@ -7,7 +7,7 @@ val grpcVersion = "1.6.0" dependencies { library("io.grpc:grpc-core:$grpcVersion") - library("cloud.filibuster:instrumentation:0.2-SNAPSHOT") + library("cloud.filibuster:instrumentation:0.3-SNAPSHOT") testLibrary("io.grpc:grpc-netty:$grpcVersion") testLibrary("io.grpc:grpc-protobuf:$grpcVersion") From fc285d9425e492adf4347601215b7cc73a4e140e Mon Sep 17 00:00:00 2001 From: Christopher Meiklejohn Date: Mon, 25 Oct 2021 00:56:45 -0400 Subject: [PATCH 05/11] new version of Filibuster instrumentation --- .../armeria-1.3/library/build.gradle.kts | 7 +- .../armeria/v1_3/ArmeriaTracing.java | 6 +- .../v1_3/OpenTelemetryContextStorage.java | 94 ++++ ...lemetryFilibusterDecoratingHttpClient.java | 80 +++ ...emetryFilibusterDecoratingHttpService.java | 62 +++ .../grpc-1.6/library/build.gradle.kts | 7 +- .../grpc/v1_6/GrpcTracing.java | 6 +- .../v1_6/OpenTelemetryContextStorage.java | 95 ++++ ...nTelemetryFilibusterClientInterceptor.java | 458 ++++++++++++++++++ ...nTelemetryFilibusterServerInterceptor.java | 329 +++++++++++++ 10 files changed, 1132 insertions(+), 12 deletions(-) create mode 100644 instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryContextStorage.java create mode 100644 instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpClient.java create mode 100644 instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpService.java create mode 100644 instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryContextStorage.java create mode 100644 instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterClientInterceptor.java create mode 100644 instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterServerInterceptor.java diff --git a/instrumentation/armeria-1.3/library/build.gradle.kts b/instrumentation/armeria-1.3/library/build.gradle.kts index 206204c58f25..571406cf3c6b 100644 --- a/instrumentation/armeria-1.3/library/build.gradle.kts +++ b/instrumentation/armeria-1.3/library/build.gradle.kts @@ -6,7 +6,10 @@ plugins { dependencies { library("com.linecorp.armeria:armeria:1.3.0") - library("cloud.filibuster:instrumentation:0.3-SNAPSHOT") + library("cloud.filibuster:instrumentation:0.12-SNAPSHOT") + + library("com.github.cliftonlabs:json-simple:2.1.2") + library("org.json:json:20210307") testImplementation(project(":instrumentation:armeria-1.3:testing")) } @@ -21,4 +24,4 @@ repositories { password = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") } } -} \ No newline at end of file +} diff --git a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaTracing.java b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaTracing.java index 35badaa7058a..1b4e01e8da3e 100644 --- a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaTracing.java +++ b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaTracing.java @@ -13,8 +13,6 @@ import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; import java.util.function.Function; -import cloud.filibuster.instrumentation.libraries.armeria.http.FilibusterDecoratingHttpClient; -import cloud.filibuster.instrumentation.libraries.armeria.http.FilibusterDecoratingHttpService; /** Entrypoint for tracing Armeria services or clients. */ @SuppressWarnings("FieldCanBeLocal") @@ -44,7 +42,7 @@ public static ArmeriaTracingBuilder newBuilder(OpenTelemetry openTelemetry) { * com.linecorp.armeria.client.ClientBuilder#decorator(Function)}. */ public Function newClientDecorator() { - return client -> new FilibusterDecoratingHttpClient(client); + return client -> new OpenTelemetryFilibusterDecoratingHttpClient(client, clientInstrumenter); } /** @@ -52,6 +50,6 @@ public static ArmeriaTracingBuilder newBuilder(OpenTelemetry openTelemetry) { * HttpService#decorate(Function)}. */ public Function newServiceDecorator() { - return service -> new FilibusterDecoratingHttpService(service); + return service -> new OpenTelemetryFilibusterDecoratingHttpService(service, serverInstrumenter); } } diff --git a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryContextStorage.java b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryContextStorage.java new file mode 100644 index 000000000000..485c0a9e69dd --- /dev/null +++ b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryContextStorage.java @@ -0,0 +1,94 @@ +package io.opentelemetry.instrumentation.armeria.v1_3; + +import cloud.filibuster.instrumentation.datatypes.VectorClock; +import cloud.filibuster.instrumentation.storage.ContextStorage; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.ContextKey; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.annotation.Nullable; + +public class OpenTelemetryContextStorage implements ContextStorage { + private static final Logger logger = Logger.getLogger(OpenTelemetryContextStorage.class.getName()); + + private Context context; + + // TODO: Context.current() should maybe be cached, who knows? + + public OpenTelemetryContextStorage() { + this.context = Context.current(); + } + + public Context getContext() { + return this.context; + } + + final private static ContextKey vClockKey = ContextKey.named("filibuster-vclock"); + final private static ContextKey originVclockKey = ContextKey.named("filibuster-origin-vclock"); + final private static ContextKey requestIdKey = ContextKey.named("filibuster-request-id"); + final private static ContextKey executionIndexKey = ContextKey.named("filibuster-execution-index"); + + @Override + @Nullable + public String getRequestId() { + return Context.current().get(requestIdKey); + } + + @Override + @Nullable + public VectorClock getVectorClock() { + String vectorClockStr = Context.current().get(vClockKey); + + VectorClock newVclock = new VectorClock(); + + if (vectorClockStr != null) { + newVclock.fromString(vectorClockStr); + } + + return newVclock; + } + + @Override + @Nullable + public VectorClock getOriginVectorClock() { + String originVectorClockStr = Context.current().get(originVclockKey); + + VectorClock newVclock = new VectorClock(); + + if (originVectorClockStr != null) { + newVclock.fromString(originVectorClockStr); + } + + return newVclock; + } + + @Override + @Nullable + public String getExecutionIndex() { + return Context.current().get(executionIndexKey); + } + + @Override + public void setRequestId(String requestId) { + this.context = this.context.with(requestIdKey, requestId); + logger.log(Level.SEVERE, "setRequestId: " + requestId); + } + + @Override + public void setVectorClock(VectorClock vectorClock) { + this.context = this.context.with(vClockKey, vectorClock.toString()); + logger.log(Level.SEVERE, "setVectorClock: " + vectorClock); + } + + @Override + public void setOriginVectorClock(VectorClock originVectorClock) { + this.context = this.context.with(originVclockKey, originVectorClock.toString()); + logger.log(Level.SEVERE, "setOriginVectorClock: " + originVectorClock); + } + + @Override + public void setExecutionIndex(String executionIndex) { + this.context = this.context.with(executionIndexKey, executionIndex); + logger.log(Level.SEVERE, "setExecutionIndex: " + executionIndex); + } +} diff --git a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpClient.java b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpClient.java new file mode 100644 index 000000000000..83267028fdf3 --- /dev/null +++ b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpClient.java @@ -0,0 +1,80 @@ +package io.opentelemetry.instrumentation.armeria.v1_3; + +import cloud.filibuster.instrumentation.libraries.armeria.http.FilibusterDecoratingHttpClient; +import com.linecorp.armeria.client.ClientRequestContext; +import com.linecorp.armeria.client.HttpClient; +import com.linecorp.armeria.common.HttpRequest; +import com.linecorp.armeria.common.HttpResponse; +import com.linecorp.armeria.common.logging.RequestLog; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class OpenTelemetryFilibusterDecoratingHttpClient extends FilibusterDecoratingHttpClient { + private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterDecoratingHttpClient.class.getName()); + + private final Instrumenter clientInstrumentor; + + @SuppressWarnings("NullAway") + private Context parentContext; + + @SuppressWarnings("NullAway") + private Context context; + + public OpenTelemetryFilibusterDecoratingHttpClient(HttpClient delegate, String serviceName, Instrumenter clientInstrumentor) { + super(delegate); + this.serviceName = serviceName; + this.clientInstrumentor = clientInstrumentor; + this.contextStorage = new OpenTelemetryContextStorage(); + } + + public OpenTelemetryFilibusterDecoratingHttpClient(HttpClient delegate, Instrumenter clientInstrumentor) { + super(delegate); + this.serviceName = System.getenv("SERVICE_NAME"); + this.clientInstrumentor = clientInstrumentor; + this.contextStorage = new OpenTelemetryContextStorage(); + } + + @Override + protected void setupContext(ClientRequestContext ctx, HttpRequest req) { + this.parentContext = Context.current(); + this.context = Context.current(); + + logger.log(Level.INFO, "****************************************************************"); + logger.log(Level.SEVERE, "CLIENT parentContext: " + parentContext.toString()); + logger.log(Level.INFO, "****************************************************************"); + + if (clientInstrumentor != null) { + this.context = clientInstrumentor.start(Context.current(), ctx); + } + + String x = contextStorage.getExecutionIndex(); + + logger.log(Level.INFO, "****************************************************************"); + logger.log(Level.SEVERE, "CLIENT context: " + context.toString()); + logger.log(Level.INFO, "****************************************************************"); + } + + @Override + protected void contextWhenComplete(ClientRequestContext ctx) { + ctx.log().whenComplete().thenAccept(log -> { + if (clientInstrumentor != null) { + clientInstrumentor.end(context, ctx, log, log.responseCause()); + } + }); + } + + @Override + protected HttpResponse delegateWithContext(ClientRequestContext ctx, HttpRequest req) throws Exception { + HttpResponse response; + + try (Scope ignored = context.makeCurrent()) { + logger.log(Level.INFO, "!!!!!!! with context: " + context.toString()); + response = unwrap().execute(ctx, req); + } + + return response; + } +} diff --git a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpService.java b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpService.java new file mode 100644 index 000000000000..717cef74467a --- /dev/null +++ b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpService.java @@ -0,0 +1,62 @@ +package io.opentelemetry.instrumentation.armeria.v1_3; + +import cloud.filibuster.instrumentation.libraries.armeria.http.FilibusterDecoratingHttpService; +import com.linecorp.armeria.common.HttpRequest; +import com.linecorp.armeria.common.HttpResponse; +import com.linecorp.armeria.common.logging.RequestLog; +import com.linecorp.armeria.server.HttpService; +import com.linecorp.armeria.server.ServiceRequestContext; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; +import java.util.logging.Logger; + +public class OpenTelemetryFilibusterDecoratingHttpService extends FilibusterDecoratingHttpService { + private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterDecoratingHttpService.class.getName()); + + private final Instrumenter serverInstrumenter; + + @SuppressWarnings("NullAway") + private Context context; + + public OpenTelemetryFilibusterDecoratingHttpService(HttpService delegate, String serviceName, Instrumenter serverInstrumenter) { + super(delegate); + this.serviceName = serviceName; + this.serverInstrumenter = serverInstrumenter; + this.contextStorage = new OpenTelemetryContextStorage(); + } + + public OpenTelemetryFilibusterDecoratingHttpService(HttpService delegate, Instrumenter serverInstrumenter) { + super(delegate); + this.serviceName = System.getenv("SERVICE_NAME"); + this.serverInstrumenter = serverInstrumenter; + this.contextStorage = new OpenTelemetryContextStorage(); + } + + @Override + protected void setupContext(ServiceRequestContext ctx, HttpRequest req) { + OpenTelemetryContextStorage openTelemetryContextStorage = (OpenTelemetryContextStorage) this.contextStorage; + context = openTelemetryContextStorage.getContext(); + + if (serverInstrumenter != null) { + context = serverInstrumenter.start(context, ctx); + } + } + + @Override + protected void contextWhenComplete(ServiceRequestContext ctx) { + ctx.log().whenComplete().thenAccept(log -> { + if (serverInstrumenter != null) { + serverInstrumenter.end(context, ctx, log, log.responseCause()); + } + }); + } + + @Override + protected HttpResponse delegateWithContext(ServiceRequestContext ctx, HttpRequest req) throws Exception { + try (Scope ignored = context.makeCurrent()) { + HttpService delegate = (HttpService) unwrap(); + return delegate.serve(ctx, req); + } + } +} diff --git a/instrumentation/grpc-1.6/library/build.gradle.kts b/instrumentation/grpc-1.6/library/build.gradle.kts index d803df43f2fb..ebd3e3f113c8 100644 --- a/instrumentation/grpc-1.6/library/build.gradle.kts +++ b/instrumentation/grpc-1.6/library/build.gradle.kts @@ -7,13 +7,16 @@ val grpcVersion = "1.6.0" dependencies { library("io.grpc:grpc-core:$grpcVersion") - library("cloud.filibuster:instrumentation:0.3-SNAPSHOT") + library("cloud.filibuster:instrumentation:0.12-SNAPSHOT") testLibrary("io.grpc:grpc-netty:$grpcVersion") testLibrary("io.grpc:grpc-protobuf:$grpcVersion") testLibrary("io.grpc:grpc-services:$grpcVersion") testLibrary("io.grpc:grpc-stub:$grpcVersion") + library("com.github.cliftonlabs:json-simple:2.1.2") + library("org.json:json:20210307") + testImplementation("org.assertj:assertj-core") testImplementation(project(":instrumentation:grpc-1.6:testing")) } @@ -28,4 +31,4 @@ repositories { password = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") } } -} \ No newline at end of file +} diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java index 7c8d654b1910..8d6197697955 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTracing.java @@ -11,8 +11,6 @@ import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.context.propagation.ContextPropagators; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; -import cloud.filibuster.instrumentation.libraries.grpc.FilibusterClientInterceptor; -import cloud.filibuster.instrumentation.libraries.grpc.FilibusterServerInterceptor; /** Entrypoint for tracing gRPC servers or clients. */ @SuppressWarnings("FieldCanBeLocal") @@ -49,7 +47,7 @@ public static GrpcTracingBuilder newBuilder(OpenTelemetry openTelemetry) { * io.grpc.ManagedChannelBuilder#intercept(ClientInterceptor...)}. */ public ClientInterceptor newClientInterceptor() { - return new FilibusterClientInterceptor(); + return new OpenTelemetryFilibusterClientInterceptor(clientInstrumenter, propagators); } /** @@ -57,6 +55,6 @@ public ClientInterceptor newClientInterceptor() { * io.grpc.ServerBuilder#intercept(ServerInterceptor)}. */ public ServerInterceptor newServerInterceptor() { - return new FilibusterServerInterceptor(); + return new OpenTelemetryFilibusterServerInterceptor(serverInstrumenter); } } diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryContextStorage.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryContextStorage.java new file mode 100644 index 000000000000..162aad5ad0f4 --- /dev/null +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryContextStorage.java @@ -0,0 +1,95 @@ +package io.opentelemetry.instrumentation.grpc.v1_6; + +import cloud.filibuster.instrumentation.datatypes.VectorClock; +import cloud.filibuster.instrumentation.storage.ContextStorage; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.ContextKey; + +import javax.annotation.Nullable; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class OpenTelemetryContextStorage implements ContextStorage { + private static final Logger logger = Logger.getLogger(OpenTelemetryContextStorage.class.getName()); + + private Context context; + + // TODO: Context.current() should maybe be cached, who knows? + + public OpenTelemetryContextStorage() { + this.context = Context.current(); + } + + public Context getContext() { + return this.context; + } + + final private static ContextKey vClockKey = ContextKey.named("filibuster-vclock"); + final private static ContextKey originVclockKey = ContextKey.named("filibuster-origin-vclock"); + final private static ContextKey requestIdKey = ContextKey.named("filibuster-request-id"); + final private static ContextKey executionIndexKey = ContextKey.named("filibuster-execution-index"); + + @Override + @Nullable + public String getRequestId() { + return Context.current().get(requestIdKey); + } + + @Override + @Nullable + public VectorClock getVectorClock() { + String vectorClockStr = Context.current().get(vClockKey); + + VectorClock newVclock = new VectorClock(); + + if (vectorClockStr != null) { + newVclock.fromString(vectorClockStr); + } + + return newVclock; + } + + @Override + @Nullable + public VectorClock getOriginVectorClock() { + String originVectorClockStr = Context.current().get(originVclockKey); + + VectorClock newVclock = new VectorClock(); + + if (originVectorClockStr != null) { + newVclock.fromString(originVectorClockStr); + } + + return newVclock; + } + + @Override + @Nullable + public String getExecutionIndex() { + return Context.current().get(executionIndexKey); + } + + @Override + public void setRequestId(String requestId) { + this.context = this.context.with(requestIdKey, requestId); + logger.log(Level.SEVERE, "setRequestId: " + requestId); + } + + @Override + public void setVectorClock(VectorClock vectorClock) { + this.context = this.context.with(vClockKey, vectorClock.toString()); + logger.log(Level.SEVERE, "setVectorClock: " + vectorClock); + } + + @Override + public void setOriginVectorClock(VectorClock originVectorClock) { + this.context = this.context.with(originVclockKey, originVectorClock.toString()); + logger.log(Level.SEVERE, "setOriginVectorClock: " + originVectorClock); + } + + @Override + public void setExecutionIndex(String executionIndex) { + this.context = this.context.with(executionIndexKey, executionIndex); + logger.log(Level.SEVERE, "setExecutionIndex: " + executionIndex); + } +} diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterClientInterceptor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterClientInterceptor.java new file mode 100644 index 000000000000..aeb334ab2c04 --- /dev/null +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterClientInterceptor.java @@ -0,0 +1,458 @@ +package io.opentelemetry.instrumentation.grpc.v1_6; + +import static cloud.filibuster.instrumentation.Helper.getDisableInstrumentationFromEnvironment; +import static cloud.filibuster.instrumentation.Helper.getDisableServerCommunicationFromEnvironment; + +import cloud.filibuster.instrumentation.datatypes.VectorClock; +import cloud.filibuster.instrumentation.instrumentors.FilibusterClientInstrumentor; +import cloud.filibuster.instrumentation.storage.ContextStorage; +import io.grpc.CallOptions; +import io.grpc.Channel; +import io.grpc.ClientCall; +import io.grpc.ClientInterceptor; +import io.grpc.ForwardingClientCall; +import io.grpc.ForwardingClientCallListener; +import io.grpc.Metadata; +import io.grpc.MethodDescriptor; +import io.grpc.Status; +import io.grpc.StatusRuntimeException; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.context.propagation.ContextPropagators; +import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; +import java.util.HashMap; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.json.JSONObject; + +public class OpenTelemetryFilibusterClientInterceptor implements ClientInterceptor { + private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterClientInterceptor.class.getName()); + + @SuppressWarnings("FieldCanBeFinal") + protected String serviceName; + + @SuppressWarnings("FieldCanBeFinal") + protected ContextStorage contextStorage; + + public static Boolean disableServerCommunication = false; + public static Boolean disableInstrumentation = false; + + private final Instrumenter clientInstrumentor; + + @SuppressWarnings("FieldCanBeLocal") + private final ContextPropagators propagators; + + private static boolean shouldInstrument() { + if (disableInstrumentation) { + return false; + } + + return !getDisableInstrumentationFromEnvironment(); + } + + private static boolean shouldCommunicateWithServer() { + if (disableServerCommunication) { + return false; + } + + return !getDisableServerCommunicationFromEnvironment(); + } + + private static Status generateCorrectStatusForAbort(FilibusterClientInstrumentor filibusterClientInstrumentor) { + JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); + JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); + String codeStr = forcedExceptionMetadata.getString("code"); + Status.Code code = Status.Code.valueOf(codeStr); + Status status = Status.fromCode(code); + return status; + } + + private static void generateAndThrowException(FilibusterClientInstrumentor filibusterClientInstrumentor) { + JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); + + // Create the exception to throw. + String exceptionNameString = forcedException.getString("name"); + JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); + String causeString = forcedExceptionMetadata.getString("cause"); + String codeStr = forcedExceptionMetadata.getString("code"); + Status.Code code = Status.Code.valueOf(codeStr); + StatusRuntimeException status = Status.fromCode(code).asRuntimeException(); + + // Notify Filibuster of failure. + HashMap additionalMetadata = new HashMap<>(); + additionalMetadata.put("code", codeStr); + filibusterClientInstrumentor.afterInvocationWithException(exceptionNameString, causeString, additionalMetadata); + + // Throw the runtime exception. + throw status; + } + + private static void generateAndThrowExceptionFromFailureMetadata(FilibusterClientInstrumentor filibusterClientInstrumentor) { + JSONObject failureMetadata = filibusterClientInstrumentor.getFailureMetadata(); + JSONObject exception = failureMetadata.getJSONObject("exception"); + JSONObject exceptionMetadata = exception.getJSONObject("metadata"); + + // Create the exception to throw. + String exceptionNameString = "io.grpc.StatusRuntimeException"; + String codeStr = exceptionMetadata.getString("code"); + Status.Code code = Status.Code.valueOf(codeStr); + StatusRuntimeException status = Status.fromCode(code).asRuntimeException(); + String causeString = ""; + + // Notify Filibuster of failure. + HashMap additionalMetadata = new HashMap<>(); + additionalMetadata.put("name", exceptionNameString); + additionalMetadata.put("code", codeStr); + filibusterClientInstrumentor.afterInvocationWithException(exceptionNameString, causeString, additionalMetadata); + + // Throw the runtime exception. + throw status; + } + + public OpenTelemetryFilibusterClientInterceptor(Instrumenter clientInstrumentor, ContextPropagators propagators) { + this.serviceName = System.getenv("SERVICE_NAME"); + this.contextStorage = new OpenTelemetryContextStorage(); + this.clientInstrumentor = clientInstrumentor; + this.propagators = propagators; + } + + public OpenTelemetryFilibusterClientInterceptor(String serviceName, Instrumenter clientInstrumentor, ContextPropagators propagators) { + this.serviceName = serviceName; + this.contextStorage = new OpenTelemetryContextStorage(); + this.clientInstrumentor = clientInstrumentor; + this.propagators = propagators; + } + + @Override + public ClientCall interceptCall( + MethodDescriptor method, CallOptions callOptions, Channel next) { + GrpcRequest request = new GrpcRequest(method, null, null); + Context parentContext = Context.current(); + + Context context; + + if (clientInstrumentor != null) { + context = clientInstrumentor.start(parentContext, request); + } else { + context = Context.current(); + } + + // call other interceptors first. + final ClientCall result = next.newCall(method, callOptions); + + // return the filibuster client interceptor. + return new FilibusterClientCall<>(result, parentContext, context, request, serviceName, contextStorage, method); + } + + // ********************************************************************* + // Client caller. + + final class FilibusterClientCall + extends ForwardingClientCall.SimpleForwardingClientCall { + + final private String serviceName; + final private MethodDescriptor method; + final private ContextStorage contextStorage; + private final Context parentContext; + private final Context context; + private final GrpcRequest request; + + FilibusterClientCall(ClientCall delegate, + Context parentContext, + Context context, + GrpcRequest request, + String serviceName, + ContextStorage contextStorage, + MethodDescriptor method) { + super(delegate); + this.serviceName = serviceName; + this.method = method; + this.contextStorage = contextStorage; + this.parentContext = parentContext; + this.context = context; + this.request = request; + } + + // ****************************************************************************************** + // Accessors for metadata. + // ****************************************************************************************** + + public String getRequestIdFromMetadata() { + return contextStorage.getRequestId(); + } + + public VectorClock getVectorClockFromMetadata() { + return contextStorage.getVectorClock(); + } + + public VectorClock getOriginVectorClockFromMetadata() { + return contextStorage.getOriginVectorClock(); + } + + public String getExecutionIndexFromMetadata() { + return contextStorage.getExecutionIndex(); + } + + // ****************************************************************************************** + // Implementation. + // ****************************************************************************************** + + @Override + public void start(Listener responseListener, Metadata headers) { + logger.log(Level.INFO, "INSIDE: start!"); + + // ****************************************************************************************** + // Extract callsite information. + // ****************************************************************************************** + + String grpcFullMethodName = method.getFullMethodName(); + String grpcRpcName = grpcFullMethodName.substring(grpcFullMethodName.indexOf("/") + 1); + String grpcServiceName = grpcFullMethodName.substring(0, grpcFullMethodName.indexOf("/")); + + logger.log(Level.INFO, "grpcFullMethodName: " + grpcFullMethodName); + logger.log(Level.INFO, "grpcServiceName: " + grpcServiceName); + logger.log(Level.INFO, "grpcRpcName: " + grpcRpcName); + + // ****************************************************************************************** + // Figure out if we are inside of instrumentation. + // ****************************************************************************************** + + String instrumentationRequestStr = headers.get( + Metadata.Key.of("x-filibuster-instrumentation", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "instrumentationRequestStr: " + instrumentationRequestStr); + boolean instrumentationRequest = Boolean.parseBoolean(instrumentationRequestStr); + logger.log(Level.INFO, "instrumentationRequest: " + instrumentationRequest); + + if (! shouldInstrument() || instrumentationRequest) { + try (Scope ignored = context.makeCurrent()) { + super.start(responseListener, headers); + } catch (Throwable e) { + if (clientInstrumentor != null) { + clientInstrumentor.end(context, request, null, e); + } + + throw e; + } + } else { + // ****************************************************************************************** + // Prepare for invocation. + // ****************************************************************************************** + + FilibusterClientInstrumentor filibusterClientInstrumentor = new FilibusterClientInstrumentor( + serviceName, + shouldCommunicateWithServer(), + contextStorage + ); + filibusterClientInstrumentor.prepareForInvocation(); + + // ****************************************************************************************** + // Record invocation. + // ****************************************************************************************** + + filibusterClientInstrumentor.beforeInvocation(grpcServiceName, grpcFullMethodName, ""); + + JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); + JSONObject failureMetadata = filibusterClientInstrumentor.getFailureMetadata(); + + logger.log(Level.INFO, "forcedException: " + forcedException); + logger.log(Level.INFO, "failureMetadata: " + failureMetadata); + + // ****************************************************************************************** + // Attach metadata to outgoing request. + // ****************************************************************************************** + + logger.log(Level.INFO, "requestId: " + filibusterClientInstrumentor.getRequestId()); + + if (filibusterClientInstrumentor.getRequestId() != null) { + headers.put( + Metadata.Key.of("x-filibuster-request-id", Metadata.ASCII_STRING_MARSHALLER), + filibusterClientInstrumentor.getRequestId() + ); + } + + if (filibusterClientInstrumentor.getGeneratedId() > -1) { + headers.put( + Metadata.Key.of("x-filibuster-generated-id", Metadata.ASCII_STRING_MARSHALLER), + String.valueOf(filibusterClientInstrumentor.getGeneratedId()) + ); + } + + headers.put( + Metadata.Key.of("x-filibuster-vclock", Metadata.ASCII_STRING_MARSHALLER), + filibusterClientInstrumentor.getVectorClock().toString() + ); + headers.put( + Metadata.Key.of("x-filibuster-origin-vclock", Metadata.ASCII_STRING_MARSHALLER), + filibusterClientInstrumentor.getOriginVectorClock().toString() + ); + headers.put( + Metadata.Key.of("x-filibuster-execution-index", Metadata.ASCII_STRING_MARSHALLER), + filibusterClientInstrumentor.getExecutionIndex().toString() + ); + + if (forcedException != null) { + JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); + + if (forcedExceptionMetadata.has("sleep")) { + int sleepInterval = forcedExceptionMetadata.getInt("sleep"); + headers.put( + Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER), + String.valueOf(sleepInterval) + ); + } else { + headers.put( + Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER), + String.valueOf(0) + ); + } + } + + // ****************************************************************************************** + // If we need to override the response, do it now before proceeding. + // ****************************************************************************************** + + if (failureMetadata != null && filibusterClientInstrumentor.shouldAbort()) { + generateAndThrowExceptionFromFailureMetadata(filibusterClientInstrumentor); + } + + // ****************************************************************************************** + // If we need to throw, this is where we throw. + // ****************************************************************************************** + + if (forcedException != null && filibusterClientInstrumentor.shouldAbort()) { + generateAndThrowException(filibusterClientInstrumentor); + } + + // ****************************************************************************************** + // Issue request. + // ****************************************************************************************** + + try (Scope ignored = context.makeCurrent()) { + super.start(new FilibusterClientCallListener<>( + responseListener, parentContext, context, request, filibusterClientInstrumentor), headers); + } catch (Throwable e) { + if (clientInstrumentor != null) { + clientInstrumentor.end(context, request, null, e); + } + + throw e; + } + } + } + + // This method is invoked with the message from the Client to the Service. + // message: type of the message issued from the Client (e.g., Hello$HelloRequest) + @Override + public void sendMessage(REQUEST message) { + logger.log(Level.INFO, "INSIDE: sendMessage!"); + logger.log(Level.INFO, "message: " + message.toString()); + try (Scope ignored = context.makeCurrent()) { + super.sendMessage(message); + } catch (Throwable e) { + if (clientInstrumentor != null) { + clientInstrumentor.end(context, request, null, e); + } + + throw e; + } + } + } + + // ********************************************************************* + // Client caller listener. + + @SuppressWarnings("ClassCanBeStatic") + final class FilibusterClientCallListener + extends ForwardingClientCallListener.SimpleForwardingClientCallListener { + + private final FilibusterClientInstrumentor filibusterClientInstrumentor; + private final Context parentContext; + private final Context context; + private final GrpcRequest request; + + FilibusterClientCallListener(ClientCall.Listener delegate, + Context parentContext, + Context context, + GrpcRequest request, + FilibusterClientInstrumentor filibusterClientInstrumentor) { + super(delegate); + this.filibusterClientInstrumentor = filibusterClientInstrumentor; + this.parentContext = parentContext; + this.context = context; + this.request = request; + } + + // invoked on successful response with the message from the Server to the Client + // message: type of message issued from the Server to the Client (e.g., Hello$HelloReply) + @Override + public void onMessage(RESPONSE message) { + logger.log(Level.INFO, "INSIDE: onMessage!"); + logger.log(Level.INFO, "message: " + message); + + if (! filibusterClientInstrumentor.shouldAbort()) { + // Request completed normally, but we want to throw the exception anyway, generate and throw. + generateAndThrowException(filibusterClientInstrumentor); + } else { + // Request completed normally. + + // Notify Filibuster of complete invocation with the proper response. + String className = message.getClass().getName(); + HashMap returnValueProperties = new HashMap<>(); + filibusterClientInstrumentor.afterInvocationComplete(className, returnValueProperties); + + // Delegate. + try (Scope ignored = context.makeCurrent()) { + delegate().onMessage(message); + } + } + } + + // invoked on an error: status set to a status message + // Status.code = FAILED_PRECONDITION, description = ..., cause = ... + // trailers metadata headers. + @Override + public void onClose(Status status, Metadata trailers) { + if (clientInstrumentor != null) { + clientInstrumentor.end(context, request, status, status.getCause()); + } + + logger.log(Level.INFO, "INSIDE: onClose!"); + logger.log(Level.INFO, "status: " + status); + logger.log(Level.INFO, "trailers: " + trailers); + + if (! filibusterClientInstrumentor.shouldAbort()) { + Status rewrittenStatus = generateCorrectStatusForAbort(filibusterClientInstrumentor); + + try (Scope ignored = parentContext.makeCurrent()) { + delegate().onClose(rewrittenStatus, trailers); + } + } + + if (! status.isOk()) { + // Request completed -- if it completed with a failure, it will be coming here for + // the first time (didn't call onMessage) and therefore, we need to notify the Filibuster + // server that the call completed with failure. If it completed successfully, we would + // have already notified the Filibuster server in the onMessage callback. + + // Notify Filibuster of error. + HashMap additionalMetadata = new HashMap<>(); + additionalMetadata.put("code", status.getCode().toString()); + String exceptionName = "io.grpc.StatusRuntimeException"; + // exception cause is always null, because it doesn't serialize and pass through even if provided. + filibusterClientInstrumentor.afterInvocationWithException(exceptionName, null, additionalMetadata); + } + + try (Scope ignored = parentContext.makeCurrent()) { + delegate().onClose(status, trailers); + } + } + + @Override + public void onReady() { + logger.log(Level.INFO, "INSIDE: onReady!"); + try (Scope ignored = context.makeCurrent()) { + delegate().onReady(); + } + } + } +} diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterServerInterceptor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterServerInterceptor.java new file mode 100644 index 000000000000..686b124b5815 --- /dev/null +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterServerInterceptor.java @@ -0,0 +1,329 @@ +package io.opentelemetry.instrumentation.grpc.v1_6; + +import static cloud.filibuster.instrumentation.Helper.getDisableInstrumentationFromEnvironment; +import static cloud.filibuster.instrumentation.Helper.getDisableServerCommunicationFromEnvironment; + +import cloud.filibuster.instrumentation.Helper; +import cloud.filibuster.instrumentation.instrumentors.FilibusterServerInstrumentor; +import cloud.filibuster.instrumentation.storage.ContextStorage; +import io.grpc.Contexts; +import io.grpc.ForwardingServerCall; +import io.grpc.ForwardingServerCallListener; +import io.grpc.Grpc; +import io.grpc.Metadata; +import io.grpc.ServerCall; +import io.grpc.ServerCallHandler; +import io.grpc.ServerInterceptor; +import io.grpc.Status; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class OpenTelemetryFilibusterServerInterceptor implements ServerInterceptor { + private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterServerInterceptor.class.getName()); + + @SuppressWarnings("FieldCanBeFinal") + protected String serviceName; + + @SuppressWarnings("FieldCanBeFinal") + protected ContextStorage contextStorage; + + public static Boolean disableServerCommunication = false; + public static Boolean disableInstrumentation = false; + + private final Instrumenter serverInstrumentor; + + private static boolean shouldInstrument() { + if (disableInstrumentation) { + return false; + } + + return !getDisableInstrumentationFromEnvironment(); + } + + private static boolean shouldCommunicateWithServer() { + if (disableServerCommunication) { + return false; + } + + return !getDisableServerCommunicationFromEnvironment(); + } + + public OpenTelemetryFilibusterServerInterceptor(Instrumenter serverInstrumentor) { + this.serviceName = System.getenv("SERVICE_NAME"); + this.contextStorage = new OpenTelemetryContextStorage(); + this.serverInstrumentor = serverInstrumentor; + } + + public OpenTelemetryFilibusterServerInterceptor(String serviceName, Instrumenter serverInstrumentor) { + this.serviceName = serviceName; + this.contextStorage = new OpenTelemetryContextStorage(); + this.serverInstrumentor = serverInstrumentor; + } + + @Override + public ServerCall.Listener interceptCall( + ServerCall call, + Metadata headers, + ServerCallHandler next) { + GrpcRequest request = + new GrpcRequest( + call.getMethodDescriptor(), + headers, + call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)); + Context context; + + if (serverInstrumentor != null) { + context = serverInstrumentor.start(Context.current(), request); + } else { + context = Context.current(); + } + + try (Scope ignored = context.makeCurrent()) { + return new FilibusterServerCall<>(call, context, request, headers).start(headers, next); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } + + throw e; + } + } + + final class FilibusterServerCall + extends ForwardingServerCall.SimpleForwardingServerCall { + final private Metadata requestHeaders; + + @SuppressWarnings("FieldCanBeLocal") + private final Context context; + + @SuppressWarnings("FieldCanBeLocal") + private final GrpcRequest request; + + private String requestId; + + public FilibusterServerCall(ServerCall delegate, Context context, GrpcRequest request, Metadata requestHeaders) { + super(delegate); + this.requestHeaders = requestHeaders; + this.context = context; + this.request = request; + } + + FilibusterServerCallListener start(Metadata headers, ServerCallHandler next) { + return new FilibusterServerCallListener( + Contexts.interceptCall(io.grpc.Context.current(), this, headers, next), context, request); + } + + // ****************************************************************************************** + // Accessors for metadata. + // ****************************************************************************************** + + public String getRequestIdFromMetadata(Metadata requestHeaders) { + if (this.requestId == null) { + this.requestId = requestHeaders.get(Metadata.Key.of("x-filibuster-request-id", Metadata.ASCII_STRING_MARSHALLER)); + + if (this.requestId == null) { + this.requestId = Helper.generateNewRequestId().toString(); + } + } + + logger.log(Level.INFO, "requestId: " + this.requestId); + return this.requestId; + } + + public String getGeneratedIdFromMetadata(Metadata requestHeaders) { + String generatedId = requestHeaders.get( + Metadata.Key.of("x-filibuster-generated-id", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "generateId: " + generatedId); + return generatedId; + } + + public String getVectorClockFromMetadata(Metadata requestHeaders) { + String vclock = requestHeaders.get( + Metadata.Key.of("x-filibuster-vclock", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "vclock: " + vclock); + return vclock; + } + + public String getOriginVectorClockFromMetadata(Metadata requestHeaders) { + String originVclock = requestHeaders.get( + Metadata.Key.of("x-filibuster-origin-vclock", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "originVclock: " + originVclock); + return originVclock; + } + + public String getExecutionIndexFromMetadata(Metadata requestHeaders) { + String executionIndex = requestHeaders.get( + Metadata.Key.of("x-filibuster-execution-index", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "executionIndex: " + executionIndex); + return executionIndex; + } + + // ****************************************************************************************** + // Implementation. + // ****************************************************************************************** + + @Override + public void sendMessage(RESPONSE message) { + try (Scope ignored = context.makeCurrent()) { + super.sendMessage(message); + } + } + + @Override + public void close(Status status, Metadata trailers) { + try { + delegate().close(status, trailers); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, status, e); + } + throw e; + } + + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, status, status.getCause()); + } + } + + @Override + public void sendHeaders(Metadata responseHeaders) { + if (!shouldInstrument()) { + try (Scope ignored = context.makeCurrent()) { + super.sendHeaders(responseHeaders); + } + } else { + logger.log(Level.INFO, "Entering server interceptor..."); + + // ****************************************************************************************** + // Setup Filibuster instrumentation. + // ****************************************************************************************** + + logger.log(Level.INFO, "!!! Entering constructor."); + + FilibusterServerInstrumentor filibusterServerInstrumentor = new FilibusterServerInstrumentor( + serviceName, + shouldCommunicateWithServer(), + getRequestIdFromMetadata(requestHeaders), + getGeneratedIdFromMetadata(requestHeaders), + getVectorClockFromMetadata(requestHeaders), + getOriginVectorClockFromMetadata(requestHeaders), + getExecutionIndexFromMetadata(requestHeaders), + contextStorage + ); + + logger.log(Level.INFO, "!!! Leaving constructor."); + + // ****************************************************************************************** + // Force sleep if necessary. + // ****************************************************************************************** + + String sleepIntervalStr = requestHeaders.get( + Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER)); + + if (sleepIntervalStr == null) { + sleepIntervalStr = "0"; + } + + int sleepInterval = Integer.parseInt(sleepIntervalStr); + if (sleepInterval > 0) { + try { + Thread.sleep(sleepInterval * 1000L); + } catch (InterruptedException e) { + // Do nothing. + } + } + + // ****************************************************************************************** + // Notify Filibuster before delegation. + // ****************************************************************************************** + + logger.log(Level.INFO, "!!! Entering beforeInvocation."); + + filibusterServerInstrumentor.beforeInvocation(); + + logger.log(Level.INFO, "!!! Leaving beforeInvocation."); + + // ****************************************************************************************** + // Delegate to underlying service. + // ****************************************************************************************** + + logger.log(Level.INFO, "Leaving server interceptor..."); + + try (Scope ignored = context.makeCurrent()) { + super.sendHeaders(responseHeaders); + } + } + } + + final class FilibusterServerCallListener + extends ForwardingServerCallListener.SimpleForwardingServerCallListener { + private final Context context; + private final GrpcRequest request; + + FilibusterServerCallListener(Listener delegate, Context context, GrpcRequest request) { + super(delegate); + this.context = context; + this.request = request; + } + + @Override + public void onMessage(REQUEST message) { + delegate().onMessage(message); + } + + @Override + public void onHalfClose() { + try { + delegate().onHalfClose(); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } + throw e; + } + } + + @Override + public void onCancel() { + try { + delegate().onCancel(); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } + throw e; + } + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, null); + } + } + + @Override + public void onComplete() { + try { + delegate().onComplete(); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } + throw e; + } + } + + @Override + public void onReady() { + try { + delegate().onReady(); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } + throw e; + } + } + } + } +} From bc3d03911bc17164d29c7708374cef535788d881 Mon Sep 17 00:00:00 2001 From: Christopher Meiklejohn Date: Wed, 27 Oct 2021 17:40:43 -0400 Subject: [PATCH 06/11] Bump version. --- instrumentation/armeria-1.3/library/build.gradle.kts | 2 +- instrumentation/grpc-1.6/library/build.gradle.kts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/armeria-1.3/library/build.gradle.kts b/instrumentation/armeria-1.3/library/build.gradle.kts index 571406cf3c6b..cd52fdabb217 100644 --- a/instrumentation/armeria-1.3/library/build.gradle.kts +++ b/instrumentation/armeria-1.3/library/build.gradle.kts @@ -6,7 +6,7 @@ plugins { dependencies { library("com.linecorp.armeria:armeria:1.3.0") - library("cloud.filibuster:instrumentation:0.12-SNAPSHOT") + library("cloud.filibuster:instrumentation:0.14-SNAPSHOT") library("com.github.cliftonlabs:json-simple:2.1.2") library("org.json:json:20210307") diff --git a/instrumentation/grpc-1.6/library/build.gradle.kts b/instrumentation/grpc-1.6/library/build.gradle.kts index ebd3e3f113c8..8340863e7c18 100644 --- a/instrumentation/grpc-1.6/library/build.gradle.kts +++ b/instrumentation/grpc-1.6/library/build.gradle.kts @@ -7,7 +7,7 @@ val grpcVersion = "1.6.0" dependencies { library("io.grpc:grpc-core:$grpcVersion") - library("cloud.filibuster:instrumentation:0.12-SNAPSHOT") + library("cloud.filibuster:instrumentation:0.14-SNAPSHOT") testLibrary("io.grpc:grpc-netty:$grpcVersion") testLibrary("io.grpc:grpc-protobuf:$grpcVersion") From a76adc0e95d7705d7e87f7a3e02090eb75ab4295 Mon Sep 17 00:00:00 2001 From: Christopher Meiklejohn Date: Sun, 21 Nov 2021 19:22:09 -0500 Subject: [PATCH 07/11] update implementation for 0.17-SNAPSHOT of filibuster --- instrumentation-api/build.gradle.kts | 2 +- .../OpenTelemetryContextStorageConstants.java | 10 + .../armeria-1.3/library/build.gradle.kts | 2 +- .../v1_3/OpenTelemetryContextStorage.java | 27 +- ...lemetryFilibusterDecoratingHttpClient.java | 121 +-- ...emetryFilibusterDecoratingHttpService.java | 77 +- .../grpc-1.6/library/build.gradle.kts | 2 +- .../v1_6/OpenTelemetryContextStorage.java | 31 +- ...nTelemetryFilibusterClientInterceptor.java | 753 +++++++++--------- ...nTelemetryFilibusterServerInterceptor.java | 533 +++++++------ 10 files changed, 798 insertions(+), 760 deletions(-) create mode 100644 instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/filibuster/OpenTelemetryContextStorageConstants.java diff --git a/instrumentation-api/build.gradle.kts b/instrumentation-api/build.gradle.kts index 0d10f0a89707..b01b458e9c7c 100644 --- a/instrumentation-api/build.gradle.kts +++ b/instrumentation-api/build.gradle.kts @@ -41,4 +41,4 @@ dependencies { testImplementation("org.awaitility:awaitility") testImplementation("io.opentelemetry:opentelemetry-sdk-metrics") testImplementation("io.opentelemetry:opentelemetry-sdk-testing") -} +} \ No newline at end of file diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/filibuster/OpenTelemetryContextStorageConstants.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/filibuster/OpenTelemetryContextStorageConstants.java new file mode 100644 index 000000000000..d35b25b9f58a --- /dev/null +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/filibuster/OpenTelemetryContextStorageConstants.java @@ -0,0 +1,10 @@ +package io.opentelemetry.instrumentation.api.filibuster; + +import io.opentelemetry.context.ContextKey; + +public class OpenTelemetryContextStorageConstants { + final public static ContextKey VCLOCK_KEY = ContextKey.named("filibuster-vclock"); + final public static ContextKey ORIGIN_VCLOCK_KEY = ContextKey.named("filibuster-origin-vclock"); + final public static ContextKey REQUEST_ID_KEY = ContextKey.named("filibuster-request-id"); + final public static ContextKey EXECUTION_INDEX_KEY = ContextKey.named("filibuster-execution-index"); +} diff --git a/instrumentation/armeria-1.3/library/build.gradle.kts b/instrumentation/armeria-1.3/library/build.gradle.kts index cd52fdabb217..ffe5b6933e78 100644 --- a/instrumentation/armeria-1.3/library/build.gradle.kts +++ b/instrumentation/armeria-1.3/library/build.gradle.kts @@ -6,7 +6,7 @@ plugins { dependencies { library("com.linecorp.armeria:armeria:1.3.0") - library("cloud.filibuster:instrumentation:0.14-SNAPSHOT") + library("cloud.filibuster:instrumentation:0.17-SNAPSHOT") library("com.github.cliftonlabs:json-simple:2.1.2") library("org.json:json:20210307") diff --git a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryContextStorage.java b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryContextStorage.java index 485c0a9e69dd..482e43e1d36c 100644 --- a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryContextStorage.java +++ b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryContextStorage.java @@ -1,9 +1,13 @@ package io.opentelemetry.instrumentation.armeria.v1_3; +import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.EXECUTION_INDEX_KEY; +import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.ORIGIN_VCLOCK_KEY; +import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.REQUEST_ID_KEY; +import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.VCLOCK_KEY; + import cloud.filibuster.instrumentation.datatypes.VectorClock; import cloud.filibuster.instrumentation.storage.ContextStorage; import io.opentelemetry.context.Context; -import io.opentelemetry.context.ContextKey; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Nullable; @@ -23,21 +27,16 @@ public Context getContext() { return this.context; } - final private static ContextKey vClockKey = ContextKey.named("filibuster-vclock"); - final private static ContextKey originVclockKey = ContextKey.named("filibuster-origin-vclock"); - final private static ContextKey requestIdKey = ContextKey.named("filibuster-request-id"); - final private static ContextKey executionIndexKey = ContextKey.named("filibuster-execution-index"); - @Override @Nullable public String getRequestId() { - return Context.current().get(requestIdKey); + return Context.current().get(REQUEST_ID_KEY); } @Override @Nullable public VectorClock getVectorClock() { - String vectorClockStr = Context.current().get(vClockKey); + String vectorClockStr = Context.current().get(VCLOCK_KEY); VectorClock newVclock = new VectorClock(); @@ -51,7 +50,7 @@ public VectorClock getVectorClock() { @Override @Nullable public VectorClock getOriginVectorClock() { - String originVectorClockStr = Context.current().get(originVclockKey); + String originVectorClockStr = Context.current().get(ORIGIN_VCLOCK_KEY); VectorClock newVclock = new VectorClock(); @@ -65,30 +64,30 @@ public VectorClock getOriginVectorClock() { @Override @Nullable public String getExecutionIndex() { - return Context.current().get(executionIndexKey); + return Context.current().get(EXECUTION_INDEX_KEY); } @Override public void setRequestId(String requestId) { - this.context = this.context.with(requestIdKey, requestId); + this.context = this.context.with(REQUEST_ID_KEY, requestId); logger.log(Level.SEVERE, "setRequestId: " + requestId); } @Override public void setVectorClock(VectorClock vectorClock) { - this.context = this.context.with(vClockKey, vectorClock.toString()); + this.context = this.context.with(VCLOCK_KEY, vectorClock.toString()); logger.log(Level.SEVERE, "setVectorClock: " + vectorClock); } @Override public void setOriginVectorClock(VectorClock originVectorClock) { - this.context = this.context.with(originVclockKey, originVectorClock.toString()); + this.context = this.context.with(ORIGIN_VCLOCK_KEY, originVectorClock.toString()); logger.log(Level.SEVERE, "setOriginVectorClock: " + originVectorClock); } @Override public void setExecutionIndex(String executionIndex) { - this.context = this.context.with(executionIndexKey, executionIndex); + this.context = this.context.with(EXECUTION_INDEX_KEY, executionIndex); logger.log(Level.SEVERE, "setExecutionIndex: " + executionIndex); } } diff --git a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpClient.java b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpClient.java index 83267028fdf3..bbfb8bee4cbe 100644 --- a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpClient.java +++ b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpClient.java @@ -9,72 +9,73 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; + +import javax.annotation.Nullable; import java.util.logging.Level; import java.util.logging.Logger; public class OpenTelemetryFilibusterDecoratingHttpClient extends FilibusterDecoratingHttpClient { - private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterDecoratingHttpClient.class.getName()); - - private final Instrumenter clientInstrumentor; - - @SuppressWarnings("NullAway") - private Context parentContext; - - @SuppressWarnings("NullAway") - private Context context; - - public OpenTelemetryFilibusterDecoratingHttpClient(HttpClient delegate, String serviceName, Instrumenter clientInstrumentor) { - super(delegate); - this.serviceName = serviceName; - this.clientInstrumentor = clientInstrumentor; - this.contextStorage = new OpenTelemetryContextStorage(); - } - - public OpenTelemetryFilibusterDecoratingHttpClient(HttpClient delegate, Instrumenter clientInstrumentor) { - super(delegate); - this.serviceName = System.getenv("SERVICE_NAME"); - this.clientInstrumentor = clientInstrumentor; - this.contextStorage = new OpenTelemetryContextStorage(); - } - - @Override - protected void setupContext(ClientRequestContext ctx, HttpRequest req) { - this.parentContext = Context.current(); - this.context = Context.current(); - - logger.log(Level.INFO, "****************************************************************"); - logger.log(Level.SEVERE, "CLIENT parentContext: " + parentContext.toString()); - logger.log(Level.INFO, "****************************************************************"); - - if (clientInstrumentor != null) { - this.context = clientInstrumentor.start(Context.current(), ctx); - } - - String x = contextStorage.getExecutionIndex(); - - logger.log(Level.INFO, "****************************************************************"); - logger.log(Level.SEVERE, "CLIENT context: " + context.toString()); - logger.log(Level.INFO, "****************************************************************"); + private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterDecoratingHttpClient.class.getName()); + + @Nullable + private final Instrumenter clientInstrumentor; + + @SuppressWarnings("NullAway") + private Context parentContext; + + @SuppressWarnings("NullAway") + private Context context; + + public OpenTelemetryFilibusterDecoratingHttpClient(HttpClient delegate, String serviceName, Instrumenter clientInstrumentor) { + super(delegate); + this.serviceName = serviceName; + this.clientInstrumentor = clientInstrumentor; + this.contextStorage = new OpenTelemetryContextStorage(); + } + + public OpenTelemetryFilibusterDecoratingHttpClient(HttpClient delegate, Instrumenter clientInstrumentor) { + super(delegate); + this.serviceName = System.getenv("SERVICE_NAME"); + this.clientInstrumentor = clientInstrumentor; + this.contextStorage = new OpenTelemetryContextStorage(); + } + + @Override + protected void setupContext(ClientRequestContext ctx, HttpRequest req) { + this.parentContext = Context.current(); + this.context = Context.current(); + + logger.log(Level.INFO, "****************************************************************"); + logger.log(Level.SEVERE, "CLIENT parentContext: " + parentContext.toString()); + logger.log(Level.INFO, "****************************************************************"); + + if (clientInstrumentor != null) { + this.context = clientInstrumentor.start(Context.current(), ctx); } - @Override - protected void contextWhenComplete(ClientRequestContext ctx) { - ctx.log().whenComplete().thenAccept(log -> { - if (clientInstrumentor != null) { - clientInstrumentor.end(context, ctx, log, log.responseCause()); - } - }); + logger.log(Level.INFO, "****************************************************************"); + logger.log(Level.SEVERE, "CLIENT context: " + context.toString()); + logger.log(Level.INFO, "****************************************************************"); + } + + @Override + protected void contextWhenComplete(ClientRequestContext ctx) { + ctx.log().whenComplete().thenAccept(log -> { + if (clientInstrumentor != null) { + clientInstrumentor.end(context, ctx, log, log.responseCause()); + } + }); + } + + @Override + protected HttpResponse delegateWithContext(ClientRequestContext ctx, HttpRequest req) throws Exception { + HttpResponse response; + + try (Scope ignored = context.makeCurrent()) { + logger.log(Level.INFO, "!!!!!!! with context: " + context.toString()); + response = unwrap().execute(ctx, req); } - @Override - protected HttpResponse delegateWithContext(ClientRequestContext ctx, HttpRequest req) throws Exception { - HttpResponse response; - - try (Scope ignored = context.makeCurrent()) { - logger.log(Level.INFO, "!!!!!!! with context: " + context.toString()); - response = unwrap().execute(ctx, req); - } - - return response; - } + return response; + } } diff --git a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpService.java b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpService.java index 717cef74467a..b5abd80b2a72 100644 --- a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpService.java +++ b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpService.java @@ -9,54 +9,57 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; + +import javax.annotation.Nullable; import java.util.logging.Logger; public class OpenTelemetryFilibusterDecoratingHttpService extends FilibusterDecoratingHttpService { - private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterDecoratingHttpService.class.getName()); + private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterDecoratingHttpService.class.getName()); - private final Instrumenter serverInstrumenter; + @Nullable + private final Instrumenter serverInstrumenter; - @SuppressWarnings("NullAway") - private Context context; + @SuppressWarnings("NullAway") + private Context context; - public OpenTelemetryFilibusterDecoratingHttpService(HttpService delegate, String serviceName, Instrumenter serverInstrumenter) { - super(delegate); - this.serviceName = serviceName; - this.serverInstrumenter = serverInstrumenter; - this.contextStorage = new OpenTelemetryContextStorage(); - } + public OpenTelemetryFilibusterDecoratingHttpService(HttpService delegate, String serviceName, Instrumenter serverInstrumenter) { + super(delegate); + this.serviceName = serviceName; + this.serverInstrumenter = serverInstrumenter; + this.contextStorage = new OpenTelemetryContextStorage(); + } - public OpenTelemetryFilibusterDecoratingHttpService(HttpService delegate, Instrumenter serverInstrumenter) { - super(delegate); - this.serviceName = System.getenv("SERVICE_NAME"); - this.serverInstrumenter = serverInstrumenter; - this.contextStorage = new OpenTelemetryContextStorage(); - } + public OpenTelemetryFilibusterDecoratingHttpService(HttpService delegate, Instrumenter serverInstrumenter) { + super(delegate); + this.serviceName = System.getenv("SERVICE_NAME"); + this.serverInstrumenter = serverInstrumenter; + this.contextStorage = new OpenTelemetryContextStorage(); + } - @Override - protected void setupContext(ServiceRequestContext ctx, HttpRequest req) { - OpenTelemetryContextStorage openTelemetryContextStorage = (OpenTelemetryContextStorage) this.contextStorage; - context = openTelemetryContextStorage.getContext(); + @Override + protected void setupContext(ServiceRequestContext ctx, HttpRequest req) { + OpenTelemetryContextStorage openTelemetryContextStorage = (OpenTelemetryContextStorage) this.contextStorage; + context = openTelemetryContextStorage.getContext(); - if (serverInstrumenter != null) { - context = serverInstrumenter.start(context, ctx); - } + if (serverInstrumenter != null) { + context = serverInstrumenter.start(context, ctx); } + } - @Override - protected void contextWhenComplete(ServiceRequestContext ctx) { - ctx.log().whenComplete().thenAccept(log -> { - if (serverInstrumenter != null) { - serverInstrumenter.end(context, ctx, log, log.responseCause()); - } - }); - } + @Override + protected void contextWhenComplete(ServiceRequestContext ctx) { + ctx.log().whenComplete().thenAccept(log -> { + if (serverInstrumenter != null) { + serverInstrumenter.end(context, ctx, log, log.responseCause()); + } + }); + } - @Override - protected HttpResponse delegateWithContext(ServiceRequestContext ctx, HttpRequest req) throws Exception { - try (Scope ignored = context.makeCurrent()) { - HttpService delegate = (HttpService) unwrap(); - return delegate.serve(ctx, req); - } + @Override + protected HttpResponse delegateWithContext(ServiceRequestContext ctx, HttpRequest req) throws Exception { + try (Scope ignored = context.makeCurrent()) { + HttpService delegate = (HttpService) unwrap(); + return delegate.serve(ctx, req); } + } } diff --git a/instrumentation/grpc-1.6/library/build.gradle.kts b/instrumentation/grpc-1.6/library/build.gradle.kts index 8340863e7c18..72af1e96bad6 100644 --- a/instrumentation/grpc-1.6/library/build.gradle.kts +++ b/instrumentation/grpc-1.6/library/build.gradle.kts @@ -7,7 +7,7 @@ val grpcVersion = "1.6.0" dependencies { library("io.grpc:grpc-core:$grpcVersion") - library("cloud.filibuster:instrumentation:0.14-SNAPSHOT") + library("cloud.filibuster:instrumentation:0.17-SNAPSHOT") testLibrary("io.grpc:grpc-netty:$grpcVersion") testLibrary("io.grpc:grpc-protobuf:$grpcVersion") diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryContextStorage.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryContextStorage.java index 162aad5ad0f4..c3dd15b9836e 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryContextStorage.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryContextStorage.java @@ -3,12 +3,16 @@ import cloud.filibuster.instrumentation.datatypes.VectorClock; import cloud.filibuster.instrumentation.storage.ContextStorage; import io.opentelemetry.context.Context; -import io.opentelemetry.context.ContextKey; import javax.annotation.Nullable; import java.util.logging.Level; import java.util.logging.Logger; +import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.EXECUTION_INDEX_KEY; +import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.ORIGIN_VCLOCK_KEY; +import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.REQUEST_ID_KEY; +import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.VCLOCK_KEY; + public class OpenTelemetryContextStorage implements ContextStorage { private static final Logger logger = Logger.getLogger(OpenTelemetryContextStorage.class.getName()); @@ -24,21 +28,16 @@ public Context getContext() { return this.context; } - final private static ContextKey vClockKey = ContextKey.named("filibuster-vclock"); - final private static ContextKey originVclockKey = ContextKey.named("filibuster-origin-vclock"); - final private static ContextKey requestIdKey = ContextKey.named("filibuster-request-id"); - final private static ContextKey executionIndexKey = ContextKey.named("filibuster-execution-index"); - @Override @Nullable public String getRequestId() { - return Context.current().get(requestIdKey); + return Context.current().get(REQUEST_ID_KEY); } @Override @Nullable public VectorClock getVectorClock() { - String vectorClockStr = Context.current().get(vClockKey); + String vectorClockStr = Context.current().get(VCLOCK_KEY); VectorClock newVclock = new VectorClock(); @@ -52,7 +51,7 @@ public VectorClock getVectorClock() { @Override @Nullable public VectorClock getOriginVectorClock() { - String originVectorClockStr = Context.current().get(originVclockKey); + String originVectorClockStr = Context.current().get(ORIGIN_VCLOCK_KEY); VectorClock newVclock = new VectorClock(); @@ -66,30 +65,34 @@ public VectorClock getOriginVectorClock() { @Override @Nullable public String getExecutionIndex() { - return Context.current().get(executionIndexKey); + return Context.current().get(EXECUTION_INDEX_KEY); + } + + public String getExecutionIndexFromContext(Context context) { + return context.get(EXECUTION_INDEX_KEY); } @Override public void setRequestId(String requestId) { - this.context = this.context.with(requestIdKey, requestId); + this.context = this.context.with(REQUEST_ID_KEY, requestId); logger.log(Level.SEVERE, "setRequestId: " + requestId); } @Override public void setVectorClock(VectorClock vectorClock) { - this.context = this.context.with(vClockKey, vectorClock.toString()); + this.context = this.context.with(VCLOCK_KEY, vectorClock.toString()); logger.log(Level.SEVERE, "setVectorClock: " + vectorClock); } @Override public void setOriginVectorClock(VectorClock originVectorClock) { - this.context = this.context.with(originVclockKey, originVectorClock.toString()); + this.context = this.context.with(ORIGIN_VCLOCK_KEY, originVectorClock.toString()); logger.log(Level.SEVERE, "setOriginVectorClock: " + originVectorClock); } @Override public void setExecutionIndex(String executionIndex) { - this.context = this.context.with(executionIndexKey, executionIndex); + this.context = this.context.with(EXECUTION_INDEX_KEY, executionIndex); logger.log(Level.SEVERE, "setExecutionIndex: " + executionIndex); } } diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterClientInterceptor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterClientInterceptor.java index aeb334ab2c04..6b6c92573ce2 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterClientInterceptor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterClientInterceptor.java @@ -1,10 +1,8 @@ package io.opentelemetry.instrumentation.grpc.v1_6; -import static cloud.filibuster.instrumentation.Helper.getDisableInstrumentationFromEnvironment; -import static cloud.filibuster.instrumentation.Helper.getDisableServerCommunicationFromEnvironment; - -import cloud.filibuster.instrumentation.datatypes.VectorClock; +import cloud.filibuster.instrumentation.datatypes.Callsite; import cloud.filibuster.instrumentation.instrumentors.FilibusterClientInstrumentor; +import cloud.filibuster.instrumentation.libraries.grpc.NoopClientCall; import cloud.filibuster.instrumentation.storage.ContextStorage; import io.grpc.CallOptions; import io.grpc.Channel; @@ -20,439 +18,450 @@ import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.ContextPropagators; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; +import org.json.JSONObject; + +import javax.annotation.Nullable; import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; -import org.json.JSONObject; -public class OpenTelemetryFilibusterClientInterceptor implements ClientInterceptor { - private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterClientInterceptor.class.getName()); +import static cloud.filibuster.instrumentation.Helper.getDisableInstrumentationFromEnvironment; +import static cloud.filibuster.instrumentation.Helper.getDisableServerCommunicationFromEnvironment; - @SuppressWarnings("FieldCanBeFinal") - protected String serviceName; +public class OpenTelemetryFilibusterClientInterceptor implements ClientInterceptor { + private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterClientInterceptor.class.getName()); - @SuppressWarnings("FieldCanBeFinal") - protected ContextStorage contextStorage; + @SuppressWarnings("FieldCanBeFinal") + protected String serviceName; - public static Boolean disableServerCommunication = false; - public static Boolean disableInstrumentation = false; + @SuppressWarnings("FieldCanBeFinal") + protected ContextStorage contextStorage; - private final Instrumenter clientInstrumentor; + public static Boolean disableServerCommunication = false; + public static Boolean disableInstrumentation = false; - @SuppressWarnings("FieldCanBeLocal") - private final ContextPropagators propagators; + @Nullable + private final Instrumenter clientInstrumentor; - private static boolean shouldInstrument() { - if (disableInstrumentation) { - return false; - } + @SuppressWarnings("FieldCanBeLocal") + private final ContextPropagators propagators; - return !getDisableInstrumentationFromEnvironment(); + private static boolean shouldInstrument() { + if (disableInstrumentation) { + return false; } - private static boolean shouldCommunicateWithServer() { - if (disableServerCommunication) { - return false; - } - - return !getDisableServerCommunicationFromEnvironment(); - } + return !getDisableInstrumentationFromEnvironment(); + } - private static Status generateCorrectStatusForAbort(FilibusterClientInstrumentor filibusterClientInstrumentor) { - JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); - JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); - String codeStr = forcedExceptionMetadata.getString("code"); - Status.Code code = Status.Code.valueOf(codeStr); - Status status = Status.fromCode(code); - return status; + private static boolean shouldCommunicateWithServer() { + if (disableServerCommunication) { + return false; } - private static void generateAndThrowException(FilibusterClientInstrumentor filibusterClientInstrumentor) { - JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); - - // Create the exception to throw. - String exceptionNameString = forcedException.getString("name"); - JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); - String causeString = forcedExceptionMetadata.getString("cause"); - String codeStr = forcedExceptionMetadata.getString("code"); - Status.Code code = Status.Code.valueOf(codeStr); - StatusRuntimeException status = Status.fromCode(code).asRuntimeException(); - - // Notify Filibuster of failure. - HashMap additionalMetadata = new HashMap<>(); - additionalMetadata.put("code", codeStr); - filibusterClientInstrumentor.afterInvocationWithException(exceptionNameString, causeString, additionalMetadata); - - // Throw the runtime exception. - throw status; + return !getDisableServerCommunicationFromEnvironment(); + } + + private static Status generateCorrectStatusForAbort(FilibusterClientInstrumentor filibusterClientInstrumentor) { + JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); + JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); + String codeStr = forcedExceptionMetadata.getString("code"); + Status.Code code = Status.Code.valueOf(codeStr); + Status status = Status.fromCode(code); + return status; + } + + private static Status generateException(FilibusterClientInstrumentor filibusterClientInstrumentor) { + JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); + + // Create the exception to throw. + String exceptionNameString = forcedException.getString("name"); + JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); + String causeString = forcedExceptionMetadata.getString("cause"); + String codeStr = forcedExceptionMetadata.getString("code"); + Status.Code code = Status.Code.valueOf(codeStr); + + // Notify Filibuster of failure. + HashMap additionalMetadata = new HashMap<>(); + additionalMetadata.put("code", codeStr); + filibusterClientInstrumentor.afterInvocationWithException(exceptionNameString, causeString, additionalMetadata); + + // Return status. + return Status.fromCode(code); + } + + private static Status generateExceptionFromFailureMetadata(FilibusterClientInstrumentor filibusterClientInstrumentor) { + JSONObject failureMetadata = filibusterClientInstrumentor.getFailureMetadata(); + JSONObject exception = failureMetadata.getJSONObject("exception"); + JSONObject exceptionMetadata = exception.getJSONObject("metadata"); + + // Create the exception to throw. + String exceptionNameString = "io.grpc.StatusRuntimeException"; + String codeStr = exceptionMetadata.getString("code"); + Status.Code code = Status.Code.valueOf(codeStr); + StatusRuntimeException status = Status.fromCode(code).asRuntimeException(); + String causeString = ""; + + // Notify Filibuster of failure. + HashMap additionalMetadata = new HashMap<>(); + additionalMetadata.put("name", exceptionNameString); + additionalMetadata.put("code", codeStr); + filibusterClientInstrumentor.afterInvocationWithException(exceptionNameString, causeString, additionalMetadata); + + // Return status. + return Status.fromCode(code); + } + + public OpenTelemetryFilibusterClientInterceptor(Instrumenter clientInstrumentor, ContextPropagators propagators) { + this.serviceName = System.getenv("SERVICE_NAME"); + this.contextStorage = new OpenTelemetryContextStorage(); + this.clientInstrumentor = clientInstrumentor; + this.propagators = propagators; + } + + public OpenTelemetryFilibusterClientInterceptor(String serviceName, Instrumenter clientInstrumentor, ContextPropagators propagators) { + this.serviceName = serviceName; + this.contextStorage = new OpenTelemetryContextStorage(); + this.clientInstrumentor = clientInstrumentor; + this.propagators = propagators; + } + + @Override + public ClientCall interceptCall( + MethodDescriptor method, CallOptions callOptions, Channel next) { + + if (method.getType() != MethodDescriptor.MethodType.UNARY) { + return next.newCall(method, callOptions); } - private static void generateAndThrowExceptionFromFailureMetadata(FilibusterClientInstrumentor filibusterClientInstrumentor) { - JSONObject failureMetadata = filibusterClientInstrumentor.getFailureMetadata(); - JSONObject exception = failureMetadata.getJSONObject("exception"); - JSONObject exceptionMetadata = exception.getJSONObject("metadata"); + GrpcRequest request = new GrpcRequest(method, null, null); + Context parentContext = Context.current(); - // Create the exception to throw. - String exceptionNameString = "io.grpc.StatusRuntimeException"; - String codeStr = exceptionMetadata.getString("code"); - Status.Code code = Status.Code.valueOf(codeStr); - StatusRuntimeException status = Status.fromCode(code).asRuntimeException(); - String causeString = ""; - - // Notify Filibuster of failure. - HashMap additionalMetadata = new HashMap<>(); - additionalMetadata.put("name", exceptionNameString); - additionalMetadata.put("code", codeStr); - filibusterClientInstrumentor.afterInvocationWithException(exceptionNameString, causeString, additionalMetadata); - - // Throw the runtime exception. - throw status; - } - - public OpenTelemetryFilibusterClientInterceptor(Instrumenter clientInstrumentor, ContextPropagators propagators) { - this.serviceName = System.getenv("SERVICE_NAME"); - this.contextStorage = new OpenTelemetryContextStorage(); - this.clientInstrumentor = clientInstrumentor; - this.propagators = propagators; - } + Context context; - public OpenTelemetryFilibusterClientInterceptor(String serviceName, Instrumenter clientInstrumentor, ContextPropagators propagators) { - this.serviceName = serviceName; - this.contextStorage = new OpenTelemetryContextStorage(); - this.clientInstrumentor = clientInstrumentor; - this.propagators = propagators; + if (clientInstrumentor != null) { + context = clientInstrumentor.start(parentContext, request); + } else { + context = Context.current(); } - @Override - public ClientCall interceptCall( - MethodDescriptor method, CallOptions callOptions, Channel next) { - GrpcRequest request = new GrpcRequest(method, null, null); - Context parentContext = Context.current(); - - Context context; - - if (clientInstrumentor != null) { - context = clientInstrumentor.start(parentContext, request); - } else { - context = Context.current(); - } - - // call other interceptors first. - final ClientCall result = next.newCall(method, callOptions); - - // return the filibuster client interceptor. - return new FilibusterClientCall<>(result, parentContext, context, request, serviceName, contextStorage, method); - } - - // ********************************************************************* - // Client caller. - - final class FilibusterClientCall - extends ForwardingClientCall.SimpleForwardingClientCall { - - final private String serviceName; - final private MethodDescriptor method; - final private ContextStorage contextStorage; - private final Context parentContext; - private final Context context; - private final GrpcRequest request; - - FilibusterClientCall(ClientCall delegate, - Context parentContext, - Context context, - GrpcRequest request, - String serviceName, - ContextStorage contextStorage, - MethodDescriptor method) { - super(delegate); - this.serviceName = serviceName; - this.method = method; - this.contextStorage = contextStorage; - this.parentContext = parentContext; - this.context = context; - this.request = request; - } - - // ****************************************************************************************** - // Accessors for metadata. - // ****************************************************************************************** - - public String getRequestIdFromMetadata() { - return contextStorage.getRequestId(); + // return the filibuster client interceptor. + return new ForwardingClientCall() { + @Nullable + private ClientCall delegate; + private Listener responseListener; + + @Nullable + private Metadata headers; + private int requestTokens; + private FilibusterClientInstrumentor filibusterClientInstrumentor; + + @Override + protected ClientCall delegate() { + if (delegate == null) { + throw new UnsupportedOperationException(); } - - public VectorClock getVectorClockFromMetadata() { - return contextStorage.getVectorClock(); + return delegate; + } + + @Override + public void start(Listener responseListener, Metadata headers) { + logger.log(Level.INFO, "INSIDE: start!"); + + this.headers = headers; + this.responseListener = responseListener; + } + + @Override + public void request(int requests) { + if (delegate == null) { + requestTokens += requests; + return; } - - public VectorClock getOriginVectorClockFromMetadata() { - return contextStorage.getOriginVectorClock(); - } - - public String getExecutionIndexFromMetadata() { - return contextStorage.getExecutionIndex(); - } - - // ****************************************************************************************** - // Implementation. - // ****************************************************************************************** - - @Override - public void start(Listener responseListener, Metadata headers) { - logger.log(Level.INFO, "INSIDE: start!"); - + super.request(requests); + } + + // This method is invoked with the message from the Client to the Service. + // message: type of the message issued from the Client (e.g., Hello$HelloRequest) + @Override + public void sendMessage(REQUEST message) { + logger.log(Level.INFO, "INSIDE: sendMessage!"); + logger.log(Level.INFO, "message: " + message.toString()); + + try (Scope ignored = context.makeCurrent()) { + OpenTelemetryContextStorage otelContextStorage = (OpenTelemetryContextStorage) contextStorage; + + logger.log(Level.INFO, "CMEIK CONTEXT: " + context.toString()); + logger.log(Level.INFO, "CMEIK Context.current(): " + Context.current()); + logger.log(Level.INFO, "CMEIK contextStorage.getExecutionIndex(): " + contextStorage.getExecutionIndex()); + logger.log(Level.INFO, "CMEIK contextStorage.getExecutionIndex(Context.current()): " + otelContextStorage.getExecutionIndexFromContext(Context.current())); + logger.log(Level.INFO, "CMEIK SOURCE SERVICE: " + serviceName); + logger.log(Level.INFO, "CMEIK: sendMessage!"); + + // ****************************************************************************************** + // Figure out if we are inside of instrumentation. + // ****************************************************************************************** + + String instrumentationRequestStr = headers.get( + Metadata.Key.of("x-filibuster-instrumentation", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "instrumentationRequestStr: " + instrumentationRequestStr); + boolean instrumentationRequest = Boolean.parseBoolean(instrumentationRequestStr); + logger.log(Level.INFO, "instrumentationRequest: " + instrumentationRequest); + + if (! shouldInstrument() || instrumentationRequest) { + delegate = next.newCall(method, callOptions); + super.start(responseListener, headers); + headers = null; + if (requestTokens > 0) { + super.request(requestTokens); + requestTokens = 0; + } + } else { // ****************************************************************************************** // Extract callsite information. // ****************************************************************************************** String grpcFullMethodName = method.getFullMethodName(); - String grpcRpcName = grpcFullMethodName.substring(grpcFullMethodName.indexOf("/") + 1); String grpcServiceName = grpcFullMethodName.substring(0, grpcFullMethodName.indexOf("/")); + String grpcRpcName = grpcFullMethodName.replace(grpcServiceName + "/", ""); +// logger.log(Level.INFO, "method: " + method); logger.log(Level.INFO, "grpcFullMethodName: " + grpcFullMethodName); logger.log(Level.INFO, "grpcServiceName: " + grpcServiceName); logger.log(Level.INFO, "grpcRpcName: " + grpcRpcName); // ****************************************************************************************** - // Figure out if we are inside of instrumentation. + // Construct preliminary call site information. // ****************************************************************************************** - String instrumentationRequestStr = headers.get( - Metadata.Key.of("x-filibuster-instrumentation", Metadata.ASCII_STRING_MARSHALLER)); - logger.log(Level.INFO, "instrumentationRequestStr: " + instrumentationRequestStr); - boolean instrumentationRequest = Boolean.parseBoolean(instrumentationRequestStr); - logger.log(Level.INFO, "instrumentationRequest: " + instrumentationRequest); - - if (! shouldInstrument() || instrumentationRequest) { - try (Scope ignored = context.makeCurrent()) { - super.start(responseListener, headers); - } catch (Throwable e) { - if (clientInstrumentor != null) { - clientInstrumentor.end(context, request, null, e); - } - - throw e; - } - } else { - // ****************************************************************************************** - // Prepare for invocation. - // ****************************************************************************************** - - FilibusterClientInstrumentor filibusterClientInstrumentor = new FilibusterClientInstrumentor( - serviceName, - shouldCommunicateWithServer(), - contextStorage - ); - filibusterClientInstrumentor.prepareForInvocation(); + Callsite callsite = new Callsite( + serviceName, + grpcServiceName, + grpcFullMethodName, + message.toString() + ); + + // ****************************************************************************************** + // Prepare for invocation. + // ****************************************************************************************** + + this.filibusterClientInstrumentor = new FilibusterClientInstrumentor( + serviceName, + shouldCommunicateWithServer(), + contextStorage, + callsite + ); + filibusterClientInstrumentor.prepareForInvocation(); - // ****************************************************************************************** - // Record invocation. - // ****************************************************************************************** + // ****************************************************************************************** + // Record invocation. + // ****************************************************************************************** - filibusterClientInstrumentor.beforeInvocation(grpcServiceName, grpcFullMethodName, ""); + filibusterClientInstrumentor.beforeInvocation(); - JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); - JSONObject failureMetadata = filibusterClientInstrumentor.getFailureMetadata(); + // ****************************************************************************************** + // Attach metadata to outgoing request. + // ****************************************************************************************** - logger.log(Level.INFO, "forcedException: " + forcedException); - logger.log(Level.INFO, "failureMetadata: " + failureMetadata); + logger.log(Level.INFO, "requestId: " + filibusterClientInstrumentor.getOutgoingRequestId()); - // ****************************************************************************************** - // Attach metadata to outgoing request. - // ****************************************************************************************** + if (filibusterClientInstrumentor.getOutgoingRequestId() != null) { + headers.put( + Metadata.Key.of("x-filibuster-request-id", Metadata.ASCII_STRING_MARSHALLER), + filibusterClientInstrumentor.getOutgoingRequestId() + ); + } - logger.log(Level.INFO, "requestId: " + filibusterClientInstrumentor.getRequestId()); + if (filibusterClientInstrumentor.getGeneratedId() > -1) { + headers.put( + Metadata.Key.of("x-filibuster-generated-id", Metadata.ASCII_STRING_MARSHALLER), + String.valueOf(filibusterClientInstrumentor.getGeneratedId()) + ); + } - if (filibusterClientInstrumentor.getRequestId() != null) { - headers.put( - Metadata.Key.of("x-filibuster-request-id", Metadata.ASCII_STRING_MARSHALLER), - filibusterClientInstrumentor.getRequestId() - ); - } + headers.put( + Metadata.Key.of("x-filibuster-vclock", Metadata.ASCII_STRING_MARSHALLER), + filibusterClientInstrumentor.getVectorClock().toString() + ); + headers.put( + Metadata.Key.of("x-filibuster-origin-vclock", Metadata.ASCII_STRING_MARSHALLER), + filibusterClientInstrumentor.getOriginVectorClock().toString() + ); + headers.put( + Metadata.Key.of("x-filibuster-execution-index", Metadata.ASCII_STRING_MARSHALLER), + filibusterClientInstrumentor.getExecutionIndex().toString() + ); - if (filibusterClientInstrumentor.getGeneratedId() > -1) { - headers.put( - Metadata.Key.of("x-filibuster-generated-id", Metadata.ASCII_STRING_MARSHALLER), - String.valueOf(filibusterClientInstrumentor.getGeneratedId()) - ); - } + // ****************************************************************************************** + // Get failure information. + // ****************************************************************************************** + JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); + JSONObject failureMetadata = filibusterClientInstrumentor.getFailureMetadata(); + + logger.log(Level.INFO, "forcedException: " + forcedException); + logger.log(Level.INFO, "failureMetadata: " + failureMetadata); + + // ****************************************************************************************** + // Setup additional failure headers, if necessary. + // ****************************************************************************************** + + if (forcedException != null) { + JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); + + if (forcedExceptionMetadata.has("sleep")) { + int sleepInterval = forcedExceptionMetadata.getInt("sleep"); headers.put( - Metadata.Key.of("x-filibuster-vclock", Metadata.ASCII_STRING_MARSHALLER), - filibusterClientInstrumentor.getVectorClock().toString() - ); - headers.put( - Metadata.Key.of("x-filibuster-origin-vclock", Metadata.ASCII_STRING_MARSHALLER), - filibusterClientInstrumentor.getOriginVectorClock().toString() + Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER), + String.valueOf(sleepInterval) ); + } else { headers.put( - Metadata.Key.of("x-filibuster-execution-index", Metadata.ASCII_STRING_MARSHALLER), - filibusterClientInstrumentor.getExecutionIndex().toString() + Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER), + String.valueOf(0) ); - - if (forcedException != null) { - JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); - - if (forcedExceptionMetadata.has("sleep")) { - int sleepInterval = forcedExceptionMetadata.getInt("sleep"); - headers.put( - Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER), - String.valueOf(sleepInterval) - ); - } else { - headers.put( - Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER), - String.valueOf(0) - ); - } - } - - // ****************************************************************************************** - // If we need to override the response, do it now before proceeding. - // ****************************************************************************************** - - if (failureMetadata != null && filibusterClientInstrumentor.shouldAbort()) { - generateAndThrowExceptionFromFailureMetadata(filibusterClientInstrumentor); - } - - // ****************************************************************************************** - // If we need to throw, this is where we throw. - // ****************************************************************************************** - - if (forcedException != null && filibusterClientInstrumentor.shouldAbort()) { - generateAndThrowException(filibusterClientInstrumentor); - } - - // ****************************************************************************************** - // Issue request. - // ****************************************************************************************** - - try (Scope ignored = context.makeCurrent()) { - super.start(new FilibusterClientCallListener<>( - responseListener, parentContext, context, request, filibusterClientInstrumentor), headers); - } catch (Throwable e) { - if (clientInstrumentor != null) { - clientInstrumentor.end(context, request, null, e); - } - - throw e; - } + } } - } - // This method is invoked with the message from the Client to the Service. - // message: type of the message issued from the Client (e.g., Hello$HelloRequest) - @Override - public void sendMessage(REQUEST message) { - logger.log(Level.INFO, "INSIDE: sendMessage!"); - logger.log(Level.INFO, "message: " + message.toString()); - try (Scope ignored = context.makeCurrent()) { - super.sendMessage(message); - } catch (Throwable e) { - if (clientInstrumentor != null) { - clientInstrumentor.end(context, request, null, e); - } - - throw e; + // ****************************************************************************************** + // If we need to override the response, do it now before proceeding. + // ****************************************************************************************** + + if (failureMetadata != null && filibusterClientInstrumentor.shouldAbort()) { + delegate = new NoopClientCall(); + Status status = generateExceptionFromFailureMetadata(filibusterClientInstrumentor); + responseListener.onClose(status, new Metadata()); + return; } - } - } - // ********************************************************************* - // Client caller listener. - - @SuppressWarnings("ClassCanBeStatic") - final class FilibusterClientCallListener - extends ForwardingClientCallListener.SimpleForwardingClientCallListener { - - private final FilibusterClientInstrumentor filibusterClientInstrumentor; - private final Context parentContext; - private final Context context; - private final GrpcRequest request; - - FilibusterClientCallListener(ClientCall.Listener delegate, - Context parentContext, - Context context, - GrpcRequest request, - FilibusterClientInstrumentor filibusterClientInstrumentor) { - super(delegate); - this.filibusterClientInstrumentor = filibusterClientInstrumentor; - this.parentContext = parentContext; - this.context = context; - this.request = request; - } + // ****************************************************************************************** + // If we need to throw, this is where we throw. + // ****************************************************************************************** - // invoked on successful response with the message from the Server to the Client - // message: type of message issued from the Server to the Client (e.g., Hello$HelloReply) - @Override - public void onMessage(RESPONSE message) { - logger.log(Level.INFO, "INSIDE: onMessage!"); - logger.log(Level.INFO, "message: " + message); - - if (! filibusterClientInstrumentor.shouldAbort()) { - // Request completed normally, but we want to throw the exception anyway, generate and throw. - generateAndThrowException(filibusterClientInstrumentor); - } else { - // Request completed normally. - - // Notify Filibuster of complete invocation with the proper response. - String className = message.getClass().getName(); - HashMap returnValueProperties = new HashMap<>(); - filibusterClientInstrumentor.afterInvocationComplete(className, returnValueProperties); - - // Delegate. - try (Scope ignored = context.makeCurrent()) { - delegate().onMessage(message); - } + if (forcedException != null && filibusterClientInstrumentor.shouldAbort()) { + delegate = new NoopClientCall(); + Status status = generateException(filibusterClientInstrumentor); + responseListener.onClose(status, new Metadata()); + return; } - } - // invoked on an error: status set to a status message - // Status.code = FAILED_PRECONDITION, description = ..., cause = ... - // trailers metadata headers. - @Override - public void onClose(Status status, Metadata trailers) { - if (clientInstrumentor != null) { - clientInstrumentor.end(context, request, status, status.getCause()); + delegate = next.newCall(method, callOptions); + super.start(new FilibusterClientCallListener<>( + responseListener, parentContext, context, request, filibusterClientInstrumentor), headers); + headers = null; + if (requestTokens > 0) { + super.request(requestTokens); + requestTokens = 0; } + } - logger.log(Level.INFO, "INSIDE: onClose!"); - logger.log(Level.INFO, "status: " + status); - logger.log(Level.INFO, "trailers: " + trailers); + super.sendMessage(message); + } catch (Throwable e) { + if (clientInstrumentor != null) { + clientInstrumentor.end(context, request, null, e); + } - if (! filibusterClientInstrumentor.shouldAbort()) { - Status rewrittenStatus = generateCorrectStatusForAbort(filibusterClientInstrumentor); + throw e; + } + } + }; + } + + // ********************************************************************* + // Client caller listener. + + @SuppressWarnings("ClassCanBeStatic") + final class FilibusterClientCallListener + extends ForwardingClientCallListener.SimpleForwardingClientCallListener { + + private final FilibusterClientInstrumentor filibusterClientInstrumentor; + private final Context parentContext; + private final Context context; + private final GrpcRequest request; + + FilibusterClientCallListener(ClientCall.Listener delegate, + Context parentContext, + Context context, + GrpcRequest request, + FilibusterClientInstrumentor filibusterClientInstrumentor) { + super(delegate); + this.filibusterClientInstrumentor = filibusterClientInstrumentor; + this.parentContext = parentContext; + this.context = context; + this.request = request; + } - try (Scope ignored = parentContext.makeCurrent()) { - delegate().onClose(rewrittenStatus, trailers); - } - } + // invoked on successful response with the message from the Server to the Client + // message: type of message issued from the Server to the Client (e.g., Hello$HelloReply) + @Override + public void onMessage(RESPONSE message) { + logger.log(Level.INFO, "INSIDE: onMessage!"); + logger.log(Level.INFO, "message: " + message); + + if (! filibusterClientInstrumentor.shouldAbort()) { + // Request completed normally, but we want to throw the exception anyway, generate and throw. + generateException(filibusterClientInstrumentor); + } else { + // Request completed normally. + + // Notify Filibuster of complete invocation with the proper response. + String className = message.getClass().getName(); + HashMap returnValueProperties = new HashMap<>(); + filibusterClientInstrumentor.afterInvocationComplete(className, returnValueProperties); + + // Delegate. + try (Scope ignored = context.makeCurrent()) { + delegate().onMessage(message); + } + } + } - if (! status.isOk()) { - // Request completed -- if it completed with a failure, it will be coming here for - // the first time (didn't call onMessage) and therefore, we need to notify the Filibuster - // server that the call completed with failure. If it completed successfully, we would - // have already notified the Filibuster server in the onMessage callback. - - // Notify Filibuster of error. - HashMap additionalMetadata = new HashMap<>(); - additionalMetadata.put("code", status.getCode().toString()); - String exceptionName = "io.grpc.StatusRuntimeException"; - // exception cause is always null, because it doesn't serialize and pass through even if provided. - filibusterClientInstrumentor.afterInvocationWithException(exceptionName, null, additionalMetadata); - } + // invoked on an error: status set to a status message + // Status.code = FAILED_PRECONDITION, description = ..., cause = ... + // trailers metadata headers. + @Override + public void onClose(Status status, Metadata trailers) { + if (clientInstrumentor != null) { + clientInstrumentor.end(context, request, status, status.getCause()); + } - try (Scope ignored = parentContext.makeCurrent()) { - delegate().onClose(status, trailers); - } - } + logger.log(Level.INFO, "INSIDE: onClose!"); + logger.log(Level.INFO, "status: " + status); + logger.log(Level.INFO, "trailers: " + trailers); - @Override - public void onReady() { - logger.log(Level.INFO, "INSIDE: onReady!"); - try (Scope ignored = context.makeCurrent()) { - delegate().onReady(); - } + if (! filibusterClientInstrumentor.shouldAbort()) { + Status rewrittenStatus = generateCorrectStatusForAbort(filibusterClientInstrumentor); + + try (Scope ignored = parentContext.makeCurrent()) { + delegate().onClose(rewrittenStatus, trailers); } + } + + if (! status.isOk()) { + // Request completed -- if it completed with a failure, it will be coming here for + // the first time (didn't call onMessage) and therefore, we need to notify the Filibuster + // server that the call completed with failure. If it completed successfully, we would + // have already notified the Filibuster server in the onMessage callback. + + // Notify Filibuster of error. + HashMap additionalMetadata = new HashMap<>(); + additionalMetadata.put("code", status.getCode().toString()); + String exceptionName = "io.grpc.StatusRuntimeException"; + // exception cause is always null, because it doesn't serialize and pass through even if provided. + filibusterClientInstrumentor.afterInvocationWithException(exceptionName, null, additionalMetadata); + } + + try (Scope ignored = parentContext.makeCurrent()) { + delegate().onClose(status, trailers); + } + } + + @Override + public void onReady() { + logger.log(Level.INFO, "INSIDE: onReady!"); + try (Scope ignored = context.makeCurrent()) { + delegate().onReady(); + } } + } } diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterServerInterceptor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterServerInterceptor.java index 686b124b5815..f2efe4737beb 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterServerInterceptor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterServerInterceptor.java @@ -1,11 +1,9 @@ package io.opentelemetry.instrumentation.grpc.v1_6; -import static cloud.filibuster.instrumentation.Helper.getDisableInstrumentationFromEnvironment; -import static cloud.filibuster.instrumentation.Helper.getDisableServerCommunicationFromEnvironment; - import cloud.filibuster.instrumentation.Helper; import cloud.filibuster.instrumentation.instrumentors.FilibusterServerInstrumentor; import cloud.filibuster.instrumentation.storage.ContextStorage; + import io.grpc.Contexts; import io.grpc.ForwardingServerCall; import io.grpc.ForwardingServerCallListener; @@ -18,312 +16,327 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; + +import javax.annotation.Nullable; import java.util.logging.Level; import java.util.logging.Logger; +import static cloud.filibuster.instrumentation.Helper.getDisableInstrumentationFromEnvironment; +import static cloud.filibuster.instrumentation.Helper.getDisableServerCommunicationFromEnvironment; + public class OpenTelemetryFilibusterServerInterceptor implements ServerInterceptor { - private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterServerInterceptor.class.getName()); + private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterServerInterceptor.class.getName()); - @SuppressWarnings("FieldCanBeFinal") - protected String serviceName; + @SuppressWarnings("FieldCanBeFinal") + protected String serviceName; - @SuppressWarnings("FieldCanBeFinal") - protected ContextStorage contextStorage; + @SuppressWarnings("FieldCanBeFinal") + protected ContextStorage contextStorage; - public static Boolean disableServerCommunication = false; - public static Boolean disableInstrumentation = false; + public static Boolean disableServerCommunication = false; + public static Boolean disableInstrumentation = false; - private final Instrumenter serverInstrumentor; + @Nullable + private String requestId; - private static boolean shouldInstrument() { - if (disableInstrumentation) { - return false; - } + @Nullable + private final Instrumenter serverInstrumentor; - return !getDisableInstrumentationFromEnvironment(); + private static boolean shouldInstrument() { + if (disableInstrumentation) { + return false; } - private static boolean shouldCommunicateWithServer() { - if (disableServerCommunication) { - return false; - } + return !getDisableInstrumentationFromEnvironment(); + } - return !getDisableServerCommunicationFromEnvironment(); + private static boolean shouldCommunicateWithServer() { + if (disableServerCommunication) { + return false; } - public OpenTelemetryFilibusterServerInterceptor(Instrumenter serverInstrumentor) { - this.serviceName = System.getenv("SERVICE_NAME"); - this.contextStorage = new OpenTelemetryContextStorage(); - this.serverInstrumentor = serverInstrumentor; + return !getDisableServerCommunicationFromEnvironment(); + } + + public OpenTelemetryFilibusterServerInterceptor(Instrumenter serverInstrumentor) { + this.serviceName = System.getenv("SERVICE_NAME"); + this.contextStorage = new OpenTelemetryContextStorage(); + this.serverInstrumentor = serverInstrumentor; + } + + public OpenTelemetryFilibusterServerInterceptor(String serviceName, Instrumenter serverInstrumentor) { + this.serviceName = serviceName; + this.contextStorage = new OpenTelemetryContextStorage(); + this.serverInstrumentor = serverInstrumentor; + } + + // ****************************************************************************************** + // Accessors for metadata. + // ****************************************************************************************** + + public String getRequestIdFromMetadata(Metadata requestHeaders) { + if (this.requestId == null) { + this.requestId = requestHeaders.get(Metadata.Key.of("x-filibuster-request-id", Metadata.ASCII_STRING_MARSHALLER)); + + if (this.requestId == null) { + this.requestId = Helper.generateNewRequestId().toString(); + } } - public OpenTelemetryFilibusterServerInterceptor(String serviceName, Instrumenter serverInstrumentor) { - this.serviceName = serviceName; - this.contextStorage = new OpenTelemetryContextStorage(); - this.serverInstrumentor = serverInstrumentor; + logger.log(Level.INFO, "requestId: " + this.requestId); + return this.requestId; + } + + public String getGeneratedIdFromMetadata(Metadata requestHeaders) { + String generatedId = requestHeaders.get( + Metadata.Key.of("x-filibuster-generated-id", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "generateId: " + generatedId); + return generatedId; + } + + public String getVectorClockFromMetadata(Metadata requestHeaders) { + String vclock = requestHeaders.get( + Metadata.Key.of("x-filibuster-vclock", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "vclock: " + vclock); + return vclock; + } + + public String getOriginVectorClockFromMetadata(Metadata requestHeaders) { + String originVclock = requestHeaders.get( + Metadata.Key.of("x-filibuster-origin-vclock", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "originVclock: " + originVclock); + return originVclock; + } + + public String getExecutionIndexFromMetadata(Metadata requestHeaders) { + String executionIndex = requestHeaders.get( + Metadata.Key.of("x-filibuster-execution-index", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "executionIndex: " + executionIndex); + return executionIndex; + } + + @Override + public ServerCall.Listener interceptCall( + ServerCall call, + Metadata headers, + ServerCallHandler next) { + GrpcRequest request = + new GrpcRequest( + call.getMethodDescriptor(), + headers, + call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)); + Context context; + + logger.log(Level.SEVERE, "SERVICE: " + serviceName + "CMEIK INTERCEPTOR CALLED"); + + if (serverInstrumentor != null) { + context = serverInstrumentor.start(Context.current(), request); + } else { + context = Context.current(); } - @Override - public ServerCall.Listener interceptCall( - ServerCall call, - Metadata headers, - ServerCallHandler next) { - GrpcRequest request = - new GrpcRequest( - call.getMethodDescriptor(), - headers, - call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)); - Context context; + try (Scope ignored = context.makeCurrent()) { + if (shouldInstrument()) { + logger.log(Level.INFO, "Entering server interceptor..."); - if (serverInstrumentor != null) { - context = serverInstrumentor.start(Context.current(), request); - } else { - context = Context.current(); - } + // ****************************************************************************************** + // Setup Filibuster instrumentation. + // ****************************************************************************************** - try (Scope ignored = context.makeCurrent()) { - return new FilibusterServerCall<>(call, context, request, headers).start(headers, next); - } catch (Throwable e) { - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, null, e); - } + logger.log(Level.INFO, "!!! Entering constructor."); - throw e; - } - } + String incomingExecutionIndexDebugString = getExecutionIndexFromMetadata(headers); + logger.log(Level.SEVERE, "SERVICE: " + serviceName + "CMEIK: " + incomingExecutionIndexDebugString); - final class FilibusterServerCall - extends ForwardingServerCall.SimpleForwardingServerCall { - final private Metadata requestHeaders; + FilibusterServerInstrumentor filibusterServerInstrumentor = new FilibusterServerInstrumentor( + serviceName, + shouldCommunicateWithServer(), + getRequestIdFromMetadata(headers), + getGeneratedIdFromMetadata(headers), + getVectorClockFromMetadata(headers), + getOriginVectorClockFromMetadata(headers), + getExecutionIndexFromMetadata(headers), + contextStorage + ); - @SuppressWarnings("FieldCanBeLocal") - private final Context context; + logger.log(Level.INFO, "!!! Leaving constructor."); - @SuppressWarnings("FieldCanBeLocal") - private final GrpcRequest request; + // ****************************************************************************************** + // Force sleep if necessary. + // ****************************************************************************************** - private String requestId; + String sleepIntervalStr = headers.get( + Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER)); - public FilibusterServerCall(ServerCall delegate, Context context, GrpcRequest request, Metadata requestHeaders) { - super(delegate); - this.requestHeaders = requestHeaders; - this.context = context; - this.request = request; + if (sleepIntervalStr == null) { + sleepIntervalStr = "0"; } - FilibusterServerCallListener start(Metadata headers, ServerCallHandler next) { - return new FilibusterServerCallListener( - Contexts.interceptCall(io.grpc.Context.current(), this, headers, next), context, request); + int sleepInterval = Integer.parseInt(sleepIntervalStr); + if (sleepInterval > 0) { + try { + Thread.sleep(sleepInterval * 1000L); + } catch (InterruptedException e) { + // Do nothing. + } } // ****************************************************************************************** - // Accessors for metadata. + // Notify Filibuster before delegation. // ****************************************************************************************** - public String getRequestIdFromMetadata(Metadata requestHeaders) { - if (this.requestId == null) { - this.requestId = requestHeaders.get(Metadata.Key.of("x-filibuster-request-id", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "!!! Entering beforeInvocation."); - if (this.requestId == null) { - this.requestId = Helper.generateNewRequestId().toString(); - } - } + filibusterServerInstrumentor.beforeInvocation(); - logger.log(Level.INFO, "requestId: " + this.requestId); - return this.requestId; - } + logger.log(Level.INFO, "!!! Leaving beforeInvocation."); - public String getGeneratedIdFromMetadata(Metadata requestHeaders) { - String generatedId = requestHeaders.get( - Metadata.Key.of("x-filibuster-generated-id", Metadata.ASCII_STRING_MARSHALLER)); - logger.log(Level.INFO, "generateId: " + generatedId); - return generatedId; - } + // ****************************************************************************************** + // Delegate to underlying service. + // ****************************************************************************************** - public String getVectorClockFromMetadata(Metadata requestHeaders) { - String vclock = requestHeaders.get( - Metadata.Key.of("x-filibuster-vclock", Metadata.ASCII_STRING_MARSHALLER)); - logger.log(Level.INFO, "vclock: " + vclock); - return vclock; - } + logger.log(Level.INFO, "Leaving server interceptor..."); + } - public String getOriginVectorClockFromMetadata(Metadata requestHeaders) { - String originVclock = requestHeaders.get( - Metadata.Key.of("x-filibuster-origin-vclock", Metadata.ASCII_STRING_MARSHALLER)); - logger.log(Level.INFO, "originVclock: " + originVclock); - return originVclock; - } + return new FilibusterServerCall<>(call, context, request, headers).start(headers, next); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } - public String getExecutionIndexFromMetadata(Metadata requestHeaders) { - String executionIndex = requestHeaders.get( - Metadata.Key.of("x-filibuster-execution-index", Metadata.ASCII_STRING_MARSHALLER)); - logger.log(Level.INFO, "executionIndex: " + executionIndex); - return executionIndex; + throw e; + } + } + + final class FilibusterServerCall + extends ForwardingServerCall.SimpleForwardingServerCall { + @SuppressWarnings("FieldCanBeLocal") + final private Metadata requestHeaders; + + @SuppressWarnings("FieldCanBeLocal") + private final Context context; + + @SuppressWarnings("FieldCanBeLocal") + private final GrpcRequest request; + + public FilibusterServerCall(ServerCall delegate, Context context, GrpcRequest request, Metadata requestHeaders) { + super(delegate); + this.requestHeaders = requestHeaders; + this.context = context; + this.request = request; + } + + FilibusterServerCallListener start(Metadata headers, ServerCallHandler next) { + return new FilibusterServerCallListener( + Contexts.interceptCall(io.grpc.Context.current(), this, headers, next), context, request); + } + + // ****************************************************************************************** + // Implementation. + // ****************************************************************************************** + + @Override + public void sendMessage(RESPONSE message) { + try (Scope ignored = context.makeCurrent()) { + super.sendMessage(message); + } + } + + @Override + public void close(Status status, Metadata trailers) { + try { + delegate().close(status, trailers); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, status, e); } + throw e; + } - // ****************************************************************************************** - // Implementation. - // ****************************************************************************************** + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, status, status.getCause()); + } + } - @Override - public void sendMessage(RESPONSE message) { - try (Scope ignored = context.makeCurrent()) { - super.sendMessage(message); - } + @Override + public void sendHeaders(Metadata responseHeaders) { + if (!shouldInstrument()) { + try (Scope ignored = context.makeCurrent()) { + super.sendHeaders(responseHeaders); } + } else { + try (Scope ignored = context.makeCurrent()) { + super.sendHeaders(responseHeaders); + } + } + } - @Override - public void close(Status status, Metadata trailers) { - try { - delegate().close(status, trailers); - } catch (Throwable e) { - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, status, e); - } - throw e; - } - - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, status, status.getCause()); - } + final class FilibusterServerCallListener + extends ForwardingServerCallListener.SimpleForwardingServerCallListener { + private final Context context; + private final GrpcRequest request; + + FilibusterServerCallListener(ServerCall.Listener delegate, Context context, GrpcRequest request) { + super(delegate); + this.context = context; + this.request = request; + } + + @Override + public void onMessage(REQUEST message) { + delegate().onMessage(message); + } + + @Override + public void onHalfClose() { + try { + delegate().onHalfClose(); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } + throw e; } + } - @Override - public void sendHeaders(Metadata responseHeaders) { - if (!shouldInstrument()) { - try (Scope ignored = context.makeCurrent()) { - super.sendHeaders(responseHeaders); - } - } else { - logger.log(Level.INFO, "Entering server interceptor..."); - - // ****************************************************************************************** - // Setup Filibuster instrumentation. - // ****************************************************************************************** - - logger.log(Level.INFO, "!!! Entering constructor."); - - FilibusterServerInstrumentor filibusterServerInstrumentor = new FilibusterServerInstrumentor( - serviceName, - shouldCommunicateWithServer(), - getRequestIdFromMetadata(requestHeaders), - getGeneratedIdFromMetadata(requestHeaders), - getVectorClockFromMetadata(requestHeaders), - getOriginVectorClockFromMetadata(requestHeaders), - getExecutionIndexFromMetadata(requestHeaders), - contextStorage - ); - - logger.log(Level.INFO, "!!! Leaving constructor."); - - // ****************************************************************************************** - // Force sleep if necessary. - // ****************************************************************************************** - - String sleepIntervalStr = requestHeaders.get( - Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER)); - - if (sleepIntervalStr == null) { - sleepIntervalStr = "0"; - } - - int sleepInterval = Integer.parseInt(sleepIntervalStr); - if (sleepInterval > 0) { - try { - Thread.sleep(sleepInterval * 1000L); - } catch (InterruptedException e) { - // Do nothing. - } - } - - // ****************************************************************************************** - // Notify Filibuster before delegation. - // ****************************************************************************************** - - logger.log(Level.INFO, "!!! Entering beforeInvocation."); - - filibusterServerInstrumentor.beforeInvocation(); - - logger.log(Level.INFO, "!!! Leaving beforeInvocation."); - - // ****************************************************************************************** - // Delegate to underlying service. - // ****************************************************************************************** - - logger.log(Level.INFO, "Leaving server interceptor..."); - - try (Scope ignored = context.makeCurrent()) { - super.sendHeaders(responseHeaders); - } - } + @Override + public void onCancel() { + try { + delegate().onCancel(); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } + throw e; + } + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, null); } + } - final class FilibusterServerCallListener - extends ForwardingServerCallListener.SimpleForwardingServerCallListener { - private final Context context; - private final GrpcRequest request; - - FilibusterServerCallListener(Listener delegate, Context context, GrpcRequest request) { - super(delegate); - this.context = context; - this.request = request; - } - - @Override - public void onMessage(REQUEST message) { - delegate().onMessage(message); - } - - @Override - public void onHalfClose() { - try { - delegate().onHalfClose(); - } catch (Throwable e) { - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, null, e); - } - throw e; - } - } - - @Override - public void onCancel() { - try { - delegate().onCancel(); - } catch (Throwable e) { - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, null, e); - } - throw e; - } - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, null, null); - } - } - - @Override - public void onComplete() { - try { - delegate().onComplete(); - } catch (Throwable e) { - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, null, e); - } - throw e; - } - } - - @Override - public void onReady() { - try { - delegate().onReady(); - } catch (Throwable e) { - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, null, e); - } - throw e; - } - } + @Override + public void onComplete() { + try { + delegate().onComplete(); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } + throw e; + } + } + + @Override + public void onReady() { + try { + delegate().onReady(); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } + throw e; } + } } + } } From 921e82506ad758f7ee07335cf43f6284448efc9f Mon Sep 17 00:00:00 2001 From: Christopher Meiklejohn Date: Thu, 10 Feb 2022 12:01:38 -0500 Subject: [PATCH 08/11] Switch dependencies to 0.19-SNAPSHOT. --- instrumentation/armeria-1.3/library/build.gradle.kts | 2 +- instrumentation/grpc-1.6/library/build.gradle.kts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/armeria-1.3/library/build.gradle.kts b/instrumentation/armeria-1.3/library/build.gradle.kts index ffe5b6933e78..dba873fbffec 100644 --- a/instrumentation/armeria-1.3/library/build.gradle.kts +++ b/instrumentation/armeria-1.3/library/build.gradle.kts @@ -6,7 +6,7 @@ plugins { dependencies { library("com.linecorp.armeria:armeria:1.3.0") - library("cloud.filibuster:instrumentation:0.17-SNAPSHOT") + library("cloud.filibuster:instrumentation:0.19-SNAPSHOT") library("com.github.cliftonlabs:json-simple:2.1.2") library("org.json:json:20210307") diff --git a/instrumentation/grpc-1.6/library/build.gradle.kts b/instrumentation/grpc-1.6/library/build.gradle.kts index 72af1e96bad6..d94d0ff18eee 100644 --- a/instrumentation/grpc-1.6/library/build.gradle.kts +++ b/instrumentation/grpc-1.6/library/build.gradle.kts @@ -7,7 +7,7 @@ val grpcVersion = "1.6.0" dependencies { library("io.grpc:grpc-core:$grpcVersion") - library("cloud.filibuster:instrumentation:0.17-SNAPSHOT") + library("cloud.filibuster:instrumentation:0.19-SNAPSHOT") testLibrary("io.grpc:grpc-netty:$grpcVersion") testLibrary("io.grpc:grpc-protobuf:$grpcVersion") From 8a45e15da3e715b7e11a98e2f84702cdf8f76548 Mon Sep 17 00:00:00 2001 From: Christopher Meiklejohn Date: Thu, 10 Feb 2022 12:28:03 -0500 Subject: [PATCH 09/11] Update client and server interceptors. --- ...nTelemetryFilibusterClientInterceptor.java | 779 +++++++++--------- ...nTelemetryFilibusterServerInterceptor.java | 507 ++++++------ 2 files changed, 636 insertions(+), 650 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterClientInterceptor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterClientInterceptor.java index 6b6c92573ce2..4dd616907c19 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterClientInterceptor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterClientInterceptor.java @@ -29,439 +29,430 @@ import static cloud.filibuster.instrumentation.Helper.getDisableServerCommunicationFromEnvironment; public class OpenTelemetryFilibusterClientInterceptor implements ClientInterceptor { - private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterClientInterceptor.class.getName()); + private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterClientInterceptor.class.getName()); - @SuppressWarnings("FieldCanBeFinal") - protected String serviceName; + @SuppressWarnings("FieldCanBeFinal") + protected String serviceName; - @SuppressWarnings("FieldCanBeFinal") - protected ContextStorage contextStorage; + @SuppressWarnings("FieldCanBeFinal") + protected ContextStorage contextStorage; - public static Boolean disableServerCommunication = false; - public static Boolean disableInstrumentation = false; + public static Boolean disableServerCommunication = false; + public static Boolean disableInstrumentation = false; - @Nullable - private final Instrumenter clientInstrumentor; + @Nullable + private final Instrumenter clientInstrumentor; - @SuppressWarnings("FieldCanBeLocal") - private final ContextPropagators propagators; + @SuppressWarnings("FieldCanBeLocal") + private final ContextPropagators propagators; - private static boolean shouldInstrument() { - if (disableInstrumentation) { - return false; + private static boolean shouldInstrument() { + if (disableInstrumentation) { + return false; + } + + return !getDisableInstrumentationFromEnvironment(); } - return !getDisableInstrumentationFromEnvironment(); - } + private static boolean shouldCommunicateWithServer() { + if (disableServerCommunication) { + return false; + } - private static boolean shouldCommunicateWithServer() { - if (disableServerCommunication) { - return false; + return !getDisableServerCommunicationFromEnvironment(); } - return !getDisableServerCommunicationFromEnvironment(); - } - - private static Status generateCorrectStatusForAbort(FilibusterClientInstrumentor filibusterClientInstrumentor) { - JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); - JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); - String codeStr = forcedExceptionMetadata.getString("code"); - Status.Code code = Status.Code.valueOf(codeStr); - Status status = Status.fromCode(code); - return status; - } - - private static Status generateException(FilibusterClientInstrumentor filibusterClientInstrumentor) { - JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); - - // Create the exception to throw. - String exceptionNameString = forcedException.getString("name"); - JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); - String causeString = forcedExceptionMetadata.getString("cause"); - String codeStr = forcedExceptionMetadata.getString("code"); - Status.Code code = Status.Code.valueOf(codeStr); - - // Notify Filibuster of failure. - HashMap additionalMetadata = new HashMap<>(); - additionalMetadata.put("code", codeStr); - filibusterClientInstrumentor.afterInvocationWithException(exceptionNameString, causeString, additionalMetadata); - - // Return status. - return Status.fromCode(code); - } - - private static Status generateExceptionFromFailureMetadata(FilibusterClientInstrumentor filibusterClientInstrumentor) { - JSONObject failureMetadata = filibusterClientInstrumentor.getFailureMetadata(); - JSONObject exception = failureMetadata.getJSONObject("exception"); - JSONObject exceptionMetadata = exception.getJSONObject("metadata"); - - // Create the exception to throw. - String exceptionNameString = "io.grpc.StatusRuntimeException"; - String codeStr = exceptionMetadata.getString("code"); - Status.Code code = Status.Code.valueOf(codeStr); - StatusRuntimeException status = Status.fromCode(code).asRuntimeException(); - String causeString = ""; - - // Notify Filibuster of failure. - HashMap additionalMetadata = new HashMap<>(); - additionalMetadata.put("name", exceptionNameString); - additionalMetadata.put("code", codeStr); - filibusterClientInstrumentor.afterInvocationWithException(exceptionNameString, causeString, additionalMetadata); - - // Return status. - return Status.fromCode(code); - } - - public OpenTelemetryFilibusterClientInterceptor(Instrumenter clientInstrumentor, ContextPropagators propagators) { - this.serviceName = System.getenv("SERVICE_NAME"); - this.contextStorage = new OpenTelemetryContextStorage(); - this.clientInstrumentor = clientInstrumentor; - this.propagators = propagators; - } - - public OpenTelemetryFilibusterClientInterceptor(String serviceName, Instrumenter clientInstrumentor, ContextPropagators propagators) { - this.serviceName = serviceName; - this.contextStorage = new OpenTelemetryContextStorage(); - this.clientInstrumentor = clientInstrumentor; - this.propagators = propagators; - } - - @Override - public ClientCall interceptCall( - MethodDescriptor method, CallOptions callOptions, Channel next) { - - if (method.getType() != MethodDescriptor.MethodType.UNARY) { - return next.newCall(method, callOptions); + private static Status generateCorrectStatusForAbort(FilibusterClientInstrumentor filibusterClientInstrumentor) { + JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); + JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); + String codeStr = forcedExceptionMetadata.getString("code"); + Status.Code code = Status.Code.valueOf(codeStr); + Status status = Status.fromCode(code); + return status; } - GrpcRequest request = new GrpcRequest(method, null, null); - Context parentContext = Context.current(); + private static Status generateException(FilibusterClientInstrumentor filibusterClientInstrumentor) { + JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); - Context context; + // Create the exception to throw. + String exceptionNameString = forcedException.getString("name"); + JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); + String causeString = forcedExceptionMetadata.getString("cause"); + String codeStr = forcedExceptionMetadata.getString("code"); + Status.Code code = Status.Code.valueOf(codeStr); - if (clientInstrumentor != null) { - context = clientInstrumentor.start(parentContext, request); - } else { - context = Context.current(); + // Notify Filibuster of failure. + HashMap additionalMetadata = new HashMap<>(); + additionalMetadata.put("code", codeStr); + filibusterClientInstrumentor.afterInvocationWithException(exceptionNameString, causeString, additionalMetadata); + + // Return status. + return Status.fromCode(code); } - // return the filibuster client interceptor. - return new ForwardingClientCall() { - @Nullable - private ClientCall delegate; - private Listener responseListener; - - @Nullable - private Metadata headers; - private int requestTokens; - private FilibusterClientInstrumentor filibusterClientInstrumentor; - - @Override - protected ClientCall delegate() { - if (delegate == null) { - throw new UnsupportedOperationException(); - } - return delegate; - } - - @Override - public void start(Listener responseListener, Metadata headers) { - logger.log(Level.INFO, "INSIDE: start!"); - - this.headers = headers; - this.responseListener = responseListener; - } - - @Override - public void request(int requests) { - if (delegate == null) { - requestTokens += requests; - return; + private static Status generateExceptionFromFailureMetadata(FilibusterClientInstrumentor filibusterClientInstrumentor) { + JSONObject failureMetadata = filibusterClientInstrumentor.getFailureMetadata(); + JSONObject exception = failureMetadata.getJSONObject("exception"); + JSONObject exceptionMetadata = exception.getJSONObject("metadata"); + + // Create the exception to throw. + String exceptionNameString = "io.grpc.StatusRuntimeException"; + String codeStr = exceptionMetadata.getString("code"); + Status.Code code = Status.Code.valueOf(codeStr); + StatusRuntimeException status = Status.fromCode(code).asRuntimeException(); + String causeString = ""; + + // Notify Filibuster of failure. + HashMap additionalMetadata = new HashMap<>(); + additionalMetadata.put("name", exceptionNameString); + additionalMetadata.put("code", codeStr); + filibusterClientInstrumentor.afterInvocationWithException(exceptionNameString, causeString, additionalMetadata); + + // Return status. + return Status.fromCode(code); + } + + public OpenTelemetryFilibusterClientInterceptor(Instrumenter clientInstrumentor, ContextPropagators propagators) { + this.serviceName = System.getenv("SERVICE_NAME"); + this.contextStorage = new OpenTelemetryContextStorage(); + this.clientInstrumentor = clientInstrumentor; + this.propagators = propagators; + } + + public OpenTelemetryFilibusterClientInterceptor(String serviceName, Instrumenter clientInstrumentor, ContextPropagators propagators) { + this.serviceName = serviceName; + this.contextStorage = new OpenTelemetryContextStorage(); + this.clientInstrumentor = clientInstrumentor; + this.propagators = propagators; + } + + @Override + public ClientCall interceptCall( + MethodDescriptor method, CallOptions callOptions, Channel next) { + + if (method.getType() != MethodDescriptor.MethodType.UNARY) { + return next.newCall(method, callOptions); } - super.request(requests); - } - - // This method is invoked with the message from the Client to the Service. - // message: type of the message issued from the Client (e.g., Hello$HelloRequest) - @Override - public void sendMessage(REQUEST message) { - logger.log(Level.INFO, "INSIDE: sendMessage!"); - logger.log(Level.INFO, "message: " + message.toString()); - - try (Scope ignored = context.makeCurrent()) { - OpenTelemetryContextStorage otelContextStorage = (OpenTelemetryContextStorage) contextStorage; - - logger.log(Level.INFO, "CMEIK CONTEXT: " + context.toString()); - logger.log(Level.INFO, "CMEIK Context.current(): " + Context.current()); - logger.log(Level.INFO, "CMEIK contextStorage.getExecutionIndex(): " + contextStorage.getExecutionIndex()); - logger.log(Level.INFO, "CMEIK contextStorage.getExecutionIndex(Context.current()): " + otelContextStorage.getExecutionIndexFromContext(Context.current())); - logger.log(Level.INFO, "CMEIK SOURCE SERVICE: " + serviceName); - logger.log(Level.INFO, "CMEIK: sendMessage!"); - - // ****************************************************************************************** - // Figure out if we are inside of instrumentation. - // ****************************************************************************************** - - String instrumentationRequestStr = headers.get( - Metadata.Key.of("x-filibuster-instrumentation", Metadata.ASCII_STRING_MARSHALLER)); - logger.log(Level.INFO, "instrumentationRequestStr: " + instrumentationRequestStr); - boolean instrumentationRequest = Boolean.parseBoolean(instrumentationRequestStr); - logger.log(Level.INFO, "instrumentationRequest: " + instrumentationRequest); - - if (! shouldInstrument() || instrumentationRequest) { - delegate = next.newCall(method, callOptions); - super.start(responseListener, headers); - headers = null; - if (requestTokens > 0) { - super.request(requestTokens); - requestTokens = 0; - } - } else { - // ****************************************************************************************** - // Extract callsite information. - // ****************************************************************************************** - - String grpcFullMethodName = method.getFullMethodName(); - String grpcServiceName = grpcFullMethodName.substring(0, grpcFullMethodName.indexOf("/")); - String grpcRpcName = grpcFullMethodName.replace(grpcServiceName + "/", ""); - -// logger.log(Level.INFO, "method: " + method); - logger.log(Level.INFO, "grpcFullMethodName: " + grpcFullMethodName); - logger.log(Level.INFO, "grpcServiceName: " + grpcServiceName); - logger.log(Level.INFO, "grpcRpcName: " + grpcRpcName); - - // ****************************************************************************************** - // Construct preliminary call site information. - // ****************************************************************************************** - - Callsite callsite = new Callsite( - serviceName, - grpcServiceName, - grpcFullMethodName, - message.toString() - ); - - // ****************************************************************************************** - // Prepare for invocation. - // ****************************************************************************************** - - this.filibusterClientInstrumentor = new FilibusterClientInstrumentor( - serviceName, - shouldCommunicateWithServer(), - contextStorage, - callsite - ); - filibusterClientInstrumentor.prepareForInvocation(); - - // ****************************************************************************************** - // Record invocation. - // ****************************************************************************************** - - filibusterClientInstrumentor.beforeInvocation(); - - // ****************************************************************************************** - // Attach metadata to outgoing request. - // ****************************************************************************************** - - logger.log(Level.INFO, "requestId: " + filibusterClientInstrumentor.getOutgoingRequestId()); - - if (filibusterClientInstrumentor.getOutgoingRequestId() != null) { - headers.put( - Metadata.Key.of("x-filibuster-request-id", Metadata.ASCII_STRING_MARSHALLER), - filibusterClientInstrumentor.getOutgoingRequestId() - ); - } - if (filibusterClientInstrumentor.getGeneratedId() > -1) { - headers.put( - Metadata.Key.of("x-filibuster-generated-id", Metadata.ASCII_STRING_MARSHALLER), - String.valueOf(filibusterClientInstrumentor.getGeneratedId()) - ); - } + GrpcRequest request = new GrpcRequest(method, null, null); + Context parentContext = Context.current(); - headers.put( - Metadata.Key.of("x-filibuster-vclock", Metadata.ASCII_STRING_MARSHALLER), - filibusterClientInstrumentor.getVectorClock().toString() - ); - headers.put( - Metadata.Key.of("x-filibuster-origin-vclock", Metadata.ASCII_STRING_MARSHALLER), - filibusterClientInstrumentor.getOriginVectorClock().toString() - ); - headers.put( - Metadata.Key.of("x-filibuster-execution-index", Metadata.ASCII_STRING_MARSHALLER), - filibusterClientInstrumentor.getExecutionIndex().toString() - ); - - // ****************************************************************************************** - // Get failure information. - // ****************************************************************************************** - - JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); - JSONObject failureMetadata = filibusterClientInstrumentor.getFailureMetadata(); - - logger.log(Level.INFO, "forcedException: " + forcedException); - logger.log(Level.INFO, "failureMetadata: " + failureMetadata); - - // ****************************************************************************************** - // Setup additional failure headers, if necessary. - // ****************************************************************************************** - - if (forcedException != null) { - JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); - - if (forcedExceptionMetadata.has("sleep")) { - int sleepInterval = forcedExceptionMetadata.getInt("sleep"); - headers.put( - Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER), - String.valueOf(sleepInterval) - ); - } else { - headers.put( - Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER), - String.valueOf(0) - ); - } - } + Context context; - // ****************************************************************************************** - // If we need to override the response, do it now before proceeding. - // ****************************************************************************************** + if (clientInstrumentor != null) { + context = clientInstrumentor.start(parentContext, request); + } else { + context = Context.current(); + } - if (failureMetadata != null && filibusterClientInstrumentor.shouldAbort()) { - delegate = new NoopClientCall(); - Status status = generateExceptionFromFailureMetadata(filibusterClientInstrumentor); - responseListener.onClose(status, new Metadata()); - return; + // return the filibuster client interceptor. + return new ForwardingClientCall() { + @Nullable + private ClientCall delegate; + private Listener responseListener; + + @Nullable + private Metadata headers; + private int requestTokens; + private FilibusterClientInstrumentor filibusterClientInstrumentor; + + @Override + protected ClientCall delegate() { + if (delegate == null) { + throw new UnsupportedOperationException(); + } + return delegate; } - // ****************************************************************************************** - // If we need to throw, this is where we throw. - // ****************************************************************************************** + @Override + public void start(Listener responseListener, Metadata headers) { + logger.log(Level.INFO, "INSIDE: start!"); - if (forcedException != null && filibusterClientInstrumentor.shouldAbort()) { - delegate = new NoopClientCall(); - Status status = generateException(filibusterClientInstrumentor); - responseListener.onClose(status, new Metadata()); - return; + this.headers = headers; + this.responseListener = responseListener; } - delegate = next.newCall(method, callOptions); - super.start(new FilibusterClientCallListener<>( - responseListener, parentContext, context, request, filibusterClientInstrumentor), headers); - headers = null; - if (requestTokens > 0) { - super.request(requestTokens); - requestTokens = 0; + @Override + public void request(int requests) { + if (delegate == null) { + requestTokens += requests; + return; + } + super.request(requests); } - } - super.sendMessage(message); - } catch (Throwable e) { - if (clientInstrumentor != null) { - clientInstrumentor.end(context, request, null, e); - } + // This method is invoked with the message from the Client to the Service. + // message: type of the message issued from the Client (e.g., Hello$HelloRequest) + @Override + public void sendMessage(REQUEST message) { + logger.log(Level.INFO, "INSIDE: sendMessage!"); + logger.log(Level.INFO, "message: " + message.toString()); + + try (Scope ignored = context.makeCurrent()) { + // ****************************************************************************************** + // Figure out if we are inside of instrumentation. + // ****************************************************************************************** + + String instrumentationRequestStr = headers.get( + Metadata.Key.of("x-filibuster-instrumentation", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "instrumentationRequestStr: " + instrumentationRequestStr); + boolean instrumentationRequest = Boolean.parseBoolean(instrumentationRequestStr); + logger.log(Level.INFO, "instrumentationRequest: " + instrumentationRequest); + + if (! shouldInstrument() || instrumentationRequest) { + delegate = next.newCall(method, callOptions); + super.start(responseListener, headers); + headers = null; + if (requestTokens > 0) { + super.request(requestTokens); + requestTokens = 0; + } + } else { + // ****************************************************************************************** + // Extract callsite information. + // ****************************************************************************************** + + String grpcFullMethodName = method.getFullMethodName(); + String grpcServiceName = grpcFullMethodName.substring(0, grpcFullMethodName.indexOf("/")); + String grpcRpcName = grpcFullMethodName.replace(grpcServiceName + "/", ""); + +// logger.log(Level.INFO, "method: " + method); + logger.log(Level.INFO, "grpcFullMethodName: " + grpcFullMethodName); + logger.log(Level.INFO, "grpcServiceName: " + grpcServiceName); + logger.log(Level.INFO, "grpcRpcName: " + grpcRpcName); + + // ****************************************************************************************** + // Construct preliminary call site information. + // ****************************************************************************************** + + Callsite callsite = new Callsite( + serviceName, + grpcServiceName, + grpcFullMethodName, + message.toString() + ); + + // ****************************************************************************************** + // Prepare for invocation. + // ****************************************************************************************** + + this.filibusterClientInstrumentor = new FilibusterClientInstrumentor( + serviceName, + shouldCommunicateWithServer(), + contextStorage, + callsite + ); + filibusterClientInstrumentor.prepareForInvocation(); + + // ****************************************************************************************** + // Record invocation. + // ****************************************************************************************** + + filibusterClientInstrumentor.beforeInvocation(); + + // ****************************************************************************************** + // Attach metadata to outgoing request. + // ****************************************************************************************** + + logger.log(Level.INFO, "requestId: " + filibusterClientInstrumentor.getOutgoingRequestId()); + + if (filibusterClientInstrumentor.getOutgoingRequestId() != null) { + headers.put( + Metadata.Key.of("x-filibuster-request-id", Metadata.ASCII_STRING_MARSHALLER), + filibusterClientInstrumentor.getOutgoingRequestId() + ); + } + + if (filibusterClientInstrumentor.getGeneratedId() > -1) { + headers.put( + Metadata.Key.of("x-filibuster-generated-id", Metadata.ASCII_STRING_MARSHALLER), + String.valueOf(filibusterClientInstrumentor.getGeneratedId()) + ); + } + + headers.put( + Metadata.Key.of("x-filibuster-vclock", Metadata.ASCII_STRING_MARSHALLER), + filibusterClientInstrumentor.getVectorClock().toString() + ); + headers.put( + Metadata.Key.of("x-filibuster-origin-vclock", Metadata.ASCII_STRING_MARSHALLER), + filibusterClientInstrumentor.getOriginVectorClock().toString() + ); + headers.put( + Metadata.Key.of("x-filibuster-execution-index", Metadata.ASCII_STRING_MARSHALLER), + filibusterClientInstrumentor.getExecutionIndex().toString() + ); + + // ****************************************************************************************** + // Get failure information. + // ****************************************************************************************** + + JSONObject forcedException = filibusterClientInstrumentor.getForcedException(); + JSONObject failureMetadata = filibusterClientInstrumentor.getFailureMetadata(); + + logger.log(Level.INFO, "forcedException: " + forcedException); + logger.log(Level.INFO, "failureMetadata: " + failureMetadata); + + // ****************************************************************************************** + // Setup additional failure headers, if necessary. + // ****************************************************************************************** + + if (forcedException != null) { + JSONObject forcedExceptionMetadata = forcedException.getJSONObject("metadata"); + + if (forcedExceptionMetadata.has("sleep")) { + int sleepInterval = forcedExceptionMetadata.getInt("sleep"); + headers.put( + Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER), + String.valueOf(sleepInterval) + ); + } else { + headers.put( + Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER), + String.valueOf(0) + ); + } + } + + // ****************************************************************************************** + // If we need to override the response, do it now before proceeding. + // ****************************************************************************************** + + if (failureMetadata != null && filibusterClientInstrumentor.shouldAbort()) { + delegate = new NoopClientCall(); + Status status = generateExceptionFromFailureMetadata(filibusterClientInstrumentor); + responseListener.onClose(status, new Metadata()); + return; + } + + // ****************************************************************************************** + // If we need to throw, this is where we throw. + // ****************************************************************************************** + + if (forcedException != null && filibusterClientInstrumentor.shouldAbort()) { + delegate = new NoopClientCall(); + Status status = generateException(filibusterClientInstrumentor); + responseListener.onClose(status, new Metadata()); + return; + } + + delegate = next.newCall(method, callOptions); + super.start(new FilibusterClientCallListener<>( + responseListener, parentContext, context, request, filibusterClientInstrumentor), headers); + headers = null; + if (requestTokens > 0) { + super.request(requestTokens); + requestTokens = 0; + } + } + + super.sendMessage(message); + } catch (Throwable e) { + if (clientInstrumentor != null) { + clientInstrumentor.end(context, request, null, e); + } + + throw e; + } + } + }; + } - throw e; + // ********************************************************************* + // Client caller listener. + + @SuppressWarnings("ClassCanBeStatic") + final class FilibusterClientCallListener + extends ForwardingClientCallListener.SimpleForwardingClientCallListener { + + private final FilibusterClientInstrumentor filibusterClientInstrumentor; + private final Context parentContext; + private final Context context; + private final GrpcRequest request; + + FilibusterClientCallListener(ClientCall.Listener delegate, + Context parentContext, + Context context, + GrpcRequest request, + FilibusterClientInstrumentor filibusterClientInstrumentor) { + super(delegate); + this.filibusterClientInstrumentor = filibusterClientInstrumentor; + this.parentContext = parentContext; + this.context = context; + this.request = request; } - } - }; - } - - // ********************************************************************* - // Client caller listener. - - @SuppressWarnings("ClassCanBeStatic") - final class FilibusterClientCallListener - extends ForwardingClientCallListener.SimpleForwardingClientCallListener { - - private final FilibusterClientInstrumentor filibusterClientInstrumentor; - private final Context parentContext; - private final Context context; - private final GrpcRequest request; - - FilibusterClientCallListener(ClientCall.Listener delegate, - Context parentContext, - Context context, - GrpcRequest request, - FilibusterClientInstrumentor filibusterClientInstrumentor) { - super(delegate); - this.filibusterClientInstrumentor = filibusterClientInstrumentor; - this.parentContext = parentContext; - this.context = context; - this.request = request; - } - // invoked on successful response with the message from the Server to the Client - // message: type of message issued from the Server to the Client (e.g., Hello$HelloReply) - @Override - public void onMessage(RESPONSE message) { - logger.log(Level.INFO, "INSIDE: onMessage!"); - logger.log(Level.INFO, "message: " + message); - - if (! filibusterClientInstrumentor.shouldAbort()) { - // Request completed normally, but we want to throw the exception anyway, generate and throw. - generateException(filibusterClientInstrumentor); - } else { - // Request completed normally. - - // Notify Filibuster of complete invocation with the proper response. - String className = message.getClass().getName(); - HashMap returnValueProperties = new HashMap<>(); - filibusterClientInstrumentor.afterInvocationComplete(className, returnValueProperties); - - // Delegate. - try (Scope ignored = context.makeCurrent()) { - delegate().onMessage(message); + // invoked on successful response with the message from the Server to the Client + // message: type of message issued from the Server to the Client (e.g., Hello$HelloReply) + @Override + public void onMessage(RESPONSE message) { + logger.log(Level.INFO, "INSIDE: onMessage!"); + logger.log(Level.INFO, "message: " + message); + + if (! filibusterClientInstrumentor.shouldAbort()) { + // Request completed normally, but we want to throw the exception anyway, generate and throw. + generateException(filibusterClientInstrumentor); + } else { + // Request completed normally. + + // Notify Filibuster of complete invocation with the proper response. + String className = message.getClass().getName(); + HashMap returnValueProperties = new HashMap<>(); + filibusterClientInstrumentor.afterInvocationComplete(className, returnValueProperties); + + // Delegate. + try (Scope ignored = context.makeCurrent()) { + delegate().onMessage(message); + } + } } - } - } - // invoked on an error: status set to a status message - // Status.code = FAILED_PRECONDITION, description = ..., cause = ... - // trailers metadata headers. - @Override - public void onClose(Status status, Metadata trailers) { - if (clientInstrumentor != null) { - clientInstrumentor.end(context, request, status, status.getCause()); - } + // invoked on an error: status set to a status message + // Status.code = FAILED_PRECONDITION, description = ..., cause = ... + // trailers metadata headers. + @Override + public void onClose(Status status, Metadata trailers) { + if (clientInstrumentor != null) { + clientInstrumentor.end(context, request, status, status.getCause()); + } - logger.log(Level.INFO, "INSIDE: onClose!"); - logger.log(Level.INFO, "status: " + status); - logger.log(Level.INFO, "trailers: " + trailers); + logger.log(Level.INFO, "INSIDE: onClose!"); + logger.log(Level.INFO, "status: " + status); + logger.log(Level.INFO, "trailers: " + trailers); - if (! filibusterClientInstrumentor.shouldAbort()) { - Status rewrittenStatus = generateCorrectStatusForAbort(filibusterClientInstrumentor); + if (! filibusterClientInstrumentor.shouldAbort()) { + Status rewrittenStatus = generateCorrectStatusForAbort(filibusterClientInstrumentor); - try (Scope ignored = parentContext.makeCurrent()) { - delegate().onClose(rewrittenStatus, trailers); - } - } + try (Scope ignored = parentContext.makeCurrent()) { + delegate().onClose(rewrittenStatus, trailers); + } + } - if (! status.isOk()) { - // Request completed -- if it completed with a failure, it will be coming here for - // the first time (didn't call onMessage) and therefore, we need to notify the Filibuster - // server that the call completed with failure. If it completed successfully, we would - // have already notified the Filibuster server in the onMessage callback. + if (! status.isOk()) { + // Request completed -- if it completed with a failure, it will be coming here for + // the first time (didn't call onMessage) and therefore, we need to notify the Filibuster + // server that the call completed with failure. If it completed successfully, we would + // have already notified the Filibuster server in the onMessage callback. + + // Notify Filibuster of error. + HashMap additionalMetadata = new HashMap<>(); + additionalMetadata.put("code", status.getCode().toString()); + String exceptionName = "io.grpc.StatusRuntimeException"; + // exception cause is always null, because it doesn't serialize and pass through even if provided. + filibusterClientInstrumentor.afterInvocationWithException(exceptionName, null, additionalMetadata); + } - // Notify Filibuster of error. - HashMap additionalMetadata = new HashMap<>(); - additionalMetadata.put("code", status.getCode().toString()); - String exceptionName = "io.grpc.StatusRuntimeException"; - // exception cause is always null, because it doesn't serialize and pass through even if provided. - filibusterClientInstrumentor.afterInvocationWithException(exceptionName, null, additionalMetadata); - } - - try (Scope ignored = parentContext.makeCurrent()) { - delegate().onClose(status, trailers); - } - } + try (Scope ignored = parentContext.makeCurrent()) { + delegate().onClose(status, trailers); + } + } - @Override - public void onReady() { - logger.log(Level.INFO, "INSIDE: onReady!"); - try (Scope ignored = context.makeCurrent()) { - delegate().onReady(); - } + @Override + public void onReady() { + logger.log(Level.INFO, "INSIDE: onReady!"); + try (Scope ignored = context.makeCurrent()) { + delegate().onReady(); + } + } } - } } diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterServerInterceptor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterServerInterceptor.java index f2efe4737beb..7846b457c8b3 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterServerInterceptor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryFilibusterServerInterceptor.java @@ -25,318 +25,313 @@ import static cloud.filibuster.instrumentation.Helper.getDisableServerCommunicationFromEnvironment; public class OpenTelemetryFilibusterServerInterceptor implements ServerInterceptor { - private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterServerInterceptor.class.getName()); + private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterServerInterceptor.class.getName()); - @SuppressWarnings("FieldCanBeFinal") - protected String serviceName; + @SuppressWarnings("FieldCanBeFinal") + protected String serviceName; - @SuppressWarnings("FieldCanBeFinal") - protected ContextStorage contextStorage; + @SuppressWarnings("FieldCanBeFinal") + protected ContextStorage contextStorage; - public static Boolean disableServerCommunication = false; - public static Boolean disableInstrumentation = false; + public static Boolean disableServerCommunication = false; + public static Boolean disableInstrumentation = false; - @Nullable - private String requestId; + @Nullable + private String requestId; - @Nullable - private final Instrumenter serverInstrumentor; + @Nullable + private final Instrumenter serverInstrumentor; - private static boolean shouldInstrument() { - if (disableInstrumentation) { - return false; + private static boolean shouldInstrument() { + if (disableInstrumentation) { + return false; + } + + return !getDisableInstrumentationFromEnvironment(); } - return !getDisableInstrumentationFromEnvironment(); - } + private static boolean shouldCommunicateWithServer() { + if (disableServerCommunication) { + return false; + } - private static boolean shouldCommunicateWithServer() { - if (disableServerCommunication) { - return false; + return !getDisableServerCommunicationFromEnvironment(); } - return !getDisableServerCommunicationFromEnvironment(); - } + public OpenTelemetryFilibusterServerInterceptor(Instrumenter serverInstrumentor) { + this.serviceName = System.getenv("SERVICE_NAME"); + this.contextStorage = new OpenTelemetryContextStorage(); + this.serverInstrumentor = serverInstrumentor; + } - public OpenTelemetryFilibusterServerInterceptor(Instrumenter serverInstrumentor) { - this.serviceName = System.getenv("SERVICE_NAME"); - this.contextStorage = new OpenTelemetryContextStorage(); - this.serverInstrumentor = serverInstrumentor; - } + public OpenTelemetryFilibusterServerInterceptor(String serviceName, Instrumenter serverInstrumentor) { + this.serviceName = serviceName; + this.contextStorage = new OpenTelemetryContextStorage(); + this.serverInstrumentor = serverInstrumentor; + } - public OpenTelemetryFilibusterServerInterceptor(String serviceName, Instrumenter serverInstrumentor) { - this.serviceName = serviceName; - this.contextStorage = new OpenTelemetryContextStorage(); - this.serverInstrumentor = serverInstrumentor; - } + // ****************************************************************************************** + // Accessors for metadata. + // ****************************************************************************************** - // ****************************************************************************************** - // Accessors for metadata. - // ****************************************************************************************** + public String getRequestIdFromMetadata(Metadata requestHeaders) { + if (this.requestId == null) { + this.requestId = requestHeaders.get(Metadata.Key.of("x-filibuster-request-id", Metadata.ASCII_STRING_MARSHALLER)); - public String getRequestIdFromMetadata(Metadata requestHeaders) { - if (this.requestId == null) { - this.requestId = requestHeaders.get(Metadata.Key.of("x-filibuster-request-id", Metadata.ASCII_STRING_MARSHALLER)); + if (this.requestId == null) { + this.requestId = Helper.generateNewRequestId().toString(); + } + } - if (this.requestId == null) { - this.requestId = Helper.generateNewRequestId().toString(); - } + logger.log(Level.INFO, "requestId: " + this.requestId); + return this.requestId; } - logger.log(Level.INFO, "requestId: " + this.requestId); - return this.requestId; - } - - public String getGeneratedIdFromMetadata(Metadata requestHeaders) { - String generatedId = requestHeaders.get( - Metadata.Key.of("x-filibuster-generated-id", Metadata.ASCII_STRING_MARSHALLER)); - logger.log(Level.INFO, "generateId: " + generatedId); - return generatedId; - } - - public String getVectorClockFromMetadata(Metadata requestHeaders) { - String vclock = requestHeaders.get( - Metadata.Key.of("x-filibuster-vclock", Metadata.ASCII_STRING_MARSHALLER)); - logger.log(Level.INFO, "vclock: " + vclock); - return vclock; - } - - public String getOriginVectorClockFromMetadata(Metadata requestHeaders) { - String originVclock = requestHeaders.get( - Metadata.Key.of("x-filibuster-origin-vclock", Metadata.ASCII_STRING_MARSHALLER)); - logger.log(Level.INFO, "originVclock: " + originVclock); - return originVclock; - } - - public String getExecutionIndexFromMetadata(Metadata requestHeaders) { - String executionIndex = requestHeaders.get( - Metadata.Key.of("x-filibuster-execution-index", Metadata.ASCII_STRING_MARSHALLER)); - logger.log(Level.INFO, "executionIndex: " + executionIndex); - return executionIndex; - } - - @Override - public ServerCall.Listener interceptCall( - ServerCall call, - Metadata headers, - ServerCallHandler next) { - GrpcRequest request = - new GrpcRequest( - call.getMethodDescriptor(), - headers, - call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)); - Context context; - - logger.log(Level.SEVERE, "SERVICE: " + serviceName + "CMEIK INTERCEPTOR CALLED"); - - if (serverInstrumentor != null) { - context = serverInstrumentor.start(Context.current(), request); - } else { - context = Context.current(); + public String getGeneratedIdFromMetadata(Metadata requestHeaders) { + String generatedId = requestHeaders.get( + Metadata.Key.of("x-filibuster-generated-id", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "generateId: " + generatedId); + return generatedId; } - try (Scope ignored = context.makeCurrent()) { - if (shouldInstrument()) { - logger.log(Level.INFO, "Entering server interceptor..."); + public String getVectorClockFromMetadata(Metadata requestHeaders) { + String vclock = requestHeaders.get( + Metadata.Key.of("x-filibuster-vclock", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "vclock: " + vclock); + return vclock; + } - // ****************************************************************************************** - // Setup Filibuster instrumentation. - // ****************************************************************************************** + public String getOriginVectorClockFromMetadata(Metadata requestHeaders) { + String originVclock = requestHeaders.get( + Metadata.Key.of("x-filibuster-origin-vclock", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "originVclock: " + originVclock); + return originVclock; + } - logger.log(Level.INFO, "!!! Entering constructor."); + public String getExecutionIndexFromMetadata(Metadata requestHeaders) { + String executionIndex = requestHeaders.get( + Metadata.Key.of("x-filibuster-execution-index", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "executionIndex: " + executionIndex); + return executionIndex; + } - String incomingExecutionIndexDebugString = getExecutionIndexFromMetadata(headers); - logger.log(Level.SEVERE, "SERVICE: " + serviceName + "CMEIK: " + incomingExecutionIndexDebugString); + @Override + public ServerCall.Listener interceptCall( + ServerCall call, + Metadata headers, + ServerCallHandler next) { + GrpcRequest request = + new GrpcRequest( + call.getMethodDescriptor(), + headers, + call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)); + Context context; - FilibusterServerInstrumentor filibusterServerInstrumentor = new FilibusterServerInstrumentor( - serviceName, - shouldCommunicateWithServer(), - getRequestIdFromMetadata(headers), - getGeneratedIdFromMetadata(headers), - getVectorClockFromMetadata(headers), - getOriginVectorClockFromMetadata(headers), - getExecutionIndexFromMetadata(headers), - contextStorage - ); + if (serverInstrumentor != null) { + context = serverInstrumentor.start(Context.current(), request); + } else { + context = Context.current(); + } - logger.log(Level.INFO, "!!! Leaving constructor."); + try (Scope ignored = context.makeCurrent()) { + if (shouldInstrument()) { + logger.log(Level.INFO, "Entering server interceptor..."); - // ****************************************************************************************** - // Force sleep if necessary. - // ****************************************************************************************** + // ****************************************************************************************** + // Setup Filibuster instrumentation. + // ****************************************************************************************** - String sleepIntervalStr = headers.get( - Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER)); + logger.log(Level.INFO, "!!! Entering constructor."); - if (sleepIntervalStr == null) { - sleepIntervalStr = "0"; - } + FilibusterServerInstrumentor filibusterServerInstrumentor = new FilibusterServerInstrumentor( + serviceName, + shouldCommunicateWithServer(), + getRequestIdFromMetadata(headers), + getGeneratedIdFromMetadata(headers), + getVectorClockFromMetadata(headers), + getOriginVectorClockFromMetadata(headers), + getExecutionIndexFromMetadata(headers), + contextStorage + ); - int sleepInterval = Integer.parseInt(sleepIntervalStr); - if (sleepInterval > 0) { - try { - Thread.sleep(sleepInterval * 1000L); - } catch (InterruptedException e) { - // Do nothing. - } - } + logger.log(Level.INFO, "!!! Leaving constructor."); - // ****************************************************************************************** - // Notify Filibuster before delegation. - // ****************************************************************************************** + // ****************************************************************************************** + // Force sleep if necessary. + // ****************************************************************************************** - logger.log(Level.INFO, "!!! Entering beforeInvocation."); + String sleepIntervalStr = headers.get( + Metadata.Key.of("x-filibuster-forced-sleep", Metadata.ASCII_STRING_MARSHALLER)); - filibusterServerInstrumentor.beforeInvocation(); + if (sleepIntervalStr == null) { + sleepIntervalStr = "0"; + } - logger.log(Level.INFO, "!!! Leaving beforeInvocation."); + int sleepInterval = Integer.parseInt(sleepIntervalStr); + if (sleepInterval > 0) { + try { + Thread.sleep(sleepInterval * 1000L); + } catch (InterruptedException e) { + // Do nothing. + } + } - // ****************************************************************************************** - // Delegate to underlying service. - // ****************************************************************************************** + // ****************************************************************************************** + // Notify Filibuster before delegation. + // ****************************************************************************************** - logger.log(Level.INFO, "Leaving server interceptor..."); - } + logger.log(Level.INFO, "!!! Entering beforeInvocation."); - return new FilibusterServerCall<>(call, context, request, headers).start(headers, next); - } catch (Throwable e) { - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, null, e); - } + filibusterServerInstrumentor.beforeInvocation(); - throw e; - } - } + logger.log(Level.INFO, "!!! Leaving beforeInvocation."); - final class FilibusterServerCall - extends ForwardingServerCall.SimpleForwardingServerCall { - @SuppressWarnings("FieldCanBeLocal") - final private Metadata requestHeaders; + // ****************************************************************************************** + // Delegate to underlying service. + // ****************************************************************************************** - @SuppressWarnings("FieldCanBeLocal") - private final Context context; + logger.log(Level.INFO, "Leaving server interceptor..."); + } - @SuppressWarnings("FieldCanBeLocal") - private final GrpcRequest request; + return new FilibusterServerCall<>(call, context, request, headers).start(headers, next); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } - public FilibusterServerCall(ServerCall delegate, Context context, GrpcRequest request, Metadata requestHeaders) { - super(delegate); - this.requestHeaders = requestHeaders; - this.context = context; - this.request = request; + throw e; + } } - FilibusterServerCallListener start(Metadata headers, ServerCallHandler next) { - return new FilibusterServerCallListener( - Contexts.interceptCall(io.grpc.Context.current(), this, headers, next), context, request); - } + final class FilibusterServerCall + extends ForwardingServerCall.SimpleForwardingServerCall { + @SuppressWarnings("FieldCanBeLocal") + final private Metadata requestHeaders; - // ****************************************************************************************** - // Implementation. - // ****************************************************************************************** + @SuppressWarnings("FieldCanBeLocal") + private final Context context; - @Override - public void sendMessage(RESPONSE message) { - try (Scope ignored = context.makeCurrent()) { - super.sendMessage(message); - } - } + @SuppressWarnings("FieldCanBeLocal") + private final GrpcRequest request; - @Override - public void close(Status status, Metadata trailers) { - try { - delegate().close(status, trailers); - } catch (Throwable e) { - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, status, e); + public FilibusterServerCall(ServerCall delegate, Context context, GrpcRequest request, Metadata requestHeaders) { + super(delegate); + this.requestHeaders = requestHeaders; + this.context = context; + this.request = request; } - throw e; - } - - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, status, status.getCause()); - } - } - @Override - public void sendHeaders(Metadata responseHeaders) { - if (!shouldInstrument()) { - try (Scope ignored = context.makeCurrent()) { - super.sendHeaders(responseHeaders); + FilibusterServerCallListener start(Metadata headers, ServerCallHandler next) { + return new FilibusterServerCallListener( + Contexts.interceptCall(io.grpc.Context.current(), this, headers, next), context, request); } - } else { - try (Scope ignored = context.makeCurrent()) { - super.sendHeaders(responseHeaders); - } - } - } - final class FilibusterServerCallListener - extends ForwardingServerCallListener.SimpleForwardingServerCallListener { - private final Context context; - private final GrpcRequest request; - - FilibusterServerCallListener(ServerCall.Listener delegate, Context context, GrpcRequest request) { - super(delegate); - this.context = context; - this.request = request; - } - - @Override - public void onMessage(REQUEST message) { - delegate().onMessage(message); - } - - @Override - public void onHalfClose() { - try { - delegate().onHalfClose(); - } catch (Throwable e) { - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, null, e); - } - throw e; - } - } + // ****************************************************************************************** + // Implementation. + // ****************************************************************************************** - @Override - public void onCancel() { - try { - delegate().onCancel(); - } catch (Throwable e) { - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, null, e); - } - throw e; + @Override + public void sendMessage(RESPONSE message) { + try (Scope ignored = context.makeCurrent()) { + super.sendMessage(message); + } } - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, null, null); + + @Override + public void close(Status status, Metadata trailers) { + try { + delegate().close(status, trailers); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, status, e); + } + throw e; + } + + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, status, status.getCause()); + } } - } - @Override - public void onComplete() { - try { - delegate().onComplete(); - } catch (Throwable e) { - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, null, e); - } - throw e; + @Override + public void sendHeaders(Metadata responseHeaders) { + if (!shouldInstrument()) { + try (Scope ignored = context.makeCurrent()) { + super.sendHeaders(responseHeaders); + } + } else { + try (Scope ignored = context.makeCurrent()) { + super.sendHeaders(responseHeaders); + } + } } - } - @Override - public void onReady() { - try { - delegate().onReady(); - } catch (Throwable e) { - if (serverInstrumentor != null) { - serverInstrumentor.end(context, request, null, e); - } - throw e; + final class FilibusterServerCallListener + extends ForwardingServerCallListener.SimpleForwardingServerCallListener { + private final Context context; + private final GrpcRequest request; + + FilibusterServerCallListener(Listener delegate, Context context, GrpcRequest request) { + super(delegate); + this.context = context; + this.request = request; + } + + @Override + public void onMessage(REQUEST message) { + delegate().onMessage(message); + } + + @Override + public void onHalfClose() { + try { + delegate().onHalfClose(); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } + throw e; + } + } + + @Override + public void onCancel() { + try { + delegate().onCancel(); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } + throw e; + } + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, null); + } + } + + @Override + public void onComplete() { + try { + delegate().onComplete(); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } + throw e; + } + } + + @Override + public void onReady() { + try { + delegate().onReady(); + } catch (Throwable e) { + if (serverInstrumentor != null) { + serverInstrumentor.end(context, request, null, e); + } + throw e; + } + } } - } } - } } From fff168be718dd5efa1df109dfdf820f6ece8bc2c Mon Sep 17 00:00:00 2001 From: Christopher Meiklejohn Date: Thu, 10 Feb 2022 12:32:13 -0500 Subject: [PATCH 10/11] Bump HTTP instrumentation client and server. --- ...lemetryFilibusterDecoratingHttpClient.java | 118 +++++++++--------- ...emetryFilibusterDecoratingHttpService.java | 76 +++++------ 2 files changed, 97 insertions(+), 97 deletions(-) diff --git a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpClient.java b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpClient.java index bbfb8bee4cbe..24c7d5ccec58 100644 --- a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpClient.java +++ b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpClient.java @@ -15,67 +15,67 @@ import java.util.logging.Logger; public class OpenTelemetryFilibusterDecoratingHttpClient extends FilibusterDecoratingHttpClient { - private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterDecoratingHttpClient.class.getName()); - - @Nullable - private final Instrumenter clientInstrumentor; - - @SuppressWarnings("NullAway") - private Context parentContext; - - @SuppressWarnings("NullAway") - private Context context; - - public OpenTelemetryFilibusterDecoratingHttpClient(HttpClient delegate, String serviceName, Instrumenter clientInstrumentor) { - super(delegate); - this.serviceName = serviceName; - this.clientInstrumentor = clientInstrumentor; - this.contextStorage = new OpenTelemetryContextStorage(); - } - - public OpenTelemetryFilibusterDecoratingHttpClient(HttpClient delegate, Instrumenter clientInstrumentor) { - super(delegate); - this.serviceName = System.getenv("SERVICE_NAME"); - this.clientInstrumentor = clientInstrumentor; - this.contextStorage = new OpenTelemetryContextStorage(); - } - - @Override - protected void setupContext(ClientRequestContext ctx, HttpRequest req) { - this.parentContext = Context.current(); - this.context = Context.current(); - - logger.log(Level.INFO, "****************************************************************"); - logger.log(Level.SEVERE, "CLIENT parentContext: " + parentContext.toString()); - logger.log(Level.INFO, "****************************************************************"); - - if (clientInstrumentor != null) { - this.context = clientInstrumentor.start(Context.current(), ctx); + private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterDecoratingHttpClient.class.getName()); + + @Nullable + private final Instrumenter clientInstrumentor; + + @SuppressWarnings("NullAway") + private Context parentContext; + + @SuppressWarnings("NullAway") + private Context context; + + public OpenTelemetryFilibusterDecoratingHttpClient(HttpClient delegate, String serviceName, Instrumenter clientInstrumentor) { + super(delegate); + this.serviceName = serviceName; + this.clientInstrumentor = clientInstrumentor; + this.contextStorage = new OpenTelemetryContextStorage(); + } + + public OpenTelemetryFilibusterDecoratingHttpClient(HttpClient delegate, Instrumenter clientInstrumentor) { + super(delegate); + this.serviceName = System.getenv("SERVICE_NAME"); + this.clientInstrumentor = clientInstrumentor; + this.contextStorage = new OpenTelemetryContextStorage(); + } + + @Override + protected void setupContext(ClientRequestContext ctx, HttpRequest req) { + this.parentContext = Context.current(); + this.context = Context.current(); + + logger.log(Level.INFO, "****************************************************************"); + logger.log(Level.SEVERE, "CLIENT parentContext: " + parentContext.toString()); + logger.log(Level.INFO, "****************************************************************"); + + if (clientInstrumentor != null) { + this.context = clientInstrumentor.start(Context.current(), ctx); + } + + logger.log(Level.INFO, "****************************************************************"); + logger.log(Level.SEVERE, "CLIENT context: " + context.toString()); + logger.log(Level.INFO, "****************************************************************"); } - logger.log(Level.INFO, "****************************************************************"); - logger.log(Level.SEVERE, "CLIENT context: " + context.toString()); - logger.log(Level.INFO, "****************************************************************"); - } - - @Override - protected void contextWhenComplete(ClientRequestContext ctx) { - ctx.log().whenComplete().thenAccept(log -> { - if (clientInstrumentor != null) { - clientInstrumentor.end(context, ctx, log, log.responseCause()); - } - }); - } - - @Override - protected HttpResponse delegateWithContext(ClientRequestContext ctx, HttpRequest req) throws Exception { - HttpResponse response; - - try (Scope ignored = context.makeCurrent()) { - logger.log(Level.INFO, "!!!!!!! with context: " + context.toString()); - response = unwrap().execute(ctx, req); + @Override + protected void contextWhenComplete(ClientRequestContext ctx) { + ctx.log().whenComplete().thenAccept(log -> { + if (clientInstrumentor != null) { + clientInstrumentor.end(context, ctx, log, log.responseCause()); + } + }); } - return response; - } + @Override + protected HttpResponse delegateWithContext(ClientRequestContext ctx, HttpRequest req) throws Exception { + HttpResponse response; + + try (Scope ignored = context.makeCurrent()) { + logger.log(Level.INFO, "!!!!!!! with context: " + context.toString()); + response = unwrap().execute(ctx, req); + } + + return response; + } } diff --git a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpService.java b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpService.java index b5abd80b2a72..d7d152550a78 100644 --- a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpService.java +++ b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryFilibusterDecoratingHttpService.java @@ -14,52 +14,52 @@ import java.util.logging.Logger; public class OpenTelemetryFilibusterDecoratingHttpService extends FilibusterDecoratingHttpService { - private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterDecoratingHttpService.class.getName()); + private static final Logger logger = Logger.getLogger(OpenTelemetryFilibusterDecoratingHttpService.class.getName()); - @Nullable - private final Instrumenter serverInstrumenter; + @Nullable + private final Instrumenter serverInstrumenter; - @SuppressWarnings("NullAway") - private Context context; + @SuppressWarnings("NullAway") + private Context context; - public OpenTelemetryFilibusterDecoratingHttpService(HttpService delegate, String serviceName, Instrumenter serverInstrumenter) { - super(delegate); - this.serviceName = serviceName; - this.serverInstrumenter = serverInstrumenter; - this.contextStorage = new OpenTelemetryContextStorage(); - } + public OpenTelemetryFilibusterDecoratingHttpService(HttpService delegate, String serviceName, Instrumenter serverInstrumenter) { + super(delegate); + this.serviceName = serviceName; + this.serverInstrumenter = serverInstrumenter; + this.contextStorage = new OpenTelemetryContextStorage(); + } - public OpenTelemetryFilibusterDecoratingHttpService(HttpService delegate, Instrumenter serverInstrumenter) { - super(delegate); - this.serviceName = System.getenv("SERVICE_NAME"); - this.serverInstrumenter = serverInstrumenter; - this.contextStorage = new OpenTelemetryContextStorage(); - } + public OpenTelemetryFilibusterDecoratingHttpService(HttpService delegate, Instrumenter serverInstrumenter) { + super(delegate); + this.serviceName = System.getenv("SERVICE_NAME"); + this.serverInstrumenter = serverInstrumenter; + this.contextStorage = new OpenTelemetryContextStorage(); + } - @Override - protected void setupContext(ServiceRequestContext ctx, HttpRequest req) { - OpenTelemetryContextStorage openTelemetryContextStorage = (OpenTelemetryContextStorage) this.contextStorage; - context = openTelemetryContextStorage.getContext(); + @Override + protected void setupContext(ServiceRequestContext ctx, HttpRequest req) { + OpenTelemetryContextStorage openTelemetryContextStorage = (OpenTelemetryContextStorage) this.contextStorage; + context = openTelemetryContextStorage.getContext(); - if (serverInstrumenter != null) { - context = serverInstrumenter.start(context, ctx); + if (serverInstrumenter != null) { + context = serverInstrumenter.start(context, ctx); + } } - } - @Override - protected void contextWhenComplete(ServiceRequestContext ctx) { - ctx.log().whenComplete().thenAccept(log -> { - if (serverInstrumenter != null) { - serverInstrumenter.end(context, ctx, log, log.responseCause()); - } - }); - } + @Override + protected void contextWhenComplete(ServiceRequestContext ctx) { + ctx.log().whenComplete().thenAccept(log -> { + if (serverInstrumenter != null) { + serverInstrumenter.end(context, ctx, log, log.responseCause()); + } + }); + } - @Override - protected HttpResponse delegateWithContext(ServiceRequestContext ctx, HttpRequest req) throws Exception { - try (Scope ignored = context.makeCurrent()) { - HttpService delegate = (HttpService) unwrap(); - return delegate.serve(ctx, req); + @Override + protected HttpResponse delegateWithContext(ServiceRequestContext ctx, HttpRequest req) throws Exception { + try (Scope ignored = context.makeCurrent()) { + HttpService delegate = (HttpService) unwrap(); + return delegate.serve(ctx, req); + } } - } } From d26fcdc510aa3d25dfde28fbec3dc194fcdd8e54 Mon Sep 17 00:00:00 2001 From: Christopher Meiklejohn Date: Thu, 10 Feb 2022 12:35:22 -0500 Subject: [PATCH 11/11] Update context storage libraries. --- .../armeria/v1_3/OpenTelemetryContextStorage.java | 14 +++++++------- .../grpc/v1_6/OpenTelemetryContextStorage.java | 7 +------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryContextStorage.java b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryContextStorage.java index 482e43e1d36c..b32fd7b9c44c 100644 --- a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryContextStorage.java +++ b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/OpenTelemetryContextStorage.java @@ -1,23 +1,23 @@ package io.opentelemetry.instrumentation.armeria.v1_3; -import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.EXECUTION_INDEX_KEY; -import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.ORIGIN_VCLOCK_KEY; -import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.REQUEST_ID_KEY; -import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.VCLOCK_KEY; - import cloud.filibuster.instrumentation.datatypes.VectorClock; import cloud.filibuster.instrumentation.storage.ContextStorage; import io.opentelemetry.context.Context; +import javax.annotation.Nullable; import java.util.logging.Level; import java.util.logging.Logger; -import javax.annotation.Nullable; + +import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.EXECUTION_INDEX_KEY; +import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.ORIGIN_VCLOCK_KEY; +import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.REQUEST_ID_KEY; +import static io.opentelemetry.instrumentation.api.filibuster.OpenTelemetryContextStorageConstants.VCLOCK_KEY; public class OpenTelemetryContextStorage implements ContextStorage { private static final Logger logger = Logger.getLogger(OpenTelemetryContextStorage.class.getName()); private Context context; - // TODO: Context.current() should maybe be cached, who knows? + // Context.current() should maybe be cached, who knows? public OpenTelemetryContextStorage() { this.context = Context.current(); diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryContextStorage.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryContextStorage.java index c3dd15b9836e..41b3ebb6c5c0 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryContextStorage.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/OpenTelemetryContextStorage.java @@ -3,7 +3,6 @@ import cloud.filibuster.instrumentation.datatypes.VectorClock; import cloud.filibuster.instrumentation.storage.ContextStorage; import io.opentelemetry.context.Context; - import javax.annotation.Nullable; import java.util.logging.Level; import java.util.logging.Logger; @@ -18,7 +17,7 @@ public class OpenTelemetryContextStorage implements ContextStorage { private Context context; - // TODO: Context.current() should maybe be cached, who knows? + // Context.current() should maybe be cached, who knows? public OpenTelemetryContextStorage() { this.context = Context.current(); @@ -68,10 +67,6 @@ public String getExecutionIndex() { return Context.current().get(EXECUTION_INDEX_KEY); } - public String getExecutionIndexFromContext(Context context) { - return context.get(EXECUTION_INDEX_KEY); - } - @Override public void setRequestId(String requestId) { this.context = this.context.with(REQUEST_ID_KEY, requestId);