Trino/Presto driver: tag queries with requestId, like BigQuery and ClickHouse
Is your feature request related to a problem? Please describe.
Cube passes each query's requestId to the driver so the driver can tag the query it sends to the database. The driver interface says so directly (driver.interface.ts#L135):
Cube request identifier, forwarded so drivers can tag the underlying database job/query for tracing (e.g. BigQuery job labels).
The orchestrator passes it through: client.query(req.query, req.values, req) for queries and stream(..., { requestId }) for pre-aggregation builds. Two drivers already use it:
- BigQuery labels each query job with
cube_request_id(#11299,buildQueryLabels()). - ClickHouse uses it as the prefix of the
query_id(#11739).
The Presto/Trino driver drops it:
query(query, values)has no options parameter.stream(query, values, _options)receives the options and ignores them.queryPromised(query, streaming)only ever sends the staticconfig.headers.
So there's no way to trace a Trino query back to the Cube request that issued it. For example, when Cube runs behind an APM-instrumented service, the incoming W3C traceparent becomes the requestId, but it stops at the driver, so Trino query logs and event-listener data can't be joined back to the originating trace.
Describe the solution you'd like
Do what BigQuery and ClickHouse do: when requestId is present, attach it to the Trino query.
- Pass
optionsfromquery()andstream()intoqueryPromised(). - In
queryPromised(), merge a per-query header into theheaderspassed toclient.execute(), on top ofconfig.headers:headers: { ...this.config.headers, ...this.requestIdHeaders(options) }, requestIdHeaders(options)returns{ 'X-Trino-Trace-Token': <id> }, orX-Presto-Trace-Tokenfor theprestoengine. As in the BigQuery and ClickHouse drivers,<id>isextractRequestUUID(requestId)from@cubejs-backend/shared, so all sub-queries of one request share an ID. The value should also be limited to header-safe characters and a sensible length.requestIdcan come from a client-suppliedx-request-id, and an invalid header value makes Node throw, which would fail the query.
TrinoDriver extends PrestoDriver, so this one change covers both engines. Only the initial POST /v1/statement needs the header. Trino records it on the query when the query is created, so the nextUri polls don't need it.
Source: cube-js/cube