@@ -70,6 +70,10 @@ final class BuiltInMetricsProvider {
7070
7171 private static String taskId ;
7272
73+ private static String location ;
74+
75+ private static final String default_location = "global" ;
76+
7377 private OpenTelemetry openTelemetry ;
7478
7579 private BuiltInMetricsProvider () {}
@@ -86,9 +90,7 @@ OpenTelemetry getOrCreateOpenTelemetry(
8690 SpannerCloudMonitoringExporter .create (
8791 projectId , credentials , monitoringHost , universeDomain ),
8892 sdkMeterProviderBuilder );
89- String location = quickCheckIsRunningOnGcp () ? null : "global" ;
90- sdkMeterProviderBuilder .setResource (
91- Resource .create (createResourceAttributes (projectId , location )));
93+ sdkMeterProviderBuilder .setResource (Resource .create (createResourceAttributes (projectId )));
9294 SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder .build ();
9395 this .openTelemetry = OpenTelemetrySdk .builder ().setMeterProvider (sdkMeterProvider ).build ();
9496 Runtime .getRuntime ().addShutdownHook (new Thread (sdkMeterProvider ::close ));
@@ -161,14 +163,14 @@ void enableGrpcMetrics(
161163 });
162164 }
163165
164- Attributes createResourceAttributes (String projectId , String location ) {
166+ Attributes createResourceAttributes (String projectId ) {
165167 AttributesBuilder attributesBuilder =
166168 Attributes .builder ()
167169 .put (PROJECT_ID_KEY .getKey (), projectId )
168170 .put (INSTANCE_CONFIG_ID_KEY .getKey (), "unknown" )
169171 .put (CLIENT_HASH_KEY .getKey (), generateClientHash (getDefaultTaskValue ()))
170172 .put (INSTANCE_ID_KEY .getKey (), "unknown" )
171- .put (LOCATION_ID_KEY .getKey (), location == null ? detectClientLocation () : location );
173+ .put (LOCATION_ID_KEY .getKey (), detectClientLocation ());
172174
173175 return attributesBuilder .build ();
174176 }
@@ -210,14 +212,21 @@ static String generateClientHash(String clientUid) {
210212 }
211213
212214 static String detectClientLocation () {
213- GCPPlatformDetector detector = GCPPlatformDetector .DEFAULT_INSTANCE ;
214- DetectedPlatform detectedPlatform = detector .detectPlatform ();
215- // All platform except GKE uses "cloud_region" for region attribute.
216- String region = detectedPlatform .getAttributes ().get ("cloud_region" );
217- if (detectedPlatform .getSupportedPlatform () == GOOGLE_KUBERNETES_ENGINE ) {
218- 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+ return region == null ? location : region ;
226+ }
227+ return location ;
219228 }
220- return region == null ? "global" : region ;
229+ return location ;
221230 }
222231
223232 /**
0 commit comments