Skip to content

Commit cd9d8a5

Browse files
wip
1 parent a8d5451 commit cd9d8a5

19 files changed

+1201
-968
lines changed

google-cloud-bigtable/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@
218218
<artifactId>grpc-testing</artifactId>
219219
<scope>test</scope>
220220
</dependency>
221+
<dependency>
222+
<groupId>io.opencensus</groupId>
223+
<artifactId>opencensus-impl</artifactId>
224+
<scope>test</scope>
225+
</dependency>
221226
<dependency>
222227
<groupId>junit</groupId>
223228
<artifactId>junit</artifactId>

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/Query.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ private void writeObject(ObjectOutputStream output) throws IOException {
7272
builder.build().writeTo(output);
7373
}
7474

75+
public String getTableId() {
76+
return tableId;
77+
}
78+
7579
/** Adds a key to looked up */
7680
public Query rowKey(String key) {
7781
Preconditions.checkNotNull(key, "Key can't be null.");

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.google.api.gax.batching.Batcher;
2020
import com.google.api.gax.batching.BatcherImpl;
2121
import com.google.api.gax.core.BackgroundResource;
22+
import com.google.api.gax.core.GaxProperties;
23+
import com.google.api.gax.grpc.GaxGrpcProperties;
2224
import com.google.api.gax.grpc.GrpcCallSettings;
2325
import com.google.api.gax.grpc.GrpcRawCallableFactory;
2426
import com.google.api.gax.retrying.ExponentialRetryAlgorithm;
@@ -31,6 +33,8 @@
3133
import com.google.api.gax.rpc.ServerStreamingCallSettings;
3234
import com.google.api.gax.rpc.ServerStreamingCallable;
3335
import com.google.api.gax.rpc.UnaryCallable;
36+
import com.google.api.gax.tracing.ApiTracerFactory;
37+
import com.google.api.gax.tracing.OpencensusTracerFactory;
3438
import com.google.api.gax.tracing.SpanName;
3539
import com.google.api.gax.tracing.TracedServerStreamingCallable;
3640
import com.google.api.gax.tracing.TracedUnaryCallable;
@@ -58,9 +62,9 @@
5862
import com.google.cloud.bigtable.data.v2.models.RowAdapter;
5963
import com.google.cloud.bigtable.data.v2.models.RowMutation;
6064
import 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;
6468
import com.google.cloud.bigtable.data.v2.stub.mutaterows.BulkMutateRowsUserFacingCallable;
6569
import com.google.cloud.bigtable.data.v2.stub.mutaterows.MutateRowsBatchingDescriptor;
6670
import com.google.cloud.bigtable.data.v2.stub.mutaterows.MutateRowsRetryingCallable;
@@ -72,10 +76,13 @@
7276
import com.google.cloud.bigtable.data.v2.stub.readrows.RowMergingCallable;
7377
import com.google.cloud.bigtable.gaxx.retrying.ApiResultRetryAlgorithm;
7478
import com.google.common.base.Preconditions;
79+
import com.google.common.collect.ImmutableList;
7580
import com.google.common.collect.ImmutableMap;
7681
import com.google.protobuf.ByteString;
7782
import io.opencensus.stats.Stats;
7883
import io.opencensus.stats.StatsRecorder;
84+
import io.opencensus.tags.TagKey;
85+
import io.opencensus.tags.TagValue;
7986
import io.opencensus.tags.Tagger;
8087
import io.opencensus.tags.Tags;
8188
import 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

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@
2020
import com.google.api.gax.batching.BatchingSettings;
2121
import com.google.api.gax.batching.FlowControlSettings;
2222
import com.google.api.gax.batching.FlowController.LimitExceededBehavior;
23-
import com.google.api.gax.core.GaxProperties;
2423
import com.google.api.gax.core.GoogleCredentialsProvider;
25-
import com.google.api.gax.grpc.GaxGrpcProperties;
2624
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
2725
import com.google.api.gax.retrying.RetrySettings;
2826
import com.google.api.gax.rpc.ServerStreamingCallSettings;
2927
import com.google.api.gax.rpc.StatusCode.Code;
3028
import com.google.api.gax.rpc.StubSettings;
3129
import com.google.api.gax.rpc.TransportChannelProvider;
3230
import com.google.api.gax.rpc.UnaryCallSettings;
33-
import com.google.api.gax.tracing.OpencensusTracerFactory;
3431
import com.google.cloud.bigtable.data.v2.internal.RefreshChannel;
3532
import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation;
3633
import com.google.cloud.bigtable.data.v2.models.KeyOffset;
@@ -42,7 +39,6 @@
4239
import com.google.cloud.bigtable.data.v2.stub.readrows.ReadRowsBatchingDescriptor;
4340
import com.google.common.base.Preconditions;
4441
import com.google.common.collect.ImmutableList;
45-
import com.google.common.collect.ImmutableMap;
4642
import com.google.common.collect.ImmutableSet;
4743
import java.util.List;
4844
import java.util.Set;
@@ -520,13 +516,6 @@ private Builder() {
520516
setStreamWatchdogCheckInterval(baseDefaults.getStreamWatchdogCheckInterval());
521517
setStreamWatchdogProvider(baseDefaults.getStreamWatchdogProvider());
522518

523-
setTracerFactory(
524-
new OpencensusTracerFactory(
525-
ImmutableMap.of(
526-
"gax", GaxGrpcProperties.getGaxGrpcVersion(),
527-
"grpc", GaxGrpcProperties.getGrpcVersion(),
528-
"gapic", GaxProperties.getLibraryVersion(EnhancedBigtableStubSettings.class))));
529-
530519
// Per-method settings using baseSettings for defaults.
531520
readRowsSettings = ServerStreamingCallSettings.newBuilder();
532521

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.bigtable.data.v2.stub.metrics;
17+
18+
import com.google.api.gax.tracing.ApiTracer;
19+
import com.google.common.collect.ImmutableList;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
import org.threeten.bp.Duration;
23+
24+
/** Combines multiple {@link ApiTracer}s into a single {@link ApiTracer}. */
25+
class CompositeTracer implements ApiTracer {
26+
private final List<ApiTracer> children;
27+
28+
public CompositeTracer(List<ApiTracer> children) {
29+
this.children = ImmutableList.copyOf(children);
30+
}
31+
32+
@Override
33+
public Scope inScope() {
34+
final List<Scope> childScopes = new ArrayList<>(children.size());
35+
36+
for (ApiTracer child : children) {
37+
childScopes.add(child.inScope());
38+
}
39+
40+
return new Scope() {
41+
@Override
42+
public void close() {
43+
for (Scope childScope : childScopes) {
44+
childScope.close();
45+
}
46+
}
47+
};
48+
}
49+
50+
@Override
51+
public void operationSucceeded() {
52+
for (ApiTracer child : children) {
53+
child.operationSucceeded();
54+
}
55+
}
56+
57+
@Override
58+
public void operationCancelled() {
59+
for (ApiTracer child : children) {
60+
child.operationCancelled();
61+
}
62+
}
63+
64+
@Override
65+
public void operationFailed(Throwable error) {
66+
for (ApiTracer child : children) {
67+
child.operationFailed(error);
68+
}
69+
}
70+
71+
@Override
72+
public void connectionSelected(String id) {
73+
for (ApiTracer child : children) {
74+
child.connectionSelected(id);
75+
}
76+
}
77+
78+
@Override
79+
public void attemptStarted(int attemptNumber) {
80+
for (ApiTracer child : children) {
81+
child.attemptStarted(attemptNumber);
82+
}
83+
}
84+
85+
@Override
86+
public void attemptSucceeded() {
87+
for (ApiTracer child : children) {
88+
child.attemptSucceeded();
89+
}
90+
}
91+
92+
@Override
93+
public void attemptCancelled() {
94+
for (ApiTracer child : children) {
95+
child.attemptCancelled();
96+
}
97+
}
98+
99+
@Override
100+
public void attemptFailed(Throwable error, Duration delay) {
101+
for (ApiTracer child : children) {
102+
child.attemptFailed(error, delay);
103+
}
104+
}
105+
106+
@Override
107+
public void attemptFailedRetriesExhausted(Throwable error) {
108+
for (ApiTracer child : children) {
109+
child.attemptFailedRetriesExhausted(error);
110+
}
111+
}
112+
113+
@Override
114+
public void attemptPermanentFailure(Throwable error) {
115+
for (ApiTracer child : children) {
116+
child.attemptPermanentFailure(error);
117+
}
118+
}
119+
120+
@Override
121+
public void lroStartFailed(Throwable error) {
122+
for (ApiTracer child : children) {
123+
child.lroStartFailed(error);
124+
}
125+
}
126+
127+
@Override
128+
public void lroStartSucceeded() {
129+
for (ApiTracer child : children) {
130+
child.lroStartSucceeded();
131+
}
132+
}
133+
134+
@Override
135+
public void responseReceived() {
136+
for (ApiTracer child : children) {
137+
child.responseReceived();
138+
}
139+
}
140+
141+
@Override
142+
public void requestSent() {
143+
for (ApiTracer child : children) {
144+
child.requestSent();
145+
}
146+
}
147+
148+
@Override
149+
public void batchRequestSent(long elementCount, long requestSize) {
150+
for (ApiTracer child : children) {
151+
child.batchRequestSent(elementCount, requestSize);
152+
}
153+
}
154+
}

0 commit comments

Comments
 (0)