forked from googleapis/java-spanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathITEndToEndTracingTest.java
158 lines (146 loc) · 6.05 KB
/
ITEndToEndTracingTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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());
}
}