Skip to content

Commit

Permalink
Support metric exporter for cloud run
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed May 18, 2023
1 parent 74a108f commit 6e9e41e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,51 @@
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. */
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 <a
* href="https://cloud.google.com/monitoring/custom-metrics/creating-metrics#create-metric-desc">Manual
* creation of metric descriptors</a>.
*
* @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<String, String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<AttributeMapping> GOOGLE_CLOUD_FUNCTION_INSTANCE_LABELS =
java.util.Arrays.asList(
Expand Down

0 comments on commit 6e9e41e

Please sign in to comment.