Azure App Service - Linux Code
Overview
This instrumentation method provides the following additional monitoring capabilities for Linux Azure App Service workloads:
- Fully distributed APM tracing using automatic instrumentation.
- Customized APM service and trace views showing relevant Azure App Service metrics and metadata.
- Support for manual APM instrumentation to customize spans.
Trace_ID
injection into application logs.- Support for submitting custom metrics using DogStatsD.
- Support for submitting logs using file tailing.
This solution uses a sidecar container and Application Settings for Linux Azure App Service to instrument the application and manage its configuration.
Supported runtimes: Java, Node.js, .NET, PHP, Python
Setup
- Install a tracing library. You must install a tracing library within the application package prior to deployment.
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.
Add the Datadog.Trace.Bundle
Nuget package to your project.
Note: When you complete Step 2, ensure that you also set the additional environment variables required by the .NET tracer.
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.
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
- Configure environment variables.
In Azure, add the following key-value pairs in Settings > Configuration > Application settings:
Name | Value | Description |
---|
DD_API_KEY | Your Datadog API key. | See Organization Settings > API Keys in Datadog. |
DD_SITE |
| Your Datadog site. Defaults to datadoghq.com . |
DD_SERVICE | Your application’s service name. | Defaults to the name field value in package.json . |
For .NET applications, the following environment variables are required unless otherwise specified:
Name | Value | Description |
---|
DD_DOTNET_TRACER_HOME | /home/site/wwwroot/datadog | Path to tracing libraries, copied within the Docker file |
DD_TRACE_LOG_DIRECTORY | /home/Logfiles/dotnet | Where tracer logs are stored |
CORECLR_ENABLE_PROFILING | 1 | Instructs the .NET CLR that profiling should be enabled. |
CORECLR_PROFILER | 846F5F1C-F9AE-4B07-969E-05C26BC060D8 | Profiler GUID. |
CORECLR_PROFILER_PATH | /home/site/wwwroot/datadog/linux-musl-x64/Datadog.Trace.ClrProfiler.Native.so | The profiler binary that the .NET CLR loads into memory, which contains the GUID. |
DD_PROFILING_ENABLED (optional) | true | Enables Datadog’s Continuous Profiler. |
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
- 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:
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
Additional helpful documentation, links, and articles: