40
40
import com .google .api .gax .rpc .ServerStreamingCallable ;
41
41
import com .google .api .gax .rpc .UnaryCallSettings ;
42
42
import com .google .api .gax .rpc .UnaryCallable ;
43
+ import com .google .api .gax .tracing .ApiTracerFactory ;
43
44
import com .google .api .gax .tracing .OpencensusTracerFactory ;
44
45
import com .google .api .gax .tracing .SpanName ;
45
46
import com .google .api .gax .tracing .TracedServerStreamingCallable ;
110
111
import com .google .cloud .bigtable .data .v2 .stub .readrows .RowMergingCallable ;
111
112
import com .google .cloud .bigtable .gaxx .retrying .ApiResultRetryAlgorithm ;
112
113
import com .google .cloud .bigtable .gaxx .retrying .RetryInfoRetryAlgorithm ;
114
+ import com .google .common .annotations .VisibleForTesting ;
113
115
import com .google .common .base .MoreObjects ;
114
116
import com .google .common .base .Preconditions ;
115
117
import com .google .common .collect .ImmutableList ;
@@ -150,6 +152,8 @@ public class EnhancedBigtableStub implements AutoCloseable {
150
152
151
153
private final EnhancedBigtableStubSettings settings ;
152
154
private final ClientContext clientContext ;
155
+
156
+ private final boolean closeClientContext ;
153
157
private final RequestContext requestContext ;
154
158
private final FlowController bulkMutationFlowController ;
155
159
private final DynamicFlowControlStats bulkMutationDynamicFlowControlStats ;
@@ -172,13 +176,20 @@ public class EnhancedBigtableStub implements AutoCloseable {
172
176
173
177
public static EnhancedBigtableStub create (EnhancedBigtableStubSettings settings )
174
178
throws IOException {
175
- settings = finalizeSettings (settings , Tags .getTagger (), Stats .getStatsRecorder ());
176
179
177
- return new EnhancedBigtableStub (settings , ClientContext .create (settings ));
180
+ settings = settings .toBuilder ().setTracerFactory (createBigtableTracerFactory (settings )).build ();
181
+ ClientContext clientContext = createClientContext (settings );
182
+
183
+ return new EnhancedBigtableStub (settings , clientContext );
178
184
}
179
185
180
- public static EnhancedBigtableStubSettings finalizeSettings (
181
- EnhancedBigtableStubSettings settings , Tagger tagger , StatsRecorder stats )
186
+ public static EnhancedBigtableStub createWithClientContext (
187
+ EnhancedBigtableStubSettings settings , ClientContext clientContext ) throws IOException {
188
+
189
+ return new EnhancedBigtableStub (settings , clientContext , false );
190
+ }
191
+
192
+ public static ClientContext createClientContext (EnhancedBigtableStubSettings settings )
182
193
throws IOException {
183
194
EnhancedBigtableStubSettings .Builder builder = settings .toBuilder ();
184
195
@@ -222,49 +233,53 @@ public static EnhancedBigtableStubSettings finalizeSettings(
222
233
builder .setTransportChannelProvider (transportProvider .build ());
223
234
}
224
235
236
+ return ClientContext .create (builder .build ());
237
+ }
238
+
239
+ public static ApiTracerFactory createBigtableTracerFactory (
240
+ EnhancedBigtableStubSettings settings ) {
241
+ return createBigtableTracerFactory (settings , Tags .getTagger (), Stats .getStatsRecorder ());
242
+ }
243
+
244
+ @ VisibleForTesting
245
+ public static ApiTracerFactory createBigtableTracerFactory (
246
+ EnhancedBigtableStubSettings settings , Tagger tagger , StatsRecorder stats ) {
247
+ String projectId = settings .getProjectId ();
248
+ String instanceId = settings .getInstanceId ();
249
+ String appProfileId = settings .getAppProfileId ();
250
+
225
251
ImmutableMap <TagKey , TagValue > attributes =
226
252
ImmutableMap .<TagKey , TagValue >builder ()
227
- .put (RpcMeasureConstants .BIGTABLE_PROJECT_ID , TagValue .create (settings .getProjectId ()))
228
- .put (
229
- RpcMeasureConstants .BIGTABLE_INSTANCE_ID , TagValue .create (settings .getInstanceId ()))
230
- .put (
231
- RpcMeasureConstants .BIGTABLE_APP_PROFILE_ID ,
232
- TagValue .create (settings .getAppProfileId ()))
253
+ .put (RpcMeasureConstants .BIGTABLE_PROJECT_ID , TagValue .create (projectId ))
254
+ .put (RpcMeasureConstants .BIGTABLE_INSTANCE_ID , TagValue .create (instanceId ))
255
+ .put (RpcMeasureConstants .BIGTABLE_APP_PROFILE_ID , TagValue .create (appProfileId ))
233
256
.build ();
234
257
ImmutableMap <String , String > builtinAttributes =
235
258
ImmutableMap .<String , String >builder ()
236
- .put ("project_id" , settings . getProjectId () )
237
- .put ("instance" , settings . getInstanceId () )
238
- .put ("app_profile" , settings . getAppProfileId () )
259
+ .put ("project_id" , projectId )
260
+ .put ("instance" , instanceId )
261
+ .put ("app_profile" , appProfileId )
239
262
.build ();
240
- // Inject Opencensus instrumentation
241
- builder .setTracerFactory (
242
- new CompositeTracerFactory (
243
- ImmutableList .of (
244
- // Add OpenCensus Tracing
245
- new OpencensusTracerFactory (
246
- ImmutableMap .<String , String >builder ()
247
- // Annotate traces with the same tags as metrics
248
- .put (
249
- RpcMeasureConstants .BIGTABLE_PROJECT_ID .getName (),
250
- settings .getProjectId ())
251
- .put (
252
- RpcMeasureConstants .BIGTABLE_INSTANCE_ID .getName (),
253
- settings .getInstanceId ())
254
- .put (
255
- RpcMeasureConstants .BIGTABLE_APP_PROFILE_ID .getName (),
256
- settings .getAppProfileId ())
257
- // Also annotate traces with library versions
258
- .put ("gax" , GaxGrpcProperties .getGaxGrpcVersion ())
259
- .put ("grpc" , GaxGrpcProperties .getGrpcVersion ())
260
- .put ("gapic" , Version .VERSION )
261
- .build ()),
262
- // Add OpenCensus Metrics
263
- MetricsTracerFactory .create (tagger , stats , attributes ),
264
- BuiltinMetricsTracerFactory .create (builtinAttributes ),
265
- // Add user configured tracer
266
- settings .getTracerFactory ())));
267
- return builder .build ();
263
+
264
+ return new CompositeTracerFactory (
265
+ ImmutableList .of (
266
+ // Add OpenCensus Tracing
267
+ new OpencensusTracerFactory (
268
+ ImmutableMap .<String , String >builder ()
269
+ // Annotate traces with the same tags as metrics
270
+ .put (RpcMeasureConstants .BIGTABLE_PROJECT_ID .getName (), projectId )
271
+ .put (RpcMeasureConstants .BIGTABLE_INSTANCE_ID .getName (), instanceId )
272
+ .put (RpcMeasureConstants .BIGTABLE_APP_PROFILE_ID .getName (), appProfileId )
273
+ // Also annotate traces with library versions
274
+ .put ("gax" , GaxGrpcProperties .getGaxGrpcVersion ())
275
+ .put ("grpc" , GaxGrpcProperties .getGrpcVersion ())
276
+ .put ("gapic" , Version .VERSION )
277
+ .build ()),
278
+ // Add OpenCensus Metrics
279
+ MetricsTracerFactory .create (tagger , stats , attributes ),
280
+ BuiltinMetricsTracerFactory .create (builtinAttributes ),
281
+ // Add user configured tracer
282
+ settings .getTracerFactory ()));
268
283
}
269
284
270
285
private static void patchCredentials (EnhancedBigtableStubSettings .Builder settings )
@@ -303,8 +318,16 @@ private static void patchCredentials(EnhancedBigtableStubSettings.Builder settin
303
318
}
304
319
305
320
public EnhancedBigtableStub (EnhancedBigtableStubSettings settings , ClientContext clientContext ) {
321
+ this (settings , clientContext , true );
322
+ }
323
+
324
+ public EnhancedBigtableStub (
325
+ EnhancedBigtableStubSettings settings ,
326
+ ClientContext clientContext ,
327
+ boolean closeClientContext ) {
306
328
this .settings = settings ;
307
329
this .clientContext = clientContext ;
330
+ this .closeClientContext = closeClientContext ;
308
331
this .requestContext =
309
332
RequestContext .create (
310
333
settings .getProjectId (), settings .getInstanceId (), settings .getAppProfileId ());
@@ -1166,11 +1189,13 @@ private SpanName getSpanName(String methodName) {
1166
1189
1167
1190
@ Override
1168
1191
public void close () {
1169
- for (BackgroundResource backgroundResource : clientContext .getBackgroundResources ()) {
1170
- try {
1171
- backgroundResource .close ();
1172
- } catch (Exception e ) {
1173
- throw new IllegalStateException ("Failed to close resource" , e );
1192
+ if (closeClientContext ) {
1193
+ for (BackgroundResource backgroundResource : clientContext .getBackgroundResources ()) {
1194
+ try {
1195
+ backgroundResource .close ();
1196
+ } catch (Exception e ) {
1197
+ throw new IllegalStateException ("Failed to close resource" , e );
1198
+ }
1174
1199
}
1175
1200
}
1176
1201
}
0 commit comments