Legacy Google Cloud Run Functions Gen 1

このページは日本語には対応しておりません。随時翻訳に取り組んでいます。
翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください

Overview

This page explains how to collect traces, trace metrics, runtime metrics, and custom metrics from your Cloud Run Functions Gen 1 (formerly known as Cloud Functions) utilizing serverless compatibility layer.

This page is only for legacy 1st Gen Cloud Run Functions. For Gen 2 support, see 2nd Gen Functions, and to collect additional metrics, install the Google Cloud integration.

Google has integrated Cloud Run functions into Cloud Run UI. Starting August 2025, creating legacy 1st Gen functions will only be possible using the Google Cloud CLI, API, or Terraform. Datadog recommends upgrading your cloud run function to Gen 2 for more features and Datadog support. Reach out to Google for more information on the migration to Cloud Run.

Setup

  1. Install dependencies. Run the following commands:

    npm install @datadog/serverless-compat
    npm install dd-trace
    

    Datadog recommends pinning the package versions and regularly upgrading to the latest versions of both @datadog/serverless-compat and dd-trace to ensure you have access to enhancements and bug fixes.

    For more information, see Tracing Node.js Applications.

  2. Start the Datadog serverless compatibility layer and initialize the Node.js tracer. Add the following lines to your main application entry point file (for example, app.js):

    require('@datadog/serverless-compat').start();
    
    // This line must come before importing any instrumented module. 
    const tracer = require('dd-trace').init()
    
  3. (Optional) Enable runtime metrics. See Node.js Runtime Metrics.

  4. (Optional) Enable custom metrics. See Metric Submission: DogStatsD.

  1. Deploy your function. Use gcloud or Google Console to deploy your 1st Gen Cloud Run Function:

    Follow Google Cloud’s Deploy a Cloud Run function (1st gen) instructions to utilize gcloud function deploy <FUNCTION_NAME> --no-gen2 to deploy a 1st Gen Cloud Run Function.

    Use the --source flag to point to the directory of your function code with dd-java-agent.jar and dd-serverless-compat-java-agent.jar at the top level.

    For more information, see Google Cloud’s gcloud functions deploy documentation for more flags for the gcloud command.

  2. Configure Datadog intake. Add the following environment variables to your function’s application settings:

    NameValue
    DD_API_KEYYour Datadog API key.
    DD_SITEYour Datadog site. For example, datadoghq.com.
  3. Configure Unified Service Tagging. You can collect metrics from your Cloud Run Function by installing the Google Cloud integration. To correlate these metrics with your traces, first set the env, service, and version tags on your resource in Google Cloud. Then, configure the following environment variables. You can add custom tags as DD_TAGS.

    NameValue
    DD_ENVHow you want to tag your env for Unified Service Tagging. For example, prod.
    DD_SERVICEHow you want to tag your service for Unified Service Tagging.
    DD_VERSIONHow you want to tag your version for Unified Service Tagging.
    DD_TAGSYour comma-separated custom tags. For example, key1:value1,key2:value2.
  4. Add Service Label in the info panel. Tag your GCP entity with the service label to correlate your traces with your service:

    Add the same value from DD_SERVICE to a service label on your cloud function, inside the info panel of your function.

    NameValue
    serviceThe name of your service matching the DD_SERVICE env var.

    For more information on how to add labels, see Google Cloud’s Configure labels for services documentation.

Example Functions

The following example contains a sample function with tracing and metrics set up.

// Example of a simple Cloud Run Function with traces and custom metrics
// dd-trace must come before any other import. 
const tracer = require('dd-trace').init();

const functions = require('@google-cloud/functions-framework');

function handler(req, res) {
   tracer.dogstatsd.increment('dd.function.sent', 1, {'runtime':'nodejs'});
   return res.send('Welcome to Datadog💜!');
}

functions.http('httpexample',  handlerWithTrace)
const handlerWithTrace = tracer.wrap('example-span', handler)

module.exports = handlerWithTrace

What’s next?

Enable/disable trace metrics

Trace metrics are enabled by default. To configure trace metrics, use the following environment variable:

DD_TRACE_STATS_COMPUTATION_ENABLED
Enables (true) or disables (false) trace metrics. Defaults to true.

Values: true, false

Troubleshooting

Enable debug logs

You can collect debug logs for troubleshooting. To configure debug logs, use the following environment variables:

DD_TRACE_DEBUG
Enables (true) or disables (false) debug logging for the Datadog Tracing Library. Defaults to false.

Values: true, false

DD_LOG_LEVEL
Sets logging level for the Datadog Serverless Compatibility Layer. Defaults to info.

Values: trace, debug, info, warn, error, critical, off

PREVIEWING: yiming.luo/fix-lambda-doc