Azure App Service - Linux Code
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.
Overview
This page describes how to instrument your Linux Azure App Service application with the Datadog Agent. The procedure on this page makes use of a sidecar container and Application Settings for Linux Azure App Service to instrument the application and manage its configuration.
If you would prefer to not use the sidecar approach (Not Recommended), you can instead follow the instructions to Instrument Azure App Service - Linux Code Deployment with the Datadog wrapper.
Supported runtimes: Java, Node.js, .NET, PHP, Python
Setup
Azure integration
If you haven’t already, install the Datadog-Azure integration to collect metrics and logs.
Application
Install the tracing library for your language:
Java supports adding instrumentation code through the use of a command line argument, javaagent
.
- Download the latest version of Datadog’s Java tracing library.
- Place the tracing library inside your project. It must be included with your deployment.
If you are using the
azure-webapp-maven
plugin, you can add the Java tracing library as a resource entry with type lib
. - Set the environment variable
JAVA_OPTS
with --javaagent:/home/site/lib/dd-java-agent.jar
. When your application is deployed, the Java tracer is copied to /home/site/lib/dd-java-agent.jar
.
Instrumentation starts when the application is launched.
- Add the
ddtrace
package to your project using your package manager. - Initialize the tracer by doing one of the following:
- Set
NODE_OPTIONS
with --require=dd-trace/init
- Include the tracer in your application’s entrypoint file:
const tracer = require('dd-trace').init({ logInjection: true, });
This also configures trace log correlation.
Run the following script to install Datadog’s PHP tracing library:
startup.sh:
#!/usr/bin/env bash
echo "Setting up Datadog tracing for PHP"
DD_PHP_TRACER_VERSION=1.8.3
DD_PHP_TRACER_URL=https://github.com/DataDog/dd-trace-php/releases/download/${DD_PHP_TRACER_VERSION}/datadog-setup.php
echo "Installing PHP tracer from ${DD_PHP_TRACER_URL}"
if curl -LO --fail "${DD_PHP_TRACER_URL}"; then
eval "php datadog-setup.php --php-bin=all"
else
echo "Downloading the tracer was unsuccessful"
return
fi
# This line is can be uncommented if the project contains an nginx configuration in the project root
# cp /home/site/wwwroot/default /etc/nginx/sites-available/default && service nginx reload
service nginx reload
This bash script is intended to run as the startup command, which installs the tracing module into PHP and then restarts the NGINX service.
Add ddtrace
to your project.
Modify your startup command. Your new command should run ddtrace-run
with your old command as an argument. That is: if your startup command is foo
, modify it to run ddtrace-run foo
.
For example:
ddtrace-run gunicorn --bind=0.0.0.0 --timeout 600 quickstartproject.wsgi
Instrumentation
Locally
Install the Datadog CLI and Azure CLI, and login to your Azure account using the Azure CLI by running az login
.
Then, run the following command to set up the sidecar container:
export DD_API_KEY=<DATADOG_API_KEY>
export DD_SITE=<DATADOG_SITE>
datadog-ci aas instrument -s <subscription-id> -g <resource-group-name> -n <app-service-name>
Set your Datadog site to
. Defaults to datadoghq.com
.
Additional flags, like --service
and --env
, can be used to set the service and environment tags. For a full list of options, run datadog-ci aas instrument --help
.
Azure Cloud Shell
To use the Datadog CLI in Azure Cloud Shell, open cloud shell and use npx
to run the CLI directly. Set your API key and site in the DD_API_KEY
and DD_SITE
environment variables, and then run the CLI:
export DD_API_KEY=<DATADOG_API_KEY>
export DD_SITE=<DATADOG_SITE>
npx @datadog/datadog-ci@4 aas instrument -s <subscription-id> -g <resource-group-name> -n <app-service-name>
The Datadog Terraform module for Linux Web Apps wraps the azurerm_linux_web_app resource and automatically configures your Web 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_service_plan" "my_asp" {
name = "my-app-service-plan" // Replace with your app service plan name
resource_group_name = "my-resource-group" // Replace with your resource group name
os_type = "Linux"
location = "eastus"
sku_name = "P1v2"
}
module "my_web_app" {
source = "DataDog/web-app-datadog/azurerm//modules/linux"
version = "~> 1.0"
name = "my-web-app" // Replace with your web app name
resource_group_name = "my-resource-group" // Replace with your resource group name
service_plan_id = azurerm_service_plan.my_asp.id
location = "eastus"
datadog_api_key = var.datadog_api_key
datadog_service = "my-service" // Replace with your service name
datadog_env = "prod" // Replace with your environment (e.g. prod, staging)
datadog_version = "0.0.0" // Replace with your application version
site_config = {
application_stack = {
python_version = "3.13" // change for your specific runtime
}
}
app_settings = {
DD_TRACE_ENABLED = "true" // Example setting
}
}
Finally, run terraform apply
, and follow any prompts.
The Datadog Linux Web App module only deploys the Web App resource, so you need to deploy your code separately.
- Configure environment variables.
In Azure, add the following key-value pairs in Settings > Environment Variables > App Settings:
DD_API_KEY
- Value: Your Datadog API key.
See Organization Settings > API Keys in Datadog.
DD_SITE
- Value:
Your Datadog site. Defaults to datadoghq.com
.
Use the “Datadog Site” drop-down menu on this page’s right navigation bar to select your site.
DD_SERVICE
- Value: Your application’s service name.
Defaults to the name field value in package.json
.
See Unified Service Tagging for more information on the service
tag.
DD_ENV
- Value: Your application’s environment name.
There is no default value for this field.
See Unified Service Tagging for more information on the env
tag.
DD_VERSION
- Value: Your application’s version.
There is no default value for this field.
See Unified Service Tagging for more information on the version
tag.
DD_SERVERLESS_LOG_PATH
- Value: The log path the sidecar uses to collect logs.
Where you write your logs. For example, /home/LogFiles/*.log
or /home/LogFiles/myapp/*.log
.
DD_AAS_INSTANCE_LOGGING_ENABLED
- Value: false
When true
, log collection is automatically configured for the additional file path: /home/LogFiles/*$COMPUTERNAME*.log
DD_AAS_INSTANCE_LOG_FILE_DESCRIPTOR
- Value: An optional file descriptor used for more precise log tailing.
Recommended for scenarios with frequent log rotation. For example, setting _default_docker
configures the log tailer to ignore rotated files and focus only on Azure’s active log file.
If your application has multiple instances, make sure your application's log filename includes the $COMPUTERNAME
variable. This ensures that log tailing does not create duplicate logs from multiple instances that are reading the same file. Enabling this feature variable also prevents DD_SERVERLESS_LOG_PATH
from being set. This is to prevent ingesting duplicate logs.
WEBSITES_ENABLE_APP_SERVICE_STORAGE
- Value:
true
Setting this environment variable to true
allows the /home/
mount to persist and be shared with the sidecar.
For .NET applications, the following environment variables are required. See the Datadog.Tracer.Bundle
Nuget package README file for more details.
DD_DOTNET_TRACER_HOME
- Value:
/home/site/wwwroot/datadog
Path to the directory containing the .NET tracing libraries.
DD_TRACE_LOG_DIRECTORY
- Value:
/home/LogFiles/dotnet
Path where the .NET tracing library will write its logs.
CORECLR_ENABLE_PROFILING
- Value:
1
Enables the instrumentation APIs in the .NET runtime.
CORECLR_PROFILER
- Value:
{846F5F1C-F9AE-4B07-969E-05C26BC060D8}
Identifier for Datadog’s .NET the instrumentation library.
CORECLR_PROFILER_PATH
- Value:
/home/site/wwwroot/datadog/
linux-x64/Datadog.Trace.ClrProfiler.Native.so
(single line)
Path to the instrumentation library loaded by the .NET runtime.
Configure a sidecar container for Datadog.
- In Azure, navigate to Deployment > Deployment Center. Select the Containers tab.
- Click Add and select Custom container.
- In the Edit container form, provide the following:
- Image source: Other container registries
- Image type: Public
- Registry server URL:
index.docker.io
- Image and tag:
datadog/serverless-init:latest
- Port: 8126
- Environment Variables: Include all previously configured Datadog environment variables.
- Select Apply.
Restart your application.
If you modified a startup command, restart your application. Azure automatically restarts the application when new Application Settings are saved.
View traces in Datadog
After your application restarts, go to Datadog’s APM Service page and search for the service name you set for your application (DD_SERVICE
).
Custom metrics
To configure your application to submit custom metrics, follow the appropriate steps for your runtime:
Continuous Profiler
To enable the Continuous Profiler, set the environment variable DD_PROFILING_ENABLED=true
. For more information, see the Continuous Profiler documentation.
Deployment
To update your Datadog instrumentation with zero downtime, use deployment slots. You can create a workflow that uses GitHub Action for Azure CLI.
See the sample GitHub workflow.
Troubleshooting
If you are not receiving traces or custom metric data as expected, enable agent debug logging by setting DD_LOG_LEVEL
in the sidecar configuration options. For tracer debugging set DD_TRACE_DEBUG
to true. This generates logs additional debug logs for the sidecar and tracing library.
Be sure to enable App Service logs to receive debugging logs.
Share the content of the Log stream with Datadog Support.
Further reading
Documentation, liens et articles supplémentaires utiles: