![]() |
VOOZH | about |
This page explains how to collect traces, trace metrics, runtime metrics, and custom metrics from your Cloud Run functions (1st gen), previously known as Cloud Functions.
cloud asset viewer role to your service account and enable the Cloud Asset Inventory API in Google Cloud.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.
Start the Datadog Serverless Compatibility Layer and initialize the Datadog Node.js SDK.
Use the --require option to load and initialize the Serverless Compatibility Layer and the Datadog Node.js SDK in one step.
NODE_OPTIONS='--require @datadog/serverless-compat/init --require dd-trace/init'
(Optional) Enable runtime metrics. See Node.js Runtime Metrics.
(Optional) Enable custom metrics. See Metric Submission: DogStatsD.
Install dependencies. Run the following commands:
pip install datadog-serverless-compat
pip install ddtrace
Datadog recommends using the latest versions of both datadog-serverless-compat and ddtrace to ensure you have access to enhancements and bug fixes.
For more information, see Tracing Python Applications.
Initialize the Datadog Python SDK and Serverless Compatibility Layer. Add the following lines to your main application entry point file:
from datadog_serverless_compat import start
from ddtrace import tracer, patch_all
start()
patch_all()
(Optional) Enable runtime metrics. See Python Runtime Metrics.
(Optional) Enable custom metrics. See Metric Submission: DogStatsD.
Install dependencies. Download the Datadog Java SDK and Serverless Compatibility Layer:
Download dd-java-agent.jar and dd-serverless-compat-java-agent.jar that contains the latest SDK class files, to a folder that is accessible by your Datadog user:
wget -O /path/to/dd-java-agent.jar 'https://dtdg.co/latest-java-tracer'
wget -O /path/to/dd-serverless-compat-java-agent.jar 'https://dtdg.co/latest-serverless-compat-java-agent'
For alternative ways to download the agent, see the Datadog Java Agent documentation.
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 SDK 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 SDK and the Serverless Compatibility Layer.
| Name | Value |
|---|---|
JAVA_TOOL_OPTIONS | -javaagent:/path/to/dd-serverless-compat-java-agent.jar -javaagent:/path/to/dd-java-agent.jar |
(Optional) Enable runtime metrics. See Java Runtime Metrics.
(Optional) Enable custom metrics. See Metric Submission: DogStatsD.
Install dependencies. Run the following commands:
go get github.com/DataDog/datadog-serverless-compat-go/datadogserverlesscompat
go get gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer
Datadog recommends pinning the package versions and regularly upgrading to the latest versions of both datadog-serverless-compat-go/datadogserverlesscompat and dd-trace-go.v1/ddtrace/trace to ensure you have access to enhancements and bug fixes.
For more information, see Tracing Go Applications and Datadog Serverless Compatibility Layer for Go.
Start the Datadog Serverless Compatibility Layer and initialize the Go SDK. Add the following lines to your main application entry point file (for example, main.go):
import("github.com/DataDog/datadog-go/statsd""github.com/DataDog/datadog-serverless-compat-go/datadogserverlesscompat""gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer")funcinit(){datadogserverlesscompat.Start()tracer.Start(tracer.WithAgentAddr("127.0.0.1:8126"))dogstatsdClient,_=statsd.New("127.0.0.1:8125")functions.HTTP("helloHTTP",helloHTTP)}(Optional) Enable runtime metrics. See Go Runtime Metrics.
(Optional) Enable custom metrics. See Metric Submission: DogStatsD.
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:
| Name | Value |
|---|---|
DD_API_KEY | Your Datadog API key. |
DD_SITE | Your Datadog site. For example, . |
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.
| Name | Value |
|---|---|
DD_ENV | How you want to tag your env for Unified Service Tagging. For example, prod. |
DD_SERVICE | How you want to tag your service for Unified Service Tagging. |
DD_VERSION | How you want to tag your version for Unified Service Tagging. |
DD_TAGS | Your comma-separated custom tags. For example, key1:value1,key2:value2. |
DD_SITE | Datadog site - Set this tag if you are in a different site. Default is US1 datadoghq.com |
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.
| Name | Value |
|---|---|
service | The 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.
Do not set the following environment variables in your serverless environment. They should only be set in non-serverless environments.
DD_AGENT_HOSTDD_TRACE_AGENT_URLThe following examples contain a sample function with tracing and metrics set up.
// Example of a simple Cloud Run Function with custom traces and custom metrics
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💜!');
}
const handlerWithTrace = tracer.wrap('example-span', handler)
functions.http('httpexample', handlerWithTrace)
module.exports = handlerWithTrace
# Example of a simple Cloud Run Function with traces and custom metrics
import functions_framework
import ddtrace
from datadog import initialize, statsd
ddtrace.patch(logging=True)
initialize(**{'statsd_port': 8125})
@ddtrace.tracer.wrap()
@functions_framework.http
def dd_log_forwader(request):
with ddtrace.tracer.trace('sending_trace') as span:
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;publicclass ExampleimplementsHttpFunction{@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 SDK using the following Maven dependency:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cloudfunctions</groupId>
<artifactId>http-function</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>com.google.cloud.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>com.datadoghq</groupId>
<artifactId>java-dogstatsd-client</artifactId>
<version>4.4.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<excludes>
<exclude>.google/</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>initialize</phase>
<configuration>
<tasks>
<get src="https://dtdg.co/latest-serverless-compat-java-agent" dest="dd-serverless-compat-java-agent.jar" />
<get src="https://dtdg.co/latest-java-tracer" dest="dd-java-agent.jar" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
#ExampleofasimpleCloudRunFunctionwithtracesandcustommetricspackagecloud_functionimport("fmt""log""net/http""github.com/DataDog/datadog-go/statsd""github.com/DataDog/datadog-serverless-compat-go/datadogserverlesscompat"_"github.com/GoogleCloudPlatform/functions-framework-go/funcframework""github.com/GoogleCloudPlatform/functions-framework-go/functions""gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer")vardogstatsdClient*statsd.Clientfuncinit(){err:=datadogserverlesscompat.Start()log.Println("Starting datadog serverless compat: ",err)tracer.Start(tracer.WithAgentAddr("127.0.0.1:8126"))dogstatsdClient,_=statsd.New("127.0.0.1:8125")functions.HTTP("helloHTTP",helloHTTP)}// helloHTTP is an HTTP Cloud Function with a request parameter.funchelloHTTP(whttp.ResponseWriter,r*http.Request){span:=tracer.StartSpan("TEST-SPAN")deferspan.Finish()err:=dogstatsdClient.Incr("nina.test.counter",[]string{"runtime:go"},1)iferr!=nil{log.Println("Error incrementing dogstatsd counter: ",err)}fmt.Fprint(w,"Hello Datadog!")}DD_SERVICE environment variable to see your traces.You can collect debug logs for troubleshooting. To configure debug logs, use the following environment variables:
DD_TRACE_DEBUGtrue) or disables (false) debug logging for the Datadog SDK. Defaults to false.Values: true, false
DD_LOG_LEVELinfo.Values: trace, debug, info, warn, error, critical, off
| |