Skip to content

Commit 554bd89

Browse files
committed
make metrics consistent with cloud monitoring
1 parent 4781fa3 commit 554bd89

File tree

8 files changed

+54
-34
lines changed

8 files changed

+54
-34
lines changed

google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableStackdriverExportUtils.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ class BigtableStackdriverExportUtils {
6060
private static final io.opencensus.common.Function<ExplicitOptions, BucketOptions>
6161
bucketOptionsExplicitFunction;
6262

63-
private static final String DOMAIN = "bigtable.googleapis.com/internal/client/";
64-
6563
private static final Set<String> PROMOTED_RESOURCE_LABELS =
6664
ImmutableSet.of(
6765
BuiltinMeasureConstants.PROJECT_ID.getName(),
@@ -142,7 +140,7 @@ static TimeSeries convertTimeSeries(
142140
metricTagValues.add(labelValues.get(i));
143141
}
144142
}
145-
metricTagKeys.add(LabelKey.create(BuiltinMeasureConstants.CLIENT_ID.getName(), "client id"));
143+
metricTagKeys.add(LabelKey.create(BuiltinMeasureConstants.CLIENT_UID.getName(), "client id"));
146144
metricTagValues.add(LabelValue.create(clientId));
147145

148146
TimeSeries.Builder builder = TimeSeries.newBuilder();
@@ -161,7 +159,7 @@ static TimeSeries convertTimeSeries(
161159
static com.google.api.Metric createMetric(
162160
String metricName, List<LabelKey> labelKeys, List<LabelValue> labelValues) {
163161
com.google.api.Metric.Builder builder = com.google.api.Metric.newBuilder();
164-
builder.setType(DOMAIN + metricName);
162+
builder.setType(metricName);
165163
Map<String, String> stringTagMap = Maps.newHashMap();
166164

167165
for (int i = 0; i < labelValues.size(); ++i) {

google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableStackdriverStatsExporter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class BigtableStackdriverStatsExporter {
4040
@GuardedBy("monitor")
4141
private static BigtableStackdriverStatsExporter instance = null;
4242

43-
private static final Duration EXPORT_INTERVAL = Duration.create(600, 0);
43+
private static final Duration EXPORT_INTERVAL = Duration.create(60, 0);
4444
private static final String RESOURCE_TYPE = "bigtable_client_raw";
4545

4646
private final IntervalMetricReader intervalMetricReader;

google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BuiltinMeasureConstants.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@
2323
class BuiltinMeasureConstants {
2424
// Monitored resource TagKeys
2525
static final TagKey PROJECT_ID = TagKey.create("project_id");
26-
static final TagKey INSTANCE_ID = TagKey.create("instance_id");
26+
static final TagKey INSTANCE_ID = TagKey.create("instance");
2727
static final TagKey CLUSTER = TagKey.create("cluster");
2828
static final TagKey TABLE = TagKey.create("table");
2929
static final TagKey ZONE = TagKey.create("zone");
30-
// Placeholder TagKey to be used in Stackdriver exporter
31-
static final TagKey CLIENT_ID = TagKey.create("client_id");
30+
static final TagKey CLIENT_UID = TagKey.create("client_uid");
3231

3332
// Metrics TagKeys
3433
static final TagKey APP_PROFILE = TagKey.create("app_profile");

google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BuiltinViewConstants.java

+4-12
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class BuiltinViewConstants {
5959
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 15.0, 20.0, 30.0, 40.0, 50.0,
6060
100.0)));
6161

62-
private static final Aggregation AGGREGATION_ERROR_COUNT = Sum.create();
62+
private static final Aggregation AGGREGATION_COUNT = Sum.create();
6363

6464
static final View OPERATION_LATENCIES_VIEW =
6565
View.create(
@@ -102,7 +102,7 @@ class BuiltinViewConstants {
102102
View.Name.create("bigtable.googleapis.com/internal/client/retry_count"),
103103
"The number of additional RPCs sent after the initial attempt.",
104104
RETRY_COUNT,
105-
AGGREGATION_RETRY_COUNT,
105+
AGGREGATION_COUNT,
106106
ImmutableList.of(
107107
PROJECT_ID,
108108
INSTANCE_ID,
@@ -154,7 +154,7 @@ class BuiltinViewConstants {
154154
View.Name.create("bigtable.googleapis.com/internal/client/connectivity_error_count"),
155155
"Number of requests that failed to reach the Google datacenter. (Requests without google response headers).",
156156
CONNECTIVITY_ERROR_COUNT,
157-
AGGREGATION_ERROR_COUNT,
157+
AGGREGATION_COUNT,
158158
ImmutableList.of(
159159
PROJECT_ID,
160160
INSTANCE_ID,
@@ -173,15 +173,7 @@ class BuiltinViewConstants {
173173
APPLICATION_LATENCIES,
174174
AGGREGATION_WITH_MILLIS_HISTOGRAM,
175175
ImmutableList.of(
176-
PROJECT_ID,
177-
INSTANCE_ID,
178-
APP_PROFILE,
179-
METHOD,
180-
STREAMING,
181-
CLIENT_NAME,
182-
CLUSTER,
183-
ZONE,
184-
TABLE));
176+
PROJECT_ID, INSTANCE_ID, APP_PROFILE, METHOD, CLIENT_NAME, CLUSTER, ZONE, TABLE));
185177

186178
static final View THROTTLING_LATENCIES_VIEW =
187179
View.create(

google-cloud-bigtable-stats/src/test/java/com/google/cloud/bigtable/stats/BigtableCreateTimeSeriesExporterTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void testTimeSeries() {
134134
assertThat(timeSeries.getMetric().getLabelsMap())
135135
.containsAtLeast(BuiltinMeasureConstants.APP_PROFILE.getName(), appProfileId);
136136
assertThat(timeSeries.getMetric().getLabelsMap())
137-
.containsKey(BuiltinMeasureConstants.CLIENT_ID.getName());
137+
.containsKey(BuiltinMeasureConstants.CLIENT_UID.getName());
138138

139139
assertThat(timeSeries.getPoints(0).getValue().getDoubleValue()).isEqualTo(fakeValue);
140140
}

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

+18-9
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public static EnhancedBigtableStubSettings finalizeSettings(
201201
ImmutableMap<String, String> builtinAttributes =
202202
ImmutableMap.<String, String>builder()
203203
.put("project_id", settings.getProjectId())
204-
.put("instance_id", settings.getInstanceId())
204+
.put("instance", settings.getInstanceId())
205205
.put("app_profile", settings.getAppProfileId())
206206
.build();
207207
// Inject Opencensus instrumentation
@@ -335,10 +335,13 @@ public <RowT> ServerStreamingCallable<Query, RowT> createReadRowsCallable(
335335
ServerStreamingCallable<Query, RowT> readRowsUserCallable =
336336
new ReadRowsUserCallable<>(readRowsCallable, requestContext);
337337

338+
ServerStreamingCallable<Query, RowT> withBigtableTracer =
339+
new BigtableTracerStreamingCallable<>(readRowsUserCallable);
340+
338341
SpanName span = getSpanName("ReadRows");
339342
ServerStreamingCallable<Query, RowT> traced =
340343
new TracedServerStreamingCallable<>(
341-
readRowsUserCallable, clientContext.getTracerFactory(), span);
344+
withBigtableTracer, clientContext.getTracerFactory(), span);
342345

343346
return traced.withDefaultCallContext(clientContext.getDefaultCallContext());
344347
}
@@ -367,10 +370,19 @@ public <RowT> UnaryCallable<Query, RowT> createReadRowCallable(RowAdapter<RowT>
367370
.build(),
368371
rowAdapter);
369372

370-
UnaryCallable<Query, RowT> readRowCallable =
371-
new ReadRowsUserCallable<>(readRowsCallable, requestContext).first();
373+
ServerStreamingCallable<Query, RowT> readRowCallable =
374+
new ReadRowsUserCallable<>(readRowsCallable, requestContext);
375+
376+
ServerStreamingCallable<Query, RowT> withBigtableTracer =
377+
new BigtableTracerStreamingCallable<>(readRowCallable);
378+
379+
SpanName span = getSpanName("ReadRow");
372380

373-
return createUserFacingUnaryCallable("ReadRow", readRowCallable);
381+
ServerStreamingCallable<Query, RowT> traced =
382+
new TracedServerStreamingCallable(
383+
withBigtableTracer, clientContext.getTracerFactory(), span);
384+
385+
return traced.first().withDefaultCallContext(clientContext.getDefaultCallContext());
374386
}
375387

376388
/**
@@ -432,13 +444,10 @@ public Map<String, String> extract(ReadRowsRequest readRowsRequest) {
432444
ServerStreamingCallable<ReadRowsRequest, RowT> watched =
433445
Callables.watched(merging, innerSettings, clientContext);
434446

435-
ServerStreamingCallable<ReadRowsRequest, RowT> withBigtableTracer =
436-
new BigtableTracerStreamingCallable<>(watched);
437-
438447
// Retry logic is split into 2 parts to workaround a rare edge case described in
439448
// ReadRowsRetryCompletedCallable
440449
ServerStreamingCallable<ReadRowsRequest, RowT> retrying1 =
441-
new ReadRowsRetryCompletedCallable<>(withBigtableTracer);
450+
new ReadRowsRetryCompletedCallable<>(watched);
442451

443452
ServerStreamingCallable<ReadRowsRequest, RowT> retrying2 =
444453
Callables.retrying(retrying1, innerSettings, clientContext);

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.api.gax.rpc.ServerStreamingCallable;
2323
import com.google.api.gax.rpc.StreamController;
2424
import com.google.bigtable.v2.ResponseParams;
25+
import com.google.common.base.MoreObjects;
2526
import com.google.common.base.Preconditions;
2627
import com.google.common.base.Stopwatch;
2728
import com.google.protobuf.InvalidProtocolBufferException;
@@ -104,8 +105,13 @@ public void onError(Throwable t) {
104105
Long latency = Util.getGfeLatency(metadata);
105106
tracer.recordGfeMetadata(latency, t);
106107
try {
108+
Metadata trailingMetadata = responseMetadata.getTrailingMetadata();
107109
byte[] trailers =
108-
metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER));
110+
MoreObjects.firstNonNull(
111+
metadata.get(
112+
Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)),
113+
trailingMetadata.get(
114+
Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)));
109115
ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers);
110116
tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId());
111117
} catch (NullPointerException | InvalidProtocolBufferException e) {
@@ -120,8 +126,13 @@ public void onComplete() {
120126
Long latency = Util.getGfeLatency(metadata);
121127
tracer.recordGfeMetadata(latency, null);
122128
try {
129+
Metadata trailingMetadata = responseMetadata.getTrailingMetadata();
123130
byte[] trailers =
124-
metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER));
131+
MoreObjects.firstNonNull(
132+
metadata.get(
133+
Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)),
134+
trailingMetadata.get(
135+
Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)));
125136
ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers);
126137
tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId());
127138
} catch (NullPointerException | InvalidProtocolBufferException e) {

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.api.gax.rpc.ApiCallContext;
2424
import com.google.api.gax.rpc.UnaryCallable;
2525
import com.google.bigtable.v2.ResponseParams;
26+
import com.google.common.base.MoreObjects;
2627
import com.google.common.base.Preconditions;
2728
import com.google.common.util.concurrent.MoreExecutors;
2829
import com.google.protobuf.InvalidProtocolBufferException;
@@ -83,8 +84,13 @@ public void onFailure(Throwable throwable) {
8384
Long latency = Util.getGfeLatency(metadata);
8485
tracer.recordGfeMetadata(latency, throwable);
8586
try {
87+
Metadata trailingMetadata = responseMetadata.getTrailingMetadata();
8688
byte[] trailers =
87-
metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER));
89+
MoreObjects.firstNonNull(
90+
metadata.get(
91+
Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)),
92+
trailingMetadata.get(
93+
Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)));
8894
ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers);
8995
tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId());
9096
} catch (NullPointerException | InvalidProtocolBufferException e) {
@@ -97,8 +103,13 @@ public void onSuccess(ResponseT response) {
97103
Long latency = Util.getGfeLatency(metadata);
98104
tracer.recordGfeMetadata(latency, null);
99105
try {
106+
Metadata trailingMetadata = responseMetadata.getTrailingMetadata();
100107
byte[] trailers =
101-
metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER));
108+
MoreObjects.firstNonNull(
109+
metadata.get(
110+
Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)),
111+
trailingMetadata.get(
112+
Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)));
102113
ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers);
103114
tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId());
104115
} catch (NullPointerException | InvalidProtocolBufferException e) {

0 commit comments

Comments
 (0)