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(logs): roll up log volume through a materialized view on logs34 (#84186)

Jon McCallum11fc54cdca8f
diff --git a/.depot/workflows/ci-backend.yml b/.depot/workflows/ci-backend.yml
index e2546df2d0c3..5bd30cfbee0e 100644
--- a/.depot/workflows/ci-backend.yml
+++ b/.depot/workflows/ci-backend.yml
@@ -1450,9 +1450,20 @@ jobs:
sqlx migrate info --source rust/persons_migrations/
python manage.py makemigrations --check --dry-run
- name: Check CH migrations
+ env:
+ BASE_REF: ${{ github.event.pull_request.base.ref }}
run: |
+ # Count only migrations this PR adds; a stacked PR's base branch already carries the lower layers'.
+ DIFF_BASE=upstream/master
+ if [ -n "$BASE_REF" ] && [ "$BASE_REF" != "master" ]; then
+ if git fetch --no-tags --depth=1 --filter=blob:none origin "$BASE_REF:refs/remotes/origin/$BASE_REF"; then
+ DIFF_BASE="origin/$BASE_REF"
+ else
+ echo "::warning::Could not fetch base ref $BASE_REF; counting CH migrations against master instead"
+ fi
+ fi
# Same as above, except now for CH looking at files that were added in posthog/clickhouse/migrations/
- git diff --name-status upstream/master..HEAD | grep "A\sposthog/clickhouse/migrations/" | grep -v README | awk '{print $2}' | python manage.py test_ch_migrations_are_safe
+ git diff --name-status "$DIFF_BASE"..HEAD | grep "A\sposthog/clickhouse/migrations/" | grep -v README | awk '{print $2}' | python manage.py test_ch_migrations_are_safe
check-openapi-types:
needs: [changes]
# Not on master push: master only primes the schema cache.
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml
index 6d13cf3861ed..337c5e99b625 100644
--- a/.github/workflows/ci-backend.yml
+++ b/.github/workflows/ci-backend.yml
@@ -2043,9 +2043,20 @@ jobs:
python manage.py makemigrations --check --dry-run
- name: Check CH migrations
+ env:
+ BASE_REF: ${{ github.event.pull_request.base.ref }}
run: |
+ # Count only migrations this PR adds; a stacked PR's base branch already carries the lower layers'.
+ DIFF_BASE=origin/master
+ if [ -n "$BASE_REF" ] && [ "$BASE_REF" != "master" ]; then
+ if git fetch --no-tags --depth=1 --filter=blob:none origin "$BASE_REF:refs/remotes/origin/$BASE_REF"; then
+ DIFF_BASE="origin/$BASE_REF"
+ else
+ echo "::warning::Could not fetch base ref $BASE_REF; counting CH migrations against master instead"
+ fi
+ fi
# Same as above, except now for CH looking at files that were added in posthog/clickhouse/migrations/
- git diff --name-status origin/master..HEAD | grep "A\sposthog/clickhouse/migrations/" | grep -v README | awk '{print $2}' | python manage.py test_ch_migrations_are_safe
+ git diff --name-status "$DIFF_BASE"..HEAD | grep "A\sposthog/clickhouse/migrations/" | grep -v README | awk '{print $2}' | python manage.py test_ch_migrations_are_safe
- name: Render CH migration SQL per cloud environment
# CH migrations can build different `operations` lists at import time depending on
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hcl
index 05b0a870b44e..9ddb6c28fd68 100644
--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl
@@ -2427,6 +2427,67 @@ SQL
}
}
+ materialized_view "logs34_to_volume_buckets" {
+ to_table = "posthog.logs_volume_buckets"
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ namespace,
+ environment,
+ severity_text,
+ sumSimpleState(1) AS log_count
+FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
+ service_name,
+ if(
+ (resource_attributes['k8s.namespace.name']) != '',
+ resource_attributes['k8s.namespace.name'],
+ resource_attributes['service.namespace']
+ ) AS namespace,
+ if(
+ (resource_attributes['deployment.environment.name']) != '',
+ resource_attributes['deployment.environment.name'],
+ if(
+ (resource_attributes['deployment.environment']) != '',
+ resource_attributes['deployment.environment'],
+ resource_attributes['env']
+ )
+ ) AS environment,
+ lower(severity_text) AS severity_text
+ FROM posthog.logs34
+ )
+GROUP BY
+ team_id, time_bucket, service_name, namespace, environment, severity_text
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "namespace" {
+ type = "LowCardinality(String)"
+ }
+ column "environment" {
+ type = "LowCardinality(String)"
+ }
+ column "severity_text" {
+ type = "LowCardinality(String)"
+ }
+ column "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "metrics1_to_metric_attributes" {
to_table = "posthog.metric_attributes"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
index 5e9ea5933654..c11b66560dec 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -2934,6 +2934,67 @@ SQL
}
}
+ materialized_view "logs34_to_volume_buckets" {
+ to_table = "posthog.logs_volume_buckets"
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ namespace,
+ environment,
+ severity_text,
+ sumSimpleState(1) AS log_count
+FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
+ service_name,
+ if(
+ (resource_attributes['k8s.namespace.name']) != '',
+ resource_attributes['k8s.namespace.name'],
+ resource_attributes['service.namespace']
+ ) AS namespace,
+ if(
+ (resource_attributes['deployment.environment.name']) != '',
+ resource_attributes['deployment.environment.name'],
+ if(
+ (resource_attributes['deployment.environment']) != '',
+ resource_attributes['deployment.environment'],
+ resource_attributes['env']
+ )
+ ) AS environment,
+ lower(severity_text) AS severity_text
+ FROM posthog.logs34
+ )
+GROUP BY
+ team_id, time_bucket, service_name, namespace, environment, severity_text
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "namespace" {
+ type = "LowCardinality(String)"
+ }
+ column "environment" {
+ type = "LowCardinality(String)"
+ }
+ column "severity_text" {
+ type = "LowCardinality(String)"
+ }
+ column "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "trace_span_to_attributes" {
to_table = "posthog.trace_attributes"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index d523389d488e..337b05128426 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -19405,6 +19405,67 @@ SQL
}
}
+ materialized_view "logs34_to_volume_buckets" {
+ to_table = "posthog.logs_volume_buckets"
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ namespace,
+ environment,
+ severity_text,
+ sumSimpleState(1) AS log_count
+FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
+ service_name,
+ if(
+ (resource_attributes['k8s.namespace.name']) != '',
+ resource_attributes['k8s.namespace.name'],
+ resource_attributes['service.namespace']
+ ) AS namespace,
+ if(
+ (resource_attributes['deployment.environment.name']) != '',
+ resource_attributes['deployment.environment.name'],
+ if(
+ (resource_attributes['deployment.environment']) != '',
+ resource_attributes['deployment.environment'],
+ resource_attributes['env']
+ )
+ ) AS environment,
+ lower(severity_text) AS severity_text
+ FROM posthog.logs34
+ )
+GROUP BY
+ team_id, time_bucket, service_name, namespace, environment, severity_text
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "namespace" {
+ type = "LowCardinality(String)"
+ }
+ column "environment" {
+ type = "LowCardinality(String)"
+ }
+ column "severity_text" {
+ type = "LowCardinality(String)"
+ }
+ column "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "message_assets_mv" {
to_table = "posthog.message_assets_data"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
index 44995cf5715b..1ec6e93e24cc 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
@@ -3010,6 +3010,67 @@ SQL
}
}
+ materialized_view "logs34_to_volume_buckets" {
+ to_table = "posthog.logs_volume_buckets"
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ namespace,
+ environment,
+ severity_text,
+ sumSimpleState(1) AS log_count
+FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
+ service_name,
+ if(
+ (resource_attributes['k8s.namespace.name']) != '',
+ resource_attributes['k8s.namespace.name'],
+ resource_attributes['service.namespace']
+ ) AS namespace,
+ if(
+ (resource_attributes['deployment.environment.name']) != '',
+ resource_attributes['deployment.environment.name'],
+ if(
+ (resource_attributes['deployment.environment']) != '',
+ resource_attributes['deployment.environment'],
+ resource_attributes['env']
+ )
+ ) AS environment,
+ lower(severity_text) AS severity_text
+ FROM posthog.logs34
+ )
+GROUP BY
+ team_id, time_bucket, service_name, namespace, environment, severity_text
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "namespace" {
+ type = "LowCardinality(String)"
+ }
+ column "environment" {
+ type = "LowCardinality(String)"
+ }
+ column "severity_text" {
+ type = "LowCardinality(String)"
+ }
+ column "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "metrics1_to_metric_attributes" {
to_table = "posthog.metric_attributes"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
index 98aa1e79f56e..a2f5e77af7b5 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
@@ -3023,6 +3023,67 @@ SQL
}
}
+ materialized_view "logs34_to_volume_buckets" {
+ to_table = "posthog.logs_volume_buckets"
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ namespace,
+ environment,
+ severity_text,
+ sumSimpleState(1) AS log_count
+FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
+ service_name,
+ if(
+ (resource_attributes['k8s.namespace.name']) != '',
+ resource_attributes['k8s.namespace.name'],
+ resource_attributes['service.namespace']
+ ) AS namespace,
+ if(
+ (resource_attributes['deployment.environment.name']) != '',
+ resource_attributes['deployment.environment.name'],
+ if(
+ (resource_attributes['deployment.environment']) != '',
+ resource_attributes['deployment.environment'],
+ resource_attributes['env']
+ )
+ ) AS environment,
+ lower(severity_text) AS severity_text
+ FROM posthog.logs34
+ )
+GROUP BY
+ team_id, time_bucket, service_name, namespace, environment, severity_text
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "namespace" {
+ type = "LowCardinality(String)"
+ }
+ column "environment" {
+ type = "LowCardinality(String)"
+ }
+ column "severity_text" {
+ type = "LowCardinality(String)"
+ }
+ column "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "metrics1_to_metric_attributes" {
to_table = "posthog.metric_attributes"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/roles/logs/base/sql/logs34_to_volume_buckets.sql b/posthog/clickhouse/hcl/roles/logs/base/sql/logs34_to_volume_buckets.sql
new file mode 100644
index 000000000000..7e3ceb49e729
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/logs/base/sql/logs34_to_volume_buckets.sql
@@ -0,0 +1,33 @@
+SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ namespace,
+ environment,
+ severity_text,
+ sumSimpleState(1) AS log_count
+FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
+ service_name,
+ if(
+ (resource_attributes['k8s.namespace.name']) != '',
+ resource_attributes['k8s.namespace.name'],
+ resource_attributes['service.namespace']
+ ) AS namespace,
+ if(
+ (resource_attributes['deployment.environment.name']) != '',
+ resource_attributes['deployment.environment.name'],
+ if(
+ (resource_attributes['deployment.environment']) != '',
+ resource_attributes['deployment.environment'],
+ resource_attributes['env']
+ )
+ ) AS environment,
+ lower(severity_text) AS severity_text
+ FROM posthog.logs34
+ )
+GROUP BY
+ team_id, time_bucket, service_name, namespace, environment, severity_text
diff --git a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
index 248e6c92baaa..42a28af31316 100644
--- a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
@@ -235,6 +235,33 @@ database "posthog" {
}
}
+ materialized_view "logs34_to_volume_buckets" {
+ to_table = "posthog.logs_volume_buckets"
+ query = file("sql/logs34_to_volume_buckets.sql")
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "namespace" {
+ type = "LowCardinality(String)"
+ }
+ column "environment" {
+ type = "LowCardinality(String)"
+ }
+ column "severity_text" {
+ type = "LowCardinality(String)"
+ }
+ column "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
table "metric_samples1" {
order_by = ["team_id", "metric_name", "series_fingerprint", "timestamp"]
partition_by = "toDate(timestamp)"
diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sql
index cab992f20afd..d187de06a98f 100644
--- a/posthog/clickhouse/hcl/sql/dev/logs.sql
+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql
@@ -673,6 +673,39 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, resource_attributes
);
+CREATE MATERIALIZED VIEW posthog.logs34_to_volume_buckets TO posthog.logs_volume_buckets (team_id Int32, time_bucket DateTime('UTC'), service_name LowCardinality(String), namespace LowCardinality(String), environment LowCardinality(String), severity_text LowCardinality(String), log_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ namespace,
+ environment,
+ severity_text,
+ sumSimpleState(1) AS log_count
+FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
+ service_name,
+ if(
+ (resource_attributes['k8s.namespace.name']) != '',
+ resource_attributes['k8s.namespace.name'],
+ resource_attributes['service.namespace']
+ ) AS namespace,
+ if(
+ (resource_attributes['deployment.environment.name']) != '',
+ resource_attributes['deployment.environment.name'],
+ if(
+ (resource_attributes['deployment.environment']) != '',
+ resource_attributes['deployment.environment'],
+ resource_attributes['env']
+ )
+ ) AS environment,
+ lower(severity_text) AS severity_text
+ FROM posthog.logs34
+ )
+GROUP BY
+ team_id, time_bucket, service_name, namespace, environment, severity_text;
CREATE MATERIALIZED VIEW posthog.metrics1_to_metric_attributes TO posthog.metric_attributes (team_id Int32, time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
team_id,
time_bucket,
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index b18f1450c619..10fa915802fd 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -810,6 +810,39 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, resource_attributes
);
+CREATE MATERIALIZED VIEW posthog.logs34_to_volume_buckets TO posthog.logs_volume_buckets (team_id Int32, time_bucket DateTime('UTC'), service_name LowCardinality(String), namespace LowCardinality(String), environment LowCardinality(String), severity_text LowCardinality(String), log_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ namespace,
+ environment,
+ severity_text,
+ sumSimpleState(1) AS log_count
+FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
+ service_name,
+ if(
+ (resource_attributes['k8s.namespace.name']) != '',
+ resource_attributes['k8s.namespace.name'],
+ resource_attributes['service.namespace']
+ ) AS namespace,
+ if(
+ (resource_attributes['deployment.environment.name']) != '',
+ resource_attributes['deployment.environment.name'],
+ if(
+ (resource_attributes['deployment.environment']) != '',
+ resource_attributes['deployment.environment'],
+ resource_attributes['env']
+ )
+ ) AS environment,
+ lower(severity_text) AS severity_text
+ FROM posthog.logs34
+ )
+GROUP BY
+ team_id, time_bucket, service_name, namespace, environment, severity_text;
CREATE MATERIALIZED VIEW posthog.trace_span_to_attributes TO posthog.trace_attributes (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
team_id,
original_expiry_time_bucket,
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 41880a6a0649..feb5f0bf061b 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -4498,6 +4498,39 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, resource_attributes
);
+CREATE MATERIALIZED VIEW posthog.logs34_to_volume_buckets TO posthog.logs_volume_buckets (team_id Int32, time_bucket DateTime('UTC'), service_name LowCardinality(String), namespace LowCardinality(String), environment LowCardinality(String), severity_text LowCardinality(String), log_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ namespace,
+ environment,
+ severity_text,
+ sumSimpleState(1) AS log_count
+FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
+ service_name,
+ if(
+ (resource_attributes['k8s.namespace.name']) != '',
+ resource_attributes['k8s.namespace.name'],
+ resource_attributes['service.namespace']
+ ) AS namespace,
+ if(
+ (resource_attributes['deployment.environment.name']) != '',
+ resource_attributes['deployment.environment.name'],
+ if(
+ (resource_attributes['deployment.environment']) != '',
+ resource_attributes['deployment.environment'],
+ resource_attributes['env']
+ )
+ ) AS environment,
+ lower(severity_text) AS severity_text
+ FROM posthog.logs34
+ )
+GROUP BY
+ team_id, time_bucket, service_name, namespace, environment, severity_text;
CREATE MATERIALIZED VIEW posthog.message_assets_mv TO posthog.message_assets_data (team_id Int64, function_kind LowCardinality(String), function_id String, parent_run_id String, invocation_id String, action_id String, kind LowCardinality(String), distinct_id String, person_id String, recipient String, subject String, status LowCardinality(String), sent_at DateTime64(6, 'UTC'), version UInt64, is_deleted UInt8, html String, _timestamp Nullable(DateTime), _offset UInt64, _partition UInt64) AS SELECT
team_id,
function_kind,
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
index 90c6968048ed..0ff54a43712f 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
@@ -829,6 +829,39 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, resource_attributes
);
+CREATE MATERIALIZED VIEW posthog.logs34_to_volume_buckets TO posthog.logs_volume_buckets (team_id Int32, time_bucket DateTime('UTC'), service_name LowCardinality(String), namespace LowCardinality(String), environment LowCardinality(String), severity_text LowCardinality(String), log_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ namespace,
+ environment,
+ severity_text,
+ sumSimpleState(1) AS log_count
+FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
+ service_name,
+ if(
+ (resource_attributes['k8s.namespace.name']) != '',
+ resource_attributes['k8s.namespace.name'],
+ resource_attributes['service.namespace']
+ ) AS namespace,
+ if(
+ (resource_attributes['deployment.environment.name']) != '',
+ resource_attributes['deployment.environment.name'],
+ if(
+ (resource_attributes['deployment.environment']) != '',
+ resource_attributes['deployment.environment'],
+ resource_attributes['env']
+ )
+ ) AS environment,
+ lower(severity_text) AS severity_text
+ FROM posthog.logs34
+ )
+GROUP BY
+ team_id, time_bucket, service_name, namespace, environment, severity_text;
CREATE MATERIALIZED VIEW posthog.metrics1_to_metric_attributes TO posthog.metric_attributes (team_id Int32, time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
team_id,
time_bucket,
diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
index d59f8e822da5..2058f8769db0 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
@@ -831,6 +831,39 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, resource_attributes
);
+CREATE MATERIALIZED VIEW posthog.logs34_to_volume_buckets TO posthog.logs_volume_buckets (team_id Int32, time_bucket DateTime('UTC'), service_name LowCardinality(String), namespace LowCardinality(String), environment LowCardinality(String), severity_text LowCardinality(String), log_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ namespace,
+ environment,
+ severity_text,
+ sumSimpleState(1) AS log_count
+FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
+ service_name,
+ if(
+ (resource_attributes['k8s.namespace.name']) != '',
+ resource_attributes['k8s.namespace.name'],
+ resource_attributes['service.namespace']
+ ) AS namespace,
+ if(
+ (resource_attributes['deployment.environment.name']) != '',
+ resource_attributes['deployment.environment.name'],
+ if(
+ (resource_attributes['deployment.environment']) != '',
+ resource_attributes['deployment.environment'],
+ resource_attributes['env']
+ )
+ ) AS environment,
+ lower(severity_text) AS severity_text
+ FROM posthog.logs34
+ )
+GROUP BY
+ team_id, time_bucket, service_name, namespace, environment, severity_text;
CREATE MATERIALIZED VIEW posthog.metrics1_to_metric_attributes TO posthog.metric_attributes (team_id Int32, time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
team_id,
time_bucket,
diff --git a/posthog/clickhouse/logs/__init__.py b/posthog/clickhouse/logs/__init__.py
index 94726b0142b2..7675f61c2a6c 100644
--- a/posthog/clickhouse/logs/__init__.py
+++ b/posthog/clickhouse/logs/__init__.py
@@ -21,6 +21,7 @@ from .logs34 import (
LOGS34_TO_LOG_ATTRIBUTES_MV,
LOGS34_TO_RESOURCE_ATTRIBUTES3_MV,
LOGS34_TO_RESOURCE_ATTRIBUTES_MV,
+ LOGS34_TO_VOLUME_BUCKETS_MV,
LOGS_DISTRIBUTED_TABLE_SQL as LOGS34_DISTRIBUTED_TABLE_SQL,
)
from .logs_billing_metrics import LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL, LOGS_BILLING_METRICS_TABLE_SQL
@@ -58,4 +59,5 @@ __all__ = [
"LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL",
"LOGS_VOLUME_BUCKETS_TABLE_SQL",
"LOGS_VOLUME_BUCKETS_DISTRIBUTED_TABLE_SQL",
+ "LOGS34_TO_VOLUME_BUCKETS_MV",
]
diff --git a/posthog/clickhouse/logs/logs34.py b/posthog/clickhouse/logs/logs34.py
index ff201333b935..8f60bb19b926 100644
--- a/posthog/clickhouse/logs/logs34.py
+++ b/posthog/clickhouse/logs/logs34.py
@@ -398,6 +398,60 @@ AS {KAFKA_LOGS34_AVRO_MV_SELECT()}
"""
+def LOGS34_TO_VOLUME_BUCKETS_MV():
+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+ # Groups rows exactly like _rollup_sql in
+ # products/logs/backend/temporal/volume_tick/aggregation.py, which carries
+ # the reasoning for the environment fallback and severity lowercasing. The
+ # 300s grid literal is frozen into the DDL at migration time; BUCKET_SECONDS
+ # there must stay equal to it or the detector reads buckets this MV never
+ # writes.
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.logs34_to_volume_buckets TO {db}.logs_volume_buckets
+(
+ `team_id` Int32,
+ `time_bucket` DateTime('UTC'),
+ `service_name` LowCardinality(String),
+ `namespace` LowCardinality(String),
+ `environment` LowCardinality(String),
+ `severity_text` LowCardinality(String),
+ `log_count` SimpleAggregateFunction(sum, UInt64)
+)
+AS SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ namespace,
+ environment,
+ severity_text,
+ sumSimpleState(1) AS log_count
+FROM
+(
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
+ service_name,
+ if(
+ resource_attributes['k8s.namespace.name'] != '',
+ resource_attributes['k8s.namespace.name'],
+ resource_attributes['service.namespace']
+ ) AS namespace,
+ if(
+ resource_attributes['deployment.environment.name'] != '',
+ resource_attributes['deployment.environment.name'],
+ if(
+ resource_attributes['deployment.environment'] != '',
+ resource_attributes['deployment.environment'],
+ resource_attributes['env']
+ )
+ ) AS environment,
+ lower(severity_text) AS severity_text
+ FROM {db}.{TABLE_NAME}
+)
+GROUP BY team_id, time_bucket, service_name, namespace, environment, severity_text
+"""
+
+
def KAFKA_LOGS_AVRO_KAFKA_METRICS_MV():
db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
return f"""
diff --git a/posthog/clickhouse/migrations/0302_logs34_to_volume_buckets_mv.py b/posthog/clickhouse/migrations/0302_logs34_to_volume_buckets_mv.py
new file mode 100644
index 000000000000..4d7eaf601d3e
--- /dev/null
+++ b/posthog/clickhouse/migrations/0302_logs34_to_volume_buckets_mv.py
@@ -0,0 +1,7 @@
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+from posthog.clickhouse.logs import LOGS34_TO_VOLUME_BUCKETS_MV
+
+operations = [
+ run_sql_with_exceptions(LOGS34_TO_VOLUME_BUCKETS_MV(), node_roles=[NodeRole.LOGS]),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 3549876e5af0..4d7f45feb63e 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0301_flag_evaluations_default_columns
+0302_logs34_to_volume_buckets_mv
diff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.py
index 422682b0a1f4..df700d71e407 100644
--- a/posthog/clickhouse/schema.py
+++ b/posthog/clickhouse/schema.py
@@ -23,6 +23,7 @@ from posthog.clickhouse.logs import (
LOGS34_TO_LOG_ATTRIBUTES_MV,
LOGS34_TO_RESOURCE_ATTRIBUTES3_MV,
LOGS34_TO_RESOURCE_ATTRIBUTES_MV,
+ LOGS34_TO_VOLUME_BUCKETS_MV,
LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL,
LOGS_BILLING_METRICS_TABLE_SQL,
LOGS_DISTRIBUTED2_TABLE_SQL,
@@ -599,6 +600,7 @@ CREATE_MV_TABLE_QUERIES = (
LOGS34_TO_RESOURCE_ATTRIBUTES_MV,
LOGS34_TO_LOG_ATTRIBUTES3_MV,
LOGS34_TO_RESOURCE_ATTRIBUTES3_MV,
+ LOGS34_TO_VOLUME_BUCKETS_MV,
METRIC_ATTRIBUTES_MV,
METRIC_RESOURCE_ATTRIBUTES_MV,
TOPHOG_MV_SQL,
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 9526421abde3..ca18dca88ef1 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -3710,6 +3710,54 @@
'''
# ---
+# name: test_create_table_query[logs34_to_volume_buckets]
+ '''
+
+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.logs34_to_volume_buckets TO posthog_test.logs_volume_buckets
+ (
+ `team_id` Int32,
+ `time_bucket` DateTime('UTC'),
+ `service_name` LowCardinality(String),
+ `namespace` LowCardinality(String),
+ `environment` LowCardinality(String),
+ `severity_text` LowCardinality(String),
+ `log_count` SimpleAggregateFunction(sum, UInt64)
+ )
+ AS SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ namespace,
+ environment,
+ severity_text,
+ sumSimpleState(1) AS log_count
+ FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
+ service_name,
+ if(
+ resource_attributes['k8s.namespace.name'] != '',
+ resource_attributes['k8s.namespace.name'],
+ resource_attributes['service.namespace']
+ ) AS namespace,
+ if(
+ resource_attributes['deployment.environment.name'] != '',
+ resource_attributes['deployment.environment.name'],
+ if(
+ resource_attributes['deployment.environment'] != '',
+ resource_attributes['deployment.environment'],
+ resource_attributes['env']
+ )
+ ) AS environment,
+ lower(severity_text) AS severity_text
+ FROM posthog_test.logs34
+ )
+ GROUP BY team_id, time_bucket, service_name, namespace, environment, severity_text
+
+ '''
+# ---
# name: test_create_table_query[logs]
'''
diff --git a/products/logs/backend/temporal/volume_tick/aggregation.py b/products/logs/backend/temporal/volume_tick/aggregation.py
index 445e0436ba70..6cae0ce4a4d9 100644
--- a/products/logs/backend/temporal/volume_tick/aggregation.py
+++ b/products/logs/backend/temporal/volume_tick/aggregation.py
@@ -22,8 +22,9 @@ TABLE_NAME = "logs_volume_buckets"
# cannot reproduce locally.
_SOURCE_TABLE = "logs_distributed"
-# `k8s.*.name` keys were never renamed, so namespace needs no fallback.
-_NAMESPACE_KEY = "k8s.namespace.name"
+# `k8s.*.name` keys were never renamed, but non-Kubernetes senders carry the
+# namespace in `service.namespace`, the semconv disambiguator for service.name.
+_NAMESPACE_KEYS = ("k8s.namespace.name", "service.namespace")
# The one resource attribute OTel has renamed: `deployment.environment` became
# `deployment.environment.name` in semantic conventions 1.27. `env` is not a
@@ -78,6 +79,7 @@ def _rollup_sql() -> str:
the grouping and the cost do not change.
"""
environment = _first_non_empty_map_key("resource_attributes", "env_key_", len(_ENVIRONMENT_KEYS))
+ namespace = _first_non_empty_map_key("resource_attributes", "ns_key_", len(_NAMESPACE_KEYS))
# The grid pins UTC explicitly. Without it the bucket edges follow the session
# timezone, so the same log would land in different buckets depending on who
# ran the query — and bucket identity has to be stable across ticks, backfills
@@ -95,7 +97,7 @@ def _rollup_sql() -> str:
team_id,
toStartOfInterval(timestamp, INTERVAL %(bucket_seconds)s SECOND, 'UTC') AS time_bucket,
service_name,
- resource_attributes[%(namespace_key)s] AS namespace,
+ {namespace} AS namespace,
{environment} AS environment,
lower(severity_text) AS severity_text,
count() AS log_count
@@ -113,9 +115,9 @@ def _rollup_parameters(team_ids: Sequence[int], start: datetime, end: datetime)
"start": start,
"end": end,
"bucket_seconds": BUCKET_SECONDS,
- "namespace_key": _NAMESPACE_KEY,
}
parameters.update({f"env_key_{index}": key for index, key in enumerate(_ENVIRONMENT_KEYS)})
+ parameters.update({f"ns_key_{index}": key for index, key in enumerate(_NAMESPACE_KEYS)})
return parameters
diff --git a/products/logs/backend/test/test_volume_bucket_aggregation.py b/products/logs/backend/test/test_volume_bucket_aggregation.py
index f9916e85ba47..7412c25fea3a 100644
--- a/products/logs/backend/test/test_volume_bucket_aggregation.py
+++ b/products/logs/backend/test/test_volume_bucket_aggregation.py
@@ -7,8 +7,11 @@ from posthog.test.base import BaseTest, ClickhouseTestMixin
from parameterized import parameterized
from posthog.clickhouse.client import sync_execute
+from posthog.clickhouse.logs import LOGS34_TO_VOLUME_BUCKETS_MV
from products.logs.backend.temporal.volume_tick.aggregation import (
+ _ENVIRONMENT_KEYS,
+ _NAMESPACE_KEYS,
RollupPreview,
_rollup_parameters,
_rollup_sql,
@@ -165,8 +168,15 @@ class TestVolumeBucketAggregation(ClickhouseTestMixin, BaseTest):
self.assertEqual([row[4] for row in self._rollup()], [expected])
- @parameterized.expand([("present", {"k8s.namespace.name": "web"}, "web"), ("absent", {}, "")])
- def test_namespace_is_verbatim_with_no_sentinel(
+ @parameterized.expand(
+ [
+ ("k8s", {"k8s.namespace.name": "web"}, "web"),
+ ("non_k8s_semconv", {"service.namespace": "web"}, "web"),
+ ("k8s_wins_when_both", {"k8s.namespace.name": "web", "service.namespace": "team-a"}, "web"),
+ ("absent", {}, ""),
+ ]
+ )
+ def test_namespace_falls_back_through_the_key_chain(
self, _name: str, resource_attributes: dict[str, str], expected: str
) -> None:
self._insert_logs([self._log(_START, resource_attributes=resource_attributes)])
@@ -197,3 +207,13 @@ class TestVolumeBucketAggregation(ClickhouseTestMixin, BaseTest):
self._insert_logs([self._log(_START), {**self._log(_START), "team_id": self.team.id + 10_000}])
self.assertEqual(self._preview().rollup_rows, 1)
+
+
+def test_mv_matches_the_detector_grid_and_dimension_keys() -> None:
+ sql = LOGS34_TO_VOLUME_BUCKETS_MV()
+
+ assert f"toIntervalSecond({BUCKET_SECONDS})" in sql
+ assert "lower(severity_text)" in sql
+ for chain in (_ENVIRONMENT_KEYS, _NAMESPACE_KEYS):
+ first_seen = [sql.index(f"'{key}'") for key in chain]
+ assert first_seen == sorted(first_seen)