Sending Service Version to Datadog

Since we want service.version to reflect the version of the application code, the value cannot be configured in Terraform.

We recommend setting service.version to one of the following values.

Semantic Version

  • Example: 1.4.2
  • This approach is appropriate if your application is deployed through GitHub Releases.
    However, only a minority of applications at BYM use this workflow.

Short Git SHA

  • Example: c5f076e

  • This is the recommended option for most applications at BYM.

    The short Git SHA can be obtained using:

    git rev-parse --short HEAD
    
  • Using the full Git SHA is discouraged, as it becomes difficult to read in the Datadog UI.

Implementation

In summary, we want to configure the following values for your application:

  • OpenTelemetry: service.version as an OpenTelemetry Resource Attribute.
  • Datadog: DD_VERSION as an environment variable (only relevant for Lambda functions).

  1. Declare OTEL_SERVICE_VERSION / DD_VERSION in the Dockerfile

    Warning

    Define the ARG and ENV variables in the final stage of the Dockerfile, typically below the last FROM instruction.

    Only the final stage of the Dockerfile is included in the resulting container image, so the variables must be declared there.

    We want OTEL_SERVICE_VERSION and DD_VERSION to be available to the application.

    Note

    OTEL_SERVICE_VERSION is not an official OpenTelemetry variable. It is a BYM convention used to pass the version value to the application.

    The most important requirement is that service.version and DD_VERSION receive the correct value.

    Dockerfile for ECS Fargate Services

    ECS Fargate services use an OTEL Collector that converts OpenTelemetry conventions to Datadog conventions. Therefore, DD_VERSION does not need to be configured.

    ARG OTEL_SERVICE_VERSION="unknown"
    ENV OTEL_SERVICE_VERSION=${OTEL_SERVICE_VERSION}
    

    Dockerfile for Lambda Functions

    Lambda functions are instrumented using the Datadog Lambda Extension.

    This extension does not support OpenTelemetry conventions, so DD_VERSION must also be set.

    ARG OTEL_SERVICE_VERSION="unknown"
    ENV OTEL_SERVICE_VERSION=${OTEL_SERVICE_VERSION}
    ENV DD_VERSION=${OTEL_SERVICE_VERSION}
    
  2. Retrieve OTEL_SERVICE_VERSION in the Application Code

    In the application code, read the OTEL_SERVICE_VERSION environment variable and set it as the OpenTelemetry Resource Attribute service.version.

    Example in .NET:

    var serviceVersion = Environment.GetEnvironmentVariable("OTEL_SERVICE_VERSION") ?? "unknown";
    
    services.AddOpenTelemetry()
        .ConfigureResource(r => r
            .AddAttributes(new Dictionary<string, object>
            {
                ["service.version"] = serviceVersion
            })
        );
    
    3. Build the Docker Image

    When building the Docker image, pass OTEL_SERVICE_VERSION as a build argument.

    Example:

    docker build --build-arg OTEL_SERVICE_VERSION="$(git rev-parse --short HEAD)" .    
    

    Example: GitHub Actions

    In GitHub Actions this can be implemented as follows:

    jobs:
    build_and_push_image:
        name: Build and push image
        runs-on: ubuntu-latest
    
        permissions:
        id-token: write
        contents: read
    
        steps:
        - name: Get short Git SHA
            id: vars
            run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
    
        - name: Build and push image
            uses: BYM-IKT/github-actions/build-and-push-image-to-ecr@master
            with:
            aws-account-id: "123456789123"
            ecr-name: "kattehotell"
            docker-context-path: "Kattehotell"
            docker-build-args: |
                NUGET_AUTH_TOKEN=${{ secrets.PACKAGES_GITHUB_ACCESS_TOKEN }}
                OTEL_SERVICE_VERSION=${{ steps.vars.outputs.short_sha }}