Overview

If you instrument your services with an OpenTelemetry SDK, or a distribution built on one, set the Git information as OpenTelemetry resource attributes:

Resource attributeValue
vcs.repository.url.fullYour repository URL, for example git-provider.example/me/my-repo.
vcs.ref.head.revisionThe commit SHA used to build your application.

If you use a Datadog SDK, including with the OpenTelemetry API, follow the instructions for your language instead.

Prerequisites

Set the resource attributes

Retrieve the commit SHA by running git rev-parse HEAD at build time, then embed it in your image. Choose the option that matches how you configure your SDK:

Your setupOption
You configure your SDK with an OpenTelemetry configuration fileAdd the Git information to your configuration file (recommended)
You configure your SDK with environment variablesUse environment variables

Use this option if you configure your SDK with an OpenTelemetry declarative configuration file.

  1. Add the following entries to the resource.attributes section of your configuration file:

    resource:
      attributes:
        - name: vcs.repository.url.full
          value: ${VCS_REPOSITORY_URL_FULL}
        - name: vcs.ref.head.revision
          value: ${VCS_REF_HEAD_REVISION}
    
  2. Add the following lines to your application’s Dockerfile:

    ARG VCS_REPOSITORY_URL_FULL
    ARG VCS_REF_HEAD_REVISION
    ENV VCS_REPOSITORY_URL_FULL=${VCS_REPOSITORY_URL_FULL} \
        VCS_REF_HEAD_REVISION=${VCS_REF_HEAD_REVISION}
    
  3. Add the following arguments to your Docker build command:

    docker build . \
      -t my-application \
      --build-arg VCS_REPOSITORY_URL_FULL=<git-provider.example/me/my-repo> \
      --build-arg VCS_REF_HEAD_REVISION=$(git rev-parse HEAD)
    

Use environment variables

Use one of the following options if you configure your SDK with environment variables. To use a configuration file instead, see Migrate to a configuration file.

Both options require your SDK to read the OTEL_RESOURCE_ATTRIBUTES environment variable. Auto-instrumentation and default SDK setups read it. If you configure the SDK resource manually, include the environment resource detector.

Use this option to embed the Git information in your image at build time.

If OTEL_RESOURCE_ATTRIBUTES is set at runtime, for example in your pod spec or by the OpenTelemetry Operator, it replaces any OTEL_RESOURCE_ATTRIBUTES value set in your image. The following startup script merges the Git information defined at build time with the OTEL_RESOURCE_ATTRIBUTES value provided at runtime.

This option requires a shell in your image. Images without a shell, such as distroless or scratch images, can’t run the script. If you use the OpenTelemetry Operator with these images, use annotations instead.

  1. Save the following script as otel-env.sh next to your Dockerfile:

    #!/bin/sh
    # Add Git information to OTEL_RESOURCE_ATTRIBUTES, then start the application.
    # Attributes already set in OTEL_RESOURCE_ATTRIBUTES are kept.
    set -e
    add() {
      [ -n "$2" ] || return 0
      case ",${OTEL_RESOURCE_ATTRIBUTES}," in *",$1="*) return 0 ;; esac
      # Percent-encode "%", "," and "=" as the OpenTelemetry specification requires.
      v=$(printf '%s' "$2" | sed -e 's/%/%25/g' -e 's/,/%2C/g' -e 's/=/%3D/g')
      OTEL_RESOURCE_ATTRIBUTES="${OTEL_RESOURCE_ATTRIBUTES:+$OTEL_RESOURCE_ATTRIBUTES,}$1=$v"
    }
    add vcs.repository.url.full "$VCS_REPOSITORY_URL_FULL"
    add vcs.ref.head.revision "$VCS_REF_HEAD_REVISION"
    export OTEL_RESOURCE_ATTRIBUTES
    exec "$@"
    
  2. Add the following lines to your application’s Dockerfile. Prepend /otel-env.sh to your existing entrypoint:

    ARG VCS_REPOSITORY_URL_FULL
    ARG VCS_REF_HEAD_REVISION
    ENV VCS_REPOSITORY_URL_FULL=${VCS_REPOSITORY_URL_FULL} \
        VCS_REF_HEAD_REVISION=${VCS_REF_HEAD_REVISION}
    COPY --chmod=755 otel-env.sh /otel-env.sh
    ENTRYPOINT ["/otel-env.sh", "<your application entrypoint>"]
    
  3. Add the following arguments to your Docker build command:

    docker build . \
      -t my-application \
      --build-arg VCS_REPOSITORY_URL_FULL=<git-provider.example/me/my-repo> \
      --build-arg VCS_REF_HEAD_REVISION=$(git rev-parse HEAD)
    

Note: If your Kubernetes pod spec sets command, it replaces the image’s ENTRYPOINT. In that case, add /otel-env.sh as the first element of command.

Use this option if you instrument Kubernetes workloads with the OpenTelemetry Operator, or if you use the OpenTelemetry Collector’s K8s Attributes processor with otel_annotations: true, and your deployment tooling can provide the commit SHA at deployment time.

Add the following annotations to your pod template:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-application
spec:
  template:
    metadata:
      annotations:
        resource.opentelemetry.io/vcs.repository.url.full: "<git-provider.example/me/my-repo>"
        resource.opentelemetry.io/vcs.ref.head.revision: "<commitSha>"

Replace <git-provider.example/me/my-repo> with your repository URL, and have your deployment tooling replace <commitSha> with the commit SHA of the deployed build. The Operator copies annotation values into OTEL_RESOURCE_ATTRIBUTES without encoding them, so percent-encode any %, ,, or = in your repository URL as %25, %2C, or %3D.

Note: The Operator ignores these annotations for attributes that are already set in the container’s OTEL_RESOURCE_ATTRIBUTES environment variable.

Migrate to a configuration file

If your SDK supports declarative configuration, you can migrate from environment variables to a configuration file:

  1. Check that your SDK supports declarative configuration in the OpenTelemetry compliance matrix. Support is experimental in some SDKs.
  2. Follow the OpenTelemetry migration instructions. The migration configuration file maps the environment variables you already use to the configuration file format.
  3. Add the Git information to your configuration file.

Further reading