Repository navigation
Expand file tree
/
Copy pathBuiltInMetricsConstant.java
More file actions
389 lines (347 loc) · 17.1 KB
/
Copy pathBuiltInMetricsConstant.java
File metadata and controls
389 lines (347 loc) · 17.1 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* Copyright 2024 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
*
* http://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.spanner;
import static com.google.cloud.spanner.XGoogSpannerRequestId.REQUEST_ID_HEADER_NAME;
import com.google.api.core.InternalApi;
import com.google.api.gax.tracing.OpenTelemetryMetricsRecorder;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.sdk.metrics.Aggregation;
import io.opentelemetry.sdk.metrics.ExplicitBucketHistogramOptions;
import io.opentelemetry.sdk.metrics.InstrumentSelector;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.View;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@InternalApi
public class BuiltInMetricsConstant {
public static final String METER_NAME = "spanner.googleapis.com/internal/client";
/**
* Instrument namespace used when client metrics are recorded on a caller-provided {@link
* io.opentelemetry.api.OpenTelemetry} via {@link CustomOpenTelemetryMetricsProvider}. This is
* deliberately distinct from the reserved {@code spanner.googleapis.com} namespace, so that
* re-exporting a caller-owned pipeline to Cloud Monitoring cannot conflict with the curated
* Spanner metric descriptors.
*/
public static final String CUSTOM_EXPORT_METER_NAME = "spanner/client";
public static final String GAX_METER_NAME = OpenTelemetryMetricsRecorder.GAX_METER_NAME;
public static final String GRPC_GCP_METER_NAME = "grpc-gcp";
static final String SPANNER_METER_NAME = "spanner-java";
static final String GRPC_METER_NAME = "grpc-java";
static final String GFE_LATENCIES_NAME = "gfe_latencies";
static final String AFE_LATENCIES_NAME = "afe_latencies";
static final String GFE_CONNECTIVITY_ERROR_NAME = "gfe_connectivity_error_count";
static final String AFE_CONNECTIVITY_ERROR_NAME = "afe_connectivity_error_count";
static final String OPERATION_LATENCIES_NAME = "operation_latencies";
static final String ATTEMPT_LATENCIES_NAME = "attempt_latencies";
static final String OPERATION_LATENCY_NAME = "operation_latency";
static final String ATTEMPT_LATENCY_NAME = "attempt_latency";
static final String OPERATION_COUNT_NAME = "operation_count";
static final String ATTEMPT_COUNT_NAME = "attempt_count";
static final String EEF_FALLBACK_COUNT_NAME = "eef.fallback_count";
static final String EEF_CALL_STATUS_NAME = "eef.call_status";
static final String CHANNEL_COUNT_NAME = "channel_pool.channel_count";
static final String CHANNEL_LOAD_NAME = "channel_pool.channel_load";
public static final Set<String> SPANNER_METRICS =
ImmutableSet.of(
OPERATION_LATENCIES_NAME,
ATTEMPT_LATENCIES_NAME,
OPERATION_COUNT_NAME,
ATTEMPT_COUNT_NAME,
GFE_LATENCIES_NAME,
AFE_LATENCIES_NAME,
GFE_CONNECTIVITY_ERROR_NAME,
AFE_CONNECTIVITY_ERROR_NAME)
.stream()
.map(m -> METER_NAME + '/' + m)
.collect(Collectors.toSet());
// The following attributes are optional and need to be enabled explicitly.
public static final String GRPC_LB_BACKEND_SERVICE_ATTRIBUTE = "grpc.lb.backend_service";
public static final String GRPC_LB_LOCALITY_ATTRIBUTE = "grpc.lb.locality";
public static final String GRPC_DISCONNECT_ERROR_ATTRIBUTE = "grpc.disconnect_error";
static final Set<String> GRPC_LB_RLS_ATTRIBUTES =
ImmutableSet.of("grpc.lb.rls.data_plane_target", "grpc.lb.pick_result");
static final Set<String> GRPC_CLIENT_ATTEMPT_STARTED_ATTRIBUTES =
ImmutableSet.of("grpc.method", "grpc.target");
static final Set<String> GRPC_SUBCHANNEL_DEFAULT_ATTRIBUTES =
ImmutableSet.of("grpc.target", GRPC_LB_BACKEND_SERVICE_ATTRIBUTE, GRPC_LB_LOCALITY_ATTRIBUTE);
static final Set<String> GRPC_SUBCHANNEL_DISCONNECTION_ATTRIBUTES =
ImmutableSet.of(
"grpc.target",
GRPC_LB_BACKEND_SERVICE_ATTRIBUTE,
GRPC_LB_LOCALITY_ATTRIBUTE,
GRPC_DISCONNECT_ERROR_ATTRIBUTE);
static final Set<String> GRPC_XDS_CLIENT_RESOURCE_UPDATE_ATTRIBUTES =
ImmutableSet.of("grpc.xds.resource_type");
// Additional gRPC attributes to enable.
static final Map<String, Set<String>> GRPC_METRIC_ADDITIONAL_ATTRIBUTES =
ImmutableMap.<String, Set<String>>builder()
.put("grpc.client.attempt.started", GRPC_CLIENT_ATTEMPT_STARTED_ATTRIBUTES)
.put("grpc.subchannel.open_connections", GRPC_SUBCHANNEL_DEFAULT_ATTRIBUTES)
.put("grpc.subchannel.disconnections", GRPC_SUBCHANNEL_DISCONNECTION_ATTRIBUTES)
.put("grpc.subchannel.connection_attempts_succeeded", GRPC_SUBCHANNEL_DEFAULT_ATTRIBUTES)
.put("grpc.subchannel.connection_attempts_failed", GRPC_SUBCHANNEL_DEFAULT_ATTRIBUTES)
.put("grpc.lb.rls.default_target_picks", GRPC_LB_RLS_ATTRIBUTES)
.put("grpc.lb.rls.target_picks", GRPC_LB_RLS_ATTRIBUTES)
.put(
"grpc.xds_client.resource_updates_invalid",
GRPC_XDS_CLIENT_RESOURCE_UPDATE_ATTRIBUTES)
.put("grpc.xds_client.resource_updates_valid", GRPC_XDS_CLIENT_RESOURCE_UPDATE_ATTRIBUTES)
.build();
static final Collection<String> GRPC_METRICS_TO_ENABLE =
ImmutableList.of(
"grpc.client.attempt.started",
"grpc.subchannel.open_connections",
"grpc.subchannel.disconnections",
"grpc.subchannel.connection_attempts_succeeded",
"grpc.subchannel.connection_attempts_failed",
"grpc.lb.rls.default_target_picks",
"grpc.lb.rls.target_picks",
"grpc.xds_client.server_failure",
"grpc.xds_client.resource_updates_invalid",
"grpc.xds_client.resource_updates_valid");
public static final AttributeKey<String> CHANNEL_NAME_KEY =
AttributeKey.stringKey("channel_name");
public static final AttributeKey<String> FROM_CHANNEL_NAME_KEY =
AttributeKey.stringKey("from_channel_name");
public static final AttributeKey<String> TO_CHANNEL_NAME_KEY =
AttributeKey.stringKey("to_channel_name");
public static final AttributeKey<String> STATUS_CODE_KEY = AttributeKey.stringKey("status_code");
static final Set<String> GRPC_GCP_EEF_FALLBACK_COUNT_ATTRIBUTES =
ImmutableSet.of(FROM_CHANNEL_NAME_KEY.getKey(), TO_CHANNEL_NAME_KEY.getKey());
static final Set<String> GRPC_GCP_EEF_CALL_STATUS_ATTRIBUTES =
ImmutableSet.of(CHANNEL_NAME_KEY.getKey(), STATUS_CODE_KEY.getKey());
public static final AttributeKey<String> LANE_KEY = AttributeKey.stringKey("lane");
public static final String LANE_DIRECTPATH = "directpath";
public static final String LANE_CLOUDPATH = "cloudpath";
static final AttributeKey<String> POOL_MODE_KEY = AttributeKey.stringKey("pool_mode");
static final AttributeKey<String> STATE_KEY = AttributeKey.stringKey("state");
static final AttributeKey<String> POOL_INDEX_KEY = AttributeKey.stringKey("pool_index");
static final Set<String> GRPC_GCP_CHANNEL_COUNT_ATTRIBUTES =
ImmutableSet.of(
POOL_MODE_KEY.getKey(), STATE_KEY.getKey(), LANE_KEY.getKey(), POOL_INDEX_KEY.getKey());
static final Set<String> GRPC_GCP_CHANNEL_LOAD_ATTRIBUTES =
ImmutableSet.of(POOL_MODE_KEY.getKey(), LANE_KEY.getKey());
static final Map<String, Set<String>> GRPC_GCP_METRIC_ADDITIONAL_ATTRIBUTES =
ImmutableMap.<String, Set<String>>builder()
.put(EEF_FALLBACK_COUNT_NAME, GRPC_GCP_EEF_FALLBACK_COUNT_ATTRIBUTES)
.put(EEF_CALL_STATUS_NAME, GRPC_GCP_EEF_CALL_STATUS_ATTRIBUTES)
.put(CHANNEL_COUNT_NAME, GRPC_GCP_CHANNEL_COUNT_ATTRIBUTES)
.put(CHANNEL_LOAD_NAME, GRPC_GCP_CHANNEL_LOAD_ATTRIBUTES)
.build();
static final Map<String, Aggregation> GRPC_GCP_METRIC_AGGREGATIONS =
ImmutableMap.of(CHANNEL_LOAD_NAME, Aggregation.defaultAggregation());
static final Collection<String> GRPC_GCP_METRICS_TO_ENABLE =
ImmutableList.of(
EEF_FALLBACK_COUNT_NAME, EEF_CALL_STATUS_NAME, CHANNEL_COUNT_NAME, CHANNEL_LOAD_NAME);
public static final String SPANNER_RESOURCE_TYPE = "spanner_instance_client";
public static final String UNDEFINED_PROJECT_ID = "undefined-project";
public static final AttributeKey<String> PROJECT_ID_KEY = AttributeKey.stringKey("project_id");
public static final AttributeKey<String> INSTANCE_ID_KEY = AttributeKey.stringKey("instance_id");
public static final AttributeKey<String> LOCATION_ID_KEY = AttributeKey.stringKey("location");
public static final AttributeKey<String> INSTANCE_CONFIG_ID_KEY =
AttributeKey.stringKey("instance_config");
public static final AttributeKey<String> CLIENT_HASH_KEY = AttributeKey.stringKey("client_hash");
// These metric labels will be promoted to the spanner monitored resource fields
public static final Set<AttributeKey<String>> SPANNER_PROMOTED_RESOURCE_LABELS =
ImmutableSet.of(INSTANCE_ID_KEY);
public static final AttributeKey<String> DATABASE_KEY = AttributeKey.stringKey("database");
public static final AttributeKey<String> CLIENT_UID_KEY = AttributeKey.stringKey("client_uid");
public static final AttributeKey<String> CLIENT_NAME_KEY = AttributeKey.stringKey("client_name");
public static final AttributeKey<String> METHOD_KEY = AttributeKey.stringKey("method");
public static final AttributeKey<String> STATUS_KEY = AttributeKey.stringKey("status");
public static final AttributeKey<String> DIRECT_PATH_ENABLED_KEY =
AttributeKey.stringKey("directpath_enabled");
public static final AttributeKey<String> DIRECT_PATH_USED_KEY =
AttributeKey.stringKey("directpath_used");
public static final AttributeKey<String> REQUEST_ID_KEY =
AttributeKey.stringKey(REQUEST_ID_HEADER_NAME);
public static Set<String> ALLOWED_EXEMPLARS_ATTRIBUTES =
new HashSet<>(Arrays.asList(REQUEST_ID_HEADER_NAME));
// IP address prefixes allocated for DirectPath backends.
public static final String DP_IPV6_PREFIX = "2001:4860:8040";
public static final String DP_IPV4_PREFIX = "34.126";
public static final Set<AttributeKey> COMMON_ATTRIBUTES =
ImmutableSet.of(
PROJECT_ID_KEY,
INSTANCE_ID_KEY,
LOCATION_ID_KEY,
INSTANCE_CONFIG_ID_KEY,
CLIENT_UID_KEY,
CLIENT_HASH_KEY,
METHOD_KEY,
STATUS_KEY,
DATABASE_KEY,
CLIENT_NAME_KEY,
DIRECT_PATH_ENABLED_KEY,
DIRECT_PATH_USED_KEY);
static List<Double> BUCKET_BOUNDARIES =
ImmutableList.of(
0.0, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0,
16.0, 17.0, 18.0, 19.0, 20.0, 25.0, 30.0, 40.0, 50.0, 65.0, 80.0, 100.0, 130.0, 160.0,
200.0, 250.0, 300.0, 400.0, 500.0, 650.0, 800.0, 1000.0, 2000.0, 5000.0, 10000.0, 20000.0,
50000.0, 100000.0, 200000.0, 400000.0, 800000.0, 1600000.0, 3200000.0);
static Aggregation AGGREGATION_WITH_MILLIS_HISTOGRAM =
Aggregation.explicitBucketHistogram(
ExplicitBucketHistogramOptions.builder().setBucketBoundaries(BUCKET_BOUNDARIES).build());
static final Collection<String> GRPC_METRICS_ENABLED_BY_DEFAULT =
ImmutableList.of(
"grpc.client.attempt.sent_total_compressed_message_size",
"grpc.client.attempt.rcvd_total_compressed_message_size",
"grpc.client.attempt.duration",
"grpc.client.call.duration");
static Map<InstrumentSelector, View> getAllViews() {
ImmutableMap.Builder<InstrumentSelector, View> views = ImmutableMap.builder();
addViews(views, METER_NAME);
defineGrpcGcpView(views);
return views.build();
}
/**
* Views for client metrics recorded on a caller-provided OpenTelemetry via {@link
* CustomOpenTelemetryMetricsProvider}. The instruments live in the {@link
* #CUSTOM_EXPORT_METER_NAME} namespace. The grpc-gcp DirectPath-fallback views are deliberately
* excluded: those metrics are only recorded on the Cloud Monitoring path, so registering their
* views on a custom sink would be dead configuration.
*/
static Map<InstrumentSelector, View> getCustomExportViews() {
ImmutableMap.Builder<InstrumentSelector, View> views = ImmutableMap.builder();
addViews(views, CUSTOM_EXPORT_METER_NAME);
return views.build();
}
private static void addViews(
ImmutableMap.Builder<InstrumentSelector, View> views, String metricPrefix) {
defineView(
views,
metricPrefix,
BuiltInMetricsConstant.GAX_METER_NAME,
BuiltInMetricsConstant.OPERATION_LATENCY_NAME,
BuiltInMetricsConstant.OPERATION_LATENCIES_NAME,
BuiltInMetricsConstant.AGGREGATION_WITH_MILLIS_HISTOGRAM,
InstrumentType.HISTOGRAM,
"ms");
defineView(
views,
metricPrefix,
BuiltInMetricsConstant.GAX_METER_NAME,
BuiltInMetricsConstant.ATTEMPT_LATENCY_NAME,
BuiltInMetricsConstant.ATTEMPT_LATENCIES_NAME,
BuiltInMetricsConstant.AGGREGATION_WITH_MILLIS_HISTOGRAM,
InstrumentType.HISTOGRAM,
"ms");
defineView(
views,
metricPrefix,
BuiltInMetricsConstant.GAX_METER_NAME,
BuiltInMetricsConstant.OPERATION_COUNT_NAME,
BuiltInMetricsConstant.OPERATION_COUNT_NAME,
Aggregation.sum(),
InstrumentType.COUNTER,
"1");
defineView(
views,
metricPrefix,
BuiltInMetricsConstant.GAX_METER_NAME,
BuiltInMetricsConstant.ATTEMPT_COUNT_NAME,
BuiltInMetricsConstant.ATTEMPT_COUNT_NAME,
Aggregation.sum(),
InstrumentType.COUNTER,
"1");
defineSpannerView(views);
defineGRPCView(views, metricPrefix);
}
private static Set<String> baseAttributesFilter() {
return BuiltInMetricsConstant.COMMON_ATTRIBUTES.stream()
.map(AttributeKey::getKey)
.collect(Collectors.toCollection(HashSet::new));
}
private static void defineView(
ImmutableMap.Builder<InstrumentSelector, View> viewMap,
String metricPrefix,
String meterName,
String metricName,
String metricViewName,
Aggregation aggregation,
InstrumentType type,
String unit) {
InstrumentSelector selector =
InstrumentSelector.builder()
.setName(metricPrefix + '/' + metricName)
.setMeterName(meterName)
.setType(type)
.setUnit(unit)
.build();
View view =
View.builder()
.setName(metricPrefix + '/' + metricViewName)
.setAggregation(aggregation)
.setAttributeFilter(baseAttributesFilter())
.build();
viewMap.put(selector, view);
}
private static void defineSpannerView(ImmutableMap.Builder<InstrumentSelector, View> viewMap) {
InstrumentSelector selector =
InstrumentSelector.builder()
.setMeterName(BuiltInMetricsConstant.SPANNER_METER_NAME)
.build();
View view = View.builder().setAttributeFilter(baseAttributesFilter()).build();
viewMap.put(selector, view);
}
private static void defineGRPCView(
ImmutableMap.Builder<InstrumentSelector, View> viewMap, String metricPrefix) {
for (String metric : BuiltInMetricsConstant.GRPC_METRICS_TO_ENABLE) {
InstrumentSelector selector =
InstrumentSelector.builder()
.setName(metric)
.setMeterName(BuiltInMetricsConstant.GRPC_METER_NAME)
.build();
Set<String> attributesFilter = baseAttributesFilter();
attributesFilter.addAll(
GRPC_METRIC_ADDITIONAL_ATTRIBUTES.getOrDefault(metric, ImmutableSet.of()));
View view =
View.builder()
.setName(metricPrefix + '/' + metric.replace(".", "/"))
.setAttributeFilter(attributesFilter)
.build();
viewMap.put(selector, view);
}
}
private static void defineGrpcGcpView(ImmutableMap.Builder<InstrumentSelector, View> viewMap) {
for (String metric : GRPC_GCP_METRICS_TO_ENABLE) {
InstrumentSelector selector =
InstrumentSelector.builder()
.setName(metric)
.setMeterName(BuiltInMetricsConstant.GRPC_GCP_METER_NAME)
.build();
Set<String> attributesFilter = baseAttributesFilter();
attributesFilter.addAll(
GRPC_GCP_METRIC_ADDITIONAL_ATTRIBUTES.getOrDefault(metric, ImmutableSet.of()));
View view =
View.builder()
.setName(BuiltInMetricsConstant.METER_NAME + '/' + metric.replace(".", "/"))
.setAggregation(GRPC_GCP_METRIC_AGGREGATIONS.getOrDefault(metric, Aggregation.sum()))
.setAttributeFilter(attributesFilter)
.build();
viewMap.put(selector, view);
}
}
}