-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat(explore): add --metric flag for auto-resolving tracemetrics format #927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4058eb1
401fb61
9832013
1477279
f645f2a
320e10a
6ea7639
f73cf75
fda1fcd
8018619
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ import { | |
| isReplaySortValue, | ||
| listReplays, | ||
| queryEvents, | ||
| queryMetricsMeta, | ||
| } from "../lib/api-client.js"; | ||
| import { buildProjectQuery, validateLimit } from "../lib/arg-parsing.js"; | ||
| import { | ||
|
|
@@ -33,6 +34,7 @@ import { | |
| paginationHint, | ||
| } from "../lib/list-command.js"; | ||
| import { logger } from "../lib/logger.js"; | ||
| import { resolveMetricField } from "../lib/metrics-transform.js"; | ||
| import { withProgress } from "../lib/polling.js"; | ||
| import { | ||
| DEFAULT_REPLAY_EXPLORE_FIELDS, | ||
|
|
@@ -123,6 +125,8 @@ const API_TO_USER_DATASET = new Map( | |
|
|
||
| type ExploreFlags = { | ||
| readonly field?: string[]; | ||
| readonly metric?: string; | ||
| readonly agg: string; | ||
| readonly dataset: string; | ||
| readonly environment?: readonly string[]; | ||
| readonly query?: string; | ||
|
|
@@ -311,7 +315,15 @@ function appendFlagHints( | |
| base: string, | ||
| flags: Pick< | ||
| ExploreFlags, | ||
| "dataset" | "environment" | "sort" | "query" | "period" | "field" | "limit" | ||
| | "dataset" | ||
| | "environment" | ||
| | "sort" | ||
| | "query" | ||
| | "period" | ||
| | "field" | ||
| | "limit" | ||
| | "metric" | ||
| | "agg" | ||
| > | ||
| ): string { | ||
| const parts: string[] = []; | ||
|
|
@@ -323,10 +335,20 @@ function appendFlagHints( | |
| API_TO_USER_DATASET.get(flags.dataset) ?? flags.dataset; | ||
| parts.push(`--dataset ${displayDataset}`); | ||
| } | ||
| if (flags.metric) { | ||
| parts.push(`-m "${flags.metric}"`); | ||
| if (flags.agg !== "sum") { | ||
| parts.push(`--agg ${flags.agg}`); | ||
| } | ||
| } | ||
| appendSortHint(parts, flags.sort, defaultSort); | ||
| appendQueryHint(parts, flags.query); | ||
| // Include --field flags when non-default | ||
| const fieldList = flags.field ?? []; | ||
| // Include --field flags when non-default. | ||
| // When --metric is active, aggregates are dropped from the query — mirror that here. | ||
| const rawFields = flags.field ?? []; | ||
| const fieldList = flags.metric | ||
| ? rawFields.filter((f) => !isAggregate(f)) | ||
| : rawFields; | ||
| const currentFieldStr = fieldList.join(","); | ||
| if ( | ||
| currentFieldStr !== defaultFieldsForDataset(flags.dataset).join(",") && | ||
|
|
@@ -356,6 +378,53 @@ function findFirstAggregate(fieldList: string[]): string | undefined { | |
| return fieldList.find((f) => f.includes("(") && f.includes(")")); | ||
| } | ||
|
|
||
| /** True when the field looks like an aggregate call: `fn(...)`. */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pagination hints omit new
|
||
| function isAggregate(field: string): boolean { | ||
| return field.includes("(") && field.endsWith(")"); | ||
| } | ||
|
|
||
| /** | ||
| * True when the aggregate uses the tracemetrics comma-separated format: | ||
| * `aggregation(value,metric_name,metric_type,unit)`. | ||
| */ | ||
| function isTracemetricsAggregate(aggregate: string): boolean { | ||
| const parenIdx = aggregate.indexOf("("); | ||
| if (parenIdx < 0) { | ||
| return false; | ||
| } | ||
| const inner = aggregate.slice(parenIdx + 1, -1); | ||
| return inner.startsWith("value,") && inner.split(",").length === 4; | ||
| } | ||
|
|
||
| /** | ||
| * Validate that aggregate fields use the tracemetrics format when querying | ||
| * the `metricsEnhanced` dataset. Standard aggregates like `count()` or | ||
| * `avg(measurements.fcp)` are invalid — the API requires the four-part | ||
| * comma-separated format: `aggregation(value,metric_name,metric_type,unit)`. | ||
| */ | ||
| function validateMetricsFields(fieldList: string[]): void { | ||
| const badAggs = fieldList.filter( | ||
| (f) => isAggregate(f) && !isTracemetricsAggregate(f) | ||
| ); | ||
| if (badAggs.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| throw new ValidationError( | ||
| `Invalid metrics aggregate${badAggs.length > 1 ? "s" : ""}: ${badAggs.join(", ")}\n\n` + | ||
| "The metrics dataset requires the format: aggregation(value,metric_name,metric_type,unit)\n\n" + | ||
| "Examples:\n" + | ||
| ' sentry explore my-org/ -F "sum(value,llm.token_usage,distribution,none)" --dataset metrics\n' + | ||
| ' sentry explore my-org/ -F gen_ai.request.model -F "avg(value,cache.hit_rate,distribution,none)" --dataset metrics\n\n' + | ||
| "Parameters:\n" + | ||
| ' - value: literal string "value"\n' + | ||
| " - metric_name: the metric name emitted by the SDK (e.g., llm.token_usage)\n" + | ||
| " - metric_type: distribution, gauge, counter, or set\n" + | ||
| " - unit: none, byte, second, millisecond, etc.", | ||
| "field" | ||
| ); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Dataset configuration | ||
| // --------------------------------------------------------------------------- | ||
|
|
@@ -508,7 +577,7 @@ export const exploreCommand = buildListCommand("explore", { | |
| "Datasets:\n" + | ||
| " errors Error events (default)\n" + | ||
| " spans Span data\n" + | ||
| " metrics Custom metrics\n" + | ||
| " metrics Custom metrics (tracemetrics format)\n" + | ||
| " logs Log entries\n" + | ||
| " replays Session replay search\n\n" + | ||
| "Targets:\n" + | ||
|
|
@@ -523,7 +592,11 @@ export const exploreCommand = buildListCommand("explore", { | |
| "--dataset spans\n" + | ||
| " sentry explore my-org/cli --dataset replays -F id -F user.email -F count_errors\n" + | ||
| ' sentry explore -F span.op -F "count()" --dataset spans --period 1h\n' + | ||
| " sentry explore --json", | ||
| " sentry explore --json\n\n" + | ||
| "Metrics (auto mode — resolves type/unit automatically):\n" + | ||
| " sentry explore my-org/ -m llm.token_usage --dataset metrics\n" + | ||
| " sentry explore my-org/seer -F gen_ai.request.model -m llm.token_usage --dataset metrics --period 7d\n" + | ||
| " sentry explore my-org/ -m cache.hit_rate --agg avg --dataset metrics", | ||
| }, | ||
| output: { | ||
| human: formatExploreHuman, | ||
|
|
@@ -551,6 +624,19 @@ export const exploreCommand = buildListCommand("explore", { | |
| variadic: true, | ||
| optional: true, | ||
| }, | ||
| metric: { | ||
| kind: "parsed", | ||
| parse: String, | ||
| brief: | ||
| "Metric name for --dataset metrics. Auto-resolves type/unit via API.", | ||
| optional: true, | ||
| }, | ||
| agg: { | ||
| kind: "parsed", | ||
| parse: String, | ||
| brief: "Aggregation for --metric (sum, avg, count, p50, p95, etc.)", | ||
| default: "sum", | ||
| }, | ||
| dataset: { | ||
| kind: "parsed", | ||
| parse: parseDataset, | ||
|
|
@@ -594,6 +680,7 @@ export const exploreCommand = buildListCommand("explore", { | |
| ...PERIOD_ALIASES, | ||
| e: "environment", | ||
| F: "field", | ||
| m: "metric", | ||
| d: "dataset", | ||
| q: "query", | ||
| s: "sort", | ||
|
|
@@ -608,14 +695,57 @@ export const exploreCommand = buildListCommand("explore", { | |
| "explore" | ||
| ); | ||
|
|
||
| const dataset = flags.dataset; | ||
| let dataset = flags.dataset; | ||
| const userSuppliedFields = flags.field && flags.field.length > 0; | ||
| let fieldList = [...defaultFieldsForDataset(dataset)]; | ||
| if (flags.field && flags.field.length > 0) { | ||
| if (userSuppliedFields) { | ||
| fieldList = flags.field; | ||
| } | ||
| const timeRange = flags.period; | ||
| const environment = parseReplayEnvironmentFilter(flags.environment); | ||
|
|
||
| // --metric auto mode: resolve metric name → tracemetrics aggregate | ||
| if (flags.metric) { | ||
| if (dataset !== "metricsEnhanced") { | ||
| log.warn("--metric implies --dataset metrics; switching dataset."); | ||
| dataset = "metricsEnhanced"; | ||
| } | ||
|
sentry[bot] marked this conversation as resolved.
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| // Use the user's --period for metadata discovery so older metrics are found | ||
| const metaParams = timeRangeToApiParams(timeRange); | ||
| const metrics = await withProgress( | ||
| { | ||
| message: `Discovering metric '${flags.metric}'...`, | ||
| json: flags.json, | ||
| }, | ||
| () => | ||
| queryMetricsMeta(org, { | ||
| ...metaParams, | ||
| project, | ||
| }) | ||
|
sentry[bot] marked this conversation as resolved.
sentry[bot] marked this conversation as resolved.
|
||
| ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolute time ranges silently ignored for metric discoveryMedium Severity When the user specifies an absolute time range (e.g., Additional Locations (1)Reviewed by Cursor Bugbot for commit f645f2a. Configure here.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch — fixed in 320e10a. |
||
|
|
||
| const aggField = resolveMetricField(flags.metric, flags.agg, metrics); | ||
| // Prepend any user-supplied grouping fields, then the resolved aggregate | ||
| const groupByFields = userSuppliedFields | ||
| ? fieldList.filter((f) => !isAggregate(f)) | ||
| : []; | ||
| fieldList = [...groupByFields, aggField]; | ||
| } else if (dataset === "metricsEnhanced") { | ||
| if (!userSuppliedFields) { | ||
| throw new ValidationError( | ||
| "The metrics dataset requires --metric or explicit --field flags.\n\n" + | ||
| "Auto mode (recommended):\n" + | ||
| " sentry explore my-org/ -m llm.token_usage --dataset metrics\n" + | ||
| " sentry explore my-org/ -m llm.token_usage --agg avg --dataset metrics\n\n" + | ||
| "Manual mode (tracemetrics format):\n" + | ||
| ' sentry explore my-org/ -F "sum(value,llm.token_usage,distribution,none)" --dataset metrics', | ||
| "field" | ||
| ); | ||
| } | ||
| validateMetricsFields(fieldList); | ||
| } | ||
|
|
||
| const config = resolveDatasetConfig({ | ||
| dataset, | ||
| fieldList, | ||
|
sentry[bot] marked this conversation as resolved.
|
||
|
|
@@ -656,11 +786,18 @@ export const exploreCommand = buildListCommand("explore", { | |
| const hasMore = !!nextCursor; | ||
|
|
||
| const baseTarget = project ? `${org}/${project}` : `${org}/`; | ||
| const hintFlags = { ...flags, dataset }; | ||
| const nav = paginationHint({ | ||
| hasPrev, | ||
| hasMore, | ||
| prevHint: appendFlagHints(`sentry explore ${baseTarget} -c prev`, flags), | ||
| nextHint: appendFlagHints(`sentry explore ${baseTarget} -c next`, flags), | ||
| prevHint: appendFlagHints( | ||
| `sentry explore ${baseTarget} -c prev`, | ||
| hintFlags | ||
| ), | ||
| nextHint: appendFlagHints( | ||
| `sentry explore ${baseTarget} -c next`, | ||
| hintFlags | ||
| ), | ||
| }); | ||
|
|
||
| const hint = buildResultHint(response.data.length, nav); | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.