Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(metrics): add service_name to metrics4_names (#109740)

Frank Hamand039b89aca7c5
diff --git a/posthog/clickhouse/hcl/golden/dev/apm.hcl b/posthog/clickhouse/hcl/golden/dev/apm.hcl
index 04760e041d5f..88eb157eb9c3 100644
--- a/posthog/clickhouse/hcl/golden/dev/apm.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/apm.hcl
@@ -674,6 +674,9 @@ database "posthog" {
column "original_expiry_timestamp" {
type = "SimpleAggregateFunction(max, DateTime64(6))"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -1524,11 +1527,12 @@ SELECT
metric_name,
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
- maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
+ service_name
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
- team_id, time_bucket, metric_name, original_expiry_time_bucket
+ team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name
SQL
column "team_id" {
@@ -1546,6 +1550,9 @@ SQL
column "original_expiry_timestamp" {
type = "SimpleAggregateFunction(max, DateTime64(6))"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
}
materialized_view "metrics4_input_to_metrics4_resource_attributes" {
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hcl
index 570cfa1eac54..bb1a5a9e75d2 100644
--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl
@@ -2030,7 +2030,7 @@ SQL
}
table "metrics4_names" {
- order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket", "service_name"]
partition_by = "toDate(original_expiry_time_bucket)"
ttl = "original_expiry_timestamp"
settings = {
@@ -2051,6 +2051,9 @@ SQL
column "original_expiry_timestamp" {
type = "SimpleAggregateFunction(max, DateTime64(6))"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
index 54d0a89a84a7..8f6a67baf71a 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -2530,7 +2530,7 @@ SQL
}
table "metrics4_names" {
- order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket", "service_name"]
partition_by = "toDate(original_expiry_time_bucket)"
ttl = "original_expiry_timestamp"
settings = {
@@ -2551,6 +2551,9 @@ SQL
column "original_expiry_timestamp" {
type = "SimpleAggregateFunction(max, DateTime64(6))"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index 476ef7ae5dc2..3af04d1ac906 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -7654,7 +7654,7 @@ SQL
}
table "metrics4_names" {
- order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket", "service_name"]
partition_by = "toDate(original_expiry_time_bucket)"
ttl = "original_expiry_timestamp"
settings = {
@@ -7675,6 +7675,9 @@ SQL
column "original_expiry_timestamp" {
type = "SimpleAggregateFunction(max, DateTime64(6))"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
replica_name = "{replica}-{shard}"
@@ -17740,6 +17743,9 @@ SQL
column "original_expiry_timestamp" {
type = "SimpleAggregateFunction(max, DateTime64(6))"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -22763,11 +22769,12 @@ SELECT
metric_name,
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
- maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
+ service_name
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
- team_id, time_bucket, metric_name, original_expiry_time_bucket
+ team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name
SQL
column "team_id" {
@@ -22785,6 +22792,9 @@ SQL
column "original_expiry_timestamp" {
type = "SimpleAggregateFunction(max, DateTime64(6))"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
}
materialized_view "metrics4_input_to_metrics4_resource_attributes" {
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/apm.hcl b/posthog/clickhouse/hcl/golden/prod-eu/apm.hcl
index 8faa280679b3..3fe614cdfca0 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/apm.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/apm.hcl
@@ -221,6 +221,9 @@ database "posthog" {
column "original_expiry_timestamp" {
type = "SimpleAggregateFunction(max, DateTime64(6))"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -671,11 +674,12 @@ SELECT
metric_name,
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
- maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
+ service_name
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
- team_id, time_bucket, metric_name, original_expiry_time_bucket
+ team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name
SQL
column "team_id" {
@@ -693,6 +697,9 @@ SQL
column "original_expiry_timestamp" {
type = "SimpleAggregateFunction(max, DateTime64(6))"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
}
materialized_view "metrics4_input_to_metrics4_resource_attributes" {
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
index 7169b17f746d..a4ceec626c4c 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
@@ -2116,7 +2116,7 @@ SQL
}
table "metrics4_names" {
- order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket", "service_name"]
partition_by = "toDate(original_expiry_time_bucket)"
ttl = "original_expiry_timestamp"
settings = {
@@ -2137,6 +2137,9 @@ SQL
column "original_expiry_timestamp" {
type = "SimpleAggregateFunction(max, DateTime64(6))"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/golden/prod-us/apm.hcl b/posthog/clickhouse/hcl/golden/prod-us/apm.hcl
index 8faa280679b3..3fe614cdfca0 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/apm.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/apm.hcl
@@ -221,6 +221,9 @@ database "posthog" {
column "original_expiry_timestamp" {
type = "SimpleAggregateFunction(max, DateTime64(6))"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -671,11 +674,12 @@ SELECT
metric_name,
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
- maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
+ service_name
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
- team_id, time_bucket, metric_name, original_expiry_time_bucket
+ team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name
SQL
column "team_id" {
@@ -693,6 +697,9 @@ SQL
column "original_expiry_timestamp" {
type = "SimpleAggregateFunction(max, DateTime64(6))"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
}
materialized_view "metrics4_input_to_metrics4_resource_attributes" {
diff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
index d7437c3d6d63..ae8b17962239 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
@@ -2116,7 +2116,7 @@ SQL
}
table "metrics4_names" {
- order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket", "service_name"]
partition_by = "toDate(original_expiry_time_bucket)"
ttl = "original_expiry_timestamp"
settings = {
@@ -2137,6 +2137,9 @@ SQL
column "original_expiry_timestamp" {
type = "SimpleAggregateFunction(max, DateTime64(6))"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/roles/apm/shared/metrics4.hcl b/posthog/clickhouse/hcl/roles/apm/shared/metrics4.hcl
index 7c68e89a1ef0..53d058d348ee 100644
--- a/posthog/clickhouse/hcl/roles/apm/shared/metrics4.hcl
+++ b/posthog/clickhouse/hcl/roles/apm/shared/metrics4.hcl
@@ -142,6 +142,7 @@ database "posthog" {
column "time_bucket" { type = "DateTime64(0)" }
column "original_expiry_time_bucket" { type = "DateTime64(0)" }
column "original_expiry_timestamp" { type = "SimpleAggregateFunction(max, DateTime64(6))" }
+ column "service_name" { type = "LowCardinality(String)" }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -252,6 +253,7 @@ database "posthog" {
column "time_bucket" { type = "DateTime64(0)" }
column "original_expiry_time_bucket" { type = "DateTime64(0)" }
column "original_expiry_timestamp" { type = "SimpleAggregateFunction(max, DateTime64(6))" }
+ column "service_name" { type = "LowCardinality(String)" }
}
materialized_view "metrics4_input_to_metrics4_attributes" {
diff --git a/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_names.sql b/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_names.sql
index 51cb46cc305c..09b5a81958d9 100644
--- a/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_names.sql
+++ b/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_names.sql
@@ -3,8 +3,9 @@ SELECT
metric_name,
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
- maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
+ service_name
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
- team_id, time_bucket, metric_name, original_expiry_time_bucket
+ team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name
diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hcl b/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hcl
index 38f3bc7c270e..28460b4b5841 100644
--- a/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hcl
@@ -66,7 +66,7 @@ database "posthog" {
}
table "metrics4_names" {
- order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket", "service_name"]
partition_by = "toDate(original_expiry_time_bucket)"
ttl = "original_expiry_timestamp"
settings = {
@@ -87,6 +87,9 @@ database "posthog" {
column "original_expiry_timestamp" {
type = "SimpleAggregateFunction(max, DateTime64(6))"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
engine "replicated_aggregating_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/sql/dev/apm.sql b/posthog/clickhouse/hcl/sql/dev/apm.sql
index 3168bc537407..da2ff4d573fe 100644
--- a/posthog/clickhouse/hcl/sql/dev/apm.sql
+++ b/posthog/clickhouse/hcl/sql/dev/apm.sql
@@ -202,7 +202,8 @@ CREATE TABLE posthog.writable_metrics4_names (
metric_name LowCardinality(String),
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
- original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
+ service_name LowCardinality(String)
) ENGINE = Distributed('logs', 'posthog', 'metrics4_names');
CREATE TABLE posthog.writable_metrics4_samples (
team_id Int32,
@@ -477,16 +478,17 @@ FROM
GROUP BY
team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
-CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))) AS SELECT
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)), service_name LowCardinality(String)) AS SELECT
team_id,
metric_name,
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
- maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
+ service_name
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
- team_id, time_bucket, metric_name, original_expiry_time_bucket;
+ team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name;
CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_resource_attributes TO posthog.writable_metrics4_attributes (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
team_id,
metric_name,
diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sql
index c0f610a77f8f..22e047ee1d57 100644
--- a/posthog/clickhouse/hcl/sql/dev/logs.sql
+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql
@@ -522,8 +522,9 @@ CREATE TABLE posthog.metrics4_names (
metric_name LowCardinality(String),
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
- original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
-) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
+ service_name LowCardinality(String)
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metrics4_samples (
team_id Int32,
metric_name LowCardinality(String),
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index f67ad0972fb4..59009ada1fb0 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -614,8 +614,9 @@ CREATE TABLE posthog.metrics4_names (
metric_name LowCardinality(String),
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
- original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
-) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
+ service_name LowCardinality(String)
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metrics4_samples (
team_id Int32,
metric_name LowCardinality(String),
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 4f266c6d1844..de2f623d4941 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -1585,8 +1585,9 @@ CREATE TABLE posthog.metrics4_names (
metric_name LowCardinality(String),
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
- original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
-) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
+ service_name LowCardinality(String)
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metrics4_samples (
team_id Int32,
metric_name LowCardinality(String),
@@ -4027,7 +4028,8 @@ CREATE TABLE posthog.writable_metrics4_names (
metric_name LowCardinality(String),
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
- original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
+ service_name LowCardinality(String)
) ENGINE = Distributed('logs', 'posthog', 'metrics4_names');
CREATE TABLE posthog.writable_metrics4_samples (
team_id Int32,
@@ -5519,16 +5521,17 @@ FROM
GROUP BY
team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
-CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))) AS SELECT
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)), service_name LowCardinality(String)) AS SELECT
team_id,
metric_name,
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
- maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
+ service_name
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
- team_id, time_bucket, metric_name, original_expiry_time_bucket;
+ team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name;
CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_resource_attributes TO posthog.writable_metrics4_attributes (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
team_id,
metric_name,
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/apm.sql b/posthog/clickhouse/hcl/sql/prod-eu/apm.sql
index e904014a4ade..177846223350 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/apm.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/apm.sql
@@ -70,7 +70,8 @@ CREATE TABLE posthog.writable_metrics4_names (
metric_name LowCardinality(String),
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
- original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
+ service_name LowCardinality(String)
) ENGINE = Distributed('logs', 'posthog', 'metrics4_names');
CREATE TABLE posthog.writable_metrics4_samples (
team_id Int32,
@@ -226,16 +227,17 @@ FROM
GROUP BY
team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
-CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))) AS SELECT
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)), service_name LowCardinality(String)) AS SELECT
team_id,
metric_name,
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
- maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
+ service_name
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
- team_id, time_bucket, metric_name, original_expiry_time_bucket;
+ team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name;
CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_resource_attributes TO posthog.writable_metrics4_attributes (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
team_id,
metric_name,
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
index 4fd02748533c..ad0cd16e90ce 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
@@ -546,8 +546,9 @@ CREATE TABLE posthog.metrics4_names (
metric_name LowCardinality(String),
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
- original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
-) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
+ service_name LowCardinality(String)
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metrics4_samples (
team_id Int32,
metric_name LowCardinality(String),
diff --git a/posthog/clickhouse/hcl/sql/prod-us/apm.sql b/posthog/clickhouse/hcl/sql/prod-us/apm.sql
index 7180c4b290fa..44c5282de894 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/apm.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/apm.sql
@@ -70,7 +70,8 @@ CREATE TABLE posthog.writable_metrics4_names (
metric_name LowCardinality(String),
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
- original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
+ service_name LowCardinality(String)
) ENGINE = Distributed('logs', 'posthog', 'metrics4_names');
CREATE TABLE posthog.writable_metrics4_samples (
team_id Int32,
@@ -226,16 +227,17 @@ FROM
GROUP BY
team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
-CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))) AS SELECT
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)), service_name LowCardinality(String)) AS SELECT
team_id,
metric_name,
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
- maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
+ service_name
FROM posthog.metrics4_input AS input
WHERE has_labels
GROUP BY
- team_id, time_bucket, metric_name, original_expiry_time_bucket;
+ team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name;
CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_resource_attributes TO posthog.writable_metrics4_attributes (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
team_id,
metric_name,
diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
index 224a3a37128d..1ae52a0a4a19 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
@@ -546,8 +546,9 @@ CREATE TABLE posthog.metrics4_names (
metric_name LowCardinality(String),
time_bucket DateTime64(0),
original_expiry_time_bucket DateTime64(0),
- original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
-) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6)),
+ service_name LowCardinality(String)
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metrics4_samples (
team_id Int32,
metric_name LowCardinality(String),
diff --git a/posthog/clickhouse/metrics/metrics4.py b/posthog/clickhouse/metrics/metrics4.py
index b1b0bf8d5635..8fc726da8b3c 100644
--- a/posthog/clickhouse/metrics/metrics4.py
+++ b/posthog/clickhouse/metrics/metrics4.py
@@ -170,7 +170,8 @@ def WRITABLE_METRICS4_NAMES_TABLE_SQL() -> str:
`metric_name` LowCardinality(String),
`time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
- `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))""",
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
+ `service_name` LowCardinality(String)""",
)
@@ -282,11 +283,12 @@ CREATE TABLE IF NOT EXISTS {_db()}.{METRICS4_NAMES_TABLE_NAME}
`metric_name` LowCardinality(String),
`time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
- `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
+ `service_name` LowCardinality(String)
)
ENGINE = {AggregatingMergeTree(METRICS4_NAMES_TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}
PARTITION BY toDate(original_expiry_time_bucket)
-ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket)
+ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name)
TTL original_expiry_timestamp
SETTINGS index_granularity = 8192
"""
@@ -387,17 +389,19 @@ CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{METRICS4_INPUT_TABLE_NAME}_to_{METR
`metric_name` LowCardinality(String),
`time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
- `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
+ `service_name` LowCardinality(String)
)
AS SELECT
team_id,
metric_name,
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
- maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
+ service_name
FROM {db}.{METRICS4_INPUT_TABLE_NAME} AS input
WHERE has_labels
-GROUP BY team_id, time_bucket, metric_name, original_expiry_time_bucket
+GROUP BY team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name
"""
diff --git a/posthog/clickhouse/migrations/0342_metrics4_names_service_name.py b/posthog/clickhouse/migrations/0342_metrics4_names_service_name.py
new file mode 100644
index 000000000000..a90c7c0cd451
--- /dev/null
+++ b/posthog/clickhouse/migrations/0342_metrics4_names_service_name.py
@@ -0,0 +1,41 @@
+from django.conf import settings
+
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+
+DB = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+
+# A new column can join the end of the sort key in the ALTER that adds it, so this is
+# metadata only: existing parts read service_name as '' and leave by TTL.
+operations = [
+ run_sql_with_exceptions(
+ f"ALTER TABLE {DB}.metrics4_names "
+ "ADD COLUMN IF NOT EXISTS service_name LowCardinality(String), "
+ "MODIFY ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name)",
+ node_roles=[NodeRole.LOGS],
+ sharded=False,
+ is_alter_on_replicated_table=True,
+ ),
+ run_sql_with_exceptions(
+ f"ALTER TABLE {DB}.writable_metrics4_names ADD COLUMN IF NOT EXISTS service_name LowCardinality(String)",
+ node_roles=[NodeRole.APM],
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ ),
+ run_sql_with_exceptions(
+ f"""ALTER TABLE {DB}.metrics4_input_to_metrics4_names MODIFY QUERY
+SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
+ service_name
+FROM {DB}.metrics4_input AS input
+WHERE has_labels
+GROUP BY team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name""",
+ node_roles=[NodeRole.APM],
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ ),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index f70975c24442..b08a9384384f 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0341_drop_trace_spans_old_projections
+0342_metrics4_names_service_name
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 57ef9f340643..c83b485814dc 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -5213,17 +5213,19 @@
`metric_name` LowCardinality(String),
`time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
- `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
+ `service_name` LowCardinality(String)
)
AS SELECT
team_id,
metric_name,
toStartOfHour(timestamp) AS time_bucket,
toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
- maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp,
+ service_name
FROM posthog_test.metrics4_input AS input
WHERE has_labels
- GROUP BY team_id, time_bucket, metric_name, original_expiry_time_bucket
+ GROUP BY team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name
'''
# ---
@@ -5349,11 +5351,12 @@
`metric_name` LowCardinality(String),
`time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
- `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
+ `service_name` LowCardinality(String)
)
ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metrics4_names', '{replica}-{shard}')
PARTITION BY toDate(original_expiry_time_bucket)
- ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket)
+ ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name)
TTL original_expiry_timestamp
SETTINGS index_granularity = 8192
@@ -10713,7 +10716,8 @@
`metric_name` LowCardinality(String),
`time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
- `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
+ `service_name` LowCardinality(String)
)
ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'metrics4_names')
@@ -12709,11 +12713,12 @@
`metric_name` LowCardinality(String),
`time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
- `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
+ `service_name` LowCardinality(String)
)
ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metrics4_names', '{replica}-{shard}')
PARTITION BY toDate(original_expiry_time_bucket)
- ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket)
+ ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket, service_name)
TTL original_expiry_timestamp
SETTINGS index_granularity = 8192
diff --git a/posthog/hogql/database/schema/metrics.py b/posthog/hogql/database/schema/metrics.py
index 509a1e49caf3..2ec8d870e7db 100644
--- a/posthog/hogql/database/schema/metrics.py
+++ b/posthog/hogql/database/schema/metrics.py
@@ -209,7 +209,7 @@ class MetricSamplesTable(Table):
class MetricNamesTable(Table):
- description: str = "One row per metric name and UTC hour with labeled data points. Use it to find the metric names of a time range."
+ description: str = "One row per metric name, service and UTC hour with labeled data points. Use it to find the metric names of a time range."
workload: Workload | None = Workload.LOGS
fields: dict[str, FieldOrTable] = {
@@ -224,6 +224,9 @@ class MetricNamesTable(Table):
"original_expiry_timestamp": DateTimeDatabaseField(
name="original_expiry_timestamp", nullable=False, description="When the row leaves retention."
),
+ "service_name": StringDatabaseField(
+ name="service_name", nullable=False, description="Name of the service that emitted the metric."
+ ),
}
def to_printed_clickhouse(self, context):