From 6e9e41e6e7b53419279ffeefb2b4f81d0097ac1c Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Thu, 18 May 2023 01:09:42 +0000 Subject: [PATCH] Support metric exporter for cloud run --- .../metric/ResourceTranslator.java | 37 ++++++++++++++++++- .../resource/ResourceTranslator.java | 1 + 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/exporters/metrics/src/main/java/com/google/cloud/opentelemetry/metric/ResourceTranslator.java b/exporters/metrics/src/main/java/com/google/cloud/opentelemetry/metric/ResourceTranslator.java index 790f7713..db1c2495 100644 --- a/exporters/metrics/src/main/java/com/google/cloud/opentelemetry/metric/ResourceTranslator.java +++ b/exporters/metrics/src/main/java/com/google/cloud/opentelemetry/metric/ResourceTranslator.java @@ -18,9 +18,13 @@ import com.google.api.MonitoredResource; import com.google.cloud.opentelemetry.resource.GcpResource; import io.opentelemetry.sdk.resources.Resource; +import java.util.Map; /** Translates from OpenTelemetry Resource into Google Cloud Monitoring's MonitoredResource. */ public class ResourceTranslator { + private static final String MR_CLOUD_RUN_REVISION = "cloud_run_revision"; + private static final String MR_GENERIC_TASK = "generic_task"; + private ResourceTranslator() {} /** Converts a Java OpenTelemetry SDK resource into a MonitoredResource from GCP. */ @@ -28,8 +32,37 @@ public static MonitoredResource mapResource(Resource resource) { GcpResource gcpResource = com.google.cloud.opentelemetry.resource.ResourceTranslator.mapResource(resource); MonitoredResource.Builder mr = MonitoredResource.newBuilder(); - mr.setType(gcpResource.getResourceType()); - gcpResource.getResourceLabels().getLabels().forEach(mr::putLabels); + if (!mapCloudRunRevisionToGenericTask(mr, gcpResource)) { + // the gcpResource was not cloud_run_revision, so no explicit mapping performed + mr.setType(gcpResource.getResourceType()); + gcpResource.getResourceLabels().getLabels().forEach(mr::putLabels); + } return mr.build(); } + + /** + * Helper function to map cloud_run_revision {@link MonitoredResource} to generic_task. This is + * done because custom metrics are not yet supported on cloud_run_revision. For details see Manual + * creation of metric descriptors. + * + * @param monitoredResourceBuilder Builder object for {@link MonitoredResource} which needs to be + * mapped to generic_task. + * @param cloudRunResource The actual Cloud Run resource which is detected. + * @return True if the mapping operation was performed, indicating that the passed {@link + * GcpResource} was cloud_run_revision. False otherwise. + */ + private static boolean mapCloudRunRevisionToGenericTask( + MonitoredResource.Builder monitoredResourceBuilder, GcpResource cloudRunResource) { + if (cloudRunResource.getResourceType().equals(MR_CLOUD_RUN_REVISION)) { + monitoredResourceBuilder.setType(MR_GENERIC_TASK); + Map cloudRunLabels = cloudRunResource.getResourceLabels().getLabels(); + monitoredResourceBuilder.putLabels("location", cloudRunLabels.get("location")); + monitoredResourceBuilder.putLabels("namespace", cloudRunLabels.get("configuration_name")); + monitoredResourceBuilder.putLabels("job", cloudRunLabels.get("service_name")); + monitoredResourceBuilder.putLabels("task_id", cloudRunLabels.get("instance_id")); + return true; + } + return false; + } } diff --git a/shared/resourcemapping/src/main/java/com/google/cloud/opentelemetry/resource/ResourceTranslator.java b/shared/resourcemapping/src/main/java/com/google/cloud/opentelemetry/resource/ResourceTranslator.java index 1936015a..3bcad73f 100644 --- a/shared/resourcemapping/src/main/java/com/google/cloud/opentelemetry/resource/ResourceTranslator.java +++ b/shared/resourcemapping/src/main/java/com/google/cloud/opentelemetry/resource/ResourceTranslator.java @@ -93,6 +93,7 @@ public static AttributeMapping create( AttributeMapping.create("location", ResourceAttributes.CLOUD_REGION), AttributeMapping.create("service_name", ResourceAttributes.FAAS_NAME), AttributeMapping.create("configuration_name", ResourceAttributes.FAAS_NAME), + AttributeMapping.create("instance_id", ResourceAttributes.FAAS_ID), AttributeMapping.create("revision_name", ResourceAttributes.FAAS_VERSION)); private static List GOOGLE_CLOUD_FUNCTION_INSTANCE_LABELS = java.util.Arrays.asList(