If you are new to Datadog serverless monitoring, launch the Datadog CLI in interactive mode to guide your first installation for a quick start.
datadog-ci cloud-run instrument -i
To set up the Datadog sidecar for your applications, run the instrument command after your normal deployment. You can specify multiple services to instrument by passing multiple --service flags.
If you don’t already have Terraform set up, install Terraform, create a new directory, and make a file called main.tf.
Then, add the following to your Terraform configuration, updating it as necessary based on your needs:
variable"datadog_api_key"{description="Your Datadog API key"type=stringsensitive=true}module"my-cloud-run-app"{source="DataDog/cloud-run-datadog/google"version="~> 1.0"project="my-gcp-project"name="my-cloud-run-app"location="us-central1"datadog_api_key=var.datadog_api_keydatadog_service="test-service" // your application service
datadog_version="0.0.0" // your code version
datadog_env="prod" // your application environment
datadog_enable_logging=truedeletion_protection=falsebuild_config={function_target="helloHttp" // your function entry point
image_uri="us-docker.pkg.dev/cloudrun/container/hello"base_image="us-central1-docker.pkg.dev/serverless-runtimes/google-22-full/runtimes/your-runtime" // base image for your runtime
enable_automatic_updates=true}template={containers=[{name="main"image="us-docker.pkg.dev/cloudrun/container/hello"base_image_uri="us-central1-docker.pkg.dev/serverless-runtimes/google-22-full/runtimes/your-runtime" // base image for your runtime
resources={limits={cpu="1"memory="512Mi"}}ports={container_port=8080}env=[{ name="DD_TRACE_ENABLED", value="true"},]},]}}
See the Environment Variables for more information on the configuration options available through the env.
Ensure the container port for the main container is the same as the one exposed in your Dockerfile/service.
If you haven’t already, initialize your Terraform project:
terraform init
To deploy your app, run:
terraform apply
After deploying your Cloud Run app, you can manually modify your app’s settings to enable Datadog monitoring.
Create a Volume with In-Memory volume type.
Add a new container with image URL: gcr.io/datadoghq/serverless-init:<YOUR_TAG>. See the latest releases on Docker Hub to pin a specific version.
Add the volume mount to every container in your application. Choose a path such as /shared-volume, and remember it for the next step.
Add the following environment variables to your serverless-init sidecar container:
DD_SERVICE: A name for your service. For example, gcr-sidecar-test.
DD_ENV: A name for your environment. For example, dev.
DD_SERVERLESS_LOG_PATH: Your log path. For example, /shared-volume/logs/*.log. The path must begin with the mount path you defined in the previous step.
Set the log source to enable a Log Pipeline for advanced parsing. To automatically apply language-specific parsing rules, set to python, or use your custom pipeline. Defaults to cloudrun.
Sidecar container
DD_TAGS
Add custom tags to your logs, metrics, and traces. Tags should be comma separated in key/value format (for example: key1:value1,key2:value2).
Sidecar container
FUNCTION_TARGET
Required for correct tagging. The entry point of your function. For example, Main. You can also find FUNCTION_TARGET on the source tab inside Google console: Function entry point.
Sidecar container
Do not set the following environment variables in your serverless environment. They should only be set in non-serverless environments.
DD_AGENT_HOST
DD_TRACE_AGENT_URL
Pub/Sub을 사용한 분산 추적
애플리케이션 컨테이너에서 DD_TRACE_INFERRED_PROXY_SERVICES_ENABLED=true를 설정하세요. 이렇게 하면 푸시 요청에 대해 추론된 gcp.pubsub.receive 스팬이 생성됩니다.
Google Cloud Pub/Sub 푸시 구독 추적을 사용하려면 ddtrace 버전 4.8.0 이상이 필요합니다.
To get end-to-end distributed traces between Pub/Sub producers and Cloud Run, configure your push subscriptions with the --push-no-wrapper and --push-no-wrapper-write-metadata flags. This moves message attributes from the JSON body to HTTP headers, allowing Datadog to extract producer trace context and create span links.
Eventarc Pub/Sub triggers use push subscriptions as the underlying delivery mechanism. When you create an Eventarc trigger, GCP automatically creates a managed push subscription. However, Eventarc does not expose --push-no-wrapper-write-metadata as a trigger creation parameter, so you must manually update the auto-created subscription.
This integration depends on your runtime having a full SSL implementation. If you are using a slim image, you may need to add the following command to your Dockerfile to include certificates:
RUN apt-get update && apt-get install -y ca-certificates
To have your Cloud Run services appear in the software catalog, you must set the DD_SERVICE, DD_VERSION, and DD_ENV environment variables.