-
Couldn't load subscription status.
- Fork 136
chore: client metrics #3125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rahul2393
merged 9 commits into
googleapis:main
from
surbhigarg92:client_builtin_metrics
Sep 19, 2024
Merged
chore: client metrics #3125
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3b64f56
feat: client metrics
surbhigarg92 cedbb61
Review comments
surbhigarg92 a7aeb57
Few issues and code optimisations
surbhigarg92 a6532a5
review comments
surbhigarg92 eeed7d3
directpath_used attribute
surbhigarg92 e8a587f
removed directpath enabled attribute
surbhigarg92 b2f9361
removed directpath enabled attribute
surbhigarg92 cf8e92d
lint
surbhigarg92 8259d03
bucket boundaries
surbhigarg92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
144 changes: 144 additions & 0 deletions
144
...d-spanner/src/main/java/com/google/cloud/spanner/BuiltInOpenTelemetryMetricsProvider.java
This file contains hidden or 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,144 @@ | ||
| /* | ||
| * 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_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.LOCATION_ID_KEY; | ||
| import static com.google.cloud.spanner.BuiltInMetricsConstant.PROJECT_ID_KEY; | ||
|
|
||
| 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 io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
| import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
| import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; | ||
| import java.io.IOException; | ||
| import java.lang.management.ManagementFactory; | ||
| import java.lang.reflect.Method; | ||
| import java.net.InetAddress; | ||
| import java.net.UnknownHostException; | ||
| 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 BuiltInOpenTelemetryMetricsProvider { | ||
|
|
||
| static BuiltInOpenTelemetryMetricsProvider INSTANCE = new BuiltInOpenTelemetryMetricsProvider(); | ||
|
|
||
| private static final Logger logger = | ||
| Logger.getLogger(BuiltInOpenTelemetryMetricsProvider.class.getName()); | ||
|
|
||
| private static String taskId; | ||
|
|
||
| private OpenTelemetry openTelemetry; | ||
|
|
||
| private BuiltInOpenTelemetryMetricsProvider() {} | ||
|
|
||
| OpenTelemetry getOrCreateOpenTelemetry(String projectId, @Nullable Credentials credentials) { | ||
| try { | ||
| if (this.openTelemetry == null) { | ||
| SdkMeterProviderBuilder sdkMeterProviderBuilder = SdkMeterProvider.builder(); | ||
| BuiltInOpenTelemetryMetricsView.registerBuiltinMetrics( | ||
| SpannerCloudMonitoringExporter.create(projectId, credentials), sdkMeterProviderBuilder); | ||
| this.openTelemetry = | ||
| OpenTelemetrySdk.builder().setMeterProvider(sdkMeterProviderBuilder.build()).build(); | ||
| } | ||
| 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; | ||
surbhigarg92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| Map<String, String> createClientAttributes(String projectId, String client_name) { | ||
| Map<String, String> clientAttributes = new HashMap<>(); | ||
| clientAttributes.put(LOCATION_ID_KEY.getKey(), detectClientLocation()); | ||
| clientAttributes.put(PROJECT_ID_KEY.getKey(), projectId); | ||
| // TODO: Replace this with real value. | ||
| clientAttributes.put(INSTANCE_CONFIG_ID_KEY.getKey(), "unknown"); | ||
| clientAttributes.put(CLIENT_NAME_KEY.getKey(), client_name); | ||
| clientAttributes.put(CLIENT_UID_KEY.getKey(), getDefaultTaskValue()); | ||
| return clientAttributes; | ||
| } | ||
|
|
||
| static String detectClientLocation() { | ||
| 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_LOCATION_TYPE_REGION); | ||
| } | ||
| return region == null ? "global" : region; | ||
| } | ||
|
|
||
| /** | ||
| * 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"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
...cloud-spanner/src/main/java/com/google/cloud/spanner/BuiltInOpenTelemetryMetricsView.java
This file contains hidden or 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,33 @@ | ||
| /* | ||
| * 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 io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; | ||
| import io.opentelemetry.sdk.metrics.export.MetricExporter; | ||
| import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; | ||
|
|
||
| class BuiltInOpenTelemetryMetricsView { | ||
|
|
||
| private BuiltInOpenTelemetryMetricsView() {} | ||
|
|
||
| /** Register built-in metrics on the {@link SdkMeterProviderBuilder} with credentials. */ | ||
| static void registerBuiltinMetrics( | ||
| MetricExporter metricExporter, SdkMeterProviderBuilder builder) { | ||
| BuiltInMetricsConstant.getAllViews().forEach(builder::registerView); | ||
| builder.registerMetricReader(PeriodicMetricReader.create(metricExporter)); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.