From e6c6a2be28547bc882aa9694ffa5d45c50057045 Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Tue, 13 Jun 2023 14:43:08 -0400 Subject: [PATCH] Remove MR mapping for cloud run and cloud functions (#250) * Support metric exporter for cloud run * Enable metrics-exporter example to run as cloud run job (#249) * Add readme with steps to run the example * Add script to run example as Cloud Run Job * Replace GCR with Artifact Registry * Rename GOOGLE_CLOUD_RUN_JOB_REGION to GOOGLE_CLOUD_RUN_REGION * Remove resource mapping for cloud-run As per the exporter spec, we do not support pushing metric to cloud-run. This change will cause cloud_run_revision to be treated as a generic_task. * Update the autoinstrument example to run in cloud-run * Remove mapping for cloud functions Cloud functions is also not writable for user-defined metrics, therefore removing its explicit mapping so that it defaults to generic_task. * Refactor: extract image name to a variable * Update README to streamline service invocation instructions * Update readme with instructions for command line --- examples/autoinstrument/README.md | 53 +++++++++++++++++++ examples/autoinstrument/run_in_cloud-run.sh | 48 +++++++++++++++++ .../resource/ResourceTranslator.java | 34 +++++------- 3 files changed, 113 insertions(+), 22 deletions(-) create mode 100755 examples/autoinstrument/run_in_cloud-run.sh diff --git a/examples/autoinstrument/README.md b/examples/autoinstrument/README.md index eb6c441d..6c48991c 100644 --- a/examples/autoinstrument/README.md +++ b/examples/autoinstrument/README.md @@ -53,6 +53,59 @@ Or, if you'd like to synthesize a parent trace: curl -H "traceparent: 00-ff000000000000000000000000000041-ff00000000000041-01" ${cluster_ip} ``` +## Running in Google Cloud Run + +To run this example in Google Cloud Run, you need to run the convenience script provided. After following the prerequisites, + +First, export the Google Cloud Region for cloud run to `GOOGLE_CLOUD_RUN_REGION` environment variable: + +```shell +# This can be any supported Google cloud region +export GOOGLE_CLOUD_RUN_REGION=us-central1 +``` + +Then, from the root of the repository, +```shell +cd examples/autoinstrument && ./run_in_cloud-run.sh +``` +This will deploy the containerized application to Cloud Run and you will be presented with a service URL which would look something like - + +```text +Service URL: https://hello-autoinstrument-cloud-run-m43qtxry5q-uc.a.run.app +``` + +#### Calling the service from browser + +Once the Cloud Run service is deployed, run: + +```shell +gcloud beta run services proxy hello-autoinstrument-cloud-run --port=8080 +``` + +This will allow you to call the service from your browser via localhost - + +```text +http://localhost:8080/ +http://localhost:8080/greeting +``` + +#### Calling the service from command line + +You can also make **authenticated** requests to the service from command line via cURL using the service URL to the application. + +```shell +# Make sure to replace the SERVICE_URL with the one that was generated for your deployment + +# Making a request to / +curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" ${SERVICE_URL}/ + +# Making a request to /greeting +curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" ${SERVICE_URL}/greeting +``` + +You can also allow public access to your cloud-run service, details for which can be found [here](https://cloud.google.com/run/docs/authenticating/public#console-ui). +With public access enabled, you would no longer need to provide the auth token within your requests. + ## Running locally in a docker container In case you do not want to spin up your own GKE cluster, but still want telemetry to be published to Google Cloud, you can run the example in a docker container. diff --git a/examples/autoinstrument/run_in_cloud-run.sh b/examples/autoinstrument/run_in_cloud-run.sh new file mode 100755 index 00000000..11901cbb --- /dev/null +++ b/examples/autoinstrument/run_in_cloud-run.sh @@ -0,0 +1,48 @@ +#!/bin/bash +# +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +CONTAINER_REGISTRY=cloud-run-applications +REGISTRY_LOCATION=us-central1 +IMAGE_NAME="${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/hello-autoinstrument-java" + +if [[ -z "${GOOGLE_CLOUD_PROJECT}" ]]; then + echo "GOOGLE_CLOUD_PROJECT environment variable not set" + exit 1 +fi + +if [[ -z "${GOOGLE_APPLICATION_CREDENTIALS}" ]]; then + echo "GOOGLE_APPLICATION_CREDENTIALS environment variable not set" + exit 1 +fi + +if [[ -z "${GOOGLE_CLOUD_RUN_REGION}" ]]; then + echo "GOOGLE_CLOUD_RUN_REGION environment variable not set" + exit 1 +fi + +echo "ENVIRONMENT VARIABLES VERIFIED" + +echo "CREATING CLOUD ARTIFACT REPOSITORY" +gcloud artifacts repositories create ${CONTAINER_REGISTRY} --repository-format=docker --location=${REGISTRY_LOCATION} --description="Sample applications to run on Google Cloud Run" +echo "CREATED ${CONTAINER_REGISTRY} in ${REGISTRY_LOCATION}" + +echo "BUILDING SAMPLE APP IMAGE" +gradle clean jib --image "${IMAGE_NAME}" + +echo "RUNNING SAMPLE APP ON PORT 8080" +gcloud run deploy hello-autoinstrument-cloud-run \ + --image="${IMAGE_NAME}" \ + --region="${GOOGLE_CLOUD_RUN_REGION}" 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..8e83937d 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 @@ -18,6 +18,7 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; +import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -70,50 +71,43 @@ public static AttributeMapping create( } private static List GCE_INSTANCE_LABELS = - java.util.Arrays.asList( + Arrays.asList( AttributeMapping.create("zone", ResourceAttributes.CLOUD_AVAILABILITY_ZONE), AttributeMapping.create("instance_id", ResourceAttributes.HOST_ID)); private static List K8S_CONTAINER_LABELS = - java.util.Arrays.asList( + Arrays.asList( AttributeMapping.create( "location", - java.util.Arrays.asList( + Arrays.asList( ResourceAttributes.CLOUD_AVAILABILITY_ZONE, ResourceAttributes.CLOUD_REGION)), AttributeMapping.create("cluster_name", ResourceAttributes.K8S_CLUSTER_NAME), AttributeMapping.create("namespace_name", ResourceAttributes.K8S_NAMESPACE_NAME), AttributeMapping.create("container_name", ResourceAttributes.K8S_CONTAINER_NAME), AttributeMapping.create("pod_name", ResourceAttributes.K8S_POD_NAME)); private static List AWS_EC2_INSTANCE_LABELS = - java.util.Arrays.asList( + Arrays.asList( AttributeMapping.create("instance_id", ResourceAttributes.HOST_ID), AttributeMapping.create("region", ResourceAttributes.CLOUD_AVAILABILITY_ZONE), AttributeMapping.create("aws_account", ResourceAttributes.CLOUD_ACCOUNT_ID)); - private static List GOOGLE_CLOUD_RUN_INSTANCE_LABELS = - java.util.Arrays.asList( - AttributeMapping.create("location", ResourceAttributes.CLOUD_REGION), - AttributeMapping.create("service_name", ResourceAttributes.FAAS_NAME), - AttributeMapping.create("configuration_name", ResourceAttributes.FAAS_NAME), - AttributeMapping.create("revision_name", ResourceAttributes.FAAS_VERSION)); - private static List GOOGLE_CLOUD_FUNCTION_INSTANCE_LABELS = - java.util.Arrays.asList( - AttributeMapping.create("region", ResourceAttributes.CLOUD_REGION), - AttributeMapping.create("function_name", ResourceAttributes.FAAS_NAME)); private static List GOOGLE_CLOUD_APP_ENGINE_INSTANCE_LABELS = - java.util.Arrays.asList( + Arrays.asList( AttributeMapping.create("module_id", ResourceAttributes.FAAS_NAME), AttributeMapping.create("version_id", ResourceAttributes.FAAS_VERSION), AttributeMapping.create("instance_id", ResourceAttributes.FAAS_ID), AttributeMapping.create("location", ResourceAttributes.CLOUD_REGION)); private static List GENERIC_TASK_LABELS = - java.util.Arrays.asList( + Arrays.asList( AttributeMapping.create( "location", - java.util.Arrays.asList( + Arrays.asList( ResourceAttributes.CLOUD_AVAILABILITY_ZONE, ResourceAttributes.CLOUD_REGION), "global"), AttributeMapping.create("namespace", ResourceAttributes.SERVICE_NAMESPACE, ""), AttributeMapping.create("job", ResourceAttributes.SERVICE_NAME, ""), - AttributeMapping.create("task_id", ResourceAttributes.SERVICE_INSTANCE_ID, "")); + AttributeMapping.create( + "task_id", + Arrays.asList(ResourceAttributes.SERVICE_INSTANCE_ID, ResourceAttributes.FAAS_ID), + "")); /** Converts a Java OpenTelemetry SDK resource into a GCP resource. */ public static GcpResource mapResource(Resource resource) { @@ -128,10 +122,6 @@ public static GcpResource mapResource(Resource resource) { return mapBase(resource, "k8s_container", K8S_CONTAINER_LABELS); case ResourceAttributes.CloudPlatformValues.AWS_EC2: return mapBase(resource, "aws_ec2_instance", AWS_EC2_INSTANCE_LABELS); - case ResourceAttributes.CloudPlatformValues.GCP_CLOUD_RUN: - return mapBase(resource, "cloud_run_revision", GOOGLE_CLOUD_RUN_INSTANCE_LABELS); - case ResourceAttributes.CloudPlatformValues.GCP_CLOUD_FUNCTIONS: - return mapBase(resource, "cloud_function", GOOGLE_CLOUD_FUNCTION_INSTANCE_LABELS); case ResourceAttributes.CloudPlatformValues.GCP_APP_ENGINE: return mapBase(resource, "gae_instance", GOOGLE_CLOUD_APP_ENGINE_INSTANCE_LABELS); default: