-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBuiltInMetricsProvider.java
More file actions
314 lines (287 loc) · 12.4 KB
/
BuiltInMetricsProvider.java
File metadata and controls
314 lines (287 loc) · 12.4 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
/*
* 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.opentelemetry.detection.GCPPlatformDetector.SupportedPlatform.GOOGLE_KUBERNETES_ENGINE;
import static com.google.cloud.spanner.BuiltInMetricsConstant.CLIENT_HASH_KEY;
import static com.google.cloud.spanner.BuiltInMetricsConstant.CLIENT_NAME_KEY;
import static com.google.cloud.spanner.BuiltInMetricsConstant.CLIENT_UID_KEY;
import static com.google.cloud.spanner.BuiltInMetricsConstant.INSTANCE_CONFIG_ID_KEY;
import static com.google.cloud.spanner.BuiltInMetricsConstant.INSTANCE_ID_KEY;
import static com.google.cloud.spanner.BuiltInMetricsConstant.LOCATION_ID_KEY;
import static com.google.cloud.spanner.BuiltInMetricsConstant.PROJECT_ID_KEY;
import com.google.api.core.ApiFunction;
import com.google.api.gax.core.GaxProperties;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.auth.Credentials;
import com.google.cloud.opentelemetry.detection.AttributeKeys;
import com.google.cloud.opentelemetry.detection.DetectedPlatform;
import com.google.cloud.opentelemetry.detection.GCPPlatformDetector;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import io.grpc.ManagedChannelBuilder;
import io.grpc.opentelemetry.GrpcOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
import io.opentelemetry.sdk.resources.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
final class BuiltInMetricsProvider {
static BuiltInMetricsProvider INSTANCE = new BuiltInMetricsProvider();
private static final Logger logger = Logger.getLogger(BuiltInMetricsProvider.class.getName());
private static String taskId;
private static String location;
private static final String default_location = "global";
private OpenTelemetry openTelemetry;
private String projectId;
private boolean mismatchedProjectIdLogged;
private BuiltInMetricsProvider() {}
synchronized OpenTelemetry getOrCreateOpenTelemetry(
String projectId,
@Nullable Credentials credentials,
@Nullable String monitoringHost,
String universeDomain) {
try {
if (this.openTelemetry == null) {
SdkMeterProviderBuilder sdkMeterProviderBuilder = SdkMeterProvider.builder();
BuiltInMetricsView.registerBuiltinMetrics(
SpannerCloudMonitoringExporter.create(
this::getProjectId, credentials, monitoringHost, universeDomain),
sdkMeterProviderBuilder);
sdkMeterProviderBuilder.setResource(Resource.create(createResourceAttributes(projectId)));
SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.build();
this.openTelemetry = OpenTelemetrySdk.builder().setMeterProvider(sdkMeterProvider).build();
Runtime.getRuntime().addShutdownHook(new Thread(sdkMeterProvider::close));
}
return this.openTelemetry;
} catch (IOException ex) {
logger.log(
Level.WARNING,
"Unable to get OpenTelemetry object for client side metrics, will skip exporting client"
+ " side metrics",
ex);
return null;
}
}
synchronized void setProjectIdIfAbsent(String projectId) {
if (this.projectId == null) {
this.projectId = projectId;
} else if (!this.projectId.equals(projectId) && !mismatchedProjectIdLogged) {
mismatchedProjectIdLogged = true;
logger.log(
Level.WARNING,
"Built-in metrics are already initialized for project {0}. Metrics for project {1} will"
+ " be exported using the existing project.",
new Object[] {this.projectId, projectId});
}
}
@Nullable
synchronized OpenTelemetry getOpenTelemetry() {
return this.openTelemetry;
}
@VisibleForTesting
synchronized String getProjectId() {
return this.projectId;
}
@VisibleForTesting
synchronized void reset() {
if (this.openTelemetry instanceof OpenTelemetrySdk) {
((OpenTelemetrySdk) this.openTelemetry).getSdkMeterProvider().close();
}
this.openTelemetry = null;
this.projectId = null;
this.mismatchedProjectIdLogged = false;
}
// TODO: Remove when
// https://github.com/GoogleCloudPlatform/opentelemetry-operations-java/issues/421
// has been fixed.
static boolean quickCheckIsRunningOnGcp() {
int timeout = 5000;
try {
timeout =
Integer.parseInt(System.getProperty("spanner.check_is_running_on_gcp_timeout", "5000"));
} catch (NumberFormatException ignore) {
// ignore
}
try {
URL url = new URL("http://metadata.google.internal/computeMetadata/v1/project/project-id");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(timeout);
connection.setRequestProperty("Metadata-Flavor", "Google");
if (connection.getResponseCode() == 200
&& ("Google").equals(connection.getHeaderField("Metadata-Flavor"))) {
InputStream input = connection.getInputStream();
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
return !Strings.isNullOrEmpty(reader.readLine());
}
}
} catch (IOException ignore) {
// ignore
}
return false;
}
void enableGrpcMetrics(
InstantiatingGrpcChannelProvider.Builder channelProviderBuilder,
String projectId,
@Nullable Credentials credentials,
@Nullable String monitoringHost,
String universeDomain) {
GrpcOpenTelemetry grpcOpenTelemetry =
GrpcOpenTelemetry.newBuilder()
.sdk(
this.getOrCreateOpenTelemetry(
projectId, credentials, monitoringHost, universeDomain))
.enableMetrics(BuiltInMetricsConstant.GRPC_METRICS_TO_ENABLE)
// Disable gRPCs default metrics as they are not needed for Spanner.
.disableMetrics(BuiltInMetricsConstant.GRPC_METRICS_ENABLED_BY_DEFAULT)
.addOptionalLabel(BuiltInMetricsConstant.GRPC_LB_BACKEND_SERVICE_ATTRIBUTE)
.addOptionalLabel(BuiltInMetricsConstant.GRPC_LB_LOCALITY_ATTRIBUTE)
.addOptionalLabel(BuiltInMetricsConstant.GRPC_DISCONNECT_ERROR_ATTRIBUTE)
.build();
ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> channelConfigurator =
channelProviderBuilder.getChannelConfigurator();
channelProviderBuilder.setChannelConfigurator(
b -> {
grpcOpenTelemetry.configureChannelBuilder(b);
if (channelConfigurator != null) {
return channelConfigurator.apply(b);
}
return b;
});
}
Attributes createResourceAttributes(String projectId) {
AttributesBuilder attributesBuilder =
Attributes.builder()
.put(PROJECT_ID_KEY.getKey(), projectId)
.put(INSTANCE_CONFIG_ID_KEY.getKey(), "unknown")
.put(CLIENT_HASH_KEY.getKey(), generateClientHash(getDefaultTaskValue()))
.put(INSTANCE_ID_KEY.getKey(), "unknown")
.put(LOCATION_ID_KEY.getKey(), detectClientLocation());
return attributesBuilder.build();
}
Map<String, String> createClientAttributes() {
Map<String, String> clientAttributes = new HashMap<>();
clientAttributes.put(
CLIENT_NAME_KEY.getKey(), "spanner-java/" + GaxProperties.getLibraryVersion(getClass()));
clientAttributes.put(CLIENT_UID_KEY.getKey(), getDefaultTaskValue());
return clientAttributes;
}
/**
* Generates a 6-digit zero-padded all lower case hexadecimal representation of hash of the
* accounting group. The hash utilizes the 10 most significant bits of the value returned by
* `Hashing.goodFastHash(64).hashBytes()`, so effectively the returned values are uniformly
* distributed in the range [000000, 0003ff].
*
* <p>The primary purpose of this function is to generate a hash value for the `client_hash`
* resource label using `client_uid` metric field. The range of values is chosen to be small
* enough to keep the cardinality of the Resource targets under control. Note: If at later time
* the range needs to be increased, it can be done by increasing the value of `kPrefixLength` to
* up to 24 bits without changing the format of the returned value.
*
* @return Returns a 6-digit zero-padded all lower case hexadecimal representation of hash of the
* accounting group.
*/
static String generateClientHash(String clientUid) {
if (clientUid == null) {
return "000000";
}
HashFunction hashFunction = Hashing.goodFastHash(64);
Long hash = hashFunction.hashBytes(clientUid.getBytes()).asLong();
// Don't change this value without reading above comment
int kPrefixLength = 10;
long shiftedValue = hash >>> (64 - kPrefixLength);
return String.format("%06x", shiftedValue);
}
static String detectClientLocation() {
if (location == null) {
location = default_location;
if (quickCheckIsRunningOnGcp()) {
GCPPlatformDetector detector = GCPPlatformDetector.DEFAULT_INSTANCE;
DetectedPlatform detectedPlatform = detector.detectPlatform();
// All platform except GKE uses "cloud_region" for region attribute.
String region = detectedPlatform.getAttributes().get("cloud_region");
if (detectedPlatform.getSupportedPlatform() == GOOGLE_KUBERNETES_ENGINE) {
region = detectedPlatform.getAttributes().get(AttributeKeys.GKE_CLUSTER_LOCATION);
}
location = region == null ? location : region;
}
}
return location;
}
/**
* Generates a unique identifier for the Client_uid metric field. The identifier is composed of a
* UUID, the process ID (PID), and the hostname.
*
* <p>For Java 9 and later, the PID is obtained using the ProcessHandle API. For Java 8, the PID
* is extracted from ManagementFactory.getRuntimeMXBean().getName().
*
* @return A unique identifier string in the format UUID@PID@hostname
*/
private static String getDefaultTaskValue() {
if (taskId == null) {
String identifier = UUID.randomUUID().toString();
String pid = getProcessId();
try {
String hostname = InetAddress.getLocalHost().getHostName();
taskId = identifier + "@" + pid + "@" + hostname;
} catch (UnknownHostException e) {
logger.log(Level.INFO, "Unable to get the hostname.", e);
taskId = identifier + "@" + pid + "@localhost";
}
}
return taskId;
}
private static String getProcessId() {
try {
// Check if Java 9+ and ProcessHandle class is available
Class<?> processHandleClass = Class.forName("java.lang.ProcessHandle");
Method currentMethod = processHandleClass.getMethod("current");
Object processHandleInstance = currentMethod.invoke(null);
Method pidMethod = processHandleClass.getMethod("pid");
long pid = (long) pidMethod.invoke(processHandleInstance);
return Long.toString(pid);
} catch (Exception e) {
// Fallback to Java 8 method
final String jvmName = ManagementFactory.getRuntimeMXBean().getName();
if (jvmName != null && jvmName.contains("@")) {
return jvmName.split("@")[0];
} else {
return "unknown";
}
}
}
}