Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reimplement metrics using the ApiTracer api
This will allow to us to keep metrics for RPCs tagged with bigtable tags like table id. This will also be the mechanism that bigtable-hbase will use to plugin its dropwizard metrics implementation
- Loading branch information
1 parent
0afccbb
commit 1e414f4
Showing
12 changed files
with
346 additions
and
902 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
...d-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/CensusTracer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package com.google.cloud.bigtable.data.v2.stub.metrics; | ||
|
||
import com.google.api.gax.tracing.ApiTracer; | ||
import com.google.api.gax.tracing.ApiTracerFactory.OperationType; | ||
import com.google.common.base.Stopwatch; | ||
import io.opencensus.stats.MeasureMap; | ||
import io.opencensus.stats.StatsRecorder; | ||
import io.opencensus.tags.TagContext; | ||
import io.opencensus.tags.TagContextBuilder; | ||
import io.opencensus.tags.TagValue; | ||
import io.opencensus.tags.Tagger; | ||
import java.util.concurrent.CancellationException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import javax.annotation.Nullable; | ||
import org.threeten.bp.Duration; | ||
|
||
class CensusTracer implements ApiTracer { | ||
private final OperationType operationType; | ||
|
||
private final TagValue methodName; | ||
private final TagContext parentCtx; | ||
|
||
private final Tagger tagger; | ||
private final StatsRecorder stats; | ||
|
||
private AtomicBoolean opFinished = new AtomicBoolean(); | ||
private Stopwatch operationTimer = Stopwatch.createStarted(); | ||
private Stopwatch firstResponseTimer = Stopwatch.createStarted(); | ||
private long operationResponseCount = 0; | ||
|
||
private Stopwatch attemptTimer; | ||
private int attemptCount = 0; | ||
private long attemptResponseCount = 0; | ||
private String attemptConnectionId = ""; | ||
|
||
CensusTracer(OperationType operationType, TagValue methodName, | ||
TagContext parentCtx, Tagger tagger, StatsRecorder stats) { | ||
this.operationType = operationType; | ||
this.methodName = methodName; | ||
this.parentCtx = parentCtx; | ||
this.tagger = tagger; | ||
this.stats = stats; | ||
} | ||
|
||
@Override | ||
public Scope inScope() { | ||
return new Scope() { | ||
@Override | ||
public void close() { } | ||
}; | ||
} | ||
|
||
@Override | ||
public void operationSucceeded() { | ||
recordOperationCompletion(null); | ||
} | ||
|
||
@Override | ||
public void operationCancelled() { | ||
recordOperationCompletion(new CancellationException()); | ||
} | ||
|
||
@Override | ||
public void operationFailed(Throwable throwable) { | ||
recordOperationCompletion(throwable); | ||
} | ||
|
||
private void recordOperationCompletion(@Nullable Throwable throwable) { | ||
if (!opFinished.compareAndSet(false, true)) { | ||
return; | ||
} | ||
|
||
TagContextBuilder tagBuilder = tagger.toBuilder(parentCtx) | ||
//TODO: projectId, instanceId, tableId, appProfileId | ||
.putLocal(RpcMeasureConstants.BIGTABLE_OP, methodName) | ||
.putLocal( | ||
RpcMeasureConstants.BIGTABLE_STATUS, Util.extractStatus(throwable)); | ||
|
||
MeasureMap measures = stats.newMeasureMap() | ||
.put(RpcMeasureConstants.BIGTABLE_OP_LATENCY, | ||
operationTimer.elapsed(TimeUnit.MILLISECONDS)); | ||
|
||
if (operationType == OperationType.ServerStreaming && methodName.asString().endsWith("ReadRows")) { | ||
measures.put(RpcMeasureConstants.BIGTABLE_ROWS_READ_PER_OP, operationResponseCount); | ||
measures.put(RpcMeasureConstants.BIGTABLE_READ_ROWS_FIRST_ROW_LATENCY, firstResponseTimer.elapsed(TimeUnit.MILLISECONDS)); | ||
} | ||
|
||
measures.record(tagBuilder.build()); | ||
} | ||
|
||
@Override | ||
public void connectionSelected(String s) { | ||
this.attemptConnectionId = s; | ||
} | ||
|
||
@Override | ||
public void attemptStarted(int i) { | ||
attemptTimer = Stopwatch.createStarted(); | ||
attemptCount++; | ||
attemptResponseCount = 0; | ||
} | ||
|
||
@Override | ||
public void attemptSucceeded() { | ||
recordAttemptCompletion(null, null); | ||
} | ||
|
||
@Override | ||
public void attemptCancelled() { | ||
recordAttemptCompletion(new CancellationException(), null); | ||
} | ||
|
||
@Override | ||
public void attemptFailed(Throwable throwable, Duration duration) { | ||
recordAttemptCompletion(throwable, duration); | ||
} | ||
|
||
@Override | ||
public void attemptFailedRetriesExhausted(Throwable throwable) { | ||
recordAttemptCompletion(throwable, null); | ||
} | ||
|
||
@Override | ||
public void attemptPermanentFailure(Throwable throwable) { | ||
recordAttemptCompletion(throwable, null); | ||
} | ||
|
||
private void recordAttemptCompletion(@Nullable Throwable throwable, @Nullable Duration nextAttemptDelay) { | ||
// TODO: record attempts | ||
} | ||
|
||
@Override | ||
public void lroStartFailed(Throwable throwable) { | ||
// noop | ||
} | ||
|
||
@Override | ||
public void lroStartSucceeded() { | ||
// noop | ||
} | ||
|
||
@Override | ||
public void responseReceived() { | ||
if(firstResponseTimer.isRunning()) { | ||
firstResponseTimer.stop(); | ||
} | ||
attemptResponseCount++; | ||
operationResponseCount++; | ||
} | ||
|
||
@Override | ||
public void requestSent() { | ||
// noop: no operations are client streaming | ||
} | ||
|
||
@Override | ||
public void batchRequestSent(long elementCount, long requestSize) { | ||
// noop: there is a server side metric that can be used instead | ||
} | ||
} |
Oops, something went wrong.