Skip to content

Commit aa201df

Browse files
authored
Support cloud run jobs in resource detection (#371)
* update sample to wait for shutdown * Add support for detecting Cloud Run Job * Add tests for Google Cloud Run Job * Style fix
1 parent eab8619 commit aa201df

File tree

5 files changed

+114
-5
lines changed

5 files changed

+114
-5
lines changed

detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detection/AttributeKeys.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public final class AttributeKeys {
5252
public static final String SERVERLESS_COMPUTE_CLOUD_REGION = AttributeKeys.CLOUD_REGION;
5353
public static final String SERVERLESS_COMPUTE_INSTANCE_ID = AttributeKeys.INSTANCE_ID;
5454

55+
// Cloud Run Job Specific Attributes
56+
public static final String GCR_JOB_EXECUTION_KEY = "gcr_job_execution_key";
57+
public static final String GCR_JOB_TASK_INDEX = "gcr_job_task_index";
58+
5559
static final String AVAILABILITY_ZONE = "availability_zone";
5660
static final String CLOUD_REGION = "cloud_region";
5761
static final String INSTANCE_ID = "instance_id";

detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detection/GCPPlatformDetector.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ private SupportedPlatform detectSupportedPlatform() {
5353
return SupportedPlatform.GOOGLE_CLOUD_RUN;
5454
} else if (environmentVariables.get("FUNCTION_TARGET") != null) {
5555
return SupportedPlatform.GOOGLE_CLOUD_FUNCTIONS;
56+
} else if (environmentVariables.get("CLOUD_RUN_JOB") != null) {
57+
return SupportedPlatform.GOOGLE_CLOUD_RUN_JOB;
5658
} else if (environmentVariables.get("GAE_SERVICE") != null) {
5759
return SupportedPlatform.GOOGLE_APP_ENGINE;
5860
}
@@ -75,6 +77,9 @@ private DetectedPlatform generateDetectedPlatform(SupportedPlatform platform) {
7577
case GOOGLE_CLOUD_FUNCTIONS:
7678
detectedPlatform = new GoogleCloudFunction(environmentVariables, metadataConfig);
7779
break;
80+
case GOOGLE_CLOUD_RUN_JOB:
81+
detectedPlatform = new GoogleCloudRunJob(environmentVariables, metadataConfig);
82+
break;
7883
case GOOGLE_APP_ENGINE:
7984
detectedPlatform = new GoogleAppEngine(environmentVariables, metadataConfig);
8085
break;
@@ -98,8 +103,10 @@ public enum SupportedPlatform {
98103
GOOGLE_KUBERNETES_ENGINE,
99104
/** Represents the Google App Engine platform. Could either be flex or standard. */
100105
GOOGLE_APP_ENGINE,
101-
/** Represents the Google Cloud Run platform. */
106+
/** Represents the Google Cloud Run platform (Service). */
102107
GOOGLE_CLOUD_RUN,
108+
/** Represents the Google Cloud Run platform (Jobs). */
109+
GOOGLE_CLOUD_RUN_JOB,
103110
/** Represents the Google Cloud Functions platform. */
104111
GOOGLE_CLOUD_FUNCTIONS,
105112
/** Represents the case when the application is not running on GCP. */
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.opentelemetry.detection;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
final class GoogleCloudRunJob implements DetectedPlatform {
22+
private final GCPMetadataConfig metadataConfig;
23+
private final EnvironmentVariables environmentVariables;
24+
private final Map<String, String> availableAttributes;
25+
26+
GoogleCloudRunJob(EnvironmentVariables environmentVariables, GCPMetadataConfig metadataConfig) {
27+
this.metadataConfig = metadataConfig;
28+
this.environmentVariables = environmentVariables;
29+
this.availableAttributes = prepareAttributes();
30+
}
31+
32+
private Map<String, String> prepareAttributes() {
33+
Map<String, String> map = new HashMap<>();
34+
map.put(AttributeKeys.SERVERLESS_COMPUTE_NAME, this.environmentVariables.get("CLOUD_RUN_JOB"));
35+
map.put(
36+
AttributeKeys.GCR_JOB_EXECUTION_KEY, this.environmentVariables.get("CLOUD_RUN_EXECUTION"));
37+
map.put(
38+
AttributeKeys.GCR_JOB_TASK_INDEX, this.environmentVariables.get("CLOUD_RUN_TASK_INDEX"));
39+
map.put(AttributeKeys.SERVERLESS_COMPUTE_INSTANCE_ID, this.metadataConfig.getInstanceId());
40+
map.put(AttributeKeys.SERVERLESS_COMPUTE_CLOUD_REGION, this.metadataConfig.getRegionFromZone());
41+
return map;
42+
}
43+
44+
@Override
45+
public GCPPlatformDetector.SupportedPlatform getSupportedPlatform() {
46+
return GCPPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_JOB;
47+
}
48+
49+
@Override
50+
public String getProjectId() {
51+
return metadataConfig.getProjectId();
52+
}
53+
54+
@Override
55+
public Map<String, String> getAttributes() {
56+
return this.availableAttributes;
57+
}
58+
}

detectors/resources-support/src/test/java/com/google/cloud/opentelemetry/detection/GCPPlatformDetectorTest.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,10 @@ public void testGCFDetectionWhenGCRAttributesPresent() {
274274
detector.detectPlatform().getAttributes());
275275
}
276276

277-
/** Google Cloud Run Tests * */
277+
/** Google Cloud Run Tests (Service) * */
278278
@Test
279279
public void testGCFResourceWithCloudRunAttributesSucceeds() {
280-
// Setup GCR required env vars
280+
// Setup GCR service required env vars
281281
envVars.put("K_SERVICE", "cloud-run-hello");
282282
envVars.put("K_REVISION", "cloud-run-hello.1");
283283
envVars.put("K_CONFIGURATION", "cloud-run-hello");
@@ -306,6 +306,37 @@ public void testGCFResourceWithCloudRunAttributesSucceeds() {
306306
assertEquals("GCR-instance-id", detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID));
307307
}
308308

309+
/** Google Cloud Run Tests (Jobs) * */
310+
@Test
311+
public void testCloudRunJobResourceWithAttributesSucceeds() {
312+
// Setup GCR Job required env vars
313+
envVars.put("CLOUD_RUN_JOB", "cloud-run-hello-job");
314+
envVars.put("CLOUD_RUN_EXECUTION", "cloud-run-hello-job-1a2b3c");
315+
envVars.put("CLOUD_RUN_TASK_INDEX", "0");
316+
317+
TestUtils.stubEndpoint("/project/project-id", "GCR-pid");
318+
TestUtils.stubEndpoint("/instance/zone", "country-region-zone");
319+
TestUtils.stubEndpoint("/instance/id", "GCR-job-instance-id");
320+
321+
EnvironmentVariables mockEnv = new EnvVarMock(envVars);
322+
GCPPlatformDetector detector = new GCPPlatformDetector(mockMetadataConfig, mockEnv);
323+
324+
Map<String, String> detectedAttributes = detector.detectPlatform().getAttributes();
325+
assertEquals(
326+
GCPPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_JOB,
327+
detector.detectPlatform().getSupportedPlatform());
328+
assertEquals(
329+
new GoogleCloudRunJob(mockEnv, mockMetadataConfig).getAttributes(), detectedAttributes);
330+
assertEquals("GCR-pid", detector.detectPlatform().getProjectId());
331+
assertEquals(5, detectedAttributes.size());
332+
333+
assertEquals("cloud-run-hello-job-1a2b3c", detectedAttributes.get(GCR_JOB_EXECUTION_KEY));
334+
assertEquals("0", detectedAttributes.get(GCR_JOB_TASK_INDEX));
335+
assertEquals("cloud-run-hello-job", detectedAttributes.get(SERVERLESS_COMPUTE_NAME));
336+
assertEquals("country-region", detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION));
337+
assertEquals("GCR-job-instance-id", detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID));
338+
}
339+
309340
/** Google App Engine Tests * */
310341
@ParameterizedTest
311342
@MethodSource("provideGAEVariantEnvironmentVariable")

examples/metrics/src/main/java/com/google/cloud/opentelemetry/example/metrics/MetricsExporterExample.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
import io.opentelemetry.api.metrics.Meter;
3131
import io.opentelemetry.contrib.gcp.resource.GCPResourceProvider;
3232
import io.opentelemetry.exporter.logging.LoggingMetricExporter;
33+
import io.opentelemetry.sdk.common.CompletableResultCode;
3334
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
3435
import io.opentelemetry.sdk.metrics.export.MetricExporter;
3536
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
3637
import io.opentelemetry.sdk.resources.Resource;
3738
import java.io.IOException;
3839
import java.time.Duration;
3940
import java.util.Random;
41+
import java.util.concurrent.TimeUnit;
4042

4143
public class MetricsExporterExample {
4244
private static SdkMeterProvider METER_PROVIDER;
@@ -146,9 +148,16 @@ public static void main(String[] args) throws InterruptedException, IOException
146148
} finally {
147149
System.out.println("Shutting down the metrics-example application");
148150

149-
METER_PROVIDER.shutdown();
151+
CompletableResultCode resultCode = METER_PROVIDER.shutdown();
152+
// Wait upto 60 seconds for job to complete
153+
resultCode.join(60, TimeUnit.SECONDS);
154+
if (resultCode.isSuccess()) {
155+
System.out.println("Shutdown completed successfully!");
156+
} else {
157+
System.out.println("Unable to shutdown gracefully!");
158+
}
150159

151-
System.out.println("Shutdown complete");
160+
System.out.println("Exiting job");
152161
}
153162
}
154163
}

0 commit comments

Comments
 (0)