> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to set up Observability with OpenTelemetry

Atoti Server is instrumented to produce traces and metrics.
Nothing is exported until you give Atoti Server an OpenTelemetry instance.
This page explains how to do so.

## How Atoti Server finds the OpenTelemetry instance

The [Atoti Server Starter](https://docs.activeviam.com/engine/java-sdk/6.2/starters/atoti_starter.md#observability) uses the application's `io.opentelemetry.api.OpenTelemetry` Spring bean.
Each setup below provides that bean: you declare it in Setups 1 and 3, and the OpenTelemetry Spring Boot starter declares it in Setup 2.

> [!WARNING]
>
> Atoti Server does not read `GlobalOpenTelemetry` by itself.
> Attaching the OpenTelemetry Java agent alone leaves Atoti Server's own spans and metrics as no-ops.
> Expose the agent's instance as a bean, as described in the first setup below.

Choose the setup that matches how your application is deployed.
Whichever you choose, the exporters, endpoints and sampling are configured by whatever owns the OpenTelemetry instance, not by Atoti Server.

## Setup 1: OpenTelemetry Java agent

Attach the [OpenTelemetry Java agent](https://opentelemetry.io/docs/zero-code/java/agent/) to the JVM and configure it with its `otel.*` system properties or `OTEL_*` environment variables.
Then declare a bean that exposes the agent's instance to Atoti Server:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
@Configuration
static class OpenTelemetryAgentConfig {
  @Bean
  OpenTelemetry openTelemetry() {
    return GlobalOpenTelemetry.get();
  }
}
```

This is the setup used by the Atoti Python SDK observability plugin.

## Setup 2: OpenTelemetry Spring Boot starter

Without an agent, add the OpenTelemetry Spring Boot starter, `io.opentelemetry.instrumentation:opentelemetry-spring-boot-starter`, to the application.
It provides the `OpenTelemetry` bean, with traces, metrics and logs exporters configured through `otel.*` properties.
No additional code is needed.

## Setup 3: OpenTelemetry SDK bean (advanced)

If neither setup fits, build an `OpenTelemetry` SDK instance yourself and declare it as a bean.
Follow the [OpenTelemetry manual setup](https://opentelemetry.io/docs/languages/java/instrumentation/#manual-instrumentation-setup).

* Traces, metrics and logs are sent only if the SDK is configured with the matching exporters.
* Logs also require the OpenTelemetry Logback appender.

> [!WARNING]
>
> Use this setup only when no OpenTelemetry Java agent is attached.
> With an agent, the SDK bean creates a second instance: Atoti Server's spans go to one and the agent's HTTP and JDBC spans go to the other, which splits traces.

## MDC attributes

Atoti Server puts these attributes in the logging MDC:

| Name | Value | Set on |
| - | - | - |
| `user_name` | The authenticated user | Query, request and WebSocket threads, and the health events of an authenticated user |
| `thread_name` | The thread that emitted the event | Health events |
| `trace_id`, `span_id`, `traceId`, `spanId` | The span the log was emitted in | Monitoring logs and health events |

To export an attribute, enable [MDC capture](https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/logback/logback-appender-1.0/library) of its name in your OpenTelemetry Logback integration.
The logs written while a request is being authenticated, such as those of the authentication filters, do not carry `user_name`.

## Known pitfalls

> [!WARNING]
>
> **Spring Boot's `spring-boot-starter-opentelemetry` drops Atoti Server's metrics.**
> Atoti Server's spans are exported, but its metrics, which are recorded through the OpenTelemetry metrics API, are silently dropped.
> Spring Boot does not auto-configure an `SdkMeterProvider` and uses one only if you declare an `SdkMeterProvider` bean.
> The Spring Boot reference states: "If you or a dependency you include make use of OpenTelemetry's MeterProvider, those metrics are not exported."
> Prefer Setup 1 or Setup 2, or declare an `SdkMeterProvider` bean.

> [!NOTE]
>
> With the agent attached and Micrometer's tracing bridge active (`micrometer-tracing-bridge-otel` and `spring-boot-micrometer-tracing-opentelemetry`), incoming HTTP, WebClient and GraphQL requests are traced twice.
> Suppress the duplicate Spring observations in your application, for example with an `ObservationRegistryCustomizer` that filters them out with a predicate.

## Check the result

Run a query, for example from a pivot table in Atoti UI, and look for the query spans in your observability backend.
See [Tracing](https://docs.activeviam.com/engine/java-sdk/6.2/monitoring/tracing.md) for what Atoti Server traces and [Metrics](https://docs.activeviam.com/engine/java-sdk/6.2/monitoring/metrics.md) for the metrics it records.
