/
StatsRecorderWrapper.java
135 lines (113 loc) · 5.14 KB
/
StatsRecorderWrapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.stats;
import com.google.api.core.InternalApi;
import com.google.api.gax.tracing.ApiTracerFactory.OperationType;
import com.google.api.gax.tracing.SpanName;
import io.opencensus.stats.MeasureMap;
import io.opencensus.stats.StatsRecorder;
import io.opencensus.tags.TagContext;
import io.opencensus.tags.TagContextBuilder;
import io.opencensus.tags.TagKey;
import io.opencensus.tags.TagValue;
import io.opencensus.tags.Tagger;
import io.opencensus.tags.Tags;
import java.util.Map;
/** A wrapper to record built-in metrics */
@InternalApi("For internal use only")
public class StatsRecorderWrapper {
private final OperationType operationType;
private final Tagger tagger;
private final StatsRecorder statsRecorder;
private final TagContext parentContext;
private final SpanName spanName;
private final Map<String, String> statsAttributes;
private MeasureMap attemptMeasureMap;
private MeasureMap operationMeasureMap;
public StatsRecorderWrapper(
OperationType operationType,
SpanName spanName,
Map<String, String> statsAttributes,
StatsRecorder statsRecorder) {
this.operationType = operationType;
this.tagger = Tags.getTagger();
this.statsRecorder = statsRecorder;
this.spanName = spanName;
this.parentContext = tagger.getCurrentTagContext();
this.statsAttributes = statsAttributes;
this.attemptMeasureMap = statsRecorder.newMeasureMap();
this.operationMeasureMap = statsRecorder.newMeasureMap();
}
public void recordOperation(String status, String tableId, String zone, String cluster) {
TagContextBuilder tagCtx =
newTagContextBuilder(tableId, zone, cluster)
.putLocal(BuiltinMeasureConstants.STATUS, TagValue.create(status));
boolean isStreaming = operationType == OperationType.ServerStreaming;
tagCtx.putLocal(
BuiltinMeasureConstants.STREAMING, TagValue.create(Boolean.toString(isStreaming)));
operationMeasureMap.record(tagCtx.build());
// Reinitialize a new map
operationMeasureMap = statsRecorder.newMeasureMap();
}
public void recordAttempt(String status, String tableId, String zone, String cluster) {
TagContextBuilder tagCtx =
newTagContextBuilder(tableId, zone, cluster)
.putLocal(BuiltinMeasureConstants.STATUS, TagValue.create(status));
boolean isStreaming = operationType == OperationType.ServerStreaming;
tagCtx.putLocal(
BuiltinMeasureConstants.STREAMING, TagValue.create(Boolean.toString(isStreaming)));
attemptMeasureMap.record(tagCtx.build());
// Reinitialize a new map
attemptMeasureMap = statsRecorder.newMeasureMap();
}
public void putOperationLatencies(long operationLatency) {
operationMeasureMap.put(BuiltinMeasureConstants.OPERATION_LATENCIES, operationLatency);
}
public void putAttemptLatencies(long attemptLatency) {
attemptMeasureMap.put(BuiltinMeasureConstants.ATTEMPT_LATENCIES, attemptLatency);
}
public void putRetryCount(int attemptCount) {
operationMeasureMap.put(BuiltinMeasureConstants.RETRY_COUNT, attemptCount);
}
public void putApplicationLatencies(long applicationLatency) {
operationMeasureMap.put(BuiltinMeasureConstants.APPLICATION_LATENCIES, applicationLatency);
}
public void putFirstResponseLatencies(long firstResponseLatency) {
operationMeasureMap.put(BuiltinMeasureConstants.FIRST_RESPONSE_LATENCIES, firstResponseLatency);
}
public void putGfeLatencies(long serverLatency) {
attemptMeasureMap.put(BuiltinMeasureConstants.SERVER_LATENCIES, serverLatency);
}
public void putGfeMissingHeaders(long connectivityErrors) {
attemptMeasureMap.put(BuiltinMeasureConstants.CONNECTIVITY_ERROR_COUNT, connectivityErrors);
}
public void putClientBlockingLatencies(long clientBlockingLatency) {
operationMeasureMap.put(BuiltinMeasureConstants.THROTTLING_LATENCIES, clientBlockingLatency);
}
private TagContextBuilder newTagContextBuilder(String tableId, String zone, String cluster) {
TagContextBuilder tagContextBuilder =
tagger
.toBuilder(parentContext)
.putLocal(BuiltinMeasureConstants.METHOD, TagValue.create(spanName.toString()))
.putLocal(BuiltinMeasureConstants.TABLE, TagValue.create(tableId))
.putLocal(BuiltinMeasureConstants.ZONE, TagValue.create(zone))
.putLocal(BuiltinMeasureConstants.CLUSTER, TagValue.create(cluster));
for (Map.Entry<String, String> entry : statsAttributes.entrySet()) {
tagContextBuilder.putLocal(TagKey.create(entry.getKey()), TagValue.create(entry.getValue()));
}
return tagContextBuilder;
}
}