Configuration

Une application d'exemple est disponible sur GitHub.
Pour une visibilité complète et un accès à toutes les fonctionnalités Datadog dans les jobs Cloud Run, assurez-vous d'avoir installé l'intégration Google Cloud et d'utiliser la version 1.9.0 ou ultérieure de serverless-init.
  1. Installez le SDK Datadog Node.js.

    1. Dans votre application principale, installez le package dd-trace.

      npm install dd-trace
    2. Initialisez le traceur Node.js avec la variable d’environnement NODE_OPTIONS :

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

    Remarque : les jobs Cloud Run s’exécutent jusqu’à leur terme plutôt que de servir des requêtes ; l’auto-instrumentation ne créera donc pas de span « job » de haut niveau. Pour une visibilité de bout en bout, créez votre propre span racine. Consultez les instructions d’instrumentation personnalisée Node.js.

    Pour plus d’informations, consultez Tracing Node.js applications.

  2. Installez serverless-init.

    Serverless-init automatically creates a span for the duration of a task, even if the tracer is not installed. You can disable this by setting DD_APM_ENABLED=false. However, tracing is recommended because it is required for task-level visibility.
    Cloud Run Jobs requires serverless-init version 1.9.0 or later.
    Datadog publishes new releases of the serverless-init container image to Google's gcr.io, AWS's ECR, and on Docker Hub:
    hub.docker.comgcr.iopublic.ecr.aws
    datadog/serverless-initgcr.io/datadoghq/serverless-initpublic.ecr.aws/datadog/serverless-init

    Images are tagged based on semantic versioning, with each new version receiving three relevant tags:

    • 1, 1-alpine: use these to track the latest minor releases, without breaking changes
    • 1.x.x, 1.x.x-alpine: use these to pin to a precise version of the library
    • latest, latest-alpine: use these to follow the latest version release, which may include breaking changes

    Add the following instructions and arguments to your Dockerfile.

    COPY --from=datadog/serverless-init:<YOUR_TAG> /datadog-init /app/datadog-init
    ENTRYPOINT ["/app/datadog-init"]
    CMD ["/nodejs/bin/node", "/path/to/your/app.js"]
    

    Datadog expects serverless-init to be the top-level application, with the rest of your app’s command line passed in for serverless-init to execute.

    If you already have an entrypoint defined inside your Dockerfile, you can instead modify the CMD argument.

    CMD ["/app/datadog-init", "/nodejs/bin/node", "/path/to/your/app.js"]
    

    If you require your entrypoint to be instrumented as well, you can instead swap your entrypoint and CMD arguments.

    ENTRYPOINT ["/app/datadog-init"]
    CMD ["/your_entrypoint.sh", "/nodejs/bin/node", "/path/to/your/app.js"]
    

    As long as your command to run is passed as an argument to datadog-init, you will receive full instrumentation.


  3. Configurez les logs.

    Pour activer la journalisation, définissez la variable d’environnement DD_LOGS_ENABLED=true. Cela permet à serverless-init de lire les logs depuis stdout et stderr.

    Datadog recommande également de définir les variables d’environnement DD_LOGS_INJECTION=true et DD_SOURCE=nodejs pour activer le parsing avancé des logs Datadog.

    Si vous souhaitez que les logs multilignes soient conservés dans un seul message de log, Datadog recommande d’écrire vos logs au format JSON. Par exemple, vous pouvez utiliser une bibliothèque de journalisation tierce telle que winston :

    const { createLogger, format, transports } = require('winston');
    
    const logger = createLogger({
      level: 'info',
      exitOnError: false,
      format: format.json(),
      transports: [
        new transports.Console()
      ],
    });
    
    logger.info('Hello world!');

    Pour plus d’informations, consultez Correlating Node.js Logs and Traces.

  4. Configurez votre application.

    After the container is built and pushed to your registry, set the required environment variables for the Datadog Agent:

    • DD_API_KEY: Your Datadog API key, used to send data to your Datadog account. For privacy and safety, configure this API key as a Google Cloud Secret.
    • DD_SITE: Your Datadog site. For example, datadoghq.com.

    For more environment variables, see the Environment variables section on this page.

    The following command deploys the job:

    gcloud run jobs deploy <JOB_NAME> \
      --image=gcr.io/<YOUR_PROJECT>/<IMAGE_NAME> \
      --update-env-vars=DD_API_KEY=$DD_API_KEY \
      --update-env-vars=DD_SITE=$DD_SITE \
    
  1. Add a service label in Google Cloud. In your Cloud Run service’s info panel, add a label with the following key and value:

    KeyValue
    serviceThe name of your service. Matches the value provided as the DD_SERVICE environment variable.

    See Configure labels for services in the Cloud Run documentation for instructions.

  1. Set up a retention filter for Cloud Run Jobs traces. Datadog relies on traces to display executions and tasks in the UI. To ensure traces are retained, create a retention filter with the query @origin:cloudrunjobs and set the span retention rate to 100%.

    Screenshot showing retention filter configuration with @origin:cloudrunjobs query and 100% retention rate

  2. Envoyer des métriques personnalisées.

    Pour envoyer des métriques personnalisées, consultez les exemples de code. Dans serverless, seul le type de métrique distribution est pris en charge.

  3. Activer le profilage (aperçu).

    Pour activer le Continuous Profiler, définissez la variable d’environnement DD_PROFILING_ENABLED=true.

    Le Continuous Profiler de Datadog est disponible en aperçu pour les jobs Google Cloud Run.

Environment variables

VariableDescription
DD_API_KEYDatadog API key - Required
DD_SITEDatadog site - Required
DD_SERVICEDatadog Service name. Required
DD_LOGS_ENABLEDWhen true, send logs (stdout and stderr) to Datadog. Defaults to false.
DD_LOGS_INJECTIONWhen true, enrich all logs with trace data for supported loggers. See Correlate Logs and Traces for more information.
DD_VERSIONSee Unified Service Tagging.
DD_ENVSee Unified Service Tagging.
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.
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).

Do not set the following environment variables in your serverless environment. They should only be set in non-serverless environments.

  • DD_AGENT_HOST
  • DD_TRACE_AGENT_URL

Dépannage

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.

Pour aller plus loin