CREATE TABLE IF NOT EXISTS metrics_sum
(
ProjectId UInt32,
ServiceName LowCardinality(String),
MetricName String,
MetricDescription String,
MetricUnit String,
Attributes Map(LowCardinality(String), String),
Timestamp DateTime64(9) CODEC (Delta, ZSTD),
RetentionDays UInt8 DEFAULT 30,
-- sum
Value Float64
-- other columns omitted for brevity
) ENGINE = MergeTree()
TTL toDateTime(Timestamp) + toIntervalDay(RetentionDays)
PARTITION BY toStartOfDay(Timestamp)
ORDER BY (ProjectId, ServiceName, MetricName, toUnixTimestamp64Nano(Timestamp));
CREATE TABLE IF NOT EXISTS metrics_histogram
(
ProjectId UInt32,
ServiceName LowCardinality(String),
MetricName String,
MetricDescription String,
MetricUnit String,
Attributes Map(LowCardinality(String), String),
Timestamp DateTime64(9) CODEC (Delta, ZSTD),
RetentionDays UInt8 DEFAULT 30,
-- common
-- histogram
Count UInt64 CODEC (Delta, ZSTD),
Sum Float64,
BucketCounts Array (UInt64),
ExplicitBounds Array (Float64),
Min Float64,
Max Float64
-- other columns omitted for brevity
) ENGINE = MergeTree()
TTL toDateTime(Timestamp) + toIntervalDay(RetentionDays)
PARTITION BY toStartOfDay(Timestamp)
ORDER BY (ProjectId, ServiceName, MetricName, toUnixTimestamp64Nano(Timestamp));
CREATE TABLE IF NOT EXISTS metrics_summary
(
ProjectId UInt32,
ServiceName LowCardinality(String),
MetricName String,
MetricDescription String,
MetricUnit String,
Attributes Map(LowCardinality(String), String),
Timestamp DateTime64(9) CODEC (Delta, ZSTD),
RetentionDays UInt8 DEFAULT 30,
-- common
Flags UInt32,
-- summary
Count Float64,
Sum Float64
-- other columns omitted for brevity
) ENGINE = MergeTree()
TTL toDateTime(Timestamp) + toIntervalDay(RetentionDays)
PARTITION BY toStartOfDay(Timestamp)
ORDER BY (ProjectId, ServiceName, MetricName, toUnixTimestamp64Nano(Timestamp));
-- the destination table which contains the aggregate across metrics formats
CREATE TABLE IF NOT EXISTS default.metrics
(
ProjectId UInt32,
ServiceName String,
MetricName String,
MetricType Enum8('Empty' = 0, 'Gauge' = 1, 'Sum' = 2, 'Histogram' = 3, 'ExponentialHistogram' = 4, 'Summary' = 5),
Attributes Map(LowCardinality(String), String),
Timestamp DateTime CODEC (Delta(4), ZSTD(1)),
-- meta
MetricDescription SimpleAggregateFunction(anyLast, String),
MetricUnit SimpleAggregateFunction(anyLast, String),
RetentionDays SimpleAggregateFunction(max, UInt8) DEFAULT 30,
-- histogram
Min SimpleAggregateFunction(min, Float64),
Max SimpleAggregateFunction(max, Float64),
BucketCounts SimpleAggregateFunction(groupArrayArray, Array(UInt64)),
ExplicitBounds SimpleAggregateFunction(groupArrayArray, Array(Float64)),
-- common
Count SimpleAggregateFunction(sum, UInt64),
Sum SimpleAggregateFunction(sum, Float64)
-- other columns omitted for brevity
) ENGINE = AggregatingMergeTree()
PARTITION BY toStartOfDay(Timestamp)
ORDER BY (ProjectId, ServiceName, MetricName, MetricType, toUnixTimestamp(Timestamp))
TTL toDateTime(Timestamp) + toIntervalDay(RetentionDays);
CREATE MATERIALIZED VIEW IF NOT EXISTS metrics_sum_mv TO metrics AS
SELECT ProjectId,
ServiceName,
MetricName,
MetricType,
Attributes,
toDateTime(toStartOfSecond(Timestamp)) as Timestamp,
-- meta
anyLastSimpleState(MetricDescription) as MetricDescription,
anyLastSimpleState(MetricUnit) as MetricUnit,
minSimpleState(StartTimestamp) as StartTimestamp,
maxSimpleState(RetentionDays) as RetentionDays,
-- sum
sumSimpleState(1) as Count,
sumSimpleState(Value) as Sum
-- other columns omitted for brevity
FROM metrics_sum
GROUP BY all;
CREATE MATERIALIZED VIEW IF NOT EXISTS metrics_histogram_mv TO metrics AS
SELECT ProjectId,
ServiceName,
MetricName,
'Histogram' as MetricType,
Attributes,
toDateTime(toStartOfSecond(Timestamp)) as Timestamp,
-- meta
anyLastSimpleState(MetricDescription) as MetricDescription,
anyLastSimpleState(MetricUnit) as MetricUnit,
minSimpleState(StartTimestamp) as StartTimestamp,
maxSimpleState(RetentionDays) as RetentionDays,
-- histogram
minSimpleState(Min) as Min,
minSimpleState(Max) as Max,
groupArrayArraySimpleState(BucketCounts) as BucketCounts,
groupArrayArraySimpleState(ExplicitBounds) as ExplicitBounds,
sumSimpleState(Count) as Count,
sumSimpleState(Sum) as Sum
-- other columns omitted for brevity
FROM metrics_histogram
GROUP BY all;
CREATE MATERIALIZED VIEW IF NOT EXISTS metrics_summary_mv TO metrics AS
SELECT ProjectId,
ServiceName,
MetricName,
'Summary' as MetricType,
Attributes,
toDateTime(toStartOfSecond(Timestamp)) as Timestamp,
-- meta
anyLastSimpleState(MetricDescription) as MetricDescription,
anyLastSimpleState(MetricUnit) as MetricUnit,
minSimpleState(StartTimestamp) as StartTimestamp,
maxSimpleState(RetentionDays) as RetentionDays,
-- summary
sumSimpleState(Count) as Count,
sumSimpleState(Sum) as Sum
-- other columns omitted for brevity
FROM metrics_summary
GROUP BY all;