Stay organized with collections Save and categorize content based on your preferences.
This tutorial shows how to write, deploy, and call a Cloud Run service that reports custom OTLP metrics to Google Cloud Managed Service For Prometheus by using the Google-Built OpenTelemetry Collector as a sidecar. For information about the Google-built Collector, see Overview of the Google-Built OpenTelemetry Collector.
If you have a Cloud Run service that reports Prometheus metrics, then use the Prometheus sidecar for Cloud Run instead.
ObjectivesIn this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage, use the pricing calculator.
New Google Cloud users might be eligible for a
free trial.
Before you beginIn the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Verify that billing is enabled for your Google Cloud project.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Run, Cloud Monitoring, Artifact Registry, and Cloud Build APIs.
gcloud components update
To get the permissions that you need to complete the tutorial, ask your administrator to grant you the following IAM roles on your project:
roles/cloudbuild.builds.editor
)roles/run.admin
)roles/iam.serviceAccountCreator
)roles/resourcemanager.projectIamAdmin
)roles/monitoring.metricWriter
)roles/iam.serviceAccountUser
)roles/serviceusage.serviceUsageConsumer
)roles/storage.admin
)For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Note: IAM basic roles might also contain permissions to complete the tutorial. You shouldn't grant basic roles in a production environment, but you can grant them in a development or test environment.Also note that the Cloud Run service identity needs the Monitoring Metric Writer (roles/monitoring.metricWriter
) role. The Compute Engine default service account might have this role by default, but you might need to add it if you have changed its permissions or are using a different service account.
To configure gcloud with defaults for your Cloud Run service:
Set your default project:
gcloud config set project PROJECT_ID
Replace PROJECT_ID with the name of the project you created for this tutorial.
Configure gcloud for your chosen region:
gcloud config set run/region REGION
Replace REGION with the supported Cloud Run region of your choice.
Cloud Run is regional, which means the infrastructure that runs your Cloud Run services is located in a specific region and is managed by Google to be redundantly available across all the zones within that region.
Meeting your latency, availability, or durability requirements are primary factors for selecting the region where your Cloud Run services are run. You can generally select the region nearest to your users but you should consider the location of the other Google Cloud products that are used by your Cloud Run service. Using Google Cloud products together across multiple locations can affect your service's latency as well as cost.
Cloud Run is available in the following regions:
Subject to Tier 1 pricing Subject to Tier 2 pricingIf you already created a Cloud Run service, you can view the region in the Cloud Run dashboard in the Google Cloud console.
Creating a Artifact Registry image repositoryCreate an Artifact Registry Docker repository to host the sample service image:
gcloud artifacts repositories create run-otel \ --repository-format=docker \ --location=REGION \ --project=PROJECT_ID
Replace the following:
To retrieve the code sample for use:
Clone the sample app repository to your local machine:
Gogit clone https://github.com/GoogleCloudPlatform/golang-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Change to the directory that contains the Cloud Run sample code:
Gocd golang-samples/run/custom-metrics/
The code for this tutorial consists of the following:
sidecar_sample_counter_total
.Dockerfile
that defines the operating environment for the service.The sample also includes files under the collector
subdirectory for building a custom collector:
A config file for the collector.
A Dockerfile
that bundles the provided config into an upstream Collector image.
Shipping code consists of three steps: building a container image with Cloud Build, uploading the container image to Artifact Registry, and deploying the container image to Cloud Run.
To ship your code:
Build your sample service container and publish on Artifact Registry:
gcloud builds submit --tag REGION-docker.pkg.dev/PROJECT_ID/run-otel/sample-metrics-app
Upon success, you should see a SUCCESS message containing the ID, creation time, and image name. The image is stored in Artifact Registry and can be re-used if desired.
Build your Collector container and publish on Artifact Registry:
gcloud builds submit collector --tag REGION-docker.pkg.dev/PROJECT_ID/run-otel/otel-collector-metrics
Upon success, you should see a SUCCESS message containing the ID, creation time, and image name. The image is stored in Artifact Registry and can be re-used if desired.
Deploy your application:
YAMLCreate a new file called service.yaml
with the following:
apiVersion: serving.knative.dev/v1 kind: Service metadata: name: SERVICE-NAME annotations: run.googleapis.com/launch-stage: BETA spec: template: metadata: annotations: run.googleapis.com/container-dependencies: "{app:[collector]}" spec: containers: - image: REGION-docker.pkg.dev/PROJECT_ID/run-otel/sample-metrics-app name: app ports: - containerPort: CONTAINER_PORT env: - name: "OTEL_EXPORTER_OTLP_ENDPOINT" value: "http://localhost:4317" - image: REGION-docker.pkg.dev/PROJECT_ID/run-otel/otel-collector-metrics name: collector startupProbe: httpGet: path: / port: 13133
8080
.custom-metrics-sample-service
.Create the new service with the following command:
gcloud run services replace service.yaml
This command returns a service URL. Use this URL to try out the sample application in Trying it out.
Using the URL from the gcloud run
command in Shipping the code, connect to the service to generate some sample metrics (you can run this command several times to generate more interesting data):
curl -H \ "Authorization: Bearer $(gcloud auth print-identity-token)" \ SERVICE_URL
Replace SERVICE_URL with the URL of your service.
Next, navigate to the Metrics Explorer within the Cloud Monitoring section of the Google Cloud console and select the sidecar_sample_counter_total
metric.
You can also query the metrics with PromQL. For example, the query below will filter metrics based on the Cloud Run instance ID:
sidecar_sample_counter_total{instance="INSTANCE_ID"}
Replace INSTANCE_ID with the ID of any instance for your service (available in the instance logs or from the metadata server).
This query produces a chart like the one below:
Success: You deployed a Cloud Run service that reports custom OTLP metrics to Google Cloud Managed Service For Prometheus. Clean upIf you created a new project for this tutorial, delete the project. If you used an existing project and wish to keep it without the changes added in this tutorial, delete resources created for the tutorial.
Deleting the projectThe easiest way to eliminate billing is to delete the project that you created for the tutorial.
To delete the project:
appspot.com
URL, delete selected resources inside the project instead of deleting the whole project.If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects can help you avoid exceeding project quota limits.
Delete the Cloud Run service you deployed in this tutorial:
gcloud run services delete SERVICE-NAME
Where SERVICE-NAME is your chosen service name.
You can also delete Cloud Run services from the Google Cloud console.
Remove the gcloud default region configuration you added during tutorial setup:
gcloud config unset run/region
Remove the project configuration:
gcloud config unset project
Delete other Google Cloud resources created in this tutorial:
More examples, including examples for traces and logs, are available on GitHub.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4