This page is not yet available in Spanish. We are working on its translation. If you have any questions or feedback about our current translation project, feel free to reach out to us!
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.
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.
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.
consttracer=require('dd-trace').init()
Install dependencies. Download the Datadog Java tracer and serverless compatibility layer:
Download dd-java-agent.jar and dd-serverless-compat-java-agent.jar that contains the latest tracer class files, to a folder that is accessible by your Datadog user:
Datadog recommends using the latest versions of both datadog-serverless-compat and dd-java-agent to ensure you have access to enhancements and bug fixes.
Initialize the Datadog Java tracer and serverless compatibility layer. Add JAVA_TOOL_OPTIONS to your runtime environment variable:
Implement and Auto instrument the Java tracer by setting the Runtime environment variable to instrument your Java cloud function with the Datadog Java tracer and the serverless compatibility layer.
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.
Configure Datadog intake. Add the following environment variables to your function’s application settings:
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.
// Example of a simple Cloud Run Function with traces and custom metrics
// dd-trace must come before any other import.
consttracer=require('dd-trace').init();constfunctions=require('@google-cloud/functions-framework');functionhandler(req,res){tracer.dogstatsd.increment('dd.function.sent',1,{'runtime':'nodejs'});returnres.send('Welcome to Datadog💜!');}functions.http('httpexample',handlerWithTrace)consthandlerWithTrace=tracer.wrap('example-span',handler)module.exports=handlerWithTrace
# Example of a simple Cloud Run Function with traces and custom metricsimportfunctions_frameworkimportddtracefromdatadogimportinitialize,statsdddtrace.patch(logging=True)initialize(**{'statsd_port':8125})@ddtrace.tracer.wrap()@functions_framework.httpdefdd_log_forwader(request):withddtrace.tracer.trace('sending_trace')asspan:span.set_tag('test','ninja')statsd.increment('dd.function.sent',tags=["runtime:python"])return"Welcome To Datadog! 💜"
// Example of a simple Cloud Run Function with traces and custom metricspackagecom.example;importcom.google.cloud.functions.HttpFunction;importcom.google.cloud.functions.HttpRequest;importcom.google.cloud.functions.HttpResponse;importjava.io.BufferedWriter;importcom.timgroup.statsd.NonBlockingStatsDClientBuilder;importcom.timgroup.statsd.StatsDClient;publicclassExampleimplementsHttpFunction{@Overridepublicvoidservice(HttpRequestrequest,HttpResponseresponse)throwsException{StatsDClientStatsd=newNonBlockingStatsDClientBuilder().hostname("localhost").port(8125).build();BufferedWriterwriter=response.getWriter();Statsd.incrementCounter("dd.function.sent",newString[]{"runtime:java"});writer.write("Welcome to Datadog!");}}
You can also install the tracer using the following Maven dependency:
You can view your Cloud Run Functions traces in Trace Explorer. Search for the service name you set in the DD_SERVICE environment variable to see your traces.