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>
Additional parameters can be found in the CLI documentation.
Create a YAML file that contains your configuration. You can use the following example and adapt it to your needs:
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: '<SERVICE_NAME>'
labels:
cloud.googleapis.com/location: '<LOCATION>'
service: '<SERVICE_NAME>'
spec:
template:
metadata:
labels:
service: '<SERVICE_NAME>'
annotations:
# The maximum number of instances that can be created for this service.
# https://cloud.google.com/run/docs/reference/rest/v1/RevisionTemplate
autoscaling.knative.dev/maxScale: '100'
# The startup CPU boost feature for revisions provides additional CPU during
# instance startup time and for 10 seconds after the instance has started.
# https://cloud.google.com/run/docs/configuring/services/cpu#startup-boost
run.googleapis.com/startup-cpu-boost: 'true'
spec:
containers:
- env:
- name: DD_SERVICE
value: '<SERVICE_NAME>'
image: '<CONTAINER_IMAGE>'
name: run-sidecar-1
ports:
- containerPort: 8080
name: http1
resources:
limits:
cpu: 1000m
memory: 512Mi
startupProbe:
failureThreshold: 1
periodSeconds: 240
tcpSocket:
port: 8080
timeoutSeconds: 240
volumeMounts:
- mountPath: /shared-volume
name: shared-volume
- env:
- name: DD_SERVERLESS_LOG_PATH
value: shared-volume/logs/*.log
- name: DD_SITE
value: '<DATADOG_SITE>'
- name: DD_ENV
value: '<ENV>'
- name: DD_API_KEY
value: '<API_KEY>'
- name: DD_SERVICE
value: '<SERVICE_NAME>'
- name: DD_VERSION
value: '<VERSION>'
- name: DD_LOG_LEVEL
value: debug
- name: DD_LOGS_INJECTION
value: 'true'
- name: DD_SOURCE
value: 'csharp'
- name: DD_HEALTH_PORT
value: '12345'
image: gcr.io/datadoghq/serverless-init:latest
name: serverless-init-1
resources:
limits:
cpu: 1000m
memory: 512Mi
startupProbe:
failureThreshold: 3
periodSeconds: 10
tcpSocket:
port: 12345
timeoutSeconds: 1
volumeMounts:
- mountPath: /shared-volume
name: shared-volume
volumes:
- emptyDir:
medium: Memory
sizeLimit: 512Mi
name: shared-volume
traffic:
- latestRevision: true
percent: 100
See the Environment Variables for more information.
In this example, the environment variables, startup health check, and volume mount are already added. If you don’t want to enable logs, remove the shared volume.
Ensure the container port for the main container is the same as the one exposed in your Dockerfile/service.
To deploy your container, run:
gcloud run services replace <FILENAME>.yaml
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
.
Add the volume mount to every container in your application. Choose a path such as /shared-volume
, and remember it for the next step. For example:

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.
For a list of all environment variables, including additional tags, see Environment variables.
Add a service label in Google Cloud
In your Cloud Run service’s info panel, add a label with the following key and value:
Key | Value |
---|
service | The name of your service. Matches the value provided as the DD_SERVICE environment variable. |
See Configure labels for services in the Cloud Run documentation for instructions.