Skip to content

Commit b9d7daf

Browse files
olavloitesurbhigarg92cloud-java-bot
authored
fix: do a quick check if the application runs on GCP (#4163)
* fix: do a quick check if the application runs on GCP The check whether the application runs on GCP did not use a timeout. This introduces a default 500ms timeout for that check. Fixes googleapis/java-spanner-jdbc#2250 * make location static and fetch it only once * chore: generate libraries at Mon Oct 13 07:06:03 UTC 2025 * changing the timeout to 5 sec * review comments --------- Co-authored-by: Surbhi Garg <gargsurbhi@google.com> Co-authored-by: surbhigarg92 <surbhigarg.92@gmail.com> Co-authored-by: cloud-java-bot <cloud-java-bot@google.com>
1 parent 02a17c6 commit b9d7daf

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

.github/workflows/update_generation_config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
# the branch into which the pull request is merged
2727
base_branch: main
2828
steps:
29-
- uses: actions/checkout@v5
29+
- uses: actions/checkout@v4
3030
with:
3131
fetch-depth: 0
3232
token: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/BuiltInMetricsProvider.java

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.cloud.opentelemetry.detection.AttributeKeys;
3333
import com.google.cloud.opentelemetry.detection.DetectedPlatform;
3434
import com.google.cloud.opentelemetry.detection.GCPPlatformDetector;
35+
import com.google.common.base.Strings;
3536
import com.google.common.hash.HashFunction;
3637
import com.google.common.hash.Hashing;
3738
import io.grpc.ManagedChannelBuilder;
@@ -43,11 +44,17 @@
4344
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
4445
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
4546
import io.opentelemetry.sdk.resources.Resource;
47+
import java.io.BufferedReader;
4648
import java.io.IOException;
49+
import java.io.InputStream;
50+
import java.io.InputStreamReader;
4751
import java.lang.management.ManagementFactory;
4852
import java.lang.reflect.Method;
53+
import java.net.HttpURLConnection;
4954
import java.net.InetAddress;
55+
import java.net.URL;
5056
import java.net.UnknownHostException;
57+
import java.nio.charset.StandardCharsets;
5158
import java.util.HashMap;
5259
import java.util.Map;
5360
import java.util.UUID;
@@ -63,6 +70,10 @@ final class BuiltInMetricsProvider {
6370

6471
private static String taskId;
6572

73+
private static String location;
74+
75+
private static final String default_location = "global";
76+
6677
private OpenTelemetry openTelemetry;
6778

6879
private BuiltInMetricsProvider() {}
@@ -95,6 +106,36 @@ OpenTelemetry getOrCreateOpenTelemetry(
95106
}
96107
}
97108

109+
// TODO: Remove when
110+
// https://github.com/GoogleCloudPlatform/opentelemetry-operations-java/issues/421
111+
// has been fixed.
112+
static boolean quickCheckIsRunningOnGcp() {
113+
int timeout = 5000;
114+
try {
115+
timeout =
116+
Integer.parseInt(System.getProperty("spanner.check_is_running_on_gcp_timeout", "5000"));
117+
} catch (NumberFormatException ignore) {
118+
// ignore
119+
}
120+
try {
121+
URL url = new URL("http://metadata.google.internal/computeMetadata/v1/project/project-id");
122+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
123+
connection.setConnectTimeout(timeout);
124+
connection.setRequestProperty("Metadata-Flavor", "Google");
125+
if (connection.getResponseCode() == 200
126+
&& ("Google").equals(connection.getHeaderField("Metadata-Flavor"))) {
127+
InputStream input = connection.getInputStream();
128+
try (BufferedReader reader =
129+
new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
130+
return !Strings.isNullOrEmpty(reader.readLine());
131+
}
132+
}
133+
} catch (IOException ignore) {
134+
// ignore
135+
}
136+
return false;
137+
}
138+
98139
void enableGrpcMetrics(
99140
InstantiatingGrpcChannelProvider.Builder channelProviderBuilder,
100141
String projectId,
@@ -171,14 +212,20 @@ static String generateClientHash(String clientUid) {
171212
}
172213

173214
static String detectClientLocation() {
174-
GCPPlatformDetector detector = GCPPlatformDetector.DEFAULT_INSTANCE;
175-
DetectedPlatform detectedPlatform = detector.detectPlatform();
176-
// All platform except GKE uses "cloud_region" for region attribute.
177-
String region = detectedPlatform.getAttributes().get("cloud_region");
178-
if (detectedPlatform.getSupportedPlatform() == GOOGLE_KUBERNETES_ENGINE) {
179-
region = detectedPlatform.getAttributes().get(AttributeKeys.GKE_CLUSTER_LOCATION);
215+
if (location == null) {
216+
location = default_location;
217+
if (quickCheckIsRunningOnGcp()) {
218+
GCPPlatformDetector detector = GCPPlatformDetector.DEFAULT_INSTANCE;
219+
DetectedPlatform detectedPlatform = detector.detectPlatform();
220+
// All platform except GKE uses "cloud_region" for region attribute.
221+
String region = detectedPlatform.getAttributes().get("cloud_region");
222+
if (detectedPlatform.getSupportedPlatform() == GOOGLE_KUBERNETES_ENGINE) {
223+
region = detectedPlatform.getAttributes().get(AttributeKeys.GKE_CLUSTER_LOCATION);
224+
}
225+
location = region == null ? location : region;
226+
}
180227
}
181-
return region == null ? "global" : region;
228+
return location;
182229
}
183230

184231
/**

0 commit comments

Comments
 (0)