Skip to content

feat: More consistent query metrics. - #20947

Open
gianm wants to merge 3 commits into
apache:masterfrom
gianm:consistent-query-metrics-two
Open

gianm wants to merge 3 commits into
apache:masterfrom
gianm:consistent-query-metrics-two

Conversation

@gianm

@gianm gianm commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

This patch improves consistency of query metrics across the native and MSQ engines in the following ways:

  1. Emit context queryId as "queryId" in all query metrics. Previously,
    native metrics emitted this as "id"; MSQ metrics emitted it as
    "queryId"; and SQL metrics did not emit it at all.

  2. Add "engine" to QueryMetrics and set it appropriately, so query
    metrics can optionally gain this dimension. Out-of-box, it is not
    included.

  3. MSQ's query/time and query/cpu/time metrics are now emitted using
    QueryMetrics rather than ad hoc metric construction. This better
    aligns them with native query metrics.

  4. Add MSQ query/load/* and query/segments/count metrics, matching the
    analogous native metrics.

Release note

MSQ query metrics, such as query/time and query/cpu/time, now use the same dimensions as native query metrics. This affects the existing dimensions type (will be a native query type on the controller if a native query was used in planning) and sqlQueryId (no longer included by default).

This patch improves consistency of query metrics across the native
and MSQ engines in the following ways:

1) Emit context queryId as "queryId" in all query metrics. Previously,
   native metrics emitted this as "id"; MSQ metrics emitted it as
   "queryId"; and SQL metrics did not emit it at all.

2) Add "engine" to QueryMetrics and set it appropriately, so query
   metrics can optionally gain this dimension. Out-of-box, it is not
   included.

3) MSQ's query/time and query/cpu/time metrics are now emitted using
   QueryMetrics rather than ad hoc metric construction. This better
   aligns them with native query metrics.

4) Add MSQ query/load/* and query/segments/count metrics, matching the
   analogous native metrics.
@Override
public Query<Object> withOverriddenContext(final Map<String, Object> contextOverride)
{
return new MSQMetricsQuery(getDataSource(), getQuerySegmentSpec(), computeOverridenContext(contextOverride));

@FrankChen021 FrankChen021 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No actionable issues found in the reviewed changes. The PR can merge from this review.

Reviewed 42 of 42 changed files.


This is an automated review by Codex GPT-5.6-Luna(max)

@FrankChen021 FrankChen021 added the Approval Recommended Used when AI completes a review and reports no findings label Nov 20, 2026

@amaechler amaechler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments from a local Claude review.

Comment on lines +243 to +248
DartSqlEngine.NAME,
new ServiceMetricEvent.Builder()
.setDimensionIfNotNull(
QueryContextParameters.DART_QUERY_ID.getName(),
context.get(QueryContextParameters.DART_QUERY_ID)
)

@amaechler amaechler Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sqlQueryId is dropped from query/time and query/cpu/time here: DefaultQueryMetrics#sqlQueryId is a no-op, and the extras only carry dartQueryId. Previously MSQMetricEventBuilder set it on every MSQ metric. makeMetricBuilder() already holds id, queryId, sqlQueryId, type, engine and dartQueryId, and ExtraDimensionsServiceEmitter only fills keys that are absent, so reusing it restores sqlQueryId and removes the throwaway builder:

Suggested change
DartSqlEngine.NAME,
new ServiceMetricEvent.Builder()
.setDimensionIfNotNull(
QueryContextParameters.DART_QUERY_ID.getName(),
context.get(QueryContextParameters.DART_QUERY_ID)
)
DartSqlEngine.NAME,
makeMetricBuilder()

One caveat: makeMetricBuilder() sets dartQueryId with setDimension, which throws on null, whereas this code uses setDimensionIfNotNull. DartSqlEngine always sets it, so I don't think it can be null here, but worth keeping in mind. Is dropping the top-level sqlQueryId intentional? If so, could it be documented? test_queryMetrics_metricsQuery in IndexerControllerContextTest sets sqlQueryId in the context, but its expected map does not include it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was intentional to change sqlQueryId in this way for MSQ query/* metrics, because this aligns them better with native query/* metrics. Sites with custom QueryMetrics extensions that override sqlQueryId will still emit it, just like native metrics.

queryMetrics,
toolbox.getEmitter(),
MSQTaskSqlEngine.NAME,
task.getMetricBuilder().getDimensions()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the Dart controller: the task dimensions do not include sqlQueryId, so it is lost on query/time. makeMetricBuilder() is the task builder plus the query dimensions, so this restores it:

Suggested change
task.getMetricBuilder().getDimensions()
makeMetricBuilder().getDimensions()

The worker contexts (IndexerWorkerContext#emitQueryMetrics, DartWorkerContext#emitQueryMetrics) have the same gap and need sqlQueryId from the query context added to their extra dimensions when non-null.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the same point as #20947 (comment).

final Query<?> metricsQuery;

if (querySpec instanceof LegacyMSQSpec) {
// Use the native query, so metrics have the same dimensions as they would for native execution.

@amaechler amaechler Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the native query here changes the controller query/time event compared to workers and ingest/*: type becomes scan/groupBy (workers stay msq), interval and duration come from the outer native query instead of the union of stage table intervals, success is a string, and context is new. MSQControllerTask always holds a LegacyMSQSpec, so this covers every task-based controller. Dashboards filtering on type=msq will stop matching. I suspect interval is eternity for nested queries with the time filter in the inner query, but I did not run it. Could you document this (suggestion on metrics.md) and mention it in the release notes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentional, the purpose is to align MSQ query/* metrics with native query metrics as much asa possible on the Broker (where the controller runs). I updated the docs to note it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a release note to the PR description too.


Query metrics have these ID dimensions:

* `queryId`: the [`queryId`](../querying/query-context-reference.md) of the query. Use it to correlate metrics across services and query types.

@amaechler amaechler Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest documenting the MSQ dimension changes, after the "Native queries always have a queryId..." paragraph:

MSQ controller query/time uses the native query's type, interval and duration when the query has one, and type=msq otherwise. MSQ worker query/time and query/cpu/time always report type=msq. success is emitted as a string, like native metrics.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added these to metrics.md in a different way, folded into the table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approval Recommended Used when AI completes a review and reports no findings Area - Batch Ingestion Area - Documentation Area - Metrics/Event Emitting Area - MSQ For multi stage queries - https://github.com/apache/druid/issues/12262 Area - Querying Release Notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants