-
Notifications
You must be signed in to change notification settings - Fork 141
feat: Integration test for End to End tracing #3691
Changes from 11 commits
6d54c9d
a0806ee
00a1735
a45ca7e
680ce7f
11baac3
8f12e88
8bc559d
9f81dbb
19eee1c
646a6b9
d752823
74b901d
9c1cd08
8ec9109
eb25e67
0611d07
66856a8
9f18bf7
e7f3109
7e42c8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -455,6 +455,24 @@ | |
| <artifactId>opentelemetry-sdk-testing</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.cloud.opentelemetry</groupId> | ||
| <artifactId>exporter-trace</artifactId> | ||
| <version>0.33.0</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-trace</artifactId> | ||
| <version>2.51.0</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.api.grpc</groupId> | ||
| <artifactId>proto-google-cloud-trace-v1</artifactId> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we adding this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We query trace using trace client which requires this dependency. |
||
| <version>2.51.0</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| <profiles> | ||
| <profile> | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -22,14 +22,28 @@ | |||||
| import com.google.api.client.util.ExponentialBackOff; | ||||||
| import com.google.api.gax.longrunning.OperationFuture; | ||||||
| import com.google.cloud.Timestamp; | ||||||
| import com.google.cloud.opentelemetry.trace.TraceConfiguration; | ||||||
| import com.google.cloud.opentelemetry.trace.TraceExporter; | ||||||
| import com.google.cloud.spanner.DatabaseInfo.DatabaseField; | ||||||
| import com.google.cloud.spanner.testing.EmulatorSpannerHelper; | ||||||
| import com.google.cloud.spanner.testing.RemoteSpannerHelper; | ||||||
| import com.google.common.collect.Iterators; | ||||||
| import com.google.spanner.admin.instance.v1.CreateInstanceMetadata; | ||||||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||||||
| import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||||||
| import io.opentelemetry.context.propagation.ContextPropagators; | ||||||
| import io.opentelemetry.sdk.OpenTelemetrySdk; | ||||||
| import io.opentelemetry.sdk.resources.Resource; | ||||||
| import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||||||
| import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; | ||||||
| import io.opentelemetry.sdk.trace.export.SpanExporter; | ||||||
| import io.opentelemetry.sdk.trace.samplers.Sampler; | ||||||
| import java.util.Collection; | ||||||
| import java.util.Collections; | ||||||
| import java.util.Objects; | ||||||
| import java.util.Random; | ||||||
| import java.util.concurrent.ExecutionException; | ||||||
| import java.util.concurrent.ThreadLocalRandom; | ||||||
| import java.util.concurrent.TimeUnit; | ||||||
| import java.util.logging.Level; | ||||||
| import java.util.logging.Logger; | ||||||
|
|
@@ -67,10 +81,22 @@ public class IntegrationTestEnv extends ExternalResource { | |||||
| private final boolean alwaysCreateNewInstance; | ||||||
| private RemoteSpannerHelper testHelper; | ||||||
|
|
||||||
| private Collection<TestEnvOptions> testEnvOptions = Collections.emptyList(); | ||||||
|
|
||||||
| public enum TestEnvOptions { | ||||||
| USE_END_TO_END_TRACING; | ||||||
| // TODO : Move alwaysCreateNewInstance to TestEnvOptions | ||||||
| } | ||||||
|
|
||||||
| public IntegrationTestEnv() { | ||||||
| this(false); | ||||||
| } | ||||||
|
|
||||||
| public IntegrationTestEnv(Collection<TestEnvOptions> testEnvOptions) { | ||||||
| this(false); | ||||||
| this.testEnvOptions = testEnvOptions; | ||||||
| } | ||||||
|
|
||||||
| public IntegrationTestEnv(final boolean alwaysCreateNewInstance) { | ||||||
| this.alwaysCreateNewInstance = alwaysCreateNewInstance; | ||||||
| } | ||||||
|
|
@@ -107,8 +133,15 @@ protected void before() throws Throwable { | |||||
| assumeFalse(alwaysCreateNewInstance && isCloudDevel()); | ||||||
|
|
||||||
| this.config.setUp(); | ||||||
|
|
||||||
| SpannerOptions options = config.spannerOptions(); | ||||||
| if (testEnvOptions.stream() | ||||||
| .anyMatch(testEnvOption -> TestEnvOptions.USE_END_TO_END_TRACING.equals(testEnvOption))) { | ||||||
| // OpenTelemetry set up for enabling End to End tracing for all integration test env. | ||||||
| // The gRPC stub and connections are created during test env set up using SpannerOptions and | ||||||
| // are | ||||||
| // reused for executing statements. | ||||||
| options = spannerOptionsWithEndToEndTracing(options); | ||||||
|
surbhigarg92 marked this conversation as resolved.
|
||||||
| } | ||||||
| String instanceProperty = System.getProperty(TEST_INSTANCE_PROPERTY, ""); | ||||||
| InstanceId instanceId; | ||||||
| if (!instanceProperty.isEmpty() && !alwaysCreateNewInstance) { | ||||||
|
|
@@ -133,6 +166,40 @@ protected void before() throws Throwable { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| public SpannerOptions spannerOptionsWithEndToEndTracing(SpannerOptions options) { | ||||||
| GlobalOpenTelemetry.resetForTest(); // reset global context for test | ||||||
| assumeFalse("This test requires credentials", EmulatorSpannerHelper.isUsingEmulator()); | ||||||
|
|
||||||
| TraceConfiguration.Builder traceConfigurationBuilder = TraceConfiguration.builder(); | ||||||
| if (options.getCredentials() != null) { | ||||||
| traceConfigurationBuilder.setCredentials(options.getCredentials()); | ||||||
| } | ||||||
| SpanExporter traceExporter = | ||||||
| TraceExporter.createWithConfiguration( | ||||||
| traceConfigurationBuilder.setProjectId(options.getProjectId()).build()); | ||||||
|
|
||||||
| String serviceName = | ||||||
| "java-spanner-jdbc-integration-tests-" + ThreadLocalRandom.current().nextInt(); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||
| SdkTracerProvider sdkTracerProvider = | ||||||
| SdkTracerProvider.builder() | ||||||
| // Always sample in this test to ensure we know what we get. | ||||||
| .setSampler(Sampler.alwaysOn()) | ||||||
| .setResource(Resource.builder().put("service.name", serviceName).build()) | ||||||
| .addSpanProcessor(BatchSpanProcessor.builder(traceExporter).build()) | ||||||
| .build(); | ||||||
| OpenTelemetrySdk openTelemetry = | ||||||
| OpenTelemetrySdk.builder() | ||||||
| .setTracerProvider(sdkTracerProvider) | ||||||
| .setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())) | ||||||
| .buildAndRegisterGlobal(); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not register the tracerProvider at global, instead inject OpenTelemerty object via spanner options. If we register it globally, we might start seeing traces coming from other parallel integration tests as
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated. |
||||||
| SpannerOptions.enableOpenTelemetryTraces(); | ||||||
| return options | ||||||
| .toBuilder() | ||||||
| .setOpenTelemetry(openTelemetry) | ||||||
| .setEnableEndToEndTracing(true) | ||||||
| .build(); | ||||||
| } | ||||||
|
|
||||||
| RemoteSpannerHelper createTestHelper(SpannerOptions options, InstanceId instanceId) | ||||||
| throws Throwable { | ||||||
| return RemoteSpannerHelper.create(options, instanceId); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.spanner.it; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assume.assumeTrue; | ||
|
|
||
| import com.google.api.gax.core.FixedCredentialsProvider; | ||
| import com.google.api.gax.rpc.ApiException; | ||
| import com.google.api.gax.rpc.ResourceExhaustedException; | ||
| import com.google.api.gax.rpc.StatusCode; | ||
| import com.google.cloud.spanner.Database; | ||
| import com.google.cloud.spanner.DatabaseClient; | ||
| import com.google.cloud.spanner.IntegrationTestEnv; | ||
| import com.google.cloud.spanner.IntegrationTestEnv.TestEnvOptions; | ||
| import com.google.cloud.spanner.ParallelIntegrationTest; | ||
| import com.google.cloud.spanner.ResultSet; | ||
| import com.google.cloud.spanner.SpannerOptions; | ||
| import com.google.cloud.spanner.SpannerOptionsHelper; | ||
| import com.google.cloud.spanner.Statement; | ||
| import com.google.cloud.spanner.Struct; | ||
| import com.google.cloud.spanner.Type; | ||
| import com.google.cloud.spanner.Type.StructField; | ||
| import com.google.cloud.spanner.connection.ConnectionOptions; | ||
| import com.google.cloud.trace.v1.TraceServiceClient; | ||
| import com.google.cloud.trace.v1.TraceServiceSettings; | ||
| import com.google.devtools.cloudtrace.v1.Trace; | ||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import io.opentelemetry.context.Scope; | ||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import org.junit.AfterClass; | ||
| import org.junit.Before; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.ClassRule; | ||
| import org.junit.Test; | ||
| import org.junit.experimental.categories.Category; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| /** Integration tests for End to End Tracing. */ | ||
| @Category(ParallelIntegrationTest.class) | ||
| @RunWith(JUnit4.class) | ||
| public class ITEndToEndTracingTest { | ||
| public static Collection<TestEnvOptions> testEnvOptions = | ||
| Arrays.asList(TestEnvOptions.USE_END_TO_END_TRACING); | ||
| @ClassRule public static IntegrationTestEnv env = new IntegrationTestEnv(testEnvOptions); | ||
| private static DatabaseClient googleStandardSQLClient; | ||
|
|
||
| static { | ||
| SpannerOptionsHelper.resetActiveTracingFramework(); | ||
| SpannerOptions.enableOpenTelemetryMetrics(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need to enable metrics?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed. |
||
| SpannerOptions.enableOpenTelemetryTraces(); | ||
| } | ||
|
|
||
| private String selectValueQuery; | ||
|
|
||
| @BeforeClass | ||
| public static void setUp() { | ||
| setUpDatabase(); | ||
| } | ||
|
|
||
| public static void setUpDatabase() { | ||
| // Empty database. | ||
| Database googleStandardSQLDatabase = env.getTestHelper().createTestDatabase(); | ||
| googleStandardSQLClient = env.getTestHelper().getDatabaseClient(googleStandardSQLDatabase); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void teardown() { | ||
| ConnectionOptions.closeSpanner(); | ||
| } | ||
|
|
||
| @Before | ||
| public void initSelectValueQuery() { | ||
| selectValueQuery = "SELECT @p1 + @p1 "; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you initialize
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
| } | ||
|
|
||
| private void assertTrace(String traceId) throws IOException, InterruptedException { | ||
| TraceServiceSettings settings = | ||
| env.getTestHelper().getOptions().getCredentials() == null | ||
| ? TraceServiceSettings.newBuilder().build() | ||
| : TraceServiceSettings.newBuilder() | ||
| .setCredentialsProvider( | ||
| FixedCredentialsProvider.create( | ||
| env.getTestHelper().getOptions().getCredentials())) | ||
| .build(); | ||
| try (TraceServiceClient client = TraceServiceClient.create(settings)) { | ||
| // It can take a few seconds before the trace is visible. | ||
| Thread.sleep(5000L); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any better way to know that trace is exported? I just wanted to make sure this is not flakky
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do a forceflush(), instead of adding a sleep.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to wait for sever side traces which are collected from request on Span FE and asynchronously exported later. |
||
| boolean foundTrace = false; | ||
| for (int attempts = 0; attempts < 2; attempts++) { | ||
| try { | ||
| Trace clientTrace = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create the traceClient in "before" block
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TraceClient is created before the block. This is actually the trace, not the client. Updated naming in recent commit to avoid confusion. |
||
| client.getTrace(env.getTestHelper().getInstanceId().getProject(), traceId); | ||
| // Assert Spanner Frontend Trace is present | ||
| assertTrue( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assertTrue will result into error if the trace is not found and the test will be marked as failed, so the for loop will never actually work. Instead of this , we could try something like below and also remove the 10 second sleep before the loop.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. If the getTrace calls fails, it throws an APIException. Have kept the catch block to handle it but changed the logic to poll upto 30 sec and sleep for 5 sec if not found. |
||
| clientTrace.getSpansList().stream() | ||
| .anyMatch( | ||
| span -> | ||
| "CloudSpannerOperation.ExecuteStreamingQuery".equals(span.getName()))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are only asserting on Client Trace and not on server trace?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated. |
||
| foundTrace = true; | ||
| break; | ||
| } catch (ApiException apiException) { | ||
| assumeTrue( | ||
| apiException.getStatusCode() != null | ||
| && StatusCode.Code.NOT_FOUND.equals(apiException.getStatusCode().getCode())); | ||
| Thread.sleep(5000L); | ||
| } | ||
| } | ||
| assertTrue(foundTrace); | ||
| } catch (ResourceExhaustedException resourceExhaustedException) { | ||
| if (resourceExhaustedException | ||
| .getMessage() | ||
| .contains("Quota exceeded for quota metric 'Read requests (free)'")) { | ||
| // Ignore and allow the test to succeed. | ||
| System.out.println("RESOURCE_EXHAUSTED error ignored"); | ||
| } else { | ||
| throw resourceExhaustedException; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private Struct executeWithRowResultType(Statement statement, Type expectedRowType) { | ||
| ResultSet resultSet = statement.executeQuery(googleStandardSQLClient.singleUse()); | ||
| assertThat(resultSet.next()).isTrue(); | ||
| assertThat(resultSet.getType()).isEqualTo(expectedRowType); | ||
| Struct row = resultSet.getCurrentRowAsStruct(); | ||
| assertThat(resultSet.next()).isFalse(); | ||
| return row; | ||
| } | ||
|
|
||
| @Test | ||
| public void simpleSelect() throws IOException, InterruptedException { | ||
| Tracer tracer = GlobalOpenTelemetry.getTracer(ITEndToEndTracingTest.class.getName()); | ||
| Span span = tracer.spanBuilder("simpleSelect").startSpan(); | ||
| Scope scope = span.makeCurrent(); | ||
| Type rowType = Type.struct(StructField.of("", Type.int64())); | ||
| Struct row = | ||
| executeWithRowResultType( | ||
| Statement.newBuilder(selectValueQuery).bind("p1").to(1234).build(), rowType); | ||
| assertThat(row.isNull(0)).isFalse(); | ||
| assertThat(row.getLong(0)).isEqualTo(2468); | ||
| scope.close(); | ||
| span.end(); | ||
| assertTrace(span.getSpanContext().getTraceId()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the version from here. It should either fetch from bom or the latest version