このページは日本語には対応しておりません。随時翻訳に取り組んでいます。
翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください

Setup

  1. Install the Datadog Node.js tracer.

    1. In your main application, add dd-trace-js.

      npm install dd-trace --save
    2. Add the following to your application code to initialize the tracer:

      const tracer = require('dd-trace').init({
       logInjection: true,
      });

    3. Set the following environment variable to specify that the dd-trace/init module is required when the Node.js process starts:

      ENV NODE_OPTIONS="--require dd-trace/init"

    For more information, see Tracing Node.js applications.

  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. In Node.js, we recommend writing logs in a JSON format. For example, you can use a third-party logging library such as winston:

    const tracer = require('dd-trace').init({
      logInjection: true,
    });
    const { createLogger, format, transports } = require('winston');
    
    const LOG_FILE = "/LogFiles/app.log"
    
    const logger = createLogger({
      level: 'info',
      exitOnError: false,
      format: format.json(),
      transports: [
        new transports.File({ filename: LOG_FILE }),
        new transports.Console()
      ],
    });
    
    logger.info('Hello world!');

    Datadog recommends setting the environment variables DD_LOGS_INJECTION=true (in your main container) and DD_SOURCE=nodejs (in your sidecar container) to enable advanced Datadog log parsing.

    For more information, see Correlating Node.js Logs and Traces.

  4. Send custom metrics.

    To send custom metrics, 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 nodejs, or use your custom pipeline. Defaults to cloudrun.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 Cloud Run services appear in the software catalog, you must set the DD_SERVICE, DD_VERSION, and DD_ENV environment variables.

Further reading