Skip to content

Commit c64be45

Browse files
committed
Add support for GCR Jobs environment
1 parent b51cedf commit c64be45

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

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 =

0 commit comments

Comments
 (0)