> **Page index:** https://km.oslo.systems/llms.txt lists every page on this site, with a link to its Markdown version.

# Instrument a Java container for Datadog

This guide shows you how to instrument Java applications running in Amazon ECS containers. OpenTelemetry Collector is configured to collect the resulting logs, metrics, and traces.

## Things to consider

> [!TIP]
>
> **Recommended practice**
>
> Enable auto-instrumentation to automatically capture useful telemetry from HTTP requests, database queries, external service calls, and application logs. You don't need to change any code in your app.

## Before you begin

Make sure you have:

- Completed the [getting started guide to set up basic Datadog integration](<https://km.oslo.systems/initial-setup-of-your-aws-environment/set-up-datadog/index.md>).
- A Java application deployed using the `app` template.

## Step 1: Enable telemetry collection and auto-instrumentation

Auto-instrumentation automatically captures logs, metrics, and traces from your Java app. You don't need to change any code in your app.

1. Update to the newest version of the `app` template for the application you want to instrument.
2. Edit your `package-config.yml` file to enable telemetry collection:

```
TelemetryCollection:
  Enable: true
  AutoInstrumentation:
    Enable: true # Disable this if you're already bundling the Java Agent with your application
    Runtime: java # This is the only supported runtime for auto-instrumentation
```

1. Apply the configuration:

```
ok pkg install
```

1. Apply the Terraform configuration:

```
terraform plan
terraform apply
```

## Step 2: Add manual instrumentation (optional)

Auto-instrumentation provides basic telemetry, but custom business metrics need manual instrumentation with the OpenTelemetry SDK. Add the API dependency and use `@WithSpan` annotations or the API directly.

For detailed instructions on manual instrumentation, see the [OpenTelemetry Java documentation](<https://opentelemetry.io/docs/instrumentation/java/manual/>).

## Step 3: Verify instrumentation

Check Datadog for incoming traces, metrics, and logs. Use the `env` and `service` tags to filter the results. For example:

```
env:pirates-dev service:too-tikki`.
```

## Next steps

With an instrumented application you can now [customize OTel configuration](<https://km.oslo.systems/observability/customize-otel-configuration/index.md>) to more granularly control which telemetry gets sent to Datadog from your application.
