導入バージョン: v25.6.0
PromQL の irate や idelta のような計算のために、時系列データを再サンプリングするための集約関数です。
この集約関数は、時系列データをタイムスタンプと値のペアとして受け取り、最新のサンプルを最大 2 件だけ保持します。これは、グリッドに揃えたタイムスタンプに対する再サンプリングの時系列データを保存する materialized view および集約テーブルと組み合わせて使用することを想定しています。
集約テーブルには、揃えられた各タイムスタンプについて直近 2 つの値だけが保存されます。これにより、生テーブルに保存されているデータよりもはるかに少ない量を読み取るだけで、PromQL の irate や idelta のような計算を実行できます。
この関数は実験的機能です。allow_experimental_ts_to_grid_aggregate_function=true を設定して有効にしてください。
構文
timeSeriesLastTwoSamples(timestamp, value)
引数
戻り値
長さが 0~2 の、同じ長さの Array のペアを返します。1 つ目の Array にはサンプリングされた時系列のタイムスタンプが含まれ、2 つ目の Array には対応する時系列の値が含まれます。Tuple(Array(DateTime), Array(Float64))
例
生データ用のテーブルの例と、再サンプリングしたデータを保存するためのテーブル
-- 生データのテーブル
CREATE TABLE t_raw_timeseries
(
metric_id UInt64,
timestamp DateTime64(3, 'UTC') CODEC(DoubleDelta, ZSTD),
value Float64 CODEC(DoubleDelta)
)
ENGINE = MergeTree()
ORDER BY (metric_id, timestamp);
-- より大きな時間ステップ(15秒)に再サンプリングされたデータのテーブル
CREATE TABLE t_resampled_timeseries_15_sec
(
metric_id UInt64,
grid_timestamp DateTime('UTC') CODEC(DoubleDelta, ZSTD), -- 15秒に揃えられたタイムスタンプ
samples AggregateFunction(timeSeriesLastTwoSamples, DateTime64(3, 'UTC'), Float64)
)
ENGINE = AggregatingMergeTree()
ORDER BY (metric_id, grid_timestamp);
-- 再サンプリングテーブルにデータを投入するためのMV
CREATE MATERIALIZED VIEW mv_resampled_timeseries TO t_resampled_timeseries_15_sec
(
metric_id UInt64,
grid_timestamp DateTime('UTC') CODEC(DoubleDelta, ZSTD),
samples AggregateFunction(timeSeriesLastTwoSamples, DateTime64(3, 'UTC'), Float64)
)
AS SELECT
metric_id,
ceil(toUnixTimestamp(timestamp + interval 999 millisecond) / 15, 0) * 15 AS grid_timestamp, -- タイムスタンプを次のグリッドポイントに切り上げる
initializeAggregation('timeSeriesLastTwoSamplesState', timestamp, value) AS samples
FROM t_raw_timeseries
ORDER BY metric_id, grid_timestamp;
-- データを挿入する
INSERT INTO t_raw_timeseries(metric_id, timestamp, value) SELECT number%10 AS metric_id, '2024-12-12 12:00:00'::DateTime64(3, 'UTC') + interval ((number/10)%100)*900 millisecond as timestamp, number%3+number%29 AS value FROM numbers(1000);
-- 生データを確認する
SELECT *
FROM t_raw_timeseries
WHERE metric_id = 3 AND timestamp BETWEEN '2024-12-12 12:00:12' AND '2024-12-12 12:00:31'
ORDER BY metric_id, timestamp;
3 2024-12-12 12:00:12.870 29
3 2024-12-12 12:00:13.770 8
3 2024-12-12 12:00:14.670 19
3 2024-12-12 12:00:15.570 30
3 2024-12-12 12:00:16.470 9
3 2024-12-12 12:00:17.370 20
3 2024-12-12 12:00:18.270 2
3 2024-12-12 12:00:19.170 10
3 2024-12-12 12:00:20.070 21
3 2024-12-12 12:00:20.970 3
3 2024-12-12 12:00:21.870 11
3 2024-12-12 12:00:22.770 22
3 2024-12-12 12:00:23.670 4
3 2024-12-12 12:00:24.570 12
3 2024-12-12 12:00:25.470 23
3 2024-12-12 12:00:26.370 5
3 2024-12-12 12:00:27.270 13
3 2024-12-12 12:00:28.170 24
3 2024-12-12 12:00:29.069 6
3 2024-12-12 12:00:29.969 14
3 2024-12-12 12:00:30.869 25
タイムスタンプ ‘2024-12-12 12:00:15’ と ‘2024-12-12 12:00:30’ の直近 2 件のサンプルをクエリする
-- 再サンプリングされたデータを確認する
SELECT metric_id, grid_timestamp, (finalizeAggregation(samples).1 as timestamp, finalizeAggregation(samples).2 as value)
FROM t_resampled_timeseries_15_sec
WHERE metric_id = 3 AND grid_timestamp BETWEEN '2024-12-12 12:00:15' AND '2024-12-12 12:00:30'
ORDER BY metric_id, grid_timestamp;
3 2024-12-12 12:00:15 (['2024-12-12 12:00:14.670','2024-12-12 12:00:13.770'],[19,8])
3 2024-12-12 12:00:30 (['2024-12-12 12:00:29.969','2024-12-12 12:00:29.069'],[14,6])
生データから idelta と irate を計算する
-- 集約テーブルは、15秒単位で整列された各タイムスタンプに対して最新の2つの値のみを保存します。
-- これにより、生テーブルに保存されているデータよりもはるかに少ないデータを読み取るだけで、PromQL のような irate および idelta を計算できます。
WITH
'2024-12-12 12:00:15'::DateTime64(3,'UTC') AS start_ts, -- タイムスタンプグリッドの開始
start_ts + INTERVAL 60 SECOND AS end_ts, -- タイムスタンプグリッドの終了
15 AS step_seconds, -- タイムスタンプグリッドのステップ
45 AS window_seconds -- "staleness" ウィンドウ
SELECT
metric_id,
timeSeriesInstantDeltaToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value),
timeSeriesInstantRateToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)
FROM t_raw_timeseries
WHERE metric_id = 3 AND timestamp BETWEEN start_ts - interval window_seconds seconds AND end_ts
GROUP BY metric_id;
3 [11,8,-18,8,11] [12.222222222222221,8.88888888888889,1.1111111111111112,8.88888888888889,12.222222222222221]
再サンプリングしたデータから idelta と irate を計算する
WITH
'2024-12-12 12:00:15'::DateTime64(3,'UTC') AS start_ts, -- タイムスタンプグリッドの開始
start_ts + INTERVAL 60 SECOND AS end_ts, -- タイムスタンプグリッドの終了
15 AS step_seconds, -- タイムスタンプグリッドのステップ
45 AS window_seconds -- "staleness" ウィンドウ
SELECT
metric_id,
timeSeriesInstantDeltaToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values),
timeSeriesInstantRateToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values)
FROM (
SELECT
metric_id,
finalizeAggregation(samples).1 AS timestamps,
finalizeAggregation(samples).2 AS values
FROM t_resampled_timeseries_15_sec
WHERE metric_id = 3 AND grid_timestamp BETWEEN start_ts - interval window_seconds seconds AND end_ts
)
GROUP BY metric_id;
3 [11,8,-18,8,11] [12.222222222222221,8.88888888888889,1.1111111111111112,8.88888888888889,12.222222222222221]