> ## 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.

# Tracing

Atoti Server provides tracing capabilities through the OpenTelemetry API, which can be used with a
variety of observability backends.

By default, Atoti enables tracing for the CSV source and every query execution, including distributed queries (i.e. all queries endpoints are traced).

## Tracing Setup

In order to use Atoti Server's tracing capabilities, you will need to use your chosen
OpenTelemetry-compatible backend, such as OpenTelemetry SDK, to generate an `OpenTelemetry` instance.
That instance must then be supplied to Atoti Server in one of these two ways:

* Through a call to `Tracing.setOtelInstance()`.
* By setting the `GlobalOpenTelemetry`.
  Please note that in the event that both of these means are used, the instance supplied
  through `Tracing.setOtelInstance()` has precedence.

For Spring-based tracing properties, see the [Micrometer Tracing reference documentation](https://docs.micrometer.io/tracing/reference/).

Tracing granularity can be configured within Atoti Server by calling `Tracing.setSpanLevel()`. There are three possibilities:

* NONE: continues the parent span.
* SPECIFIC: mode creates new child span when some significant tasks are being invoked, specified in `Tracing#IMPORTANT_TASKS`.
* TASK:  mode creates new child span whenever a new ForkJoinTask is being called.

Please refer the [SpanLevel](https://assets.activeviam.com/products/atoti/server/latest/javadoc/com/activeviam/tech/core/internal/observability/SpanLevel.html) for more details.

## What identifies a query in a trace?

The span of each query execution carries the `query.id` attribute, and all the parts of the execution plan of one query report the same value.
The same value is reported as `queryId` in the query execution reports of the `atoti.server.query.execution` logger, so a trace can be joined with them.
The [query history](https://docs.activeviam.com/engine/java-sdk/6.1/starters/atoti_query_history_starter.md) records the same 64-bit number as the id of its entry, written as a decimal number.

Each query runs in a `Query` span of its own, a child of the span current when the query starts, such as the `select query` span of an MDX query, and its query id is the id of that span.
The id can therefore be looked up in the tracing backend as the span of that query, and several queries sent in parallel, even by a single request, get distinct query ids.

A random id is used instead when tracing is not set up.

In the following diagram, each box is a span: one request sends two queries, and the sub-queries of each query carry the `query.id` of that query.

```mermaid theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
flowchart TD
    R["request span · trace T"]
    QA["Query span · id A<br/>query.id = A"]
    QB["Query span · id B<br/>query.id = B"]
    SA["sub-queries<br/>query.id = A"]
    SB["sub-queries<br/>query.id = B"]
    R --> QA --> SA
    R --> QB --> SB
```

Each part of the execution plan also has an id of its own: a sub-query, such as a `GET_AGGREGATES` query, runs in a `Query` span of its own too, and takes its id.
The query execution reports give this id under a name that depends on the kind of the part: `mdxQueryId`, `gaqId`, `drillthroughQueryId` or `dataNodeQueryId`.
Each of these ids is the id of the `Query` span of that part, written as 16 lowercase hexadecimal characters like any OpenTelemetry span id, so it can be searched as is as a span id in the tracing backend.
For the top-level query, it is equal to `queryId`.

In the following diagram, each box is a span: an MDX query spawns two `GET_AGGREGATES` queries, and all three spans carry the same `query.id`.

```mermaid theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
flowchart TD
    R["request span<br/>(REST, WebSocket, XMLA…)"]
    Q1["Query span · id A<br/>MDX query<br/>query.id = A<br/>reports: mdxQueryId = A · queryId = A"]
    Q2["Query span · id B<br/>GET_AGGREGATES query<br/>query.id = A<br/>reports: gaqId = B · queryId = A"]
    Q3["Query span · id C<br/>GET_AGGREGATES query<br/>query.id = A<br/>reports: gaqId = C · queryId = A"]
    R --> Q1
    Q1 --> Q2
    Q1 --> Q3
```

## Component specifics

In addition to the out-of-the-box information provided by OpenTelemetry,
some components of Atoti add extra metrics (by adding metadata to the spans) in order to provide more insights.

Check the tracing documentation of the component you want to monitor to see what information you can get:

* [DirectQuery](https://docs.activeviam.com/engine/java-sdk/6.1/directquery/directquery_monitoring.md#tracing)
* [Tableau Adapter](https://docs.activeviam.com/engine/java-sdk/6.1/user_guide/querying/front_ends/tableau/tableau_adapter_how_to.md#tracing)
