A sample application is available on GitHub.

Setup

  1. Install the Datadog Python tracer.

    Add ddtrace to your requirements.txt or pyproject.toml. This ensures the tracer is included in your container image when it is built and deployed. You can find the latest version on PyPI:

    requirements.txt

    ddtrace==<VERSION>

    For more information, see Tracing Python applications.

  2. Install serverless-init as a sidecar.

    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.

    After deploying your Cloud Run app, you can manually modify your app’s settings to enable Datadog monitoring.

    1. Create a Volume with In-Memory volume type.

    2. 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.

    3. Add the volume mount to every container in your application. Choose a path such as /shared-volume, and remember it for the next step.

    4. 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.

  3. Set up logs.

    In the previous step, you created a shared volume. You may have also set the DD_SERVERLESS_LOG_PATH environment variable, which defaults to /shared-volume/logs/app.log.

    In this step, configure your logging library to write logs to the file set in DD_SERVERLESS_LOG_PATH. You can also set a custom format for log/trace correlation and other features. Datadog recommends setting the following environment variables:

    • PYTHONUNBUFFERED=1: In your main container. Ensure Python outputs appear immediately in container logs instead of being buffered.
    • DD_LOGS_INJECTION=true: In your main container. Enable log/trace correlation for supported loggers.
    • DD_SOURCE=python: In your sidecar container. Enable advanced Datadog log parsing.

    Then, update your logging library. For example, you can use Python’s native logging library:

    LOG_FILE = "/shared-logs/logs/app.log"
    os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True)
    
    FORMAT = ('%(asctime)s %(levelname)s [%(name)s] [%(filename)s:%(lineno)d] '
            '[dd.service=%(dd.service)s dd.env=%(dd.env)s dd.version=%(dd.version)s dd.trace_id=%(dd.trace_id)s dd.span_id=%(dd.span_id)s] '
            '- %(message)s')
    
    logging.basicConfig(
        level=logging.INFO,
        format=FORMAT,
        handlers=[
            logging.FileHandler(LOG_FILE),
            logging.StreamHandler(sys.stdout)
        ]
    )
    logger = logging.getLogger(__name__)
    logger.level = logging.INFO
    
    logger.info('Hello world!')

    For more information, see Correlating Python Logs and Traces.

  4. Add a service label in Google Cloud. In your Cloud Run service’s info panel, add a label with the following key and value:

    KeyValue
    serviceThe 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.

  1. Send custom metrics.

    To send custom metrics, install the DogStatsD client and view code examples. In Serverless Monitoring, only the distribution metric type is supported.

Environment variables

VariableDescriptionContainer
DD_API_KEYDatadog API key - RequiredSidecar container
DD_SITEDatadog site - RequiredSidecar container
DD_SERVICEDatadog Service name. RequiredBoth containers
DD_SERVERLESS_LOG_PATHThe path where the sidecar should tail logs from. Recommended to set to /shared-volume/logs/app.log.Sidecar container
DD_LOGS_INJECTIONWhen true, enrich all logs with trace data for supported loggers. See Correlate Logs and Traces for more information.Application container
DD_VERSIONSee Unified Service Tagging.Both containers
DD_ENVSee Unified Service Tagging.Both containers
DD_SOURCESet 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_TAGSAdd 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_TARGETRequired 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

Troubleshooting

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.

If you are missing logs or traces during container shutdown, specify a container start up order to make your main container depend on the sidecar container.

Further reading