Instalación

  1. Instala el rastreador de Java Datadog.

    1. Añade el rastreador de Datadog Java a tu archivo Docker:

      Dockerfile

      ADD 'https://dtdg.co/latest-java-tracer' agent.jar
      ENV JAVA_TOOL_OPTIONS="-javaagent:agent.jar"
    2. Añade los artefactos del rastreador.

      <dependency>
        <groupId>com.datadoghq</groupId>
        <artifactId>dd-trace-api</artifactId>
        <version>DD_TRACE_JAVA_VERSION_HERE</version>
      </dependency>
      implementation 'com.datadoghq:dd-trace-api:DD_TRACE_JAVA_VERSION_HERE'

      Consulta las versiones de dd-trace-java para obtener la última versión del rastreador.

    3. Añade la anotación @Trace a cualquier método que desees rastrear.

    Para más información, consulta Rastreo de aplicaciones Java.

  2. Instalar serverless-init.

    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 ["./mvnw", "spring-boot:run"]
    

    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", "./mvnw", "spring-boot:run"]
    

    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", "./mvnw", "spring-boot:run"]
    

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


  3. Configurar logs.

    Para activar el registro, configura la variable de entorno DD_LOGS_ENABLED=true. Esto permite a serverless-init leer logs de stdout y stderr.

    Datadog también recomienda configurar la variable de entorno DD_LOGS_INJECTION=true y DD_SOURCE=java para activar el parseo avanzado de logs de Datadog.

    Si deseas que los logs de varias líneas se conserven en un único mensaje de logs, Datadog te recomienda escribir tus logs en formato JSON compacto. Por ejemplo, puedes utilizar una biblioteca de registro de terceros como Log4j 2:

    private static final Logger logger = LogManager.getLogger(App.class);
    logger.info("Hello World!");

    resources/log4j2.xml

    <Configuration>
      <Appenders>
        <Console name="Console"><JsonLayout compact="true" eventEol="true" properties="true"/></Console>
      </Appenders>
      <Loggers><Root level="info"><AppenderRef ref="Console"/></Root></Loggers>
    </Configuration>

    Para obtener más información, consulta Correlación de logs y trazas de Java.

  4. Configurar tu aplicación.

    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.

  1. Enviar métricas personalizadas.

    Para enviar métricas personalizadas, instala el cliente de DogStatsD y visualiza ejemplos de código. En serverless, solo se admite el tipo de métrica distribution.

Environment variables

VariableDescription
DD_API_KEYDatadog API key - Required
DD_SITEDatadog site - Required
DD_SERVICEDatadog Service name. Required
DD_AZURE_SUBSCRIPTION_IDAzure Subscription ID. Required
DD_AZURE_RESOURCE_GROUPAzure Resource Group 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 java, or use your custom pipeline. Defaults to containerapp.
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).
JAVA_TOOL_OPTIONSRequired for tracing. The path to the Datadog Java agent. For example, -javaagent:/path/to/dd-java-agent.jar.

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

Solucionar problemas

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 Azure Container Apps appear in the software catalog, you must set the DD_SERVICE, DD_VERSION, and DD_ENV environment variables.

Referencias adicionales