Skip to content

Commit f73cf75

Browse files
fix: paginate queryMetricsMeta to handle orgs with >100 metrics
Previously queryMetricsMeta fetched a single page (100 results) and discarded the cursor, silently truncating the metric list for large orgs. Now uses the same pagination loop as queryEvents, bounded by MAX_PAGINATION_PAGES.
1 parent 6ea7639 commit f73cf75

1 file changed

Lines changed: 32 additions & 18 deletions

File tree

src/lib/api/discover.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ export type MetricMeta = {
9898
*
9999
* Queries `dataset=metricsEnhanced` with meta-fields (`metric.name`, etc.)
100100
* — the same technique the Sentry Explore Metrics UI uses.
101+
*
102+
* Auto-paginates to collect all available metrics (bounded by
103+
* {@link MAX_PAGINATION_PAGES} to prevent runaway loops).
101104
*/
102105
export async function queryMetricsMeta(
103106
orgSlug: string,
@@ -111,25 +114,36 @@ export async function queryMetricsMeta(
111114
const regionUrl = await resolveOrgRegion(orgSlug);
112115
const query = options?.project ? `project:${options.project}` : undefined;
113116

114-
const { data } = await fetchEventsPage(
115-
regionUrl,
116-
orgSlug,
117-
{
118-
fields: ["metric.name", "metric.type", "metric.unit"],
119-
dataset: "metricsEnhanced",
120-
query,
121-
statsPeriod:
122-
options?.start || options?.end
123-
? undefined
124-
: (options?.statsPeriod ?? "7d"),
125-
start: options?.start,
126-
end: options?.end,
127-
limit: 100,
128-
},
129-
100
130-
);
117+
const baseOptions: ExploreQueryOptions = {
118+
fields: ["metric.name", "metric.type", "metric.unit"],
119+
dataset: "metricsEnhanced",
120+
query,
121+
statsPeriod:
122+
options?.start || options?.end
123+
? undefined
124+
: (options?.statsPeriod ?? "7d"),
125+
start: options?.start,
126+
end: options?.end,
127+
};
128+
129+
const allRows: Record<string, unknown>[] = [];
130+
let cursor: string | undefined;
131+
132+
for (let page = 0; page < MAX_PAGINATION_PAGES; page += 1) {
133+
const result = await fetchEventsPage(
134+
regionUrl,
135+
orgSlug,
136+
{ ...baseOptions, cursor },
137+
API_MAX_PER_PAGE
138+
);
139+
140+
allRows.push(...result.data.data);
141+
142+
if (!result.nextCursor) break;
143+
cursor = result.nextCursor;
144+
}
131145

132-
return data.data.map((row) => ({
146+
return allRows.map((row) => ({
133147
name: String(row["metric.name"] ?? ""),
134148
type: String(row["metric.type"] ?? "distribution"),
135149
unit: String(row["metric.unit"] ?? "none"),

0 commit comments

Comments
 (0)