Skip to content

Commit

Permalink
e2e_tests: send back trace_id in response (GoogleCloudPlatform#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
aabmass authored Jul 12, 2021
1 parent 3a73c52 commit 2f2825c
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 10 deletions.
26 changes: 26 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Gradle
.gradle/
out/
.project
.settings
.classpath

# IDE(A) files
.idea/
.settings/
.vscode/
.classpath
.project

# Build artifacts
**/build/
**/bin/

# Mock server download
mock_server*

# Docker files
*Dockerfile

# Git repo contents
.git/
4 changes: 2 additions & 2 deletions cloudbuild-e2e-gce.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ steps:

logsBucket: gs://opentelemetry-ops-e2e-cloud-build-logs
substitutions:
_TEST_RUNNER_IMAGE: gcr.io/opentelemetry-ops-e2e/opentelemetry-operations-e2e-testing:0.5.23
_TEST_SERVER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-java-e2e-test-server:${SHORT_SHA}
_TEST_RUNNER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-e2e-testing:0.9.0
_TEST_SERVER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-java-e2e-test-server:${SHORT_SHA}
4 changes: 2 additions & 2 deletions cloudbuild-e2e-gke.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ steps:

logsBucket: gs://opentelemetry-ops-e2e-cloud-build-logs
substitutions:
_TEST_RUNNER_IMAGE: gcr.io/opentelemetry-ops-e2e/opentelemetry-operations-e2e-testing:0.5.23
_TEST_SERVER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-java-e2e-test-server:${SHORT_SHA}
_TEST_RUNNER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-e2e-testing:0.9.0
_TEST_SERVER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-java-e2e-test-server:${SHORT_SHA}
4 changes: 2 additions & 2 deletions cloudbuild-e2e-local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ steps:

logsBucket: gs://opentelemetry-ops-e2e-cloud-build-logs
substitutions:
_TEST_RUNNER_IMAGE: gcr.io/opentelemetry-ops-e2e/opentelemetry-operations-e2e-testing:0.5.23
_TEST_SERVER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-java-e2e-test-server:${SHORT_SHA}
_TEST_RUNNER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-e2e-testing:0.9.0
_TEST_SERVER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-java-e2e-test-server:${SHORT_SHA}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Constants {
public static String SCENARIO = "scenario";
public static String STATUS_CODE = "status_code";
public static String TEST_ID = "test_id";
public static String TRACE_ID = "trace_id";

// TODO: Add good error messages below.
public static String SUBCRIPTION_MODE = System.getenv().getOrDefault("SUBSCRIPTION_MODE", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import com.google.api.gax.rpc.StatusCode.Code;
import com.google.protobuf.ByteString;
import java.util.HashMap;
import java.util.Map;

/**
* An "RPC Response", generified.
Expand All @@ -30,10 +32,17 @@ public interface Response {
* <p>If this is `OK`, then data will be empty.
*/
Code statusCode();

/** string explanation of error codes. */
ByteString data();

Map<String, String> headers();

static Response make(final Code code, final ByteString data) {
return make(code, data, new HashMap<>());
}

static Response make(final Code code, final ByteString data, final Map<String, String> headers) {
return new Response() {
public Code statusCode() {
return code;
Expand All @@ -42,6 +51,10 @@ public Code statusCode() {
public ByteString data() {
return data;
}

public Map<String, String> headers() {
return headers;
}
};
}

Expand All @@ -61,5 +74,9 @@ public static Response ok() {
return make(Code.OK, ByteString.EMPTY);
}

public static Response ok(Map<String, String> headers) {
return make(Code.OK, ByteString.EMPTY, headers);
}

public static Response EMPTY = make(Code.UNKNOWN, ByteString.EMPTY);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private Response basicTrace(Request request) {
.setAttribute(Constants.TEST_ID, request.testId())
.startSpan();
try {
return Response.ok();
return Response.ok(Map.of(Constants.TRACE_ID, span.getSpanContext().getTraceId()));
} finally {
span.end();
}
Expand All @@ -65,7 +65,7 @@ private Response basicTrace(Request request) {

/** Default test scenario runner for unknown test cases. */
private Response unimplemented(Request request) {
return Response.unimplemented("UNhandled request: " + request.testId());
return Response.unimplemented("Unhandled request: " + request.testId());
}

/** Registers test scenario "urls" with handlers for the requests. */
Expand All @@ -92,7 +92,8 @@ private static <R> R withTemporaryTracer(Function<Tracer, R> handler) {
sdk.getSdkTracerProvider().shutdown();
}
} catch (IOException e) {
// Lift checked exception into runtime to feed through lambda impls and make it out to
// Lift checked exception into runtime to feed through lambda impls and make it
// out to
// test status.
throw new RuntimeException(e);
}
Expand All @@ -102,7 +103,10 @@ private static <R> R withTemporaryTracer(Function<Tracer, R> handler) {
private static OpenTelemetrySdk setupTraceExporter() throws IOException {
// Using default project ID and Credentials
TraceConfiguration configuration =
TraceConfiguration.builder().setDeadline(Duration.ofMillis(30000)).build();
TraceConfiguration.builder()
.setDeadline(Duration.ofMillis(30000))
.setProjectId(Constants.PROJECT_ID != "" ? Constants.PROJECT_ID : null)
.build();

TraceExporter traceExporter = TraceExporter.createWithConfiguration(configuration);
// Register the TraceExporter with OpenTelemetry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void close() {
public void respond(final String testId, final Response response) {
final PubsubMessage message =
PubsubMessage.newBuilder()
.putAllAttributes(response.headers())
.putAttributes(Constants.TEST_ID, testId)
.putAttributes(Constants.STATUS_CODE, Integer.toString(response.statusCode().ordinal()))
.setData(response.data())
Expand Down Expand Up @@ -122,6 +123,7 @@ public void handleMessage(PubsubMessage message, AckReplyConsumer consumer) {
try {
response = scenarioHandlers.handleScenario(scenario, request);
} catch (Throwable e) {
e.printStackTrace(System.err);
response = Response.internalError(e);
} finally {
respond(testId, response);
Expand Down

0 comments on commit 2f2825c

Please sign in to comment.