1919import com .google .api .gax .batching .Batcher ;
2020import com .google .api .gax .batching .BatcherImpl ;
2121import com .google .api .gax .core .BackgroundResource ;
22+ import com .google .api .gax .core .GaxProperties ;
23+ import com .google .api .gax .grpc .GaxGrpcProperties ;
2224import com .google .api .gax .grpc .GrpcCallSettings ;
2325import com .google .api .gax .grpc .GrpcRawCallableFactory ;
2426import com .google .api .gax .retrying .ExponentialRetryAlgorithm ;
3133import com .google .api .gax .rpc .ServerStreamingCallSettings ;
3234import com .google .api .gax .rpc .ServerStreamingCallable ;
3335import com .google .api .gax .rpc .UnaryCallable ;
36+ import com .google .api .gax .tracing .ApiTracerFactory ;
37+ import com .google .api .gax .tracing .OpencensusTracerFactory ;
3438import com .google .api .gax .tracing .SpanName ;
3539import com .google .api .gax .tracing .TracedServerStreamingCallable ;
3640import com .google .api .gax .tracing .TracedUnaryCallable ;
5862import com .google .cloud .bigtable .data .v2 .models .RowAdapter ;
5963import com .google .cloud .bigtable .data .v2 .models .RowMutation ;
6064import com .google .cloud .bigtable .data .v2 .models .RowMutationEntry ;
61- import com .google .cloud .bigtable .data .v2 .stub .metrics .MeasuredMutateRowsCallable ;
62- import com .google .cloud .bigtable .data .v2 .stub .metrics .MeasuredReadRowsCallable ;
63- import com .google .cloud .bigtable .data .v2 .stub .metrics .MeasuredUnaryCallable ;
65+ import com .google .cloud .bigtable .data .v2 .stub .metrics .CompositeTracerFactory ;
66+ import com .google .cloud .bigtable .data .v2 .stub .metrics .MetricsTracerFactory ;
67+ import com .google .cloud .bigtable .data .v2 .stub .metrics .RpcMeasureConstants ;
6468import com .google .cloud .bigtable .data .v2 .stub .mutaterows .BulkMutateRowsUserFacingCallable ;
6569import com .google .cloud .bigtable .data .v2 .stub .mutaterows .MutateRowsBatchingDescriptor ;
6670import com .google .cloud .bigtable .data .v2 .stub .mutaterows .MutateRowsRetryingCallable ;
7276import com .google .cloud .bigtable .data .v2 .stub .readrows .RowMergingCallable ;
7377import com .google .cloud .bigtable .gaxx .retrying .ApiResultRetryAlgorithm ;
7478import com .google .common .base .Preconditions ;
79+ import com .google .common .collect .ImmutableList ;
7580import com .google .common .collect .ImmutableMap ;
7681import com .google .protobuf .ByteString ;
7782import io .opencensus .stats .Stats ;
7883import io .opencensus .stats .StatsRecorder ;
84+ import io .opencensus .tags .TagKey ;
85+ import io .opencensus .tags .TagValue ;
7986import io .opencensus .tags .Tagger ;
8087import io .opencensus .tags .Tags ;
8188import java .io .IOException ;
@@ -103,10 +110,6 @@ public class EnhancedBigtableStub implements AutoCloseable {
103110 private final ClientContext clientContext ;
104111 private final RequestContext requestContext ;
105112
106- // TODO: This should probably move to ClientContext
107- private final Tagger tagger ;
108- private final StatsRecorder statsRecorder ;
109-
110113 private final ServerStreamingCallable <Query , Row > readRowsCallable ;
111114 private final UnaryCallable <Query , Row > readRowCallable ;
112115 private final UnaryCallable <String , List <KeyOffset >> sampleRowKeysCallable ;
@@ -117,22 +120,57 @@ public class EnhancedBigtableStub implements AutoCloseable {
117120
118121 public static EnhancedBigtableStub create (EnhancedBigtableStubSettings settings )
119122 throws IOException {
120- ClientContext clientContext = ClientContext .create (settings );
121123
122124 return new EnhancedBigtableStub (
123- settings , clientContext , Tags .getTagger (), Stats .getStatsRecorder ());
125+ settings , ClientContext . create ( settings ) , Tags .getTagger (), Stats .getStatsRecorder ());
124126 }
125127
126128 @ InternalApi ("Visible for testing" )
127- private EnhancedBigtableStub (
129+ public EnhancedBigtableStub (
128130 EnhancedBigtableStubSettings settings ,
129131 ClientContext clientContext ,
130132 Tagger tagger ,
131133 StatsRecorder statsRecorder ) {
132134 this .settings = settings ;
133- this .clientContext = clientContext ;
134- this .tagger = tagger ;
135- this .statsRecorder = statsRecorder ;
135+ this .clientContext =
136+ clientContext
137+ .toBuilder ()
138+ .setTracerFactory (
139+ new CompositeTracerFactory (
140+ ImmutableList .<ApiTracerFactory >of (
141+ // Ad OpenCensus Tracing
142+ new OpencensusTracerFactory (
143+ ImmutableMap .<String , String >builder ()
144+ // TODO: use constants to make sure that these align with OpenCensus
145+ // Metric tags
146+ .put ("bigtable_project_id" , settings .getProjectId ())
147+ .put ("bigtable_instance_id" , settings .getInstanceId ())
148+ .put ("bigtable_app_profile_id" , settings .getAppProfileId ())
149+ .put ("gax" , GaxGrpcProperties .getGaxGrpcVersion ())
150+ .put ("grpc" , GaxGrpcProperties .getGrpcVersion ())
151+ .put (
152+ "gapic" ,
153+ GaxProperties .getLibraryVersion (
154+ EnhancedBigtableStubSettings .class ))
155+ .build ()),
156+ // Add OpenCensus Metrics
157+ MetricsTracerFactory .create (
158+ tagger ,
159+ statsRecorder ,
160+ ImmutableMap .<TagKey , TagValue >builder ()
161+ .put (
162+ RpcMeasureConstants .BIGTABLE_PROJECT_ID ,
163+ TagValue .create (settings .getProjectId ()))
164+ .put (
165+ RpcMeasureConstants .BIGTABLE_INSTANCE_ID ,
166+ TagValue .create (settings .getInstanceId ()))
167+ .put (
168+ RpcMeasureConstants .BIGTABLE_APP_PROFILE_ID ,
169+ TagValue .create (settings .getAppProfileId ()))
170+ .build ()),
171+ // Add user configured tracer
172+ clientContext .getTracerFactory ())))
173+ .build ();
136174 this .requestContext =
137175 RequestContext .create (
138176 settings .getProjectId (), settings .getInstanceId (), settings .getAppProfileId ());
@@ -164,24 +202,16 @@ private EnhancedBigtableStub(
164202 */
165203 public <RowT > ServerStreamingCallable <Query , RowT > createReadRowsCallable (
166204 RowAdapter <RowT > rowAdapter ) {
167- ServerStreamingCallable <Query , RowT > readRowsCallable =
205+ final ServerStreamingCallable <Query , RowT > readRowsCallable =
168206 createReadRowsBaseCallable (settings .readRowsSettings (), rowAdapter );
169207
170- ServerStreamingCallable <Query , RowT > traced =
208+ final ServerStreamingCallable <Query , RowT > traced =
171209 new TracedServerStreamingCallable <>(
172210 readRowsCallable ,
173211 clientContext .getTracerFactory (),
174212 SpanName .of (TRACING_OUTER_CLIENT_NAME , "ReadRows" ));
175213
176- ServerStreamingCallable <Query , RowT > measured =
177- new MeasuredReadRowsCallable <>(
178- traced ,
179- TRACING_OUTER_CLIENT_NAME + ".ReadRows" ,
180- tagger ,
181- statsRecorder ,
182- clientContext .getClock ());
183-
184- return measured .withDefaultCallContext (clientContext .getDefaultCallContext ());
214+ return traced .withDefaultCallContext (clientContext .getDefaultCallContext ());
185215 }
186216
187217 /**
@@ -368,15 +398,7 @@ private UnaryCallable<BulkMutation, Void> createBulkMutateRowsCallable() {
368398 clientContext .getTracerFactory (),
369399 SpanName .of (TRACING_OUTER_CLIENT_NAME , "MutateRows" ));
370400
371- UnaryCallable <BulkMutation , Void > measured =
372- new MeasuredMutateRowsCallable (
373- traced ,
374- TRACING_OUTER_CLIENT_NAME + ".MutateRows" ,
375- tagger ,
376- statsRecorder ,
377- clientContext .getClock ());
378-
379- return measured .withDefaultCallContext (clientContext .getDefaultCallContext ());
401+ return traced .withDefaultCallContext (clientContext .getDefaultCallContext ());
380402 }
381403
382404 /**
@@ -548,15 +570,7 @@ private <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUserFacin
548570 clientContext .getTracerFactory (),
549571 SpanName .of (TRACING_OUTER_CLIENT_NAME , methodName ));
550572
551- UnaryCallable <RequestT , ResponseT > measured =
552- new MeasuredUnaryCallable <>(
553- traced ,
554- TRACING_OUTER_CLIENT_NAME + "." + methodName ,
555- tagger ,
556- statsRecorder ,
557- clientContext .getClock ());
558-
559- return measured .withDefaultCallContext (clientContext .getDefaultCallContext ());
573+ return traced .withDefaultCallContext (clientContext .getDefaultCallContext ());
560574 }
561575 // </editor-fold>
562576
0 commit comments