Skip to content

feat(search_events): add metrics dataset support - #899

Closed
sentry-junior[bot] wants to merge 1 commit into
mainfrom
feat/metrics-dataset
Closed

sentry-junior[bot] wants to merge 1 commit into
mainfrom
feat/metrics-dataset

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds the metrics dataset to the search_events MCP tool, enabling queries against Sentry's transaction performance metrics (backed by metrics_performance in snuba).

Previously the tool only knew about spans, errors, and logs. This PR makes the agent aware of metrics so it can answer questions like:

  • "what are the slowest transactions by p75?"
  • "show me failure_rate by endpoint"
  • "which pages have the worst apdex score?"
  • "what's the transaction throughput (tpm)?"

Dataset confirmed from getsentry/sentry

src/sentry/snuba/utils.py maps "metrics" -> metrics_performance

src/sentry/search/events/datasets/metrics.py confirms supported aggregates: apdex, failure_rate, epm, p50-p99(transaction.duration), user_misery.

Changes

  • config.ts: Added metrics to dataset selection prompt, NUMERIC_FIELDS, DATASET_FIELDS, DATASET_EXAMPLES, RECOMMENDED_FIELDS
  • agent.ts: Extended dataset enum to include metrics
  • utils.ts: Extended fetchCustomAttributes + createDatasetAttributesTool enum
  • formatters.ts: Added formatMetricsResults()
  • handler.ts: Added metrics to tool description and switch
  • client.ts: Routes metrics through discover API with dataset=metrics; extended buildDiscoverUrl and buildDiscoverApiQuery to accept dataset override
  • mcp-server-mocks: New fixtures + mock handler for dataset=metrics
  • search-events.test.ts: New test for metrics dataset

Test results

All 804 tests pass. Lint clean.

Adds the 'metrics' dataset to the search_events tool, enabling the MCP
to query Sentry's transaction performance metrics (backed by
metrics_performance / snuba's MetricsQueryBuilder).

## What changed

### search-events/config.ts
- Added 'metrics' to DATASET SELECTION GUIDELINES in systemPrompt
- Updated systemPrompt return format to include 'metrics' as valid dataset
- Added NUMERIC_FIELDS for metrics (transaction.duration, web vitals)
- Added DATASET_FIELDS['metrics'] with transaction fields, web vitals,
  and all supported aggregate functions (apdex, failure_rate, tpm, epm,
  p50-p99, user_misery, avg, sum, count, count_unique)
- Added DATASET_EXAMPLES['metrics'] with 5 representative query patterns
- Added RECOMMENDED_FIELDS['metrics'] with throughput and latency defaults

### search-events/agent.ts
- Extended dataset enum to include 'metrics'

### search-events/utils.ts
- Extended fetchCustomAttributes signature to accept 'metrics'
- For metrics, falls back to listing 'events' tags (transaction-level)
- Extended createDatasetAttributesTool dataset enum to include 'metrics'

### search-events/formatters.ts
- Added formatMetricsResults() following existing formatter pattern
- Metrics results are always aggregate; outputs JSON block for table rendering

### search-events/handler.ts
- Imports formatMetricsResults
- Added 'metrics' to tool description
- Added 'metrics' case to the dataset switch

### api-client/client.ts
- Extended searchEvents() dataset type to include 'metrics'
- Routes 'metrics' through buildDiscoverApiQuery (same endpoint as errors)
  with dataset=metrics query param
- Extended getEventsExplorerUrl() to handle 'metrics' via buildDiscoverUrl
- Extended buildDiscoverApiQuery() and buildDiscoverUrl() to accept
  optional dataset override param

### mcp-server-mocks
- Added events-metrics.json and events-metrics-empty.json fixtures
- Added metrics mock handler in the /events/ path

### search-events.test.ts
- Added test: 'should handle metrics dataset queries for transaction performance'
  verifying dataset=metrics is sent to API and results are formatted correctly

## Confirmed behaviors from getsentry/sentry
- src/sentry/snuba/utils.py maps 'metrics' -> metrics_performance
- src/sentry/search/events/datasets/metrics.py confirms apdex, failure_rate,
  epm, p75, p95, p99 of transaction.duration as primary aggregates
- Uses /organizations/{org}/events/ with dataset=metrics (discover-style API)
@dcramer dcramer closed this Apr 14, 2026
dcramer added a commit that referenced this pull request Apr 14, 2026
Route newer span metrics queries through Sentry's tracemetrics dataset instead of the older metrics attempt from PR #899. Preserve raw aggregate sort expressions, fetch tracemetrics attributes, and generate Metrics page links for aggregate and sample results.

Add mocks, formatter coverage, and docs for the tracemetrics dataset so search_events and list_events surface the current-generation span metrics correctly.

Co-Authored-By: Codex <codex@openai.com>
dcramer added a commit that referenced this pull request Apr 15, 2026
Add support for Sentry's current-generation span metrics in
search_events and list_events.

The previous attempt in #899 targeted the wrong dataset. The current
Sentry path uses dataset=tracemetrics on the events endpoint, raw metric
aggregate expressions such as
p95(value,http.request.duration,distribution,millisecond), and Metrics
page URLs under /explore/metrics/. This updates the MCP query builder,
formatting, mocks, tests, generated definitions, and docs to match that
behavior.

This also preserves raw aggregate sort expressions for tracemetrics so
grouped metric queries keep working instead of being rewritten into
Discover-style aliases.

---------

Co-authored-by: Codex <codex@openai.com>

This branch was previously deployed

1 inactive deployment
Actions — ddf07c8c Deployed Apr 13, 2026 by sentry-junior[bot] via eval #792
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.

1 participant