Setup
Install the Datadog CLI client
npm install -g @datadog/datadog-ci
Install the gcloud CLI and authenticate with gcloud auth login
.
Configuration
Configure the Datadog site and Datadog API key, and define the service name to use in Datadog.
export DATADOG_SITE="<DATADOG_SITE>"
export DD_API_KEY="<DD_API_KEY>"
export DD_SERVICE="<SERVICE_NAME>"
Instrument
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 permanently install Datadog for your production applications, run the instrument
command in your CI/CD pipelines after your normal deployment. You can specify multiple services to instrument by passing multiple --service
flags.
datadog-ci cloud-run instrument --project <GCP-PROJECT-ID> --service <CLOUD-RUN-SERVICE-NAME> --region <GCP-REGION>
You can pin to a specific image with the --sidecar-image
flag. See the latest releases on Docker Hub.
Additional parameters can be found in the CLI documentation.
The Datadog Terraform module for Google Cloud Run wraps the google_cloud_run_v2_service resource and automatically configures your Cloud Run app for Datadog Serverless Monitoring by adding required environment variables and the serverless-init sidecar.
Create a .tf
file that contains your configuration. You can use the following example and adapt it to your needs:
variable "datadog_api_key" {
description = "Your Datadog API key"
type = string
sensitive = 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_key
datadog_service = "test-service" // your application service
datadog_version = "0.0.0" // your code version
datadog_env = "prod" // your application environment
datadog_enable_logging = true
deletion_protection = false
build_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:
To deploy your app, run:
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:latest
. 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.DD_API_KEY
: Your Datadog API key.FUNCTION_TARGET
: The entry point of your function. For example, Main
.
For a list of all environment variables, including additional tags, see Environment variables.