Skip to content

Commit 23f2c85

Browse files
committed
Merge branch 'main' into PR #3928 to update
2 parents 06a133f + 94d0474 commit 23f2c85

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed

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
/**

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/CredentialsServiceTest.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
import com.google.auth.oauth2.ServiceAccountCredentials;
2626
import com.google.cloud.spanner.ErrorCode;
2727
import com.google.cloud.spanner.SpannerException;
28-
import java.io.FileInputStream;
28+
import java.io.File;
2929
import java.io.IOException;
30+
import java.nio.file.Files;
3031
import org.junit.Test;
3132
import org.junit.runner.RunWith;
3233
import org.junit.runners.JUnit4;
@@ -35,9 +36,9 @@
3536
@RunWith(JUnit4.class)
3637
public class CredentialsServiceTest {
3738
private static final String FILE_TEST_PATH =
38-
CredentialsServiceTest.class.getResource("test-key.json").getFile();
39-
private static final String APP_DEFAULT_FILE_TEST_PATH =
40-
CredentialsServiceTest.class.getResource("test-key-app-default.json").getFile();
39+
CredentialsServiceTest.class.getResource("test-key.json").getPath();
40+
private static final String SA_APP_DEFAULT_FILE_TEST_PATH =
41+
CredentialsServiceTest.class.getResource("test-key-app-default.json").getPath();
4142

4243
private static final String TEST_PROJECT_ID = "test-project";
4344
private static final String APP_DEFAULT_PROJECT_ID = "app-default-test-project";
@@ -49,7 +50,11 @@ public class CredentialsServiceTest {
4950
GoogleCredentials internalGetApplicationDefault() throws IOException {
5051
// Read application default credentials directly from a specific file instead of actually
5152
// fetching the default from the environment.
52-
return GoogleCredentials.fromStream(new FileInputStream(APP_DEFAULT_FILE_TEST_PATH));
53+
return ServiceAccountCredentials.fromStream(
54+
// Calling `getResource().getPath()` on Windows returns a string that might start with
55+
// something like `/C:/...`. Paths.get() interprets the leading / as part of the path
56+
// and would be invalid. Use `new File().toPath()` to read from these files.
57+
Files.newInputStream(new File(SA_APP_DEFAULT_FILE_TEST_PATH).toPath()));
5358
}
5459
};
5560

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITVPCNegativeTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import com.google.common.base.Strings;
4949
import com.google.longrunning.OperationsClient;
5050
import com.google.longrunning.OperationsSettings;
51-
import java.io.FileInputStream;
5251
import java.io.IOException;
5352
import java.util.ArrayList;
5453
import java.util.Collections;
@@ -352,9 +351,7 @@ public void deniedListBackupOperations() throws IOException {
352351
.setTransportChannelProvider(InstantiatingGrpcChannelProvider.newBuilder().build())
353352
.setEndpoint("spanner.googleapis.com:443")
354353
.setCredentialsProvider(
355-
FixedCredentialsProvider.create(
356-
GoogleCredentials.fromStream(
357-
new FileInputStream(System.getenv("GOOGLE_APPLICATION_CREDENTIALS")))))
354+
FixedCredentialsProvider.create(GoogleCredentials.getApplicationDefault()))
358355
.build())) {
359356
client.listOperations(backupId.getName() + "/operations", "");
360357
fail("Expected PermissionDeniedException");

0 commit comments

Comments
 (0)