From 6e9e41e6e7b53419279ffeefb2b4f81d0097ac1c Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Thu, 18 May 2023 01:09:42 +0000 Subject: [PATCH 1/8] 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( From 01abb0a8f07e707ebb08893f249bb079105e784a Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Tue, 6 Jun 2023 14:08:33 -0400 Subject: [PATCH 2/8] 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 --- examples/metrics/README.md | 58 ++++++++++++++++++++++++ examples/metrics/run_as_cloud-run-job.sh | 55 ++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 examples/metrics/README.md create mode 100755 examples/metrics/run_as_cloud-run-job.sh diff --git a/examples/metrics/README.md b/examples/metrics/README.md new file mode 100644 index 00000000..572a32ce --- /dev/null +++ b/examples/metrics/README.md @@ -0,0 +1,58 @@ +### Running the example to export metrics to Google Cloud + +You can run this example to generate sample metrics on any Google Cloud project. + +#### Prerequisites + +##### Get Google Cloud Credentials on your machine + +```shell +gcloud auth application-default login +``` +Executing this command will save your application credentials to default path which will depend on the type of machine - +- Linux, macOS: `$HOME/.config/gcloud/application_default_credentials.json` +- Windows: `%APPDATA%\gcloud\application_default_credentials.json` + +##### Export the retrieved credentials to `GOOGLE_APPLICATION_CREDENTIALS` environment variable. + +```shell +export GOOGLE_APPLICATION_CREDENTIALS=$HOME/.config/gcloud/application_default_credentials.json +``` + +##### Export the Google Cloud Project ID to `GOOGLE_CLOUD_PROJECT` environment variable: + +```shell +export GOOGLE_CLOUD_PROJECT="my-awesome-gcp-project-id" +``` + +### Running the example + +#### Run the example locally + +You can run the example application via gradle. From the project root: + +```shell +cd examples/metrics/ && gradle run +``` + +#### Run the example as a Cloud Run Job + +You can run the example application as a Google Cloud Run Job. A convenience script has been provided for this. + +First, export your preferred Google cloud region where you want to create and run the job: + +```shell +# This can be any valid Google Cloud Region +export GOOGLE_CLOUD_RUN_REGION="us-central1" +``` + +Then, from the project root: + +```shell +cd examples/metrics/ && ./run_as_cloud-run-job.sh +``` + +*Note: When using the convenience script, it will create a Google Cloud Artifact Registry named `cloud-run-applications` in your +selected project.* + +You should now see the exported metrics in your Google Cloud project. diff --git a/examples/metrics/run_as_cloud-run-job.sh b/examples/metrics/run_as_cloud-run-job.sh new file mode 100755 index 00000000..e4fec279 --- /dev/null +++ b/examples/metrics/run_as_cloud-run-job.sh @@ -0,0 +1,55 @@ +#!/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 + +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 "${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/metrics-export-java" + +echo "CREATING A CLOUD RUN JOB TO RUN THE CONTAINER" +gcloud run jobs create job-metrics-export \ + --image "${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/metrics-export-java" \ + --max-retries 5 \ + --region ${GOOGLE_CLOUD_RUN_REGION} \ + --project="${GOOGLE_CLOUD_PROJECT}" + +echo "SETTING CLOUD RUN JOB REGION" +gcloud config set run/region "${GOOGLE_CLOUD_RUN_REGION}" + +echo "RUNNING THE CREATED JOB" +gcloud run jobs execute job-metrics-export From 805f917262a4581d6bf1e0420792e36f24388d8d Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Thu, 8 Jun 2023 18:53:46 +0000 Subject: [PATCH 3/8] 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. --- .../metric/ResourceTranslator.java | 37 +------------------ .../resource/ResourceTranslator.java | 31 +++++++--------- 2 files changed, 15 insertions(+), 53 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 db1c2495..790f7713 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,13 +18,9 @@ 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. */ @@ -32,37 +28,8 @@ public static MonitoredResource mapResource(Resource resource) { GcpResource gcpResource = com.google.cloud.opentelemetry.resource.ResourceTranslator.mapResource(resource); MonitoredResource.Builder mr = MonitoredResource.newBuilder(); - 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); - } + 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 3bcad73f..98f26e09 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,51 +71,47 @@ 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("instance_id", ResourceAttributes.FAAS_ID), - AttributeMapping.create("revision_name", ResourceAttributes.FAAS_VERSION)); private static List GOOGLE_CLOUD_FUNCTION_INSTANCE_LABELS = - java.util.Arrays.asList( + 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) { @@ -129,8 +126,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: From d78c789eacbe512b9e0b9fce807140ec14b4ee29 Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Thu, 8 Jun 2023 21:31:09 +0000 Subject: [PATCH 4/8] Update the autoinstrument example to run in cloud-run --- examples/autoinstrument/README.md | 36 ++++++++++++++++ examples/autoinstrument/run_in_cloud-run.sh | 47 +++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100755 examples/autoinstrument/run_in_cloud-run.sh diff --git a/examples/autoinstrument/README.md b/examples/autoinstrument/README.md index eb6c441d..79ff1700 100644 --- a/examples/autoinstrument/README.md +++ b/examples/autoinstrument/README.md @@ -53,6 +53,42 @@ 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 +``` + +Once you have the service URL to the application, you can make **authenticated** requests to it. Authenticated requests can be made from the command line by passing an auth token in a cURL request - + +```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..1cc1d1b4 --- /dev/null +++ b/examples/autoinstrument/run_in_cloud-run.sh @@ -0,0 +1,47 @@ +#!/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 + +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 "${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/hello-autoinstrument-java" + +echo "RUNNING SAMPLE APP ON PORT 8080" +gcloud run deploy hello-autoinstrument-cloud-run \ + --image="${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/hello-autoinstrument-java" \ + --region="${GOOGLE_CLOUD_RUN_REGION}" From c51599369a38c67b81497af044716cb9272fe878 Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Thu, 8 Jun 2023 22:06:35 +0000 Subject: [PATCH 5/8] 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. --- .../cloud/opentelemetry/resource/ResourceTranslator.java | 6 ------ 1 file changed, 6 deletions(-) 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 98f26e09..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 @@ -89,10 +89,6 @@ public static AttributeMapping create( 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_FUNCTION_INSTANCE_LABELS = - Arrays.asList( - AttributeMapping.create("region", ResourceAttributes.CLOUD_REGION), - AttributeMapping.create("function_name", ResourceAttributes.FAAS_NAME)); private static List GOOGLE_CLOUD_APP_ENGINE_INSTANCE_LABELS = Arrays.asList( AttributeMapping.create("module_id", ResourceAttributes.FAAS_NAME), @@ -126,8 +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_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: From 3a165ae9df34a9eb4bff1ec986e97153c5fc74fe Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Mon, 12 Jun 2023 21:33:07 +0000 Subject: [PATCH 6/8] Refactor: extract image name to a variable --- examples/autoinstrument/run_in_cloud-run.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/autoinstrument/run_in_cloud-run.sh b/examples/autoinstrument/run_in_cloud-run.sh index 1cc1d1b4..11901cbb 100755 --- a/examples/autoinstrument/run_in_cloud-run.sh +++ b/examples/autoinstrument/run_in_cloud-run.sh @@ -16,6 +16,7 @@ # 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" @@ -39,9 +40,9 @@ gcloud artifacts repositories create ${CONTAINER_REGISTRY} --repository-format=d echo "CREATED ${CONTAINER_REGISTRY} in ${REGISTRY_LOCATION}" echo "BUILDING SAMPLE APP IMAGE" -gradle clean jib --image "${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/hello-autoinstrument-java" +gradle clean jib --image "${IMAGE_NAME}" echo "RUNNING SAMPLE APP ON PORT 8080" gcloud run deploy hello-autoinstrument-cloud-run \ - --image="${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/hello-autoinstrument-java" \ + --image="${IMAGE_NAME}" \ --region="${GOOGLE_CLOUD_RUN_REGION}" From ede8f80b5fff9463d70cea0c53b6e5846ee0df53 Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Mon, 12 Jun 2023 22:04:32 +0000 Subject: [PATCH 7/8] Update README to streamline service invocation instructions --- examples/autoinstrument/README.md | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/examples/autoinstrument/README.md b/examples/autoinstrument/README.md index 79ff1700..ab2dbffa 100644 --- a/examples/autoinstrument/README.md +++ b/examples/autoinstrument/README.md @@ -74,21 +74,19 @@ This will deploy the containerized application to Cloud Run and you will be pres Service URL: https://hello-autoinstrument-cloud-run-m43qtxry5q-uc.a.run.app ``` -Once you have the service URL to the application, you can make **authenticated** requests to it. Authenticated requests can be made from the command line by passing an auth token in a cURL request - +Once the Cloud Run service is deployed, run: ```shell -# Make sure to replace the SERVICE_URL with the one that was generated for your deployment +gcloud beta run services proxy hello-autoinstrument-cloud-run --port=8080 +``` -# Making a request to / -curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" ${SERVICE_URL}/ +This will allow you to call the service from your browser via localhost - -# Making a request to /greeting -curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" ${SERVICE_URL}/greeting +```text +http://localhost:8080/ +http://localhost:8080/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. From 5580d409c3c1acf0ba1d6b7a31cff67ecd1ebe85 Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Tue, 13 Jun 2023 15:03:39 +0000 Subject: [PATCH 8/8] Update readme with instructions for command line --- examples/autoinstrument/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/examples/autoinstrument/README.md b/examples/autoinstrument/README.md index ab2dbffa..6c48991c 100644 --- a/examples/autoinstrument/README.md +++ b/examples/autoinstrument/README.md @@ -74,6 +74,8 @@ This will deploy the containerized application to Cloud Run and you will be pres 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 @@ -87,6 +89,23 @@ 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.