Tracing
In your main application, add the dd-trace-js
library. See Tracing Node.js applications for instructions.
Set ENV NODE_OPTIONS="--require dd-trace/init"
. This specifies that the dd-trace/init
module is required when the Node.js process starts.
Profiling
The profiler is shipped within Datadog tracing libraries. If you are already using APM to collect traces for your application, you can skip installing the library and proceed to enabling the profiler. See Enabling the Node.js Profiler to add the environment variables.
Metrics
The tracing library also collects custom metrics. See the code examples.
Logs
The Datadog sidecar collects logs through a shared volume. To forward logs from your main container to the sidecar, configure your application to write all logs to a location such as shared-volume/logs/*.log
using the steps below. You must follow the setup in the GCP UI to add the environment variable DD_SERVERLESS_LOG_PATH
and a shared Volume Mount to both the main and sidecar container.
To set up logging in your application, see Node.js Log Collection. To set up trace log correlation, see Correlating Node.js Logs and Traces.
Tracing
Add functions-framework-api
and other dependencies like java-dogstatsd-client
to your pom.xml
.
Example pom.xml
:
<?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>functions</groupId>
<artifactId>functions-hello-world</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<artifactId>libraries-bom</artifactId>
<groupId>com.google.cloud</groupId>
<scope>import</scope>
<type>pom</type>
<version>26.32.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
</properties>
<dependencies>
<!-- Required for Function primitives -->
<dependency>
<groupId>com.google.cloud.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>com.google.cloud.functions.invoker</groupId>
<artifactId>java-function-invoker</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.datadoghq</groupId>
<artifactId>java-dogstatsd-client</artifactId>
<version>4.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.19.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!--
Google Cloud Functions Framework Maven plugin
This plugin allows you to run Cloud Functions Java code
locally. Use the following terminal command to run a
given function locally:
mvn function:run -Drun.functionTarget=your.package.yourFunction
-->
<groupId>com.google.cloud.functions</groupId>
<artifactId>function-maven-plugin</artifactId>
<version>0.11.0</version>
<configuration>
<functionTarget>functions.HelloWorld</functionTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Run mvn clean package
to update the target
directory with the new .jar
used in your Dockerfile.
As an alternative to the provided Dockerfile, you can use Artifact Registry to store the images built from your function source code. You can use
Google Cloud Build or
Buildpacks to build and deploy your image.
For example:
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
Add dd-java-agent.jar
and java-function-invoker.jar
to your Dockerfile.
Cloud Run Function code runs with a classpath that includes the function code and its dependencies. The Maven plugin automatically determines the classpath based on the dependencies in pom.xml
.
If invoking the Functions Framework directly with the Datadog Agent, update your Dockerfile ENTRYPOINT
to include the --classpath
and --target
options, along with the Java agent flag -javaagent:dd-java-agent.jar
:
java -javaagent:dd-java-agent.jar -jar java-function-invoker-1.3.2 \
--classpath 'FUNCTION_JAR' \
--target 'FUNCTION_TARGET'
- Replace
FUNCTION_JAR
with the target JAR generated from the Maven build, including all dependencies. - Replace
FUNCTION_TARGET
with the function’s entry point (for example, gcfv2.HelloworldApplication
).
Example Dockerfile
:
# Download Datadog Java Agent
FROM maven:3.8.3-openjdk-17 AS build
# Set working directory
WORKDIR /
# Download the required Maven dependency
RUN mvn dependency:get -Dartifact=com.google.cloud.functions.invoker:java-function-invoker:1.4.0 \
&& mvn dependency:copy -Dartifact=com.google.cloud.functions.invoker:java-function-invoker:1.4.0 -DoutputDirectory=/
FROM openjdk:17-jdk
# Set the working directory in the container
WORKDIR /
ADD 'https://dtdg.co/latest-java-tracer' dd-java-agent.jar
COPY --from=build java-function-invoker-1.4.0.jar java-function-invoker.jar
# Copy the JAR file into the container
COPY target/functions-hello-world-1.0.0-SNAPSHOT.jar helloworld.jar
ENV JAVA_OPTS=-javaagent:dd-java-agent.jar
# Expose the port (Cloud Run automatically assigns the actual port via $PORT)
ENV PORT=8080
EXPOSE 8080 8125/udp
ENTRYPOINT ["java","-javaagent:/dd-java-agent.jar", "-jar", "/java-function-invoker.jar","--classpath", "/helloworld.jar","--target", "functions.HelloWorld"]
To deploy the Java function, run the following command from the top-level directory containing your pom.xml
and Dockerfile
:
gcloud beta run deploy FUNCTION_NAME \
--source . \
--function FUNCTION_TARGET \
--clear-base-image \
--region REGION
- Replace
REGION
with the region where you want to deploy the function. - Replace
FUNCTION_TARGET
with your function entry point. For example, gcfv2.HelloworldApplication
. - Replace
FUNCTION_NAME
with the name of your Cloud Run function. - Ensure that you set –clear-base-image to deploy your Cloud Function with the Dockerfile.
When setting up your containers, use the same container image deployed in the previous steps.
Profiling
The profiler is shipped within Datadog tracing libraries. If you are already using APM to collect traces for your application, you can skip installing the library and proceed to enabling the profiler. See Enabling the Java Profiler to add the environment variables.
Metrics
To collect custom metrics, install the Java DogStatsD client.
Logs
The Datadog sidecar collects logs through a shared volume. To forward logs from your main container to the sidecar, configure your application to write all logs to a location such as shared-volume/logs/*.log
using the steps below. You must follow the setup in the GCP UI to add the environment variable DD_SERVERLESS_LOG_PATH
and a shared Volume Mount to both the main and sidecar container.
To set up logging in your application, see Java Log Collection. To set up trace log correlation, see Correlating Java Logs and Traces.
Tracing
In your main application, add the dd-trace-go
library. See Tracing Go Applications for instructions.
Profiling
The profiler is shipped within Datadog tracing libraries. If you are already using APM to collect traces for your application, you can skip installing the library and proceed to enabling the profiler. See Enabling the Go Profiler to add the environment variables.
Metrics
The tracing library also collects custom metrics. See the code examples.
Logs
The Datadog sidecar collects logs through a shared volume. To forward logs from your main container to the sidecar, configure your application to write all logs to a location such as shared-volume/logs/*.log
using the steps below. You must follow the setup in the GCP UI to add the environment variable DD_SERVERLESS_LOG_PATH
and a shared Volume Mount to both the main and sidecar container.
To set up logging in your application, see Go Log Collection. To set up trace log correlation, see Correlating Go Logs and Traces.
Tracing
In your main application, add the .NET tracing library. See Tracing .NET Applications for instructions.
Profiling
The profiler is shipped within Datadog tracing libraries. If you are already using APM to collect traces for your application, you can skip installing the library and proceed to enabling the profiler. See Enabling the .NET Profiler to add the environment variables.
Metrics
The tracing library also collects custom metrics. See the code examples.
Logs
The Datadog sidecar collects logs through a shared volume. To forward logs from your main container to the sidecar, configure your application to write all logs to a location such as shared-volume/logs/*.log
using the steps below. You must follow the setup in the GCP UI to add the environment variable DD_SERVERLESS_LOG_PATH
and a shared Volume Mount to both the main and sidecar container.
To set up logging in your application, see C# Log Collection. To set up trace log correlation, see Correlating .NET Logs and Traces.