Azure App Service - Linux Code

Overview

This solution uses 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

Application

Install the tracing library for your language:

Java supports adding instrumentation code through the use of a command line argument, javaagent.

  1. Download the latest version of Datadog’s Java tracing library.
  2. 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.
  3. 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.

  1. Add the ddtrace package to your project using your package manager.
  2. 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.

Add the Datadog.Trace.Bundle Nuget package to your project. See the Nuget package page for more details.

For example:

dotnet add package Datadog.Trace.Bundle --version 3.21.0

Run the following script to install Datadog’s PHP tracing library:

#!/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

cp /home/site/wwwroot/default /etc/nginx/sites-available/default && service nginx reload

This script is intended to run as the startup command, which installs the tracing module into PHP and then restarts the application.

  1. Add ddtrace to your project.

  2. 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

First, install the Datadog CLI and Azure CLI.

Login to your Azure account using the Azure CLI:

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.

  1. Configure environment variables. In Azure, add the following key-value pairs in Settings > Configuration > Application 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.
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.
  1. Configure a sidecar container for Datadog.

    1. In Azure, navigate to Deployment > Deployment Center. Select the Containers tab.
    2. Click Add and select Custom container.
    3. 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
    4. Select Apply.
  2. 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.

Azure App Service Configuration: App Service logs, under the Monitoring section of Settings in the Azure UI. The 'Application logging' option is set to 'File System'.

Share the content of the Log stream with Datadog Support.

Further reading

Additional helpful documentation, links, and articles: