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.
Recommended Version Formats
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:
-
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.versionas an OpenTelemetry Resource Attribute. - Datadog:
DD_VERSIONas an environment variable (only relevant for Lambda functions).
-
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.
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.
-
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:
3. Build the Docker Imagevar serviceVersion = Environment.GetEnvironmentVariable("OTEL_SERVICE_VERSION") ?? "unknown"; services.AddOpenTelemetry() .ConfigureResource(r => r .AddAttributes(new Dictionary<string, object> { ["service.version"] = serviceVersion }) );When building the Docker image, pass OTEL_SERVICE_VERSION as a build argument.
Example:
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 }}