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.
To instrument your Azure App Service containers with serverless-init, see Instrument Azure App Service - Linux Container with serverless-init.

Prerequisites

This document assumes that your application is set up for sidecars according to Azure’s Configure a sidecar container for custom container in Azure App Service tutorial.

Setup

Application

Tracing

Instrument your main application with the dd-trace-js library. See Tracing Node.js applications for instructions.

Metrics

Custom metrics are also collected through the tracer. See the code examples.

Logs

The Datadog sidecar uses file tailing to collect logs. Datadog recommends writing application logs to /home/LogFiles/ because this directory is persisted across restarts.

You can also create a subdirectory, such as /home/LogFiles/myapp, if you want more control over what is sent to Datadog. However, if you do not tail all log files in /home/LogFiles, then Azure App Service application logs related to startups and errors are not collected.

To set up logging in your application, see Node.js Log Collection. To set up trace log correlation, see Correlating Node.js Logs and Traces.

Tracing

Instrument your main application with the dd-trace-py library. See Tracing Python applications for instructions.

Metrics

Custom metrics are also collected through the tracer. See the code examples.

Logs

The Datadog sidecar uses file tailing to collect logs. Datadog recommends writing application logs to /home/LogFiles/ because this directory is persisted across restarts.

You can also create a subdirectory, such as /home/LogFiles/myapp, if you want more control over what is sent to Datadog. However, if you do not tail all log files in /home/LogFiles, then Azure App Service application logs related to startups and errors are not collected.

To set up logging in your application, see Node.js Log Collection. To set up trace log correlation, see Correlating Node.js Logs and Traces.

Tracing

Instrument your main application with the dd-trace-java library. See Tracing Java applications for instructions.

Metrics

Custom metrics are also collected through the tracer. See the code examples.

Logs

The Datadog sidecar uses file tailing to collect logs. Datadog recommends writing application logs to /home/LogFiles/ because this directory is persisted across restarts.

You can also create a subdirectory, such as /home/LogFiles/myapp, if you want more control over what is sent to Datadog. However, if you do not tail all log files in /home/LogFiles, then Azure App Service application logs related to startups and errors are not collected.

To set up logging in your application, see Node.js Log Collection. To set up trace log correlation, see Correlating Node.js Logs and Traces.

Tracing

Instrument your main application with the dd-trace-go library. See Tracing Go applications for instructions.

Metrics

Custom metrics are also collected through the tracer. See the code examples.

Logs

The Datadog sidecar uses file tailing to collect logs. Datadog recommends writing application logs to /home/LogFiles/ because this directory is persisted across restarts.

You can also create a subdirectory, such as /home/LogFiles/myapp, if you want more control over what is sent to Datadog. However, if you do not tail all log files in /home/LogFiles, then Azure App Service application logs related to startups and errors are not collected.

To set up logging in your application, see Node.js Log Collection. To set up trace log correlation, see Correlating Node.js Logs and Traces.

Tracing

Instrument your main application with the dd-trace-php library. See Tracing PHP applications for instructions.

Metrics

Custom metrics are also collected through the tracer. See the code examples.

Logs

The Datadog sidecar uses file tailing to collect logs. Datadog recommends writing application logs to /home/LogFiles/ because this directory is persisted across restarts.

You can also create a subdirectory, such as /home/LogFiles/myapp, if you want more control over what is sent to Datadog. However, if you do not tail all log files in /home/LogFiles, then Azure App Service application logs related to startups and errors are not collected.

To set up logging in your application, see Node.js Log Collection. To set up trace log correlation, see Correlating Node.js Logs and Traces.

Sidecar container

  1. In the Azure Portal, go to Deployment Center and select Add.
  2. In the Edit container form, provide the following:
    • Image source: Docker Hub or other registries
    • Image type: Public
    • Registry server URL: index.docker.io
    • Image and tag: datadog/serverless-init:latest
    • Port: 8126
  3. Select Apply.

Application settings

In your App settings in Azure, set the following environment variables:

  • DD_API_KEY: Your Datadog API key
  • DD_SERVICE: How you want to tag your service. For example, sidecar-azure
  • DD_ENV: How you want to tag your env. For example, prod
  • DD_SERVERLESS_LOG_PATH: Where you write your logs. For example, /home/LogFiles/*.log or /home/LogFiles/myapp/*.log

Example application

The following example contains a single app with tracing, metrics, and logs set up.

const tracer = require('dd-trace').init({
 logInjection: true,
});
const express = require("express");
const app = express();
const { createLogger, format, transports } = require('winston');

const logger = createLogger({
 level: 'info',
 exitOnError: false,
 format: format.json(),
 transports: [new transports.File({ filename: `/home/LogFiles/app.log`}),
  ],
});

app.get("/", (_, res) => {
 logger.info("Welcome!");
 res.sendStatus(200);
});

app.get("/hello", (_, res) => {
 logger.info("Hello!");
 metricPrefix = "nodejs-azure-sidecar";
 // Send three unique metrics, just so we're testing more than one single metric
 metricsToSend = ["sample_metric_1", "sample_metric_2", "sample_metric_3"];
 metricsToSend.forEach((metric) => {
   for (let i = 0; i < 20; i++) {
     tracer.dogstatsd.distribution(`${metricPrefix}.${metric}`, 1);
   }
 });
 res.status(200).json({ msg: "Sending metrics to Datadog" });
});

const port = process.env.PORT || 8080;
app.listen(port);
from flask import Flask, Response
from datadog import initialize, statsd
import ddtrace
import logging

ddtrace.patch(logging=True)

FORMAT = ('%(asctime)s %(levelname)s [%(name)s] [%(filename)s:%(lineno)d] '
         '[dd.service=%(dd.service)s dd.env=%(dd.env)s dd.version=%(dd.version)s dd.trace_id=%(dd.trace_id)s dd.span_id=%(dd.span_id)s] '
         '- %(message)s')
logging.basicConfig(filename='/home/LogFiles/app.log', format=FORMAT)
log = logging.getLogger(__name__)
log.level = logging.INFO

options = {
   'statsd_host':'127.0.0.1',
   'statsd_port':8125
}

initialize(**options)

app = Flask(__name__)

@app.route("/")
def home():
   statsd.increment('page.views')
   log.info('Hello Datadog!!')
   return Response('💜 Hello Datadog!! 💜', status=200, mimetype='application/json')

app.run(host="0.0.0.0", port=8080)
package com.example.springboot;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.timgroup.statsd.NonBlockingStatsDClientBuilder;
import com.timgroup.statsd.StatsDClient;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@RestController
public class HelloController {
   private static final StatsDClient Statsd = new NonBlockingStatsDClientBuilder().hostname("localhost").port(8125).build();
   private static final Log logger = LogFactory.getLog(HelloController.class);
   @GetMapping("/")
   public String index() {
       Statsd.incrementCounter("page.views");
       logger.info("Hello Azure!");
       return "💜 Hello Azure! 💜";
   }

}
package main

import (
   "fmt"
   "log"
   "net/http"
   "os"
   "path/filepath"
   "github.com/DataDog/datadog-go/v5/statsd"
   "gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
   "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

const logDir = "/home/LogFiles"

var logFile *os.File
var logCounter int
var dogstatsdClient *statsd.Client

func handler(w http.ResponseWriter, r *http.Request) {
   log.Println("Hello Datadog!")
   span := tracer.StartSpan("maincontainer", tracer.ResourceName("/handler"))
   defer span.Finish()
   logCounter++
   writeLogsToFile(fmt.Sprintf("received request %d", logCounter), span.Context())
   dogstatsdClient.Incr("request.count", []string{}, 1)
   fmt.Fprintf(w, "💜 Hello Datadog! 💜")
}

func writeLogsToFile(log_msg string, context ddtrace.SpanContext) {
   span := tracer.StartSpan(
       "writeLogToFile",
       tracer.ResourceName("/writeLogsToFile"),
       tracer.ChildOf(context))
   defer span.Finish()
   _, err := logFile.WriteString(log_msg + "\n")
   if err != nil {
       log.Println("Error writing to log file:", err)
   }
}

func main() {
   log.Print("Main container started...")

   err := os.MkdirAll(logDir, 0755)
   if err != nil {
       panic(err)
   }
   logFilePath := filepath.Join(logDir, "app.log")
   log.Println("Saving logs in ", logFilePath)
   logFileLocal, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
   if err != nil {
       panic(err)
   }
   defer logFileLocal.Close()

   logFile = logFileLocal

   dogstatsdClient, err = statsd.New("localhost:8125")
   if err != nil {
       panic(err)
   }
   defer dogstatsdClient.Close()

   tracer.Start()
   defer tracer.Stop()

   http.HandleFunc("/", handler)
   log.Fatal(http.ListenAndServe(":8080", nil))
}
<?php

require __DIR__ . '/vendor/autoload.php';

use DataDog\DogStatsd;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\JsonFormatter;

$statsd = new DogStatsd(
   array('host' => '127.0.0.1',
         'port' => 8125,
    )
 );

$log = new logger('datadog');
$formatter = new JsonFormatter();

$stream = new StreamHandler('/home/LogFiles/app.log', Logger::DEBUG);
$stream->setFormatter($formatter);

$log->pushHandler($stream);

$log->pushProcessor(function ($record) {
 $record['message'] .= sprintf(
     ' [dd.trace_id=%s dd.span_id=%s]',
     \DDTrace\logs_correlation_trace_id(),
     \dd_trace_peek_span_id()
 );
 return $record;
});

$log->info("Hello Datadog!");
echo '💜 Hello Datadog! 💜';

$log->info("sending a metric");
$statsd->increment('page.views', 1, array('environment'=>'dev'));

?>