|
18 | 18 | import static com.google.common.truth.Truth.assertThat;
|
19 | 19 |
|
20 | 20 | import com.google.api.gax.core.NoCredentialsProvider;
|
| 21 | +import com.google.api.gax.grpc.GaxGrpcProperties; |
21 | 22 | import com.google.api.gax.rpc.ServerStreamingCallable;
|
22 | 23 | import com.google.bigtable.v2.BigtableGrpc;
|
23 | 24 | import com.google.bigtable.v2.ReadRowsRequest;
|
24 | 25 | import com.google.bigtable.v2.ReadRowsResponse;
|
25 | 26 | import com.google.bigtable.v2.RowSet;
|
| 27 | +import com.google.cloud.bigtable.Version; |
26 | 28 | import com.google.cloud.bigtable.admin.v2.internal.NameUtil;
|
27 | 29 | import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
|
28 | 30 | import com.google.cloud.bigtable.data.v2.FakeServiceHelper;
|
|
39 | 41 | import io.grpc.ServerCall.Listener;
|
40 | 42 | import io.grpc.ServerCallHandler;
|
41 | 43 | import io.grpc.ServerInterceptor;
|
| 44 | +import io.grpc.internal.GrpcUtil; |
42 | 45 | import io.grpc.stub.StreamObserver;
|
| 46 | +import io.opencensus.common.Scope; |
| 47 | +import io.opencensus.trace.AttributeValue; |
| 48 | +import io.opencensus.trace.Tracing; |
| 49 | +import io.opencensus.trace.export.SpanData; |
| 50 | +import io.opencensus.trace.export.SpanExporter.Handler; |
| 51 | +import io.opencensus.trace.samplers.Samplers; |
43 | 52 | import java.io.IOException;
|
| 53 | +import java.util.Collection; |
| 54 | +import java.util.concurrent.ArrayBlockingQueue; |
44 | 55 | import java.util.concurrent.BlockingQueue;
|
45 | 56 | import java.util.concurrent.TimeUnit;
|
46 | 57 | import org.junit.After;
|
@@ -152,6 +163,61 @@ public void testUserAgent() throws InterruptedException {
|
152 | 163 | .containsMatch("bigtable-java/\\d+\\.\\d+\\.\\d+(?:-SNAPSHOT)?");
|
153 | 164 | }
|
154 | 165 |
|
| 166 | + @Test |
| 167 | + public void testSpanAttributes() throws InterruptedException { |
| 168 | + final BlockingQueue<SpanData> spans = new ArrayBlockingQueue<>(100); |
| 169 | + |
| 170 | + // inject a temporary trace exporter |
| 171 | + String handlerName = "stub-test-exporter"; |
| 172 | + |
| 173 | + Tracing.getExportComponent() |
| 174 | + .getSpanExporter() |
| 175 | + .registerHandler( |
| 176 | + handlerName, |
| 177 | + new Handler() { |
| 178 | + @Override |
| 179 | + public void export(Collection<SpanData> collection) { |
| 180 | + spans.addAll(collection); |
| 181 | + } |
| 182 | + }); |
| 183 | + |
| 184 | + SpanData foundSpanData = null; |
| 185 | + // Issue the rpc and grab the span |
| 186 | + try { |
| 187 | + try (Scope ignored = |
| 188 | + Tracing.getTracer() |
| 189 | + .spanBuilder("fake-parent-span") |
| 190 | + .setSampler(Samplers.alwaysSample()) |
| 191 | + .startScopedSpan()) { |
| 192 | + enhancedBigtableStub.readRowCallable().call(Query.create("table-id").rowKey("row-key")); |
| 193 | + } |
| 194 | + |
| 195 | + for (int i = 0; i < 100; i++) { |
| 196 | + SpanData spanData = spans.poll(10, TimeUnit.SECONDS); |
| 197 | + if ("Bigtable.ReadRow".equals(spanData.getName())) { |
| 198 | + foundSpanData = spanData; |
| 199 | + break; |
| 200 | + } |
| 201 | + } |
| 202 | + } finally { |
| 203 | + // cleanup |
| 204 | + Tracing.getExportComponent().getSpanExporter().unregisterHandler(handlerName); |
| 205 | + } |
| 206 | + |
| 207 | + // Examine the caught span |
| 208 | + assertThat(foundSpanData).isNotNull(); |
| 209 | + assertThat(foundSpanData.getAttributes().getAttributeMap()) |
| 210 | + .containsEntry("gapic", AttributeValue.stringAttributeValue(Version.VERSION)); |
| 211 | + assertThat(foundSpanData.getAttributes().getAttributeMap()) |
| 212 | + .containsEntry( |
| 213 | + "grpc", |
| 214 | + AttributeValue.stringAttributeValue( |
| 215 | + GrpcUtil.getGrpcBuildVersion().getImplementationVersion())); |
| 216 | + assertThat(foundSpanData.getAttributes().getAttributeMap()) |
| 217 | + .containsEntry( |
| 218 | + "gax", AttributeValue.stringAttributeValue(GaxGrpcProperties.getGaxGrpcVersion())); |
| 219 | + } |
| 220 | + |
155 | 221 | private static class MetadataInterceptor implements ServerInterceptor {
|
156 | 222 | final BlockingQueue<Metadata> headers = Queues.newLinkedBlockingDeque();
|
157 | 223 |
|
|
0 commit comments