Skip to content

feat: add better instrumentation to querier methods using new metrics stats#7571

Open
javiermolinar wants to merge 5 commits into
mainfrom
instrument-querier-methods
Open

feat: add better instrumentation to querier methods using new metrics stats#7571
javiermolinar wants to merge 5 commits into
mainfrom
instrument-querier-methods

Conversation

@javiermolinar

@javiermolinar javiermolinar commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

What this PR does:
Add span traces for all querier methods with standardized attributes and the new stats implemented in #7504

Which issue(s) this PR fixes:
Fixes #

Checklist

  • Tests updated
  • Documentation added
  • Changelog entry added under .chloggen/ (run make chlog-new, or make chlog-new FILENAME=<name> to override the default branch-name file; see .chloggen/README.md)

@github-actions

This comment has been minimized.

Copilot AI 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.

Pull request overview

This PR adds shared OpenTelemetry span helpers for the querier and wires them into several querier request paths to attach request parameters and the new “stats metrics” fields onto spans.

Changes:

  • Introduces start*/finish* helpers to consistently set querier span attributes and attach SearchMetrics / MetadataMetrics / TraceByIDMetrics to spans.
  • Updates multiple querier methods (trace-by-id, search/tags/tag-values, query-range) to use the new span helpers and record errors via a shared finishQuerierSpan.
  • Adds focused unit tests validating that span attributes and AdditionalMetrics are exported as span attributes.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
modules/querier/span_attrs.go New helper functions to start/end spans and attach request attributes + metrics.
modules/querier/span_attrs_test.go Tests that span attributes + metrics are correctly emitted (including AdditionalMetrics).
modules/querier/querier.go Replaces ad-hoc span code with shared helpers across several querier RPC methods.
modules/querier/querier_query_range.go Adds spans around QueryRange execution paths using the shared helpers.
.chloggen/instrument-querier-methods.yaml Adds a changelog entry for the querier instrumentation change.

Comment thread modules/querier/querier.go Outdated
Comment thread modules/querier/querier.go
Comment thread modules/querier/span_attrs.go Dismissed
Comment thread modules/querier/span_attrs.go Dismissed
Comment thread modules/querier/span_attrs.go Dismissed
Comment thread modules/querier/span_attrs.go Dismissed
@github-actions

This comment has been minimized.

Copilot AI review requested due to automatic review settings July 2, 2026 15:11
@github-actions

This comment has been minimized.

Copilot AI 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.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Comment thread modules/querier/span_attrs.go
Comment thread modules/querier/span_attrs_test.go
Comment thread modules/querier/querier.go Outdated
Comment thread modules/querier/querier.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 2, 2026 15:19
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@github-actions

This comment has been minimized.

1 similar comment
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

⚠️ The workflow jobs listed below don't declare a permissions: block and may break when the organization's default GITHUB_TOKEN permissions are restricted to read-only.

Expand for findings
warning[excessive-permissions]: overly broad permissions
  --> ./.github/workflows/deploy-pr-preview.yml:13:3
   |
13 | /   deploy-pr-preview:
14 | |     if: "!github.event.pull_request.head.repo.fork"
15 | |     uses: grafana/writers-toolkit/.github/workflows/deploy-preview.yml@main # zizmor: ignore[unpinned-uses]
16 | |     with:
...  |
31 | |       title: ${{ github.event.pull_request.title }}
   | |                                                    ^
   | |                                                    |
   | |____________________________________________________this job
   |                                                      default permissions used due to no permissions: block
   |
   = note: audit confidence → Medium
   = help: audit documentation → https://docs.zizmor.sh/audits/#excessive-permissions

Copilot AI 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.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Comment on lines +703 to +707
ctx, span, tenantID, err := startTagsBlockSpan(ctx, "Querier.internalTagsSearchBlockV2", req)
if err != nil {
return nil, fmt.Errorf("error extracting org id in Querier.BackendSearch: %w", err)
}
defer func() { finishQuerierSpan(span, err, resp.GetMetrics()) }()

@stoewer stoewer 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.

I left a few minor comments. Otherwise this looks good 👍


func (q *Querier) SearchTagValuesBlocks(ctx context.Context, req *tempopb.SearchTagValuesBlockRequest) (*tempopb.SearchTagValuesResponse, error) {
func (q *Querier) SearchTagValuesBlocks(ctx context.Context, req *tempopb.SearchTagValuesBlockRequest) (resp *tempopb.SearchTagValuesResponse, err error) {
ctx, span, _, err := startTagValuesBlockSpan(ctx, "Querier.SearchTagValuesBlocks", req)

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.

The call to q.internalTagValuesSearchBlock(ctx, req) later in this function creates a span with the name Querier.internalTagsSearchBlockV2 that carries similar (identical?) information. This is a bit redundant. Should we remove the internal one?

The same pattern with similarly duplicated spans occurs in SearchTagsBlocks, SearchTagsBlocksV2, and SearchTagsBlocksV2 as well

resp, err := q.store.SearchTagValues(ctx, meta, req, opts)
resp, err = q.store.SearchTagValues(ctx, meta, req, opts)
if err != nil {
return &tempopb.SearchTagValuesResponse{}, err

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.

I think this overwrites the named return value resp that is used in the deferred call to finishQuerierSpan() above. But readerWriter.SearchTagValues() populates metrics also when the function returns an error. Due to this overwrite, the metrics are not captured by the deferred call to finishQuerierSpan()

I think the main culprit here are the named return values (resp *tempopb.SearchTagValuesResponse, err error) as those tend to cause maintenance hazards in longer functions. Not a must, but finding an alternative pattern to capture the error and stats in the finish call would be nice (but not worth it, if it makes the function complex in other ways)

)
}

func setQuerierSpanMetrics(span oteltrace.Span, metrics any) {

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.

The type switch without default case can silently do nothing when additional metrics structs are added in the future. Instead of one function with a type switch, this function can be refactored into 3 small separate function for each type

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants