Skip to content
Open
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
45 changes: 45 additions & 0 deletions packages/common-utils/src/__tests__/queryChartConfig.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ClickHouseClient } from '@clickhouse/client';
import { convertCHDataTypeToJSType, JSDataType } from '@/clickhouse';
import { ClickhouseClient as HdxClickhouseClient } from '@/clickhouse/node';
import { Metadata, MetadataCache } from '@/core/metadata';
import { renderMetricExemplarsChartConfig } from '@/core/renderChartConfig';
import { convertToCategoricalChartConfig } from '@/core/utils';
import {
ChartConfigWithOptDateRange,
Expand Down Expand Up @@ -2786,6 +2787,50 @@ describe('queryChartConfig Integration Tests', () => {
).rejects.toThrow();
});
});
// HIST_TABLE mirrors the seed schema, where Exemplars.TimeUnix is
// Array(DateTime). toUnixTimestamp64Milli only accepts DateTime64, so the
// scan must cast it; this runs the generated SQL against that column type.
it('reads exemplars from an Array(DateTime) Exemplars.TimeUnix column', async () => {
const traceId = '0af7651916cd43dd8448eb211c80319c';
await client.insert({
table: `${DATABASE}.${HIST_TABLE}`,
values: [
{
...histRow('exemplar.latency', insertTs(1), 'svc-a', [3, 1], [1]),
'Exemplars.FilteredAttributes': [{}],
'Exemplars.TimeUnix': [insertTs(1)],
'Exemplars.Value': [2.5],
'Exemplars.SpanId': ['b7ad6b7169203331'],
'Exemplars.TraceId': [traceId],
},
],
format: 'JSONEachRow',
});

const exemplarSql = await renderMetricExemplarsChartConfig(
baseConfig({ select: [histQuantileSelect('exemplar.latency', 0.99)] }),
metadata,
);
expect(exemplarSql).not.toBeNull();

const resp = await hdxClient.query({
query: exemplarSql!.sql,
query_params: exemplarSql!.params,
format: 'JSON',
});
const { data } = await resp.json<{
timestamp: string;
value: number;
traceId: string;
}>();

expect(data).toHaveLength(1);
expect(Number(data[0].timestamp)).toBe(
Date.parse(`${insertTs(1).replace(' ', 'T')}Z`),
);
expect(data[0].value).toBe(2.5);
expect(data[0].traceId).toBe(traceId);
});
});

// Formulas on event (log/trace) sources compile inline in the single-scan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5971,7 +5971,7 @@ describe('renderMetricExemplarsChartConfig', () => {
// Surfaces the exemplar columns
expect(sql).toContain('ARRAY JOIN');
expect(sql).toContain('`Exemplars.TraceId` AS ex_TraceId');
expect(sql).toContain('toUnixTimestamp64Milli(ex_TimeUnix)');
expect(sql).toContain('toUnixTimestamp64Milli(toDateTime64(ex_TimeUnix, 9))');
// Points at the histogram table and filters by metric name + time
expect(sql).toContain('otel_metrics_histogram');
expect(sql).toContain("MetricName = 'http.server.duration'");
Expand Down
5 changes: 4 additions & 1 deletion packages/common-utils/src/core/renderChartConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3688,9 +3688,12 @@ export async function renderMetricExemplarsChartConfig(
alias: 'bucket',
});

// Exemplars.TimeUnix is Array(DateTime) in the seed schema and the OTel
// exporter's, Array(DateTime64(9)) elsewhere. toUnixTimestamp64Milli only
// accepts DateTime64, so the cast is required (and lossless for both).
return concatChSql(' ', [
chSql`SELECT
toUnixTimestamp64Milli(ex_TimeUnix) AS timestamp,
toUnixTimestamp64Milli(toDateTime64(ex_TimeUnix, 9)) AS timestamp,
ex_Value AS value,
ex_TraceId AS traceId,
ex_SpanId AS spanId,
Expand Down
Loading