|
| 1 | +package com.google.cloud.spanner.it; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import com.google.api.gax.core.FixedCredentialsProvider; |
| 7 | +import com.google.api.gax.rpc.ApiException; |
| 8 | +import com.google.api.gax.rpc.ResourceExhaustedException; |
| 9 | +import com.google.api.gax.rpc.StatusCode; |
| 10 | +import com.google.cloud.spanner.Database; |
| 11 | +import com.google.cloud.spanner.DatabaseClient; |
| 12 | +import com.google.cloud.spanner.IntegrationTestEnv; |
| 13 | +import com.google.cloud.spanner.ParallelIntegrationTest; |
| 14 | +import com.google.cloud.spanner.ResultSet; |
| 15 | +import com.google.cloud.spanner.SpannerOptions; |
| 16 | +import com.google.cloud.spanner.SpannerOptionsHelper; |
| 17 | +import com.google.cloud.spanner.Statement; |
| 18 | +import com.google.cloud.spanner.Struct; |
| 19 | +import com.google.cloud.spanner.Type; |
| 20 | +import com.google.cloud.spanner.Type.StructField; |
| 21 | +import com.google.cloud.spanner.connection.ConnectionOptions; |
| 22 | +import com.google.cloud.trace.v1.TraceServiceClient; |
| 23 | +import com.google.cloud.trace.v1.TraceServiceSettings; |
| 24 | +import com.google.devtools.cloudtrace.v1.Trace; |
| 25 | +import io.opentelemetry.api.GlobalOpenTelemetry; |
| 26 | +import io.opentelemetry.api.trace.Span; |
| 27 | +import io.opentelemetry.api.trace.Tracer; |
| 28 | +import io.opentelemetry.context.Scope; |
| 29 | +import java.io.IOException; |
| 30 | +import org.junit.AfterClass; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.BeforeClass; |
| 33 | +import org.junit.ClassRule; |
| 34 | +import org.junit.Test; |
| 35 | +import org.junit.experimental.categories.Category; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | +import org.junit.runners.JUnit4; |
| 38 | + |
| 39 | +/** |
| 40 | + * Integration tests for End to End Tracing. |
| 41 | + */ |
| 42 | +@Category(ParallelIntegrationTest.class) |
| 43 | +@RunWith(JUnit4.class) |
| 44 | +public class ITEndToEndTracingTest { |
| 45 | + |
| 46 | + @ClassRule |
| 47 | + public static IntegrationTestEnv env = new IntegrationTestEnv(); |
| 48 | + private static DatabaseClient googleStandardSQLClient; |
| 49 | + private String selectValueQuery; |
| 50 | + |
| 51 | + static { |
| 52 | + SpannerOptionsHelper.resetActiveTracingFramework(); |
| 53 | + SpannerOptions.enableOpenTelemetryMetrics(); |
| 54 | + SpannerOptions.enableOpenTelemetryTraces(); |
| 55 | + } |
| 56 | + |
| 57 | + @BeforeClass |
| 58 | + public static void setUp() { |
| 59 | + setUpDatabase(); |
| 60 | + } |
| 61 | + |
| 62 | + public static void setUpDatabase() { |
| 63 | + // Empty database. |
| 64 | + Database googleStandardSQLDatabase = env.getTestHelper().createTestDatabase(); |
| 65 | + googleStandardSQLClient = env.getTestHelper().getDatabaseClient(googleStandardSQLDatabase); |
| 66 | + } |
| 67 | + |
| 68 | + @Before |
| 69 | + public void initSelectValueQuery() { |
| 70 | + selectValueQuery = "SELECT @p1 + @p1 "; |
| 71 | + } |
| 72 | + |
| 73 | + @AfterClass |
| 74 | + public static void teardown() { |
| 75 | + ConnectionOptions.closeSpanner(); |
| 76 | + } |
| 77 | + |
| 78 | + private void assertTrace(String spanName, String traceid) |
| 79 | + throws IOException, InterruptedException { |
| 80 | + TraceServiceSettings settings = |
| 81 | + env.getTestHelper().getOptions().getCredentials() == null |
| 82 | + ? TraceServiceSettings.newBuilder().build() |
| 83 | + : TraceServiceSettings.newBuilder() |
| 84 | + .setCredentialsProvider( |
| 85 | + FixedCredentialsProvider.create( |
| 86 | + env.getTestHelper().getOptions().getCredentials())) |
| 87 | + .build(); |
| 88 | + try (TraceServiceClient client = TraceServiceClient.create(settings)) { |
| 89 | + // It can take a few seconds before the trace is visible. |
| 90 | + Thread.sleep(5000L); |
| 91 | + boolean foundTrace = false; |
| 92 | + for (int attempts = 0; attempts < 2; attempts++) { |
| 93 | + try { |
| 94 | + Trace clientTrace = client.getTrace(env.getTestHelper().getInstanceId().getProject(), |
| 95 | + traceid); |
| 96 | + // Assert Spanner Frontend Trace is present |
| 97 | + assertTrue(clientTrace.getSpansList().stream().anyMatch( |
| 98 | + span -> "CloudSpannerOperation.ExecuteStreamingQuery".equals(span.getName()))); |
| 99 | + foundTrace = true; |
| 100 | + break; |
| 101 | + } catch (ApiException apiException) { |
| 102 | + assertThat(apiException.getStatusCode().getCode()).isEqualTo(StatusCode.Code.NOT_FOUND); |
| 103 | + Thread.sleep(5000L); |
| 104 | + } |
| 105 | + } |
| 106 | + assertTrue(foundTrace); |
| 107 | + } catch (ResourceExhaustedException resourceExhaustedException) { |
| 108 | + if (resourceExhaustedException |
| 109 | + .getMessage() |
| 110 | + .contains("Quota exceeded for quota metric 'Read requests (free)'")) { |
| 111 | + // Ignore and allow the test to succeed. |
| 112 | + System.out.println("RESOURCE_EXHAUSTED error ignored"); |
| 113 | + } else { |
| 114 | + throw resourceExhaustedException; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private Struct executeWithRowResultType(Statement statement, Type expectedRowType) { |
| 120 | + ResultSet resultSet = |
| 121 | + statement.executeQuery(googleStandardSQLClient.singleUse()); |
| 122 | + assertThat(resultSet.next()).isTrue(); |
| 123 | + assertThat(resultSet.getType()).isEqualTo(expectedRowType); |
| 124 | + Struct row = resultSet.getCurrentRowAsStruct(); |
| 125 | + assertThat(resultSet.next()).isFalse(); |
| 126 | + return row; |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void simpleSelect() throws IOException, InterruptedException { |
| 131 | + Tracer tracer = GlobalOpenTelemetry.getTracer(ITEndToEndTracingTest.class.getName()); |
| 132 | + String spanName = "simpleSelect"; |
| 133 | + Span span = tracer.spanBuilder(spanName).startSpan(); |
| 134 | + Scope scope = span.makeCurrent(); |
| 135 | + Type rowType = Type.struct(StructField.of("", Type.int64())); |
| 136 | + Struct row = executeWithRowResultType( |
| 137 | + Statement.newBuilder(selectValueQuery).bind("p1").to(1234).build(), rowType); |
| 138 | + assertThat(row.isNull(0)).isFalse(); |
| 139 | + assertThat(row.getLong(0)).isEqualTo(2468); |
| 140 | + scope.close(); |
| 141 | + span.end(); |
| 142 | + assertTrace(spanName, span.getSpanContext().getTraceId()); |
| 143 | + } |
| 144 | +} |
0 commit comments