-
Notifications
You must be signed in to change notification settings - Fork 130
feat: Integration test for End to End tracing #3691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 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> | ||
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? 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 |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* 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.common.base.Stopwatch; | ||
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 java.util.concurrent.TimeUnit; | ||
import org.junit.AfterClass; | ||
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.enableOpenTelemetryTraces(); | ||
} | ||
|
||
private static String selectValueQuery = "SELECT @p1 + @p1"; | ||
|
||
@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(); | ||
} | ||
|
||
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)) { | ||
boolean foundTrace = false; | ||
Stopwatch metricsPollingStopwatch = Stopwatch.createStarted(); | ||
while (!foundTrace && metricsPollingStopwatch.elapsed(TimeUnit.SECONDS) < 30) { | ||
// Try every 5 seconds | ||
Thread.sleep(5000); | ||
try { | ||
foundTrace = | ||
client.getTrace(env.getTestHelper().getInstanceId().getProject(), traceId) | ||
.getSpansList().stream() | ||
.anyMatch(span -> "Spanner.ExecuteStreamingSql".equals(span.getName())); | ||
} catch (ApiException apiException) { | ||
assumeTrue( | ||
apiException.getStatusCode() != null | ||
&& StatusCode.Code.NOT_FOUND.equals(apiException.getStatusCode().getCode())); | ||
System.out.println("Trace NOT_FOUND error ignored"); | ||
} | ||
} | ||
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 = | ||
env.getTestHelper() | ||
.getOptions() | ||
.getOpenTelemetry() | ||
.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