Overview

To embed Git information in your Java build artifacts, follow the instructions for your deployment model: Containers, Serverless, or Host.

Prerequisites

  • Datadog Agent v7.35.0 or later is required.
  • The Java client library version 1.48.0 or later is required for embedded Git properties. Version 1.12.0 or later is required for other options.

Containers

If you are using Docker containers, you have the following options: embedding Git properties in your build artifact, using Docker, or configuring your application with DD_GIT_* environment variables.

If you use Spring Boot, add a build plugin to embed git information in your JAR. The Datadog Java tracer reads the git.properties file that Spring Boot Actuator generates at startup.

Maven

Add the following plugin to your pom.xml:

<plugin>
    <groupId>io.github.git-commit-id</groupId>
    <artifactId>git-commit-id-maven-plugin</artifactId>
    <version>9.0.2</version>
    <executions>
        <execution>
            <id>get-the-git-infos</id>
            <goals>
                <goal>revision</goal>
            </goals>
            <phase>initialize</phase>
        </execution>
    </executions>
    <configuration>
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
        <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
        <commitIdGenerationMode>full</commitIdGenerationMode>
    </configuration>
</plugin>

Gradle

Add the following to your build.gradle.kts:

plugins {
    id("org.springframework.boot") version "2.5.7" // required
    // ...
    id("com.gorylenko.gradle-git-properties") version "2.5.7"
}
Both plugins require access to the .git folder at build time.

You can embed git information in your Docker image using either build arguments with environment variables, or image labels.

Using build arguments and environment variables
  1. Add the following lines to your application’s Dockerfile:

    ARG DD_GIT_REPOSITORY_URL
    ARG DD_GIT_COMMIT_SHA
    ENV DD_GIT_REPOSITORY_URL=${DD_GIT_REPOSITORY_URL} 
    ENV DD_GIT_COMMIT_SHA=${DD_GIT_COMMIT_SHA}
    
  2. Add the following arguments to your Docker build command:

    docker build . \
     -t my-application \
     --build-arg DD_GIT_REPOSITORY_URL=<git-provider.example/me/my-repo> \
     --build-arg DD_GIT_COMMIT_SHA=$(git rev-parse HEAD)
    
Using image labels

This approach requires Docker, or containerd >= 1.5.6. It doesn’t support containers running on AWS Fargate.

Datadog can extract source code information directly from your images’ Docker labels. During build time, follow the Open Containers standard to add the git commit SHA and repository URL as Docker labels:

docker build . \
  -t my-application \
  --label org.opencontainers.image.revision=$(git rev-parse HEAD) \
  --label org.opencontainers.image.source=$(git config --get remote.origin.url)

Configure your application with the DD_GIT_* environment variables:

export DD_GIT_COMMIT_SHA="<commitSha>"
export DD_GIT_REPOSITORY_URL="<git-provider.example/me/my-repo>"

Replace <commitSha> with the commit SHA used to build your application. You can retrieve this by running git rev-parse HEAD at build time, and it needs to be passed into the runtime environment variables. Replace <git-provider.example/me/my-repo> with your repository URL.

Serverless

If you are using Serverless, you have the following options depending on your serverless application’s setup.

If you use Spring Boot, add a build plugin to embed git information in your JAR. The Datadog Java tracer reads the git.properties file that Spring Boot Actuator generates at startup.

Maven

Add the following plugin to your pom.xml:

<plugin>
    <groupId>io.github.git-commit-id</groupId>
    <artifactId>git-commit-id-maven-plugin</artifactId>
    <version>9.0.2</version>
    <executions>
        <execution>
            <id>get-the-git-infos</id>
            <goals>
                <goal>revision</goal>
            </goals>
            <phase>initialize</phase>
        </execution>
    </executions>
    <configuration>
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
        <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
        <commitIdGenerationMode>full</commitIdGenerationMode>
    </configuration>
</plugin>

Gradle

Add the following to your build.gradle.kts:

plugins {
    id("org.springframework.boot") version "2.5.7" // required
    // ...
    id("com.gorylenko.gradle-git-properties") version "2.5.7"
}
Both plugins require access to the .git folder at build time.
Datadog CLI tool
Use the datadog-ci client version 2.10.0 or later. You must run the CLI tool in the same directory as the code repository.
Datadog Serverless Plugin
Use the plugin version 5.60.0 or later.
Datadog CDK Construct
Use the datadog-cdk-constructs version 0.8.5 or later for AWS CDK v1.
Use the datadog-cdk-constructs version 1.4.0 or later for AWS CDK v2.

Configure your application with the DD_GIT_* environment variables:

export DD_GIT_COMMIT_SHA="<commitSha>"
export DD_GIT_REPOSITORY_URL="<git-provider.example/me/my-repo>"

Replace <commitSha> with the commit SHA used to build your application. You can retrieve this by running git rev-parse HEAD at build time, and it needs to be passed into the runtime environment variables. Replace <git-provider.example/me/my-repo> with your repository URL.

Host

If you are using a host, you have the following options.

If you use Spring Boot, add a build plugin to embed git information in your JAR. The Datadog Java tracer reads the git.properties file that Spring Boot Actuator generates at startup.

Maven

Add the following plugin to your pom.xml:

<plugin>
    <groupId>io.github.git-commit-id</groupId>
    <artifactId>git-commit-id-maven-plugin</artifactId>
    <version>9.0.2</version>
    <executions>
        <execution>
            <id>get-the-git-infos</id>
            <goals>
                <goal>revision</goal>
            </goals>
            <phase>initialize</phase>
        </execution>
    </executions>
    <configuration>
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
        <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
        <commitIdGenerationMode>full</commitIdGenerationMode>
    </configuration>
</plugin>

Gradle

Add the following to your build.gradle.kts:

plugins {
    id("org.springframework.boot") version "2.5.7" // required
    // ...
    id("com.gorylenko.gradle-git-properties") version "2.5.7"
}
Both plugins require access to the .git folder at build time.

Configure your application with the DD_GIT_* environment variables:

export DD_GIT_COMMIT_SHA="<commitSha>"
export DD_GIT_REPOSITORY_URL="<git-provider.example/me/my-repo>"

Replace <commitSha> with the commit SHA used to build your application. You can retrieve this by running git rev-parse HEAD at build time, and it needs to be passed into the runtime environment variables. Replace <git-provider.example/me/my-repo> with your repository URL.