3232import com .google .cloud .opentelemetry .detection .AttributeKeys ;
3333import com .google .cloud .opentelemetry .detection .DetectedPlatform ;
3434import com .google .cloud .opentelemetry .detection .GCPPlatformDetector ;
35+ import com .google .common .base .Strings ;
3536import com .google .common .hash .HashFunction ;
3637import com .google .common .hash .Hashing ;
3738import io .grpc .ManagedChannelBuilder ;
4344import io .opentelemetry .sdk .metrics .SdkMeterProvider ;
4445import io .opentelemetry .sdk .metrics .SdkMeterProviderBuilder ;
4546import io .opentelemetry .sdk .resources .Resource ;
47+ import java .io .BufferedReader ;
4648import java .io .IOException ;
49+ import java .io .InputStream ;
50+ import java .io .InputStreamReader ;
4751import java .lang .management .ManagementFactory ;
4852import java .lang .reflect .Method ;
53+ import java .net .HttpURLConnection ;
4954import java .net .InetAddress ;
55+ import java .net .URL ;
5056import java .net .UnknownHostException ;
57+ import java .nio .charset .StandardCharsets ;
5158import java .util .HashMap ;
5259import java .util .Map ;
5360import 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