---
title: Embed Git Information with OpenTelemetry SDKs
description: >-
  Set Git information as OpenTelemetry resource attributes to link services
  instrumented with OpenTelemetry SDKs to source code.
breadcrumbs: >-
  Docs > Source Code Integration > Service Mapping for Source Code Integration >
  Embed Git Information with OpenTelemetry SDKs
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Embed Git Information with OpenTelemetry SDKs

## Overview{% #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 attribute        | Value                                                               |
| ------------------------- | ------------------------------------------------------------------- |
| `vcs.repository.url.full` | Your repository URL, for example `git-provider.example/me/my-repo`. |
| `vcs.ref.head.revision`   | The commit SHA used to build your application.                      |

If you use a Datadog SDK, including with the OpenTelemetry API, follow the [instructions for your language](https://docs.datadoghq.com/source_code/service-mapping.md#embed-git-information-in-your-build-artifacts) instead.

## Prerequisites{% #prerequisites %}

- Your services are instrumented with an OpenTelemetry SDK. See [Instrument Your Applications](https://docs.datadoghq.com/opentelemetry/instrument.md).
- Your OpenTelemetry data reaches Datadog. See [Send OpenTelemetry Data to Datadog](https://docs.datadoghq.com/opentelemetry/setup.md).

## Set the resource attributes{% #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 setup                                                      | Option                                                           |
| --------------------------------------------------------------- | ---------------------------------------------------------------- |
| You configure your SDK with an OpenTelemetry configuration file | Add the Git information to your configuration file (recommended) |
| You configure your SDK with environment variables               | Use environment variables                                        |

### Add the Git information to your configuration file (recommended){% #add-the-git-information-to-your-configuration-file-recommended %}

Use this option if you configure your SDK with an OpenTelemetry [declarative configuration](https://opentelemetry.io/docs/languages/sdk-configuration/declarative-configuration/) file.

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

   ```yaml
   resource:
     attributes:
       - name: vcs.repository.url.full
         value: ${VCS_REPOSITORY_URL_FULL}
       - name: vcs.ref.head.revision
         value: ${VCS_REF_HEAD_REVISION}
   ```

1. Add the following lines to your application's Dockerfile:

   ```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}
   ```

1. Add the following arguments to your Docker build command:

   ```shell
   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-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.

{% collapsible-section %}
#### Add resource attributes at startup

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](https://opentelemetry.io/docs/platforms/kubernetes/operator/automatic/), 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:

   ```sh
   #!/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 "$@"
   ```

1. Add the following lines to your application's Dockerfile. Prepend `/otel-env.sh` to your existing entrypoint:

   ```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}
   COPY --chmod=755 otel-env.sh /otel-env.sh
   ENTRYPOINT ["/otel-env.sh", "<your application entrypoint>"]
   ```

1. Add the following arguments to your Docker build command:

   ```shell
   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`.
{% /collapsible-section %}

{% collapsible-section %}
#### Set resource attributes with OpenTelemetry Kubernetes pod annotations

Use this option if you instrument Kubernetes workloads with the [OpenTelemetry Operator](https://opentelemetry.io/docs/platforms/kubernetes/operator/automatic/), or if you use the OpenTelemetry Collector's [K8s Attributes processor](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/k8sattributesprocessor) with `otel_annotations: true`, and your deployment tooling can provide the commit SHA at deployment time.

Add the following [annotations](https://opentelemetry.io/docs/platforms/kubernetes/operator/automatic/#configure-resource-attributes-with-annotations) to your pod template:

```yaml
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.
{% /collapsible-section %}

### Migrate to a configuration file{% #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](https://github.com/open-telemetry/opentelemetry-specification/blob/main/spec-compliance-matrix.md#declarative-configuration). Support is experimental in some SDKs.
1. Follow the OpenTelemetry [migration instructions](https://opentelemetry.io/docs/languages/sdk-configuration/declarative-configuration/#migration-configuration). The migration configuration file maps the environment variables you already use to the configuration file format.
1. Add the Git information to your configuration file.

## Further reading{% #further-reading %}

Additional helpful documentation, links, and articles:

- [Learn about OpenTelemetry in Datadog](https://docs.datadoghq.com/opentelemetry.md)
- [OpenTelemetry declarative configuration](https://opentelemetry.io/docs/languages/sdk-configuration/declarative-configuration/)
