Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/promql-min-auto-granularity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@hyperdx/common-utils': minor
'@hyperdx/app': minor
'@hyperdx/api': patch
---

Support "Minimum auto granularity" on PromQL sources. It floors the step of charts on auto granularity, as it does for metric sources, and `$__rate_interval` now uses it as the source's scrape interval (still 15s when unset) instead of always assuming 15s. A granularity picked on a tile is never changed.
21 changes: 13 additions & 8 deletions packages/api/src/models/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ const MetricTablesSchema = new Schema(
{ _id: false },
);

// See MinAutoGranularitySchema in @hyperdx/common-utils, shared by the metric
// and PromQL sources. The setter mirrors that schema's '' -> undefined
// preprocess, since validateRequest doesn't write the parsed body back to
// req.body.
const minAutoGranularityField = {
type: String,
set: (v: string | null | undefined) => (v === '' ? undefined : v),
};

type IMetricSource = Extract<ISource, { kind: SourceKind.Metric }>;
export const MetricSource = Source.discriminator<IMetricSource>(
SourceKind.Metric,
Expand All @@ -284,13 +293,7 @@ export const MetricSource = Source.discriminator<IMetricSource>(
logSourceId: String,
// Unified metrics series table. Available only when `isMetricsSeriesTableEnabled` is set on the team document.
seriesTable: String,
// See MetricSourceSchema.minAutoGranularity in @hyperdx/common-utils.
// The setter mirrors that schema's '' -> undefined preprocess, since
// validateRequest doesn't write the parsed body back to req.body.
minAutoGranularity: {
type: String,
set: (v: string | null | undefined) => (v === '' ? undefined : v),
},
minAutoGranularity: minAutoGranularityField,
}),
);

Expand All @@ -300,5 +303,7 @@ export const MetricSource = Source.discriminator<IMetricSource>(
type IPromqlSource = Extract<ISource, { kind: SourceKind.Promql }>;
export const PromqlSource = Source.discriminator<IPromqlSource>(
SourceKind.Promql,
new Schema<Extract<ISource, { kind: SourceKind.Promql }>>({}),
new Schema<Extract<ISource, { kind: SourceKind.Promql }>>({
minAutoGranularity: minAutoGranularityField,
}),
);
98 changes: 98 additions & 0 deletions packages/api/src/routers/api/__tests__/sources.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ const MOCK_METRIC_SOURCE: Omit<Extract<TSource, { kind: 'metric' }>, 'id'> = {
},
};

const MOCK_PROMQL_SOURCE: Omit<Extract<TSource, { kind: 'promql' }>, 'id'> = {
kind: SourceKind.Promql,
name: 'Test PromQL Source',
connection: new Types.ObjectId().toString(),
from: {
databaseName: 'test_db',
tableName: 'metrics_ts',
},
timestampValueExpression: 'timestamp',
};

const createTestConnection = (team: Types.ObjectId, id: string) =>
Connection.create({
_id: id,
Expand All @@ -63,6 +74,7 @@ const getLoggedInAgent = async (server: ReturnType<typeof getServer>) => {
await Promise.all([
createTestConnection(result.team._id, MOCK_SOURCE.connection),
createTestConnection(result.team._id, MOCK_METRIC_SOURCE.connection),
createTestConnection(result.team._id, MOCK_PROMQL_SOURCE.connection),
]);

return result;
Expand Down Expand Up @@ -803,6 +815,92 @@ describe('sources router', () => {
});
});

describe('minAutoGranularity on a PromQL source', () => {
it('POST / - creates a PromQL source with minAutoGranularity and it round-trips', async () => {
const { agent } = await getLoggedInAgent(server);

const response = await agent
.post('/sources')
.send({
...MOCK_PROMQL_SOURCE,
minAutoGranularity: '1 minute',
})
.expect(200);

expect(response.body.minAutoGranularity).toBe('1 minute');

const sources = await Source.find({}).lean();
expect(sources).toHaveLength(1);
const persisted = sources[0];
if (persisted?.kind !== SourceKind.Promql) {
expect(persisted?.kind).toBe(SourceKind.Promql);
throw new Error('Source is not a PromQL source');
}
expect(persisted.minAutoGranularity).toBe('1 minute');
});

it("POST / - the form's \"No minimum\" value ('') persists as unset, not as an empty string", async () => {
const { agent } = await getLoggedInAgent(server);

const response = await agent
.post('/sources')
.send({
...MOCK_PROMQL_SOURCE,
minAutoGranularity: '',
})
.expect(200);

expect(response.body).not.toHaveProperty('minAutoGranularity');

const sources = await Source.find({}).lean();
expect(sources).toHaveLength(1);
expect(sources[0]).not.toHaveProperty('minAutoGranularity');
});

it('PUT /:id - updates minAutoGranularity', async () => {
const { agent, team } = await getLoggedInAgent(server);

const promqlSource = await Source.create({
...MOCK_PROMQL_SOURCE,
minAutoGranularity: '1 minute',
team: team._id,
});

await agent
.put(`/sources/${promqlSource._id}`)
.send({
id: promqlSource._id.toString(),
...MOCK_PROMQL_SOURCE,
minAutoGranularity: '5 minute',
})
.expect(200);

const updatedSource = await Source.findById(promqlSource._id).lean();
expect(updatedSource).toHaveProperty('minAutoGranularity', '5 minute');
});

it('PUT /:id - removes minAutoGranularity when omitted from the update payload', async () => {
const { agent, team } = await getLoggedInAgent(server);

const promqlSource = await Source.create({
...MOCK_PROMQL_SOURCE,
minAutoGranularity: '1 minute',
team: team._id,
});

await agent
.put(`/sources/${promqlSource._id}`)
.send({
id: promqlSource._id.toString(),
...MOCK_PROMQL_SOURCE,
})
.expect(200);

const updatedSource = await Source.findById(promqlSource._id).lean();
expect(updatedSource).not.toHaveProperty('minAutoGranularity');
});
});

it('DELETE /:id - deletes a source', async () => {
const { agent, team } = await getLoggedInAgent(server);

Expand Down
1 change: 1 addition & 0 deletions packages/app/src/ChartUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ function getAlignedRangeAndGranularity(
const granularity = getTimeChartGranularity(
config.granularity,
config.dateRange,
config.minGranularitySeconds,
);
return {
granularity,
Expand Down
46 changes: 46 additions & 0 deletions packages/app/src/__tests__/ChartUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,29 @@ describe('ChartUtils', () => {

expect(result.dateRange).toEqual(promqlConfig.dateRange);
});

it("floors an auto granularity at the source's minimum", () => {
const result = convertToPromqlTableChartConfig({
...promqlConfig,
minGranularitySeconds: 300,
});

expect(result.granularity).toBe('5 minute');
expect(result.dateRange).toEqual([
new Date('2025-11-26T00:00:00Z'),
new Date('2025-11-26T01:05:00Z'),
]);
});

it('never floors a granularity the tile picked', () => {
const result = convertToPromqlTableChartConfig({
...promqlConfig,
granularity: '15 second',
minGranularitySeconds: 300,
});

expect(result.granularity).toBe('15 second');
});
});

describe('convertToReducedPromqlChartConfig and convertToPromqlSparklineChartConfig', () => {
Expand Down Expand Up @@ -1364,6 +1387,29 @@ describe('ChartUtils', () => {
expect(result.granularity).toBe('30 minute');
});

it("floors an auto granularity at the source's minimum", () => {
const result = convertToReducedPromqlChartConfig({
...promqlConfig,
minGranularitySeconds: 300,
});

expect(result.granularity).toBe('5 minute');
expect(result.dateRange).toEqual([
new Date('2025-11-26T00:00:00Z'),
new Date('2025-11-26T01:05:00Z'),
]);
});

it('never floors a granularity the tile picked', () => {
const result = convertToReducedPromqlChartConfig({
...promqlConfig,
granularity: '15 second',
minGranularitySeconds: 300,
});

expect(result.granularity).toBe('15 second');
});

it('names the reducer, defaulting it, and queries one expression', () => {
const result = convertToReducedPromqlChartConfig(promqlConfig);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ import { DBTimeChart } from '@/components/DBTimeChart';
import EmptyState from '@/components/EmptyState';
import PatternTable from '@/components/PatternTable';
import PromQLPreview from '@/components/PromQLEditor/PromQLPreview';
import { getMinGranularitySeconds } from '@/hooks/useChartConfig';
import {
getEventBody,
getFirstTimestampValueExpression,
isSingleExpression,
useSource,
} from '@/source';
import {
orderByStringToSortingState,
Expand Down Expand Up @@ -169,9 +171,11 @@ export function ChartPreviewPanel({
}: ChartPreviewPanelProps) {
const [isSampleEventsOpen, setIsSampleEventsOpen] = useState(false);

const { data: queriedSource } = useSource({ id: queriedConfig?.source });
const minGranularitySeconds = getMinGranularitySeconds(queriedSource);
const renderedPromql = useMemo(
() => buildRenderedPromqlExpression(queriedConfig),
[queriedConfig],
() => buildRenderedPromqlExpression(queriedConfig, minGranularitySeconds),
[queriedConfig, minGranularitySeconds],

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.

🔵 minor — On the time tab, the Generated PromQL preview and the chart can take the step floor from different sources

On the time tab, DBTimeChart gets sourceId={sourceId}, which is the form's currently selected source (useWatch('source') in EditTimeChartForm.tsx:267). It resolves auto granularity from that source's minAutoGranularity (DBTimeChart.tsx:408-431). The preview now resolves it from queriedConfig.source. So if the user picks a different PromQL source without re-running, $__interval and the step in the preview won't match what the chart sent. The comment in the new test says "the chart reads the floor from the source it ran with", which isn't true for the line chart. To fix, pass queriedConfig.source as the DBTimeChart sourceId in the preview, or make both read from the same source.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

);

const blockingFilterNames = missingRequiredFilterNames ?? [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ jest.mock('@/components/DBHeatmapChart', () => ({
HEATMAP_N_BUCKETS: 80,
}));

const mockUseSource = jest.fn();
jest.mock('@/source', () => ({
getFirstTimestampValueExpression: jest.fn().mockReturnValue('Timestamp'),
useSource: (opts: { id?: string }) => mockUseSource(opts),
}));

const dateRange: [Date, Date] = [
Expand Down Expand Up @@ -118,6 +120,7 @@ const renderPanel = (
describe('ChartPreviewPanel', () => {
beforeEach(() => {
jest.clearAllMocks();
mockUseSource.mockReturnValue({ data: undefined });
});

describe('when no query has been run', () => {
Expand Down Expand Up @@ -278,6 +281,8 @@ describe('ChartPreviewPanel', () => {
overrides: {
promqlExpression?: PromqlExpressionList;
displayType?: DisplayType;
source?: string;
granularity?: string;
variables?: ChartVariable[];
} = {},
): ChartConfigWithDateRange => ({
Expand Down Expand Up @@ -423,6 +428,44 @@ describe('ChartPreviewPanel', () => {
).not.toBeInTheDocument();
});

// Selecting another source doesn't rerun the query, and the chart reads the
// floor from the source it ran with, so the preview has to as well.
it("expands macros with the queried source's floor, not the selected one's", async () => {
const promqlSource = (
id: string,
minAutoGranularity?: string,
): TSource => ({
id,
kind: SourceKind.Promql,
name: id,
connection: 'default',
from: { databaseName: 'default', tableName: 'metrics_ts' },
timestampValueExpression: 'timestamp',
minAutoGranularity,
});
mockUseSource.mockImplementation(({ id }: { id?: string }) => ({
data:
id === 'queried' ? promqlSource('queried', '1 minute') : undefined,
}));

renderPanel({
queriedConfig: promqlConfig({
promqlExpression: 'rate(up[$__rate_interval])',
source: 'queried',
granularity: '15 second',
}),
tableSource: promqlSource('selected'),
showGeneratedPromql: true,
});
await openGeneratedPromql();

// max(15 + 60, 4 * 60). The selected source has no floor, which would
// give max(15 + 15, 4 * 15) = 60s.
expect(screen.getByTestId('chart-promql-preview')).toHaveTextContent(
'rate(up[240s])',
);
});

it('shows one preview per expression, labelled by alias', async () => {
renderPanel({
queriedConfig: promqlConfig({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,59 @@ describe('buildRenderedPromqlExpression', () => {
},
);

describe("with the source's minimum auto granularity", () => {
const render = (
displayType: DisplayType,
granularity: string,
minGranularitySeconds?: number,
) =>
buildRenderedPromqlExpression(
promqlConfig({
displayType,
granularity,
promqlExpression: [
{ expression: 'rate(up[$__rate_interval]) / $__interval' },
],
}),
minGranularitySeconds,
)?.expressions?.[0].expression;

// 70 minutes on auto resolves to 60s, below the 300s floor.
it('floors an auto step that is below it', () => {
const seventyMinutes: [Date, Date] = [
new Date('2024-01-01T00:00:00Z'),
new Date('2024-01-01T01:10:00Z'),
];
const renderShort = (displayType: DisplayType) =>
buildRenderedPromqlExpression(
{
...promqlConfig({
displayType,
granularity: 'auto',
promqlExpression: [{ expression: '$__interval' }],
}),
dateRange: seventyMinutes,
},
300,
)?.expressions?.[0].expression;

expect(renderShort(DisplayType.Line)).toBe('300s');
expect(renderShort(DisplayType.Table)).toBe('300s');
expect(renderShort(DisplayType.Number)).toBe('300s');
});

it('sizes $__rate_interval from it, however the granularity was chosen', () => {
// max(15 + 60, 4 * 60), with the hand-picked 15s step kept
expect(render(DisplayType.Line, '15 second', 60)).toBe(
'rate(up[240s]) / 15s',
);
});

it('assumes a 15s scrape interval without one', () => {
expect(render(DisplayType.Line, '15 second')).toBe('rate(up[60s]) / 15s');
});
});

it('reports a substitution failure instead of an expression', () => {
const result = buildRenderedPromqlExpression(
promqlConfig({
Expand Down
Loading
Loading