Skip to content

Commit 81fce14

Browse files
authored
Gcp add gcr job support (#1462)
1 parent 8ece6d7 commit 81fce14

File tree

4 files changed

+88
-4
lines changed

4 files changed

+88
-4
lines changed

gcp-resources/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ dependencies {
1212
api("io.opentelemetry:opentelemetry-sdk")
1313

1414
// Provides GCP resource detection support
15-
implementation("com.google.cloud.opentelemetry:detector-resources-support:0.31.0")
15+
implementation("com.google.cloud.opentelemetry:detector-resources-support:0.32.0")
1616

1717
implementation("io.opentelemetry.semconv:opentelemetry-semconv")
1818
testImplementation("io.opentelemetry.semconv:opentelemetry-semconv-incubating")

gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProvider.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_INSTANCE_ID;
1717
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_INSTANCE_NAME;
1818
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_MACHINE_TYPE;
19+
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCR_JOB_EXECUTION_KEY;
20+
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCR_JOB_TASK_INDEX;
1921
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_LOCATION;
2022
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_LOCATION_TYPE;
2123
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_NAME;
@@ -106,6 +108,9 @@ public Attributes getAttributes() {
106108
case GOOGLE_CLOUD_FUNCTIONS:
107109
addGcfAttributes(attrBuilder, detectedPlatform.getAttributes());
108110
break;
111+
case GOOGLE_CLOUD_RUN_JOB:
112+
addGcrJobAttributes(attrBuilder, detectedPlatform.getAttributes());
113+
break;
109114
case GOOGLE_APP_ENGINE:
110115
addGaeAttributes(attrBuilder, detectedPlatform.getAttributes());
111116
break;
@@ -192,8 +197,8 @@ private static void addGkeAttributes(
192197
}
193198

194199
/**
195-
* Updates the attributes with the required keys for a GCR (Google Cloud Run) environment. The
196-
* attributes are not updated in case the environment is not deemed to be GCR.
200+
* Updates the attributes with the required keys for a GCR (Google Cloud Run) Service environment.
201+
* The attributes are not updated in case the environment is not deemed to be GCR.
197202
*
198203
* @param attrBuilder The {@link AttributesBuilder} object that needs to be updated with the
199204
* necessary keys.
@@ -217,6 +222,34 @@ private static void addGcfAttributes(
217222
addCommonAttributesForServerlessCompute(attrBuilder, attributesMap);
218223
}
219224

225+
/**
226+
* Update the attributes with the required keys for a GCR (Google Cloud Run) Jobs environment. The
227+
* attributes are not updated in case the environment is not deemed to be GCR jobs environment.
228+
*
229+
* @param attrBuilder The {@link AttributesBuilder} object that needs to be updated with the
230+
* necessary keys.
231+
*/
232+
private static void addGcrJobAttributes(
233+
AttributesBuilder attrBuilder, Map<String, String> attributesMap) {
234+
attrBuilder.put(CLOUD_PLATFORM, GCP_CLOUD_RUN);
235+
Optional.ofNullable(attributesMap.get(SERVERLESS_COMPUTE_NAME))
236+
.ifPresent(name -> attrBuilder.put(FAAS_NAME, name));
237+
Optional.ofNullable(attributesMap.get(SERVERLESS_COMPUTE_INSTANCE_ID))
238+
.ifPresent(instanceId -> attrBuilder.put(FAAS_INSTANCE, instanceId));
239+
Optional.ofNullable(attributesMap.get(SERVERLESS_COMPUTE_CLOUD_REGION))
240+
.ifPresent(cloudRegion -> attrBuilder.put(CLOUD_REGION, cloudRegion));
241+
Optional.ofNullable(attributesMap.get(GCR_JOB_EXECUTION_KEY))
242+
.ifPresent(
243+
jobExecutionKey ->
244+
attrBuilder.put(IncubatingAttributes.GCP_CLOUD_RUN_JOB_EXECUTION, jobExecutionKey));
245+
Optional.ofNullable(attributesMap.get(GCR_JOB_TASK_INDEX))
246+
.ifPresent(
247+
jobTaskIndex ->
248+
attrBuilder.put(
249+
IncubatingAttributes.GCP_CLOUD_RUN_JOB_TASK_INDEX,
250+
Integer.parseInt(jobTaskIndex)));
251+
}
252+
220253
/**
221254
* Updates the attributes with the required keys for a GAE (Google App Engine) environment. The
222255
* attributes are not updated in case the environment is not deemed to be GAE.

gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/IncubatingAttributes.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ private CloudPlatformValues() {}
4040
public static final AttributeKey<String> FAAS_NAME = AttributeKey.stringKey("faas.name");
4141
public static final AttributeKey<String> FAAS_VERSION = AttributeKey.stringKey("faas.version");
4242

43+
public static final AttributeKey<String> GCP_CLOUD_RUN_JOB_EXECUTION =
44+
AttributeKey.stringKey("gcp.cloud_run.job.execution");
45+
public static final AttributeKey<Long> GCP_CLOUD_RUN_JOB_TASK_INDEX =
46+
AttributeKey.longKey("gcp.cloud_run.job.task_index");
47+
4348
public static final AttributeKey<String> GCP_GCE_INSTANCE_HOSTNAME =
4449
AttributeKey.stringKey("gcp.gce.instance.hostname");
4550
public static final AttributeKey<String> GCP_GCE_INSTANCE_NAME =

gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProviderTest.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_INSTANCE_ID;
1717
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_INSTANCE_NAME;
1818
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_MACHINE_TYPE;
19+
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCR_JOB_EXECUTION_KEY;
20+
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCR_JOB_TASK_INDEX;
1921
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_LOCATION;
2022
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_LOCATION_TYPE;
2123
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_NAME;
@@ -27,6 +29,7 @@
2729
import static com.google.cloud.opentelemetry.detection.AttributeKeys.SERVERLESS_COMPUTE_INSTANCE_ID;
2830
import static com.google.cloud.opentelemetry.detection.AttributeKeys.SERVERLESS_COMPUTE_NAME;
2931
import static com.google.cloud.opentelemetry.detection.AttributeKeys.SERVERLESS_COMPUTE_REVISION;
32+
import static io.opentelemetry.contrib.gcp.resource.IncubatingAttributes.GCP_CLOUD_RUN_JOB_TASK_INDEX;
3033
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
3134
import static io.opentelemetry.semconv.incubating.CloudIncubatingAttributes.CLOUD_ACCOUNT_ID;
3235
import static io.opentelemetry.semconv.incubating.CloudIncubatingAttributes.CLOUD_AVAILABILITY_ZONE;
@@ -42,6 +45,7 @@
4245
import static io.opentelemetry.semconv.incubating.FaasIncubatingAttributes.FAAS_INSTANCE;
4346
import static io.opentelemetry.semconv.incubating.FaasIncubatingAttributes.FAAS_NAME;
4447
import static io.opentelemetry.semconv.incubating.FaasIncubatingAttributes.FAAS_VERSION;
48+
import static io.opentelemetry.semconv.incubating.GcpIncubatingAttributes.GCP_CLOUD_RUN_JOB_EXECUTION;
4549
import static io.opentelemetry.semconv.incubating.GcpIncubatingAttributes.GCP_GCE_INSTANCE_HOSTNAME;
4650
import static io.opentelemetry.semconv.incubating.GcpIncubatingAttributes.GCP_GCE_INSTANCE_NAME;
4751
import static io.opentelemetry.semconv.incubating.HostIncubatingAttributes.HOST_ID;
@@ -133,6 +137,23 @@ private static DetectedPlatform generateMockServerlessPlatform(
133137
return mockServerlessPlatform;
134138
}
135139

140+
private static DetectedPlatform generateMockGcrJobPlatform() {
141+
Map<String, String> mockAttributes =
142+
new HashMap<>(
143+
ImmutableMap.of(
144+
SERVERLESS_COMPUTE_NAME, "serverless-job",
145+
SERVERLESS_COMPUTE_INSTANCE_ID, "serverless-instance-id",
146+
SERVERLESS_COMPUTE_CLOUD_REGION, "us-central1",
147+
GCR_JOB_TASK_INDEX, "1",
148+
GCR_JOB_EXECUTION_KEY, "serverless-job-a1b2c3"));
149+
DetectedPlatform mockServerlessPlatform = Mockito.mock(DetectedPlatform.class);
150+
Mockito.when(mockServerlessPlatform.getSupportedPlatform())
151+
.thenReturn(GCPPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_JOB);
152+
Mockito.when(mockServerlessPlatform.getAttributes()).thenReturn(mockAttributes);
153+
Mockito.when(mockServerlessPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
154+
return mockServerlessPlatform;
155+
}
156+
136157
private static DetectedPlatform generateMockGaePlatform() {
137158
Map<String, String> mockAttributes =
138159
new HashMap<>(
@@ -274,7 +295,7 @@ private static void verifyGkeMapping(Resource gotResource, DetectedPlatform dete
274295
}
275296

276297
@Test
277-
public void testGcrResourceAttributesMapping() {
298+
public void testGcrServiceResourceAttributesMapping() {
278299
GCPPlatformDetector mockDetector = Mockito.mock(GCPPlatformDetector.class);
279300
DetectedPlatform mockPlatform =
280301
generateMockServerlessPlatform(GCPPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN);
@@ -321,6 +342,31 @@ private static void verifyServerlessMapping(
321342
.containsEntry(CLOUD_REGION, detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION));
322343
}
323344

345+
@Test
346+
public void testGcrJobResourceAttributesMapping() {
347+
GCPPlatformDetector mockDetector = Mockito.mock(GCPPlatformDetector.class);
348+
DetectedPlatform mockPlatform = generateMockGcrJobPlatform();
349+
Mockito.when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
350+
Map<String, String> detectedAttributes = mockPlatform.getAttributes();
351+
352+
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);
353+
verify(mockPlatform, Mockito.times(1)).getProjectId();
354+
355+
assertThat(gotResource.getAttributes())
356+
.hasSize(8)
357+
.containsEntry(CLOUD_PROVIDER, GCP)
358+
.containsEntry(CLOUD_PLATFORM, GCP_CLOUD_RUN)
359+
.containsEntry(CLOUD_ACCOUNT_ID, DUMMY_PROJECT_ID)
360+
.containsEntry(FAAS_NAME, detectedAttributes.get(SERVERLESS_COMPUTE_NAME))
361+
.containsEntry(FAAS_NAME, detectedAttributes.get(SERVERLESS_COMPUTE_NAME))
362+
.containsEntry(FAAS_INSTANCE, detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID))
363+
.containsEntry(GCP_CLOUD_RUN_JOB_EXECUTION, detectedAttributes.get(GCR_JOB_EXECUTION_KEY))
364+
.containsEntry(
365+
GCP_CLOUD_RUN_JOB_TASK_INDEX,
366+
Integer.parseInt(detectedAttributes.get(GCR_JOB_TASK_INDEX)))
367+
.containsEntry(CLOUD_REGION, detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION));
368+
}
369+
324370
@Test
325371
public void testGaeResourceAttributeMapping() {
326372
GCPPlatformDetector mockDetector = Mockito.mock(GCPPlatformDetector.class);

0 commit comments

Comments
 (0)