GeezerVison is showing github.com — update comments · googleapis/java-bigtable@9c71da9
This repository was archived by the owner on Aug 30, 2026. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings google-cloud-bigtable/src main/java/com/google/cloud/bigtable/data/v2/stub test/java/com/google/cloud/bigtable/data/v2/stub/metrics
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 90 | 90 | import com.google.cloud.bigtable.data.v2.stub.readrows.ReadRowsUserCallable; | 91 | 91 | import com.google.cloud.bigtable.data.v2.stub.readrows.RowMergingCallable; | 92 | 92 | import com.google.cloud.bigtable.gaxx.retrying.ApiResultRetryAlgorithm; | 93 | | -import com.google.cloud.bigtable.stats.StatsWrapper; | 94 | 93 | import com.google.common.base.MoreObjects; | 95 | 94 | import com.google.common.base.Preconditions; | 96 | 95 | import com.google.common.collect.ImmutableList; | @@ -154,7 +153,6 @@ public static EnhancedBigtableStubSettings finalizeSettings( | 154 | 153 | EnhancedBigtableStubSettings settings, Tagger tagger, StatsRecorder stats) | 155 | 154 | throws IOException { | 156 | 155 | EnhancedBigtableStubSettings.Builder builder = settings.toBuilder(); | 157 | | - StatsWrapper statsWrapper = StatsWrapper.create(); | 158 | 156 |
| 159 | 157 | // TODO: this implementation is on the cusp of unwieldy, if we end up adding more features | 160 | 158 | // consider splitting it up by feature. | @@ -227,7 +225,7 @@ public static EnhancedBigtableStubSettings finalizeSettings( | 227 | 225 | .build()), | 228 | 226 | // Add OpenCensus Metrics | 229 | 227 | MetricsTracerFactory.create(tagger, stats, attributes), | 230 | | - BuiltinMetricsTracerFactory.create(statsWrapper, builtinAttributes), | | 228 | + BuiltinMetricsTracerFactory.create(builtinAttributes), | 231 | 229 | // Add user configured tracer | 232 | 230 | settings.getTracerFactory()))); | 233 | 231 | return builder.build(); | |
Collapse file Copy file name to clipboardExpand all lines: google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracer.java +9-48Lines changed: 9 additions & 48 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | */ | 16 | 16 | package com.google.cloud.bigtable.data.v2.stub.metrics; | 17 | 17 |
| 18 | | -import com.google.api.gax.tracing.ApiTracerFactory; | | 18 | +import static com.google.api.gax.tracing.ApiTracerFactory.OperationType; | | 19 | + | 19 | 20 | import com.google.api.gax.tracing.SpanName; | 20 | 21 | import com.google.cloud.bigtable.stats.StatsRecorderWrapper; | 21 | | -import com.google.cloud.bigtable.stats.StatsWrapper; | | 22 | +import com.google.common.annotations.VisibleForTesting; | 22 | 23 | import com.google.common.base.Stopwatch; | 23 | | -import java.util.Map; | 24 | 24 | import java.util.concurrent.CancellationException; | 25 | 25 | import java.util.concurrent.TimeUnit; | 26 | 26 | import java.util.concurrent.atomic.AtomicBoolean; | @@ -36,7 +36,7 @@ class BuiltinMetricsTracer extends BigtableTracer { | 36 | 36 |
| 37 | 37 | private final StatsRecorderWrapper recorder; | 38 | 38 |
| 39 | | - private final ApiTracerFactory.OperationType operationType; | | 39 | + private final OperationType operationType; | 40 | 40 | private final SpanName spanName; | 41 | 41 |
| 42 | 42 | // Operation level metrics | @@ -51,8 +51,7 @@ class BuiltinMetricsTracer extends BigtableTracer { | 51 | 51 |
| 52 | 52 | // Total application latency | 53 | 53 | // Total application latency needs to be atomic because it's accessed from different threads. E.g. | 54 | | - // request() from | 55 | | - // user thread and attempt failed from grpc thread. | | 54 | + // request() from user thread and attempt failed from grpc thread. | 56 | 55 | private final AtomicLong totalApplicationLatency = new AtomicLong(0); | 57 | 56 | // Stopwatch is not thread safe so this is a workaround to check if the stopwatch changes is | 58 | 57 | // flushed to memory. | @@ -67,20 +66,12 @@ class BuiltinMetricsTracer extends BigtableTracer { | 67 | 66 | // gfe stats | 68 | 67 | private AtomicLong gfeMissingHeaders = new AtomicLong(0); | 69 | 68 |
| | 69 | + @VisibleForTesting | 70 | 70 | BuiltinMetricsTracer( | 71 | | - ApiTracerFactory.OperationType operationType, | 72 | | - SpanName spanName, | 73 | | - Map<String, String> attributes, | 74 | | - StatsWrapper statsWrapper, | 75 | | - @Nullable StatsRecorderWrapper statsRecorderWrapper) { | | 71 | + OperationType operationType, SpanName spanName, StatsRecorderWrapper recorder) { | 76 | 72 | this.operationType = operationType; | 77 | 73 | this.spanName = spanName; | 78 | | - if (statsRecorderWrapper != null) { | 79 | | - // A workaround for test to pass in a mock StatsRecorderWrapper | 80 | | - this.recorder = statsRecorderWrapper; | 81 | | - } else { | 82 | | - this.recorder = new StatsRecorderWrapper(operationType, spanName, attributes, statsWrapper); | 83 | | - } | | 74 | + this.recorder = recorder; | 84 | 75 | } | 85 | 76 |
| 86 | 77 | @Override | @@ -143,26 +134,6 @@ public void attemptFailed(Throwable error, Duration delay) { | 143 | 134 | recordAttemptCompletion(error); | 144 | 135 | } | 145 | 136 |
| 146 | | - @Override | 147 | | - public void attemptFailedRetriesExhausted(Throwable error) { | 148 | | - super.attemptFailedRetriesExhausted(error); | 149 | | - } | 150 | | - | 151 | | - @Override | 152 | | - public void attemptPermanentFailure(Throwable error) { | 153 | | - super.attemptPermanentFailure(error); | 154 | | - } | 155 | | - | 156 | | - @Override | 157 | | - public void lroStartFailed(Throwable error) { | 158 | | - super.lroStartFailed(error); | 159 | | - } | 160 | | - | 161 | | - @Override | 162 | | - public void lroStartSucceeded() { | 163 | | - super.lroStartSucceeded(); | 164 | | - } | 165 | | - | 166 | 137 | @Override | 167 | 138 | public void onRequest() { | 168 | 139 | if (applicationLatencyTimerIsRunning.compareAndSet(true, false)) { | @@ -181,16 +152,6 @@ public void responseReceived() { | 181 | 152 | } | 182 | 153 | } | 183 | 154 |
| 184 | | - @Override | 185 | | - public void requestSent() { | 186 | | - super.requestSent(); | 187 | | - } | 188 | | - | 189 | | - @Override | 190 | | - public void batchRequestSent(long elementCount, long requestSize) { | 191 | | - super.batchRequestSent(elementCount, requestSize); | 192 | | - } | 193 | | - | 194 | 155 | @Override | 195 | 156 | public int getAttempt() { | 196 | 157 | return attempt; | @@ -235,7 +196,7 @@ private void recordOperationCompletion(@Nullable Throwable status) { | 235 | 196 |
| 236 | 197 | recorder.putOperationLatencies(operationTimer.elapsed(TimeUnit.MILLISECONDS)); | 237 | 198 |
| 238 | | - if (operationType == ApiTracerFactory.OperationType.ServerStreaming | | 199 | + if (operationType == OperationType.ServerStreaming | 239 | 200 | && spanName.getMethodName().equals("ReadRows")) { | 240 | 201 | recorder.putFirstResponseLatencies(firstResponsePerOpTimer.elapsed(TimeUnit.MILLISECONDS)); | 241 | 202 | } | |
Collapse file Copy file name to clipboardExpand all lines: google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerFactory.java +6-23Lines changed: 6 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | import com.google.api.gax.tracing.ApiTracerFactory; | 21 | 21 | import com.google.api.gax.tracing.BaseApiTracerFactory; | 22 | 22 | import com.google.api.gax.tracing.SpanName; | 23 | | -import com.google.cloud.bigtable.stats.StatsRecorderWrapper; | 24 | 23 | import com.google.cloud.bigtable.stats.StatsWrapper; | 25 | | -import com.google.common.annotations.VisibleForTesting; | 26 | 24 | import com.google.common.collect.ImmutableMap; | 27 | 25 |
| 28 | 26 | /** | | 33 | 31 | public class BuiltinMetricsTracerFactory extends BaseApiTracerFactory { | 34 | 32 |
| 35 | 33 | private final ImmutableMap<String, String> statsAttributes; | 36 | | - private final StatsWrapper statsWrapper; | 37 | | - private final StatsRecorderWrapper statsRecorderWrapper; | 38 | 34 |
| 39 | | - public static BuiltinMetricsTracerFactory create( | 40 | | - StatsWrapper statsWrapper, ImmutableMap<String, String> statsAttributes) { | 41 | | - return new BuiltinMetricsTracerFactory(statsWrapper, statsAttributes, null); | | 35 | + public static BuiltinMetricsTracerFactory create(ImmutableMap<String, String> statsAttributes) { | | 36 | + return new BuiltinMetricsTracerFactory(statsAttributes); | 42 | 37 | } | 43 | 38 |
| 44 | | - // A workaround for test to pass in a mock StatsRecorderWrapper | 45 | | - @VisibleForTesting | 46 | | - static BuiltinMetricsTracerFactory createWithRecorder( | 47 | | - StatsWrapper statsWrapper, | 48 | | - ImmutableMap<String, String> statsAttributes, | 49 | | - StatsRecorderWrapper statsRecorderWrapper) { | 50 | | - return new BuiltinMetricsTracerFactory(statsWrapper, statsAttributes, statsRecorderWrapper); | 51 | | - } | 52 | | - | 53 | | - private BuiltinMetricsTracerFactory( | 54 | | - StatsWrapper statsWrapper, | 55 | | - ImmutableMap<String, String> statsAttributes, | 56 | | - StatsRecorderWrapper statsRecorderWrapper) { | | 39 | + private BuiltinMetricsTracerFactory(ImmutableMap<String, String> statsAttributes) { | 57 | 40 | this.statsAttributes = statsAttributes; | 58 | | - this.statsWrapper = statsWrapper; | 59 | | - this.statsRecorderWrapper = statsRecorderWrapper; | 60 | 41 | } | 61 | 42 |
| 62 | 43 | @Override | 63 | 44 | public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType operationType) { | 64 | 45 | return new BuiltinMetricsTracer( | 65 | | - operationType, spanName, statsAttributes, statsWrapper, statsRecorderWrapper); | | 46 | + operationType, | | 47 | + spanName, | | 48 | + StatsWrapper.createRecorder(operationType, spanName, statsAttributes)); | 66 | 49 | } | 67 | 50 | } |
Collapse file Copy file name to clipboardExpand all lines: google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/HeaderTracerStreamingCallable.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -82,7 +82,7 @@ private class HeaderTracerResponseObserver<ResponseT> implements ResponseObserve | 82 | 82 |
| 83 | 83 | @Override | 84 | 84 | public void onStart(final StreamController controller) { | 85 | | - final StreamController tracedController = new TracedStreamController(controller, tracer); | | 85 | + StreamController tracedController = new TracedStreamController(controller, tracer); | 86 | 86 | outerObserver.onStart(tracedController); | 87 | 87 | } | 88 | 88 |
| |
Collapse file Copy file name to clipboardExpand all lines: google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/MetricsTracer.java -35Lines changed: 0 additions & 35 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -125,11 +125,6 @@ private void recordOperationCompletion(@Nullable Throwable throwable) { | 125 | 125 | measures.record(tagCtx.build()); | 126 | 126 | } | 127 | 127 |
| 128 | | - @Override | 129 | | - public void connectionSelected(String s) { | 130 | | - // noop: cardinality for connection ids is too high to use as tags | 131 | | - } | 132 | | - | 133 | 128 | @Override | 134 | 129 | public void attemptStarted(int attemptNumber) { | 135 | 130 | attempt = attemptNumber; | @@ -180,16 +175,6 @@ private void recordAttemptCompletion(@Nullable Throwable throwable) { | 180 | 175 | measures.record(tagCtx.build()); | 181 | 176 | } | 182 | 177 |
| 183 | | - @Override | 184 | | - public void lroStartFailed(Throwable throwable) { | 185 | | - // noop | 186 | | - } | 187 | | - | 188 | | - @Override | 189 | | - public void lroStartSucceeded() { | 190 | | - // noop | 191 | | - } | 192 | | - | 193 | 178 | @Override | 194 | 179 | public void responseReceived() { | 195 | 180 | if (firstResponsePerOpTimer.isRunning()) { | @@ -199,16 +184,6 @@ public void responseReceived() { | 199 | 184 | operationResponseCount++; | 200 | 185 | } | 201 | 186 |
| 202 | | - @Override | 203 | | - public void requestSent() { | 204 | | - // noop: no operations are client streaming | 205 | | - } | 206 | | - | 207 | | - @Override | 208 | | - public void batchRequestSent(long elementCount, long requestSize) { | 209 | | - // noop | 210 | | - } | 211 | | - | 212 | 187 | @Override | 213 | 188 | public int getAttempt() { | 214 | 189 | return attempt; | @@ -253,14 +228,4 @@ private TagContextBuilder newTagCtxBuilder() { | 253 | 228 |
| 254 | 229 | return tagCtx; | 255 | 230 | } | 256 | | - | 257 | | - @Override | 258 | | - public void setLocations(String zone, String cluster) { | 259 | | - // noop | 260 | | - } | 261 | | - | 262 | | - @Override | 263 | | - public void onRequest() { | 264 | | - // noop | 265 | | - } | 266 | 231 | } |
Collapse file Copy file name to clipboardExpand all lines: google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java +39-5Lines changed: 39 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | */ | 16 | 16 | package com.google.cloud.bigtable.data.v2.stub.metrics; | 17 | 17 |
| | 18 | +import static com.google.api.gax.tracing.ApiTracerFactory.OperationType; | 18 | 19 | import static com.google.common.truth.Truth.assertThat; | | 20 | +import static org.mockito.ArgumentMatchers.any; | 19 | 21 | import static org.mockito.Mockito.times; | 20 | 22 | import static org.mockito.Mockito.verify; | | 23 | +import static org.mockito.Mockito.when; | 21 | 24 |
| 22 | 25 | import com.google.api.client.util.Lists; | 23 | 26 | import com.google.api.core.SettableApiFuture; | 24 | 27 | import com.google.api.gax.rpc.ClientContext; | 25 | 28 | import com.google.api.gax.rpc.ResponseObserver; | 26 | 29 | import com.google.api.gax.rpc.StreamController; | | 30 | +import com.google.api.gax.tracing.SpanName; | 27 | 31 | import com.google.bigtable.v2.BigtableGrpc; | 28 | 32 | import com.google.bigtable.v2.MutateRowRequest; | 29 | 33 | import com.google.bigtable.v2.MutateRowResponse; | | 37 | 41 | import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStub; | 38 | 42 | import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings; | 39 | 43 | import com.google.cloud.bigtable.stats.StatsRecorderWrapper; | 40 | | -import com.google.cloud.bigtable.stats.StatsWrapper; | 41 | 44 | import com.google.common.base.Stopwatch; | 42 | | -import com.google.common.collect.ImmutableMap; | 43 | 45 | import com.google.common.collect.Range; | 44 | 46 | import com.google.protobuf.ByteString; | 45 | 47 | import com.google.protobuf.BytesValue; | | 59 | 61 | import org.junit.Before; | 60 | 62 | import org.junit.Rule; | 61 | 63 | import org.junit.Test; | | 64 | +import org.junit.runner.RunWith; | | 65 | +import org.junit.runners.JUnit4; | 62 | 66 | import org.mockito.ArgumentCaptor; | 63 | 67 | import org.mockito.Captor; | 64 | 68 | import org.mockito.Mock; | 65 | 69 | import org.mockito.junit.MockitoJUnit; | 66 | 70 | import org.mockito.junit.MockitoRule; | 67 | 71 | import org.threeten.bp.Duration; | 68 | 72 |
| | 73 | +@RunWith(JUnit4.class) | 69 | 74 | public class BuiltinMetricsTracerTest { | 70 | 75 | private static final String PROJECT_ID = "fake-project"; | 71 | 76 | private static final String INSTANCE_ID = "fake-instance"; | @@ -118,6 +123,7 @@ public class BuiltinMetricsTracerTest { | 118 | 123 |
| 119 | 124 | private EnhancedBigtableStub stub; | 120 | 125 |
| | 126 | + @Mock private BuiltinMetricsTracerFactory mockFactory; | 121 | 127 | @Mock private StatsRecorderWrapper statsRecorderWrapper; | 122 | 128 |
| 123 | 129 | @Captor private ArgumentCaptor<Long> longValue; | @@ -169,9 +175,7 @@ public void sendHeaders(Metadata headers) { | 169 | 175 | .mutateRowSettings() | 170 | 176 | .retrySettings() | 171 | 177 | .setInitialRetryDelay(Duration.ofMillis(200)); | 172 | | - stubSettingsBuilder.setTracerFactory( | 173 | | - BuiltinMetricsTracerFactory.createWithRecorder( | 174 | | - StatsWrapper.create(), ImmutableMap.of(), statsRecorderWrapper)); | | 178 | + stubSettingsBuilder.setTracerFactory(mockFactory); | 175 | 179 |
| 176 | 180 | EnhancedBigtableStubSettings stubSettings = stubSettingsBuilder.build(); | 177 | 181 | stub = new EnhancedBigtableStub(stubSettings, ClientContext.create(stubSettings)); | @@ -185,6 +189,12 @@ public void tearDown() { | 185 | 189 |
| 186 | 190 | @Test | 187 | 191 | public void testOperationLatencies() { | | 192 | + when(mockFactory.newTracer(any(), any(), any())) | | 193 | + .thenReturn( | | 194 | + new BuiltinMetricsTracer( | | 195 | + OperationType.ServerStreaming, | | 196 | + SpanName.of("Bigtable", "ReadRows"), | | 197 | + statsRecorderWrapper)); | 188 | 198 | Stopwatch stopwatch = Stopwatch.createStarted(); | 189 | 199 | Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE_ID)).iterator()); | 190 | 200 | long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS); | @@ -196,6 +206,13 @@ public void testOperationLatencies() { | 196 | 206 |
| 197 | 207 | @Test | 198 | 208 | public void testGfeMetrics() { | | 209 | + when(mockFactory.newTracer(any(), any(), any())) | | 210 | + .thenReturn( | | 211 | + new BuiltinMetricsTracer( | | 212 | + OperationType.ServerStreaming, | | 213 | + SpanName.of("Bigtable", "ReadRows"), | | 214 | + statsRecorderWrapper)); | | 215 | + | 199 | 216 | Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE_ID))); | 200 | 217 |
| 201 | 218 | verify(statsRecorderWrapper).putGfeLatencies(longValue.capture()); | @@ -207,6 +224,13 @@ public void testGfeMetrics() { | 207 | 224 |
| 208 | 225 | @Test | 209 | 226 | public void testReadRowsApplicationLatency() throws Exception { | | 227 | + when(mockFactory.newTracer(any(), any(), any())) | | 228 | + .thenReturn( | | 229 | + new BuiltinMetricsTracer( | | 230 | + OperationType.ServerStreaming, | | 231 | + SpanName.of("Bigtable", "ReadRows"), | | 232 | + statsRecorderWrapper)); | | 233 | + | 210 | 234 | final long applicationLatency = 1000; | 211 | 235 | final SettableApiFuture future = SettableApiFuture.create(); | 212 | 236 | final AtomicInteger counter = new AtomicInteger(0); | @@ -258,6 +282,11 @@ public void onComplete() { | 258 | 282 |
| 259 | 283 | @Test | 260 | 284 | public void testRetryCount() { | | 285 | + when(mockFactory.newTracer(any(), any(), any())) | | 286 | + .thenReturn( | | 287 | + new BuiltinMetricsTracer( | | 288 | + OperationType.Unary, SpanName.of("Bigtable", "MutateRow"), statsRecorderWrapper)); | | 289 | + | 261 | 290 | stub.mutateRowCallable() | 262 | 291 | .call(RowMutation.create(TABLE_ID, "random-row").setCell("cf", "q", "value")); | 263 | 292 |
| @@ -268,6 +297,11 @@ public void testRetryCount() { | 268 | 297 |
| 269 | 298 | @Test | 270 | 299 | public void testMutateRowAttempts() { | | 300 | + when(mockFactory.newTracer(any(), any(), any())) | | 301 | + .thenReturn( | | 302 | + new BuiltinMetricsTracer( | | 303 | + OperationType.Unary, SpanName.of("Bigtable", "MutateRow"), statsRecorderWrapper)); | | 304 | + | 271 | 305 | stub.mutateRowCallable() | 272 | 306 | .call(RowMutation.create(TABLE_ID, "random-row").setCell("cf", "q", "value")); | 273 | 307 |
| |
|