18
18
import com .google .api .core .ApiFunction ;
19
19
import com .google .api .gax .grpc .InstantiatingGrpcChannelProvider ;
20
20
import com .google .api .gax .rpc .FixedHeaderProvider ;
21
+ import com .google .api .gax .rpc .HeaderProvider ;
21
22
import com .google .api .gax .rpc .StubSettings ;
22
23
import com .google .cloud .bigtable .admin .v2 .BigtableInstanceAdminClient ;
23
24
import com .google .cloud .bigtable .admin .v2 .BigtableInstanceAdminSettings ;
31
32
import com .google .common .base .Predicate ;
32
33
import com .google .common .base .Predicates ;
33
34
import com .google .common .base .Strings ;
35
+ import com .google .common .collect .ImmutableMap ;
34
36
import io .grpc .CallOptions ;
35
37
import io .grpc .Channel ;
36
38
import io .grpc .ClientCall ;
48
50
import java .util .HashMap ;
49
51
import java .util .List ;
50
52
import java .util .Map ;
53
+ import java .util .Optional ;
51
54
import javax .annotation .Nullable ;
52
55
53
56
/**
@@ -84,9 +87,12 @@ public boolean apply(InetSocketAddress input) {
84
87
private static final String TABLE_PROPERTY_NAME = "bigtable.table" ;
85
88
private static final String CMEK_KMS_KEY_PROPERTY_NAME = "bigtable.kms_key_name" ;
86
89
90
+ private static final String TRACING_COOKIE_PROPERTY_NAME = "bigtable.tracing_cookie" ;
91
+
87
92
private final String projectId ;
88
93
private final String instanceId ;
89
94
private final String tableId ;
95
+ private final String tracingCookie ;
90
96
private final String kmsKeyName ;
91
97
92
98
private final BigtableDataSettings .Builder dataSettings ;
@@ -104,7 +110,8 @@ static CloudEnv fromSystemProperties() {
104
110
getOptionalProperty (CMEK_KMS_KEY_PROPERTY_NAME , "" ),
105
111
getRequiredProperty (PROJECT_PROPERTY_NAME ),
106
112
getRequiredProperty (INSTANCE_PROPERTY_NAME ),
107
- getRequiredProperty (TABLE_PROPERTY_NAME ));
113
+ getRequiredProperty (TABLE_PROPERTY_NAME ),
114
+ getOptionalProperty (TRACING_COOKIE_PROPERTY_NAME ));
108
115
}
109
116
110
117
private CloudEnv (
@@ -113,10 +120,12 @@ private CloudEnv(
113
120
@ Nullable String kmsKeyName ,
114
121
String projectId ,
115
122
String instanceId ,
116
- String tableId ) {
123
+ String tableId ,
124
+ @ Nullable String tracingCookie ) {
117
125
this .projectId = projectId ;
118
126
this .instanceId = instanceId ;
119
127
this .tableId = tableId ;
128
+ this .tracingCookie = tracingCookie ;
120
129
this .kmsKeyName = kmsKeyName ;
121
130
122
131
this .dataSettings =
@@ -127,6 +136,9 @@ private CloudEnv(
127
136
128
137
setupRemoteAddrInterceptor (dataSettings .stubSettings ());
129
138
configureUserAgent (dataSettings .stubSettings ());
139
+ if (tracingCookie != null ) {
140
+ injectTracingCookie (tracingCookie , dataSettings .stubSettings ());
141
+ }
130
142
131
143
this .tableAdminSettings =
132
144
BigtableTableAdminSettings .newBuilder ().setProjectId (projectId ).setInstanceId (instanceId );
@@ -140,6 +152,18 @@ private CloudEnv(
140
152
}
141
153
}
142
154
155
+ private static void injectTracingCookie (
156
+ String tracingCookie , EnhancedBigtableStubSettings .Builder settings ) {
157
+ HeaderProvider oldHeaderProvider = settings .getHeaderProvider ();
158
+ settings .setHeaderProvider (
159
+ () ->
160
+ Optional .ofNullable (oldHeaderProvider )
161
+ .map (p -> ImmutableMap .<String , String >builder ().putAll (p .getHeaders ()))
162
+ .orElse (ImmutableMap .builder ())
163
+ .put ("cookie" , tracingCookie )
164
+ .build ());
165
+ }
166
+
143
167
private void setupRemoteAddrInterceptor (StubSettings .Builder stubSettings ) {
144
168
// Build an remote address restricting interceptor
145
169
final ClientInterceptor interceptor ;
@@ -204,6 +228,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
204
228
return new SimpleForwardingClientCall <ReqT , RespT >(clientCall ) {
205
229
@ Override
206
230
public void start (Listener <RespT > responseListener , Metadata headers ) {
231
+ System .out .println (headers );
207
232
super .start (
208
233
new SimpleForwardingClientCallListener <RespT >(responseListener ) {
209
234
@ Override
@@ -361,6 +386,11 @@ public String getKmsKeyName() {
361
386
return kmsKeyName ;
362
387
}
363
388
389
+ @ Nullable
390
+ private static String getOptionalProperty (String prop ) {
391
+ return System .getProperty (prop );
392
+ }
393
+
364
394
private static String getOptionalProperty (String prop , String defaultValue ) {
365
395
return MoreObjects .firstNonNull (System .getProperty (prop ), defaultValue );
366
396
}
0 commit comments