Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Setup

  1. Install the Datadog Ruby tracer.

    Add the datadog gem to your Gemfile:

    source 'https://rubygems.org'
    gem 'datadog'

    See Tracing Ruby applications for additional information on how to configure the tracer and enable auto instrumentation.

  2. Install serverless-init as a sidecar.

    The Datadog Terraform module for Container Apps wraps the azurerm_container_app resource and automatically configures your Azure Container App for Datadog Serverless Monitoring by adding required environment variables and the serverless-init sidecar.

    If you don’t already have Terraform set up, install Terraform, create a new directory, and make a file called main.tf.

    Then, add the following to your Terraform configuration, updating it as necessary based on your needs:

    variable "datadog_api_key" {
      description = "Your Datadog API key"
      type        = string
      sensitive   = true
    }
    
    provider "azurerm" {
      features {}
      subscription_id = "00000000-0000-0000-0000-000000000000" // Replace with your subscription ID
    }
    
    resource "azurerm_container_app_environment" "my_env" {
        name                = "my-container-app-env" // Replace with your container app environment name
        resource_group_name = "my-resource-group"    // Replace with your resource group name
        location            = "eastus"
    }
    
    module "my_container_app" {
      source  = "DataDog/container-app-datadog/azurerm"
      version = "~> 1.0"
    
      name                         = "my-container-app" // Replace with your container app name
      resource_group_name          = "my-resource-group" // Replace with your resource group name
      container_app_environment_id = azurerm_container_app_environment.my_env.id
    
      datadog_api_key = var.datadog_api_key
      datadog_site    = "datadoghq.com" // Replace with your Datadog site
      datadog_service = "my-service"    // Replace with your service name
      datadog_env     = "dev"           // Replace with your environment (e.g. prod, staging, dev)
      datadog_version = "0.1.0"         // Replace with your application version
    
      revision_mode         = "Single"
      workload_profile_name = "Consumption"
      ingress = {
        external_enabled = true
        target_port      = 8080
        traffic_weight = [{
          percentage      = 100
          latest_revision = true
        }]
      }
      template = {
        container = [{
          cpu    = 0.5
          memory = "1Gi"
          image  = "docker.io/your-docker-image:latest" // Replace with your Docker image
          name   = "main"
        }]
      }
    }
    

    Finally, run terraform apply, and follow any prompts.

    The Datadog Container App module only deploys the Container App resource, so you need to build and push your container separately.

    See the Environment Variables for more information on the configuration options available through the env.

    Ensure the container port for the main container is the same as the one exposed in your Dockerfile/service.

    If you haven’t already, initialize your Terraform project:

    terraform init
    

    To deploy your app, run:

    terraform apply
    

    Application environment variables

    Because Azure Container Apps is built on Kubernetes, you cannot share environment variables between containers.

    NameDescription
    DD_SERVICEHow you want to tag your service. For example, sidecar-azure.
    DD_ENVHow you want to tag your env. For example, prod.
    DD_VERSIONHow you want to tag your application version.

    Sidecar container

    1. In the Azure Portal, navigate to Application > Revisions and replicas. Select Create new revision.
    2. On the Container tab, under Container image, select Add. Choose App container.
    3. In the Add a container form, provide the following:
      • Name: datadog
      • Image source: Docker Hub or other registries
      • Image type: Public
      • Registry login server: docker.io
      • Image and tag: datadog/serverless-init:latest
      • Define your container resource allocation based on your usage.
    4. Add a volume mount using replica-scoped storage. Use type “Ephemeral storage” when creating your volume. Ensure that the name and mount path matches the mount you configured in the application container.
    5. Set the environment variables in the following table:

    Sidecar Environment variables

    NameDescription
    DD_AZURE_SUBSCRIPTION_IDRequired. Your Azure subscription ID.
    DD_AZURE_RESOURCE_GROUPRequired. Your Azure resource group.
    DD_API_KEYRequired. Your Datadog API key.
    DD_SITEYour Datadog site. For example, datadoghq.com.
    DD_SERVICEHow you want to tag your service. For example, sidecar-azure.
    DD_ENVHow you want to tag your env. For example, prod.
    DD_VERSIONHow you want to tag your application version.
    DD_SERVERLESS_LOG_PATHIf using the agent for log collection, where you write your logs. For example, /LogFiles/*.log. This must match the logging path set up in Application

    Logging

    If using the Datadog Agent for log collection, add a volume mount to the sidecar container and your application containers using replica-scoped storage. Use type Ephemeral storage when creating your volume. The examples on this page use the volume name logs and the mount path /LogFiles.

    Adding a volume mount to a container in Azure
  3. Set up logs.

    In the previous step, you created a shared volume. In this step, configure your logging library to write logs to the file set in DD_SERVERLESS_LOG_PATH. You can also set a custom format for log/trace correlation and other features. Datadog recommends setting the environment variable DD_SOURCE=ruby in your sidecar container to enable advanced Datadog log parsing.

    Then, update your logging library. For example, you can use Ruby’s native logger library:

    LOG_FILE = "/LogFiles/app.log"
    FileUtils.mkdir_p(File.dirname(LOG_FILE))
    
    logger = Logger.new(LOG_FILE)
    logger.formatter = proc do |severity, datetime, progname, msg|
      "[#{datetime}] #{severity}: [#{Datadog::Tracing.log_correlation}] #{msg}\n"
    end
    
    logger.info "Hello World!"

    For more information, see Correlating Ruby Logs and Traces.

  4. Send custom metrics.

    To send custom metrics, install the DogStatsD client and view code examples. In serverless, only the distribution metric type is supported.

Environment variables

VariableDescriptionContainer
DD_API_KEYDatadog API key - RequiredSidecar container
DD_SITEDatadog site - RequiredSidecar container
DD_SERVICEDatadog Service name. RequiredBoth containers
DD_SERVERLESS_LOG_PATHThe path where the sidecar should tail logs from. Recommended to set to /shared-volume/logs/app.log.Sidecar container
DD_LOGS_INJECTIONWhen true, enrich all logs with trace data for supported loggers. See Correlate Logs and Traces for more information.Application container
DD_VERSIONSee Unified Service Tagging.Both containers
DD_ENVSee Unified Service Tagging.Both containers
DD_SOURCESet the log source to enable a Log Pipeline for advanced parsing. To automatically apply language-specific parsing rules, set to ruby, or use your custom pipeline. Defaults to containerapp.Sidecar container
DD_TAGSAdd custom tags to your logs, metrics, and traces. Tags should be comma separated in key/value format (for example: key1:value1,key2:value2).Sidecar container

Troubleshooting

This integration depends on your runtime having a full SSL implementation. If you are using a slim image, you may need to add the following command to your Dockerfile to include certificates:

RUN apt-get update && apt-get install -y ca-certificates

To have your Azure Container Apps appear in the software catalog, you must set the DD_SERVICE, DD_VERSION, and DD_ENV environment variables.

Further reading

Documentation, liens et articles supplémentaires utiles: