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 dashboard variables to suggested dashboards (#119631)

Frank Hamanda704ca76010f
diff --git a/products/dashboards/backend/dashboard_creation.py b/products/dashboards/backend/dashboard_creation.py
index 5597e3b27a34..e34c433a023e 100644
--- a/products/dashboards/backend/dashboard_creation.py
+++ b/products/dashboards/backend/dashboard_creation.py
@@ -237,6 +237,17 @@ def read_dashboard_tiles(*, team_id: int, dashboard_id: int) -> tuple[DashboardT
)
+def read_dashboard_metric_variables(*, team_id: int, dashboard_id: int) -> list[dict[str, Any]]:
+ """The metrics dashboard variables of a dashboard, as MetricsDashboardVariable JSON."""
+ filters = (
+ Dashboard.objects.filter(team_id=team_id, id=dashboard_id, deleted=False)
+ .values_list("filters", flat=True)
+ .first()
+ )
+ variables = filters.get("metricVariables") if isinstance(filters, dict) else None
+ return [variable for variable in variables if isinstance(variable, dict)] if isinstance(variables, list) else []
+
+
def delete_unlisted_dashboard(*, team_id: int, dashboard_id: int) -> bool:
"""Soft-delete an unlisted dashboard that a product created. Returns False when there is no such dashboard."""
deleted = Dashboard.objects.filter(
diff --git a/products/dashboards/backend/facade/dashboard_creation.py b/products/dashboards/backend/facade/dashboard_creation.py
index 67eb9304d5a2..5d442db38e99 100644
--- a/products/dashboards/backend/facade/dashboard_creation.py
+++ b/products/dashboards/backend/facade/dashboard_creation.py
@@ -9,6 +9,7 @@ from products.dashboards.backend.dashboard_creation import (
create_dashboard_with_tiles,
delete_unlisted_dashboard,
move_dashboard_tiles,
+ read_dashboard_metric_variables,
read_dashboard_tiles,
)
@@ -23,5 +24,6 @@ __all__ = [
"create_dashboard_with_tiles",
"delete_unlisted_dashboard",
"move_dashboard_tiles",
+ "read_dashboard_metric_variables",
"read_dashboard_tiles",
]
diff --git a/products/metrics/backend/migrations/0002_metricsdashboardtemplate_variables.py b/products/metrics/backend/migrations/0002_metricsdashboardtemplate_variables.py
new file mode 100644
index 000000000000..526cbbc96111
--- /dev/null
+++ b/products/metrics/backend/migrations/0002_metricsdashboardtemplate_variables.py
@@ -0,0 +1,21 @@
+# Generated by Django 5.2.18 on 2026-10-19 14:31
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("metrics", "0001_initial_migration"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="metricsdashboardtemplate",
+ name="variables",
+ field=models.JSONField(
+ blank=True,
+ default=list,
+ help_text="The dashboard variables that the panels refer to, such as $service.",
+ ),
+ ),
+ ]
diff --git a/products/metrics/backend/migrations/max_migration.txt b/products/metrics/backend/migrations/max_migration.txt
index b0345048105e..0b4d40f8d207 100644
--- a/products/metrics/backend/migrations/max_migration.txt
+++ b/products/metrics/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0001_initial_migration
+0002_metricsdashboardtemplate_variables
diff --git a/products/metrics/backend/models.py b/products/metrics/backend/models.py
index fea17582c575..8eac6d655941 100644
--- a/products/metrics/backend/models.py
+++ b/products/metrics/backend/models.py
@@ -48,6 +48,9 @@ class MetricsDashboardTemplate(UUIDModel):
help_text="The metric names that the panels read. Matching compares them with the metric names of a team.",
)
panels = models.JSONField(default=list, blank=True, help_text="The panels, each with an insight query and a box.")
+ variables = models.JSONField(
+ default=list, blank=True, help_text="The dashboard variables that the panels refer to, such as $service."
+ )
content_hash = models.CharField(max_length=64, blank=True, default="")
generation = models.JSONField(
default=dict, blank=True, help_text="The record of the AI generation: rounds, pictures and verdicts."
diff --git a/products/metrics/backend/suggested_dashboards/bank.py b/products/metrics/backend/suggested_dashboards/bank.py
index 0371f24653e5..457e9f77775f 100644
--- a/products/metrics/backend/suggested_dashboards/bank.py
+++ b/products/metrics/backend/suggested_dashboards/bank.py
@@ -70,6 +70,7 @@ def sync_curated_templates() -> int:
"source": MetricsDashboardTemplate.Source.CURATED,
"status": MetricsDashboardTemplate.Status.APPROVED,
"panels": [panel.model_dump(mode="json") for panel in definition.panels],
+ "variables": definition.variables,
"metric_names": template_metric_names(definition.panels),
"content_hash": template.content_hash,
}
diff --git a/products/metrics/backend/suggested_dashboards/bank/envoy.json b/products/metrics/backend/suggested_dashboards/bank/envoy.json
index 7bc5eade71b5..ade93c6a4dcb 100644
--- a/products/metrics/backend/suggested_dashboards/bank/envoy.json
+++ b/products/metrics/backend/suggested_dashboards/bank/envoy.json
@@ -1,6 +1,20 @@
{
"name": "Envoy proxy",
"description": "Upstream traffic, errors, latency and connections of Envoy clusters.",
+ "variables": [
+ {
+ "name": "instance",
+ "label": "Instance",
+ "source": "sql",
+ "query": "SELECT DISTINCT attribute_value FROM posthog.metric_attributes WHERE attribute_key = 'instance' AND time_bucket >= toStartOfHour($__from) AND time_bucket < $__to",
+ "multi": false,
+ "includeAll": true,
+ "current": {
+ "values": [],
+ "all": true
+ }
+ }
+ ],
"panels": [
{
"key": "requests",
@@ -11,7 +25,14 @@
{
"name": "a",
"metricName": "envoy_cluster_upstream_rq_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -42,13 +63,25 @@
"key": "envoy_response_code_class",
"op": "eq",
"value": "5"
+ },
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
}
]
},
{
"name": "b",
"metricName": "envoy_cluster_upstream_rq_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -88,7 +121,14 @@
{
"name": "a",
"metricName": "envoy_cluster_upstream_cx_active",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -112,7 +152,14 @@
{
"name": "a",
"metricName": "envoy_cluster_membership_healthy",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -141,6 +188,13 @@
{
"key": "envoy_cluster_name"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -171,6 +225,13 @@
{
"key": "envoy_cluster_name"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -200,6 +261,13 @@
{
"key": "envoy_response_code_class"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -224,12 +292,26 @@
{
"name": "retries",
"metricName": "envoy_cluster_upstream_rq_retry",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "timeouts",
"metricName": "envoy_cluster_upstream_rq_timeout",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -258,6 +340,13 @@
{
"key": "envoy_http_conn_manager_prefix"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -287,6 +376,13 @@
{
"key": "envoy_cluster_name"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -307,7 +403,14 @@
"query": {
"kind": "MetricsHistogramQuery",
"metricName": "envoy_cluster_upstream_rq_time",
- "unit": "ms"
+ "unit": "ms",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
"layout": {
"x": 0,
diff --git a/products/metrics/backend/suggested_dashboards/bank/go-runtime.json b/products/metrics/backend/suggested_dashboards/bank/go-runtime.json
index 0e26644b20aa..e1018fecbecd 100644
--- a/products/metrics/backend/suggested_dashboards/bank/go-runtime.json
+++ b/products/metrics/backend/suggested_dashboards/bank/go-runtime.json
@@ -1,6 +1,20 @@
{
"name": "Go runtime",
"description": "Goroutines, heap, CPU and file descriptors of Go services that export Prometheus client metrics.",
+ "variables": [
+ {
+ "name": "service",
+ "label": "Service",
+ "source": "sql",
+ "query": "SELECT DISTINCT service_name FROM posthog.metric_names WHERE time_bucket >= toStartOfHour($__from) AND time_bucket < $__to",
+ "multi": false,
+ "includeAll": true,
+ "current": {
+ "values": [],
+ "all": true
+ }
+ }
+ ],
"panels": [
{
"key": "goroutines",
@@ -11,7 +25,14 @@
{
"name": "a",
"metricName": "go_goroutines",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -35,7 +56,14 @@
{
"name": "a",
"metricName": "go_memstats_heap_inuse_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -60,7 +88,14 @@
{
"name": "a",
"metricName": "process_resident_memory_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -85,7 +120,14 @@
{
"name": "a",
"metricName": "process_cpu_seconds_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -114,6 +156,13 @@
{
"key": "service_name"
}
+ ],
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
]
}
],
@@ -137,12 +186,26 @@
{
"name": "allocated",
"metricName": "go_memstats_heap_alloc_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
},
{
"name": "in_use",
"metricName": "go_memstats_heap_inuse_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -171,6 +234,13 @@
{
"key": "service_name"
}
+ ],
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
]
}
],
@@ -200,6 +270,13 @@
{
"key": "service_name"
}
+ ],
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
]
}
],
@@ -228,6 +305,13 @@
{
"key": "service_name"
}
+ ],
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
]
}
],
@@ -251,12 +335,26 @@
{
"name": "open",
"metricName": "process_open_fds",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
},
{
"name": "limit",
"metricName": "process_max_fds",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
diff --git a/products/metrics/backend/suggested_dashboards/bank/http-server-otel.json b/products/metrics/backend/suggested_dashboards/bank/http-server-otel.json
index 102e27b1e0e3..192273f7a43f 100644
--- a/products/metrics/backend/suggested_dashboards/bank/http-server-otel.json
+++ b/products/metrics/backend/suggested_dashboards/bank/http-server-otel.json
@@ -1,6 +1,20 @@
{
"name": "HTTP server",
"description": "Latency and in-flight requests of HTTP servers that report OpenTelemetry semantic conventions.",
+ "variables": [
+ {
+ "name": "service",
+ "label": "Service",
+ "source": "sql",
+ "query": "SELECT DISTINCT service_name FROM posthog.metric_names WHERE time_bucket >= toStartOfHour($__from) AND time_bucket < $__to",
+ "multi": false,
+ "includeAll": true,
+ "current": {
+ "values": [],
+ "all": true
+ }
+ }
+ ],
"panels": [
{
"key": "p50",
@@ -12,7 +26,14 @@
"name": "a",
"metricName": "http.server.request.duration",
"aggregation": "histogram_quantile",
- "quantile": 0.5
+ "quantile": 0.5,
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -38,7 +59,14 @@
"name": "a",
"metricName": "http.server.request.duration",
"aggregation": "histogram_quantile",
- "quantile": 0.95
+ "quantile": 0.95,
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -64,7 +92,14 @@
"name": "a",
"metricName": "http.server.request.duration",
"aggregation": "histogram_quantile",
- "quantile": 0.99
+ "quantile": 0.99,
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -89,7 +124,14 @@
{
"name": "a",
"metricName": "http.server.active_requests",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -119,6 +161,13 @@
{
"key": "http.route"
}
+ ],
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
]
}
],
@@ -149,6 +198,13 @@
{
"key": "service_name"
}
+ ],
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
]
}
],
@@ -179,6 +235,13 @@
{
"key": "http.response.status_code"
}
+ ],
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
]
}
],
@@ -208,6 +271,13 @@
{
"key": "service_name"
}
+ ],
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
]
}
],
@@ -228,7 +298,14 @@
"query": {
"kind": "MetricsHistogramQuery",
"metricName": "http.server.request.duration",
- "unit": "s"
+ "unit": "s",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
},
"layout": {
"x": 0,
diff --git a/products/metrics/backend/suggested_dashboards/bank/kafka.json b/products/metrics/backend/suggested_dashboards/bank/kafka.json
index 7f4ad7951d58..b4304b4cfaa2 100644
--- a/products/metrics/backend/suggested_dashboards/bank/kafka.json
+++ b/products/metrics/backend/suggested_dashboards/bank/kafka.json
@@ -1,6 +1,20 @@
{
"name": "Kafka consumers",
"description": "Consumer lag and throughput of Kafka topics and consumer groups from the Kafka exporter.",
+ "variables": [
+ {
+ "name": "instance",
+ "label": "Instance",
+ "source": "sql",
+ "query": "SELECT DISTINCT attribute_value FROM posthog.metric_attributes WHERE attribute_key = 'instance' AND time_bucket >= toStartOfHour($__from) AND time_bucket < $__to",
+ "multi": false,
+ "includeAll": true,
+ "current": {
+ "values": [],
+ "all": true
+ }
+ }
+ ],
"panels": [
{
"key": "lag",
@@ -11,7 +25,14 @@
{
"name": "a",
"metricName": "kafka_consumergroup_lag",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -36,7 +57,14 @@
{
"name": "a",
"metricName": "kafka_topic_partition_current_offset",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -61,7 +89,14 @@
{
"name": "a",
"metricName": "kafka_consumergroup_current_offset",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -86,7 +121,14 @@
{
"name": "a",
"metricName": "kafka_brokers",
- "aggregation": "max"
+ "aggregation": "max",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -115,6 +157,13 @@
{
"key": "consumergroup"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -144,6 +193,13 @@
{
"key": "topic"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -173,6 +229,13 @@
{
"key": "topic"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -202,6 +265,13 @@
{
"key": "consumergroup"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
diff --git a/products/metrics/backend/suggested_dashboards/bank/kubernetes.json b/products/metrics/backend/suggested_dashboards/bank/kubernetes.json
index 80bd91720478..740b89750b97 100644
--- a/products/metrics/backend/suggested_dashboards/bank/kubernetes.json
+++ b/products/metrics/backend/suggested_dashboards/bank/kubernetes.json
@@ -1,6 +1,20 @@
{
"name": "Kubernetes workloads",
"description": "Pods, restarts, CPU, memory and network of Kubernetes workloads from cAdvisor and kube-state-metrics.",
+ "variables": [
+ {
+ "name": "namespace",
+ "label": "Namespace",
+ "source": "sql",
+ "query": "SELECT DISTINCT attribute_value FROM posthog.metric_attributes WHERE attribute_key = 'namespace' AND time_bucket >= toStartOfHour($__from) AND time_bucket < $__to",
+ "multi": false,
+ "includeAll": true,
+ "current": {
+ "values": [],
+ "all": true
+ }
+ }
+ ],
"panels": [
{
"key": "running_pods",
@@ -17,6 +31,11 @@
"key": "phase",
"op": "eq",
"value": "Running"
+ },
+ {
+ "key": "namespace",
+ "op": "eq",
+ "value": "$namespace"
}
]
}
@@ -42,7 +61,14 @@
{
"name": "a",
"metricName": "kube_pod_container_status_restarts_total",
- "aggregation": "increase"
+ "aggregation": "increase",
+ "filters": [
+ {
+ "key": "namespace",
+ "op": "eq",
+ "value": "$namespace"
+ }
+ ]
}
],
"display": {
@@ -80,7 +106,14 @@
{
"name": "a",
"metricName": "container_cpu_usage_seconds_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "namespace",
+ "op": "eq",
+ "value": "$namespace"
+ }
+ ]
}
],
"display": {
@@ -104,7 +137,14 @@
{
"name": "a",
"metricName": "container_memory_working_set_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "namespace",
+ "op": "eq",
+ "value": "$namespace"
+ }
+ ]
}
],
"display": {
@@ -134,6 +174,13 @@
{
"key": "namespace"
}
+ ],
+ "filters": [
+ {
+ "key": "namespace",
+ "op": "eq",
+ "value": "$namespace"
+ }
]
}
],
@@ -162,6 +209,13 @@
{
"key": "namespace"
}
+ ],
+ "filters": [
+ {
+ "key": "namespace",
+ "op": "eq",
+ "value": "$namespace"
+ }
]
}
],
@@ -191,6 +245,13 @@
{
"key": "pod"
}
+ ],
+ "filters": [
+ {
+ "key": "namespace",
+ "op": "eq",
+ "value": "$namespace"
+ }
]
}
],
@@ -214,12 +275,26 @@
{
"name": "received",
"metricName": "container_network_receive_bytes_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "namespace",
+ "op": "eq",
+ "value": "$namespace"
+ }
+ ]
},
{
"name": "sent",
"metricName": "container_network_transmit_bytes_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "namespace",
+ "op": "eq",
+ "value": "$namespace"
+ }
+ ]
}
],
"display": {
@@ -243,12 +318,26 @@
{
"name": "available",
"metricName": "kube_deployment_status_replicas_available",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "namespace",
+ "op": "eq",
+ "value": "$namespace"
+ }
+ ]
},
{
"name": "desired",
"metricName": "kube_deployment_spec_replicas",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "namespace",
+ "op": "eq",
+ "value": "$namespace"
+ }
+ ]
}
],
"display": {
@@ -276,6 +365,13 @@
{
"key": "phase"
}
+ ],
+ "filters": [
+ {
+ "key": "namespace",
+ "op": "eq",
+ "value": "$namespace"
+ }
]
}
],
diff --git a/products/metrics/backend/suggested_dashboards/bank/nginx.json b/products/metrics/backend/suggested_dashboards/bank/nginx.json
index b345aac53576..c776336573ff 100644
--- a/products/metrics/backend/suggested_dashboards/bank/nginx.json
+++ b/products/metrics/backend/suggested_dashboards/bank/nginx.json
@@ -1,6 +1,20 @@
{
"name": "NGINX",
"description": "Requests and connections of NGINX servers that run the NGINX Prometheus exporter.",
+ "variables": [
+ {
+ "name": "instance",
+ "label": "Instance",
+ "source": "sql",
+ "query": "SELECT DISTINCT attribute_value FROM posthog.metric_attributes WHERE attribute_key = 'instance' AND time_bucket >= toStartOfHour($__from) AND time_bucket < $__to",
+ "multi": false,
+ "includeAll": true,
+ "current": {
+ "values": [],
+ "all": true
+ }
+ }
+ ],
"panels": [
{
"key": "requests",
@@ -11,7 +25,14 @@
{
"name": "a",
"metricName": "nginx_http_requests_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -36,7 +57,14 @@
{
"name": "a",
"metricName": "nginx_connections_active",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -60,12 +88,26 @@
{
"name": "a",
"metricName": "nginx_connections_accepted",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "b",
"metricName": "nginx_connections_handled",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -101,7 +143,14 @@
{
"name": "a",
"metricName": "nginx_up",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -130,6 +179,13 @@
{
"key": "instance"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -154,17 +210,38 @@
{
"name": "reading",
"metricName": "nginx_connections_reading",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "writing",
"metricName": "nginx_connections_writing",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "waiting",
"metricName": "nginx_connections_waiting",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -187,12 +264,26 @@
{
"name": "accepted",
"metricName": "nginx_connections_accepted",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "handled",
"metricName": "nginx_connections_handled",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -221,6 +312,13 @@
{
"key": "instance"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
diff --git a/products/metrics/backend/suggested_dashboards/bank/node-exporter.json b/products/metrics/backend/suggested_dashboards/bank/node-exporter.json
index 02ff0b17c01c..ba5877f4b8d6 100644
--- a/products/metrics/backend/suggested_dashboards/bank/node-exporter.json
+++ b/products/metrics/backend/suggested_dashboards/bank/node-exporter.json
@@ -1,6 +1,20 @@
{
"name": "Host metrics",
"description": "CPU, memory, disk and network of hosts that run the Prometheus node exporter.",
+ "variables": [
+ {
+ "name": "instance",
+ "label": "Instance",
+ "source": "sql",
+ "query": "SELECT DISTINCT attribute_value FROM posthog.metric_attributes WHERE attribute_key = 'instance' AND time_bucket >= toStartOfHour($__from) AND time_bucket < $__to",
+ "multi": false,
+ "includeAll": true,
+ "current": {
+ "values": [],
+ "all": true
+ }
+ }
+ ],
"panels": [
{
"key": "cpu_busy",
@@ -17,13 +31,25 @@
"key": "mode",
"op": "eq",
"value": "idle"
+ },
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
}
]
},
{
"name": "b",
"metricName": "node_cpu_seconds_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -63,12 +89,26 @@
{
"name": "a",
"metricName": "node_memory_MemAvailable_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "b",
"metricName": "node_memory_MemTotal_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -108,7 +148,14 @@
{
"name": "a",
"metricName": "node_load1",
- "aggregation": "avg"
+ "aggregation": "avg",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -132,12 +179,26 @@
{
"name": "a",
"metricName": "node_filesystem_avail_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "b",
"metricName": "node_filesystem_size_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -182,6 +243,13 @@
{
"key": "mode"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -210,6 +278,13 @@
{
"key": "instance"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -234,12 +309,26 @@
{
"name": "received",
"metricName": "node_network_receive_bytes_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "sent",
"metricName": "node_network_transmit_bytes_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -263,12 +352,26 @@
{
"name": "read",
"metricName": "node_disk_read_bytes_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "written",
"metricName": "node_disk_written_bytes_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -292,17 +395,38 @@
{
"name": "load1",
"metricName": "node_load1",
- "aggregation": "avg"
+ "aggregation": "avg",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "load5",
"metricName": "node_load5",
- "aggregation": "avg"
+ "aggregation": "avg",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "load15",
"metricName": "node_load15",
- "aggregation": "avg"
+ "aggregation": "avg",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -330,6 +454,13 @@
{
"key": "mountpoint"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
diff --git a/products/metrics/backend/suggested_dashboards/bank/nodejs-runtime.json b/products/metrics/backend/suggested_dashboards/bank/nodejs-runtime.json
index 430ab61c9d61..5412f0cf2f4f 100644
--- a/products/metrics/backend/suggested_dashboards/bank/nodejs-runtime.json
+++ b/products/metrics/backend/suggested_dashboards/bank/nodejs-runtime.json
@@ -1,6 +1,20 @@
{
"name": "Node.js runtime",
"description": "Event loop lag, heap, garbage collection and CPU of Node.js services that export prom-client metrics.",
+ "variables": [
+ {
+ "name": "service",
+ "label": "Service",
+ "source": "sql",
+ "query": "SELECT DISTINCT service_name FROM posthog.metric_names WHERE time_bucket >= toStartOfHour($__from) AND time_bucket < $__to",
+ "multi": false,
+ "includeAll": true,
+ "current": {
+ "values": [],
+ "all": true
+ }
+ }
+ ],
"panels": [
{
"key": "eventloop_p99",
@@ -11,7 +25,14 @@
{
"name": "a",
"metricName": "nodejs_eventloop_lag_p99_seconds",
- "aggregation": "max"
+ "aggregation": "max",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -50,7 +71,14 @@
{
"name": "a",
"metricName": "nodejs_heap_size_used_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -75,7 +103,14 @@
{
"name": "a",
"metricName": "process_resident_memory_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -100,7 +135,14 @@
{
"name": "a",
"metricName": "process_cpu_seconds_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -124,17 +166,38 @@
{
"name": "p50",
"metricName": "nodejs_eventloop_lag_p50_seconds",
- "aggregation": "max"
+ "aggregation": "max",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
},
{
"name": "p90",
"metricName": "nodejs_eventloop_lag_p90_seconds",
- "aggregation": "max"
+ "aggregation": "max",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
},
{
"name": "p99",
"metricName": "nodejs_eventloop_lag_p99_seconds",
- "aggregation": "max"
+ "aggregation": "max",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -158,12 +221,26 @@
{
"name": "used",
"metricName": "nodejs_heap_size_used_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
},
{
"name": "total",
"metricName": "nodejs_heap_size_total_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -192,6 +269,13 @@
{
"key": "space"
}
+ ],
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
]
}
],
@@ -222,6 +306,13 @@
{
"key": "kind"
}
+ ],
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
]
}
],
@@ -246,12 +337,26 @@
{
"name": "handles",
"metricName": "nodejs_active_handles_total",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
},
{
"name": "requests",
"metricName": "nodejs_active_requests_total",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
+ ]
}
],
"display": {
@@ -279,6 +384,13 @@
{
"key": "service_name"
}
+ ],
+ "filters": [
+ {
+ "key": "service_name",
+ "op": "eq",
+ "value": "$service"
+ }
]
}
],
diff --git a/products/metrics/backend/suggested_dashboards/bank/postgres.json b/products/metrics/backend/suggested_dashboards/bank/postgres.json
index a991c408d978..65607b58d95a 100644
--- a/products/metrics/backend/suggested_dashboards/bank/postgres.json
+++ b/products/metrics/backend/suggested_dashboards/bank/postgres.json
@@ -1,6 +1,20 @@
{
"name": "PostgreSQL",
"description": "Transactions, cache hits, rows, size and locks of PostgreSQL databases from the postgres exporter.",
+ "variables": [
+ {
+ "name": "instance",
+ "label": "Instance",
+ "source": "sql",
+ "query": "SELECT DISTINCT attribute_value FROM posthog.metric_attributes WHERE attribute_key = 'instance' AND time_bucket >= toStartOfHour($__from) AND time_bucket < $__to",
+ "multi": false,
+ "includeAll": true,
+ "current": {
+ "values": [],
+ "all": true
+ }
+ }
+ ],
"panels": [
{
"key": "connections",
@@ -11,7 +25,14 @@
{
"name": "a",
"metricName": "pg_stat_database_numbackends",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -35,12 +56,26 @@
{
"name": "a",
"metricName": "pg_stat_database_xact_commit",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "b",
"metricName": "pg_stat_database_xact_rollback",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -66,12 +101,26 @@
{
"name": "a",
"metricName": "pg_stat_database_blks_hit",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "b",
"metricName": "pg_stat_database_blks_read",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -111,7 +160,14 @@
{
"name": "a",
"metricName": "pg_stat_database_deadlocks",
- "aggregation": "increase"
+ "aggregation": "increase",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -135,12 +191,26 @@
{
"name": "commits",
"metricName": "pg_stat_database_xact_commit",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "rollbacks",
"metricName": "pg_stat_database_xact_rollback",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -164,22 +234,50 @@
{
"name": "fetched",
"metricName": "pg_stat_database_tup_fetched",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "inserted",
"metricName": "pg_stat_database_tup_inserted",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "updated",
"metricName": "pg_stat_database_tup_updated",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "deleted",
"metricName": "pg_stat_database_tup_deleted",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -208,6 +306,13 @@
{
"key": "datname"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -236,6 +341,13 @@
{
"key": "datname"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -265,6 +377,13 @@
{
"key": "mode"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -293,6 +412,13 @@
{
"key": "datname"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
diff --git a/products/metrics/backend/suggested_dashboards/bank/redis.json b/products/metrics/backend/suggested_dashboards/bank/redis.json
index dc7b4c1a95c1..fe9acb15e47e 100644
--- a/products/metrics/backend/suggested_dashboards/bank/redis.json
+++ b/products/metrics/backend/suggested_dashboards/bank/redis.json
@@ -1,6 +1,20 @@
{
"name": "Redis",
"description": "Commands, memory, hit rate and keys of Redis servers that run the Redis exporter.",
+ "variables": [
+ {
+ "name": "instance",
+ "label": "Instance",
+ "source": "sql",
+ "query": "SELECT DISTINCT attribute_value FROM posthog.metric_attributes WHERE attribute_key = 'instance' AND time_bucket >= toStartOfHour($__from) AND time_bucket < $__to",
+ "multi": false,
+ "includeAll": true,
+ "current": {
+ "values": [],
+ "all": true
+ }
+ }
+ ],
"panels": [
{
"key": "clients",
@@ -11,7 +25,14 @@
{
"name": "a",
"metricName": "redis_connected_clients",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -35,7 +56,14 @@
{
"name": "a",
"metricName": "redis_commands_processed_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -60,12 +88,26 @@
{
"name": "a",
"metricName": "redis_keyspace_hits_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "b",
"metricName": "redis_keyspace_misses_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -105,7 +147,14 @@
{
"name": "a",
"metricName": "redis_memory_used_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -135,6 +184,13 @@
{
"key": "instance"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -159,12 +215,26 @@
{
"name": "used",
"metricName": "redis_memory_used_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "limit",
"metricName": "redis_memory_max_bytes",
- "aggregation": "sum"
+ "aggregation": "sum",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -188,12 +258,26 @@
{
"name": "hits",
"metricName": "redis_keyspace_hits_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "misses",
"metricName": "redis_keyspace_misses_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -217,12 +301,26 @@
{
"name": "evicted",
"metricName": "redis_evicted_keys_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "expired",
"metricName": "redis_expired_keys_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
@@ -250,6 +348,13 @@
{
"key": "db"
}
+ ],
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
]
}
],
@@ -273,12 +378,26 @@
{
"name": "input",
"metricName": "redis_net_input_bytes_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
},
{
"name": "output",
"metricName": "redis_net_output_bytes_total",
- "aggregation": "rate"
+ "aggregation": "rate",
+ "filters": [
+ {
+ "key": "instance",
+ "op": "eq",
+ "value": "$instance"
+ }
+ ]
}
],
"display": {
diff --git a/products/metrics/backend/suggested_dashboards/dashboards.py b/products/metrics/backend/suggested_dashboards/dashboards.py
index a2699f5f5d4f..3d8e52604548 100644
--- a/products/metrics/backend/suggested_dashboards/dashboards.py
+++ b/products/metrics/backend/suggested_dashboards/dashboards.py
@@ -3,13 +3,22 @@
from __future__ import annotations
import copy
+import dataclasses
from collections import defaultdict
-from collections.abc import Sequence
+from collections.abc import Iterable, Sequence
from typing import Any
from pydantic import ValidationError
-from posthog.schema import MetricsHistogramQuery, MetricsOtelType, MetricsQuery
+from posthog.schema import (
+ MetricsDashboardVariable,
+ MetricsDashboardVariableSelection,
+ MetricsDashboardVariableSource,
+ MetricsHistogramQuery,
+ MetricsOtelType,
+ MetricsQuery,
+ MetricsQueryFilter,
+)
from posthog.dataclasses import frozen
from posthog.models import OrganizationMembership, Team, User
@@ -30,8 +39,13 @@ from products.metrics.backend.dashboard_import.spec import GRID_COLUMNS, GridLay
from products.metrics.backend.models import MetricsDashboardTemplate
from products.metrics.backend.suggested_dashboards.matching import query_metric_names
from products.metrics.backend.suggested_dashboards.spec import TemplatePanel
+from products.metrics.backend.variables import filter_referenced_names, referenced_names, sql_referenced_names
MAX_TEXT_LENGTH = 4000
+# The service is a column, not an attribute key. Metrics queries accept both names.
+SERVICE_ATTRIBUTES = frozenset({"service_name", "service.name"})
+# The name and attribute tables store hourly buckets, so the bucket that holds the start of the range counts too.
+OPTIONS_RANGE = "time_bucket >= toStartOfHour($__from) AND time_bucket < $__to"
_OTEL_TYPES = frozenset(member.value for member in MetricsOtelType)
@@ -41,6 +55,8 @@ class BuiltTiles:
# The titles of the panels that the team cannot fill, because it does not send their metrics.
dropped: tuple[str, ...]
dropped_keys: tuple[str, ...] = ()
+ # The names of the dashboard variables that the tiles refer to. `create_dashboard` fills them in.
+ variable_names: tuple[str, ...] = ()
def _with_team_metric_types(query: dict[str, Any], catalog: MetricCatalog) -> dict[str, Any] | None:
@@ -130,6 +146,63 @@ def build_tiles(panels: Sequence[TemplatePanel], catalog: MetricCatalog, *, refi
)
+def attribute_variable(*, name: str, label: str, attribute: str, multi: bool = False) -> dict[str, Any]:
+ """A dashboard variable whose options are the values of one attribute in the team that opens the dashboard.
+
+ It selects All, so its filters match everything until the user picks a value.
+ """
+ if attribute in SERVICE_ATTRIBUTES:
+ # The name table has far fewer rows than the attribute table, and holds every service too.
+ query = f"SELECT DISTINCT service_name FROM posthog.metric_names WHERE {OPTIONS_RANGE}"
+ else:
+ key = attribute.replace("\\", "\\\\").replace("'", "\\'")
+ query = (
+ "SELECT DISTINCT attribute_value FROM posthog.metric_attributes "
+ f"WHERE attribute_key = '{key}' AND {OPTIONS_RANGE}"
+ )
+ return MetricsDashboardVariable(
+ name=name,
+ label=label or None,
+ source=MetricsDashboardVariableSource.SQL,
+ query=query,
+ multi=multi,
+ includeAll=True,
+ current=MetricsDashboardVariableSelection(values=[], all=True),
+ ).model_dump(mode="json", exclude_none=True)
+
+
+def _query_references(query: dict[str, Any]) -> list[str]:
+ filters = [*(query.get("filters") or []), *(query.get("metricFilters") or [])]
+ for clause in query.get("clauses") or []:
+ filters.extend(clause.get("filters") or [])
+ return [
+ *filter_referenced_names(MetricsQueryFilter.model_validate(item) for item in filters),
+ *referenced_names(query.get("promql")),
+ *sql_referenced_names(query.get("sql")),
+ ]
+
+
+def referenced_variables(
+ queries: Iterable[dict[str, Any]], variables: Sequence[dict[str, Any]]
+) -> list[dict[str, Any]]:
+ """The variables that the queries refer to, and the variables that their options queries refer to, in order."""
+ by_name = {str(variable.get("name")): variable for variable in variables}
+ pending = [name for query in queries for name in _query_references(query)]
+ needed: set[str] = set()
+ while pending:
+ name = pending.pop()
+ if name in needed or name not in by_name:
+ continue
+ needed.add(name)
+ definition = by_name[name]
+ options_query = definition.get("query")
+ if definition.get("source") == MetricsDashboardVariableSource.SQL:
+ pending.extend(sql_referenced_names(options_query))
+ else:
+ pending.extend(referenced_names(options_query))
+ return [variable for variable in variables if variable.get("name") in needed]
+
+
def create_dashboard(
*,
team: Team,
@@ -141,11 +214,19 @@ def create_dashboard(
idempotency_key: str,
unlisted: bool = False,
refill: bool = True,
+ variables: Sequence[dict[str, Any]] = (),
) -> tuple[CreatedDashboard | None, BuiltTiles]:
- """Create the dashboard as the user. Returns no dashboard when no panel has data in this team."""
+ """Create the dashboard as the user. Returns no dashboard when no panel has data in this team.
+
+ The dashboard gets only the variables that its tiles refer to.
+ """
built = build_tiles(panels, catalog, refill=refill)
if not any(isinstance(tile, NewInsightTile) for tile in built.tiles):
return None, built
+ kept_variables = referenced_variables(
+ (tile.query for tile in built.tiles if isinstance(tile, NewInsightTile)), variables
+ )
+ built = dataclasses.replace(built, variable_names=tuple(str(variable["name"]) for variable in kept_variables))
created = create_dashboard_with_tiles(
team_id=team.id,
user_id=user.id,
@@ -155,6 +236,7 @@ def create_dashboard(
tiles=built.tiles,
idempotency_key=idempotency_key,
unlisted=unlisted,
+ filters={"metricVariables": kept_variables} if kept_variables else None,
),
)
return created, built
diff --git a/products/metrics/backend/suggested_dashboards/generation.py b/products/metrics/backend/suggested_dashboards/generation.py
index 7f89a6f5b816..65788a891b88 100644
--- a/products/metrics/backend/suggested_dashboards/generation.py
+++ b/products/metrics/backend/suggested_dashboards/generation.py
@@ -11,7 +11,8 @@ import io
import uuid
import hashlib
import datetime as dt
-from collections.abc import Sequence
+from collections.abc import Mapping, Sequence
+from typing import Any
from django.conf import settings
from django.db import transaction
@@ -30,7 +31,7 @@ from products.dashboards.backend.facade.dashboard_creation import delete_unliste
from products.exports.backend.facade.api import read_export_asset_content, render_png_export
from products.metrics.backend.dashboard_import.catalog import CatalogEntry, MetricCatalog
from products.metrics.backend.dashboard_import.layout import RequestedBox, place_screenshot_boxes
-from products.metrics.backend.dashboard_import.spec import PanelQuery
+from products.metrics.backend.dashboard_import.spec import BuilderFilter, PanelQuery
from products.metrics.backend.dashboard_import.tiles import insight_query
from products.metrics.backend.dashboard_import.validation import PanelValidator
from products.metrics.backend.metric_attributes_query_runner import (
@@ -39,12 +40,19 @@ from products.metrics.backend.metric_attributes_query_runner import (
)
from products.metrics.backend.models import MetricsDashboardTemplate
from products.metrics.backend.suggested_dashboards import prompts
-from products.metrics.backend.suggested_dashboards.dashboards import acting_user, create_dashboard, drop_preview
+from products.metrics.backend.suggested_dashboards.dashboards import (
+ SERVICE_ATTRIBUTES,
+ acting_user,
+ attribute_variable,
+ create_dashboard,
+ drop_preview,
+)
from products.metrics.backend.suggested_dashboards.llm import MODEL, ask_model, image_block, text_block
from products.metrics.backend.suggested_dashboards.matching import template_metric_names
from products.metrics.backend.suggested_dashboards.spec import (
DashboardDraft,
DraftPanel,
+ DraftVariable,
GenerationRecord,
GenerationRound,
PreviewCritique,
@@ -52,6 +60,7 @@ from products.metrics.backend.suggested_dashboards.spec import (
template_panels,
)
from products.metrics.backend.suggested_dashboards.timeouts import GENERATE_TIMEOUT
+from products.metrics.backend.variables import RANGE_VARIABLES, VARIABLE_NAME, referenced_names
logger = structlog.get_logger(__name__)
@@ -60,6 +69,7 @@ MAX_ATTRIBUTES_PER_METRIC = 8
# The values of an attribute with this many values or fewer go into the prompt, so that a filter uses a real value.
MAX_LISTED_VALUES = 8
CHECK_DEADLINE_SECONDS = 30.0
+MAX_VARIABLES = 2
PICTURE_TTL = dt.timedelta(days=90)
# A generation that has not saved for this long has stopped: it is longer than the whole workflow may run.
STALE_GENERATION = GENERATE_TIMEOUT + dt.timedelta(hours=1)
@@ -172,10 +182,57 @@ class _Converted:
panels: tuple[TemplatePanel, ...]
accepted: tuple[DraftPanel, ...]
dropped: tuple[str, ...]
+ # MetricsDashboardVariable JSON for the variables that the kept panels refer to.
+ variables: tuple[dict[str, Any], ...] = ()
+ accepted_variables: tuple[DraftVariable, ...] = ()
+
+
+def _valid_variables(drafts: Sequence[DraftVariable], attributes: Mapping[str, Sequence[str]]) -> list[DraftVariable]:
+ """The drafted variables that list a known attribute, with unique names."""
+ known = {key for keys in attributes.values() for key in keys} | SERVICE_ATTRIBUTES
+ valid: list[DraftVariable] = []
+ for draft in drafts:
+ if len(valid) >= MAX_VARIABLES:
+ break
+ if (
+ not VARIABLE_NAME.match(draft.name)
+ or draft.name in RANGE_VARIABLES
+ or draft.attribute not in known
+ or any(variable.name == draft.name for variable in valid)
+ ):
+ continue
+ valid.append(draft)
+ return valid
+
+
+def _with_known_references(draft: DraftPanel, names: set[str]) -> DraftPanel:
+ """The panel without the filters that refer to a variable that the dashboard does not have."""
+
+ def known(builder_filter: BuilderFilter) -> bool:
+ return set(referenced_names(builder_filter.value)) <= names
+ clauses = [
+ clause.model_copy(update={"filters": [item for item in clause.filters if known(item)]})
+ for clause in draft.query.clauses
+ ]
+ return draft.model_copy(update={"query": draft.query.model_copy(update={"clauses": clauses})})
-def _convert(drafts: Sequence[DraftPanel], team: Team, catalog: MetricCatalog) -> _Converted:
- """Check each drafted query as PostHog would run it, and keep the panels that pass, with no overlapping boxes."""
+
+def _convert(
+ drafts: Sequence[DraftPanel],
+ team: Team,
+ catalog: MetricCatalog,
+ *,
+ variables: Sequence[DraftVariable] = (),
+ attributes: Mapping[str, Sequence[str]] | None = None,
+) -> _Converted:
+ """Check each drafted query as PostHog would run it, and keep the panels that pass, with no overlapping boxes.
+
+ A drafted variable stays when it lists a known attribute and a kept panel refers to it. It selects All, because
+ the template is for every project and a value of the source project must not reach the others.
+ """
+ valid_variables = _valid_variables(variables, attributes or {})
+ drafts = [_with_known_references(draft, {variable.name for variable in valid_variables}) for draft in drafts]
validator = PanelValidator(team=team, catalog=catalog, promql_available=False)
queries = {draft.key: PanelQuery(language="builder", builder=draft.query) for draft in drafts}
checks = validator.check_all(queries, deadline_seconds=CHECK_DEADLINE_SECONDS)
@@ -202,12 +259,36 @@ def _convert(drafts: Sequence[DraftPanel], team: Team, catalog: MetricCatalog) -
TemplatePanel(key=draft.key, title=draft.title[:200], query=queries_by_key[draft.key], layout=boxes[draft.key])
for draft in accepted
)
- return _Converted(panels=panels, accepted=tuple(accepted), dropped=tuple(dropped))
+ used = {
+ name
+ for draft in accepted
+ for clause in draft.query.clauses
+ for item in clause.filters
+ for name in referenced_names(item.value)
+ }
+ kept_variables = tuple(variable for variable in valid_variables if variable.name in used)
+ return _Converted(
+ panels=panels,
+ accepted=tuple(accepted),
+ dropped=tuple(dropped),
+ variables=tuple(
+ attribute_variable(
+ name=variable.name,
+ label=" ".join(variable.label.split())[:100],
+ attribute=variable.attribute,
+ multi=variable.multi,
+ )
+ for variable in kept_variables
+ ),
+ accepted_variables=kept_variables,
+ )
def _store_panels(template: MetricsDashboardTemplate, record: GenerationRecord, converted: _Converted) -> None:
template.panels = [panel.model_dump(mode="json") for panel in converted.panels]
+ template.variables = list(converted.variables)
record.draft_panels = list(converted.accepted)
+ record.draft_variables = list(converted.accepted_variables)
record.dropped_panels = list(converted.dropped)
@@ -233,13 +314,19 @@ def draft_template(template_id: str) -> int:
],
answer_type=DashboardDraft,
)
- converted = _convert(draft.panels, context.team, context.catalog)
+ converted = _convert(
+ draft.panels,
+ context.team,
+ context.catalog,
+ variables=draft.variables,
+ attributes=attributes.keys,
+ )
template.name = " ".join(draft.name.split())[:200] or template.name
template.description = " ".join(draft.description.split())[:300] or template.description
_store_panels(template, record, converted)
record.attributes = attributes.keys
record.attribute_values = attributes.values
- _save_record(template, record, "name", "description", "panels")
+ _save_record(template, record, "name", "description", "panels", "variables")
return len(converted.panels)
@@ -260,6 +347,7 @@ def _refresh_preview(context: _Context, round_number: int) -> int | None:
catalog=context.catalog,
idempotency_key=f"metrics-template-preview:{template.id}:{round_number}:{uuid.uuid4()}",
unlisted=True,
+ variables=template.variables or [],
)
template.preview_team_id = context.team.id if created else None
template.preview_dashboard_id = created.id if created else None
@@ -337,6 +425,7 @@ def check_preview(template_id: str, round_number: int, *, revise: bool) -> bool:
text_block(
prompts.critique_input(
panels=record.draft_panels,
+ variables=record.draft_variables,
entries=context.entries,
attributes=record.attributes,
attribute_values=record.attribute_values,
@@ -350,7 +439,13 @@ def check_preview(template_id: str, round_number: int, *, revise: bool) -> bool:
current.looks_good = critique.looks_good
current.problems = [" ".join(problem.split())[:300] for problem in critique.problems][:10]
if not looks_good and revise:
- converted = _convert(critique.panels, context.team, context.catalog)
+ converted = _convert(
+ critique.panels,
+ context.team,
+ context.catalog,
+ variables=critique.variables,
+ attributes=record.attributes,
+ )
if converted.panels:
current.revised = True
_store_panels(template, record, converted)
@@ -360,6 +455,7 @@ def check_preview(template_id: str, round_number: int, *, revise: bool) -> bool:
template,
record,
"panels",
+ "variables",
"preview_team_id",
"preview_dashboard_id",
"preview_unbuilt_keys",
@@ -436,6 +532,7 @@ def _capture_generated(template: MetricsDashboardTemplate, record: GenerationRec
"error": record.error,
"panel_count": len(template.panels or []),
"dropped_panel_count": len(record.dropped_panels),
+ "variable_count": len(template.variables or []),
"check_rounds": len(record.rounds),
"looks_good": record.rounds[-1].looks_good if record.rounds else None,
# No project in the path: the reviewer opens it in their own project, not the source team's.
diff --git a/products/metrics/backend/suggested_dashboards/prompts.py b/products/metrics/backend/suggested_dashboards/prompts.py
index 095abc828ad9..65f6ac776996 100644
--- a/products/metrics/backend/suggested_dashboards/prompts.py
+++ b/products/metrics/backend/suggested_dashboards/prompts.py
@@ -8,7 +8,7 @@ from typing import Any
from products.metrics.backend.dashboard_import.catalog import CatalogEntry
from products.metrics.backend.suggested_dashboards.matching import MetricFamily, TemplateMatch
-from products.metrics.backend.suggested_dashboards.spec import DraftPanel
+from products.metrics.backend.suggested_dashboards.spec import DraftPanel, DraftVariable
MAX_PROMPT_METRICS = 400
MAX_MATCHED_NAMES = 20
@@ -54,6 +54,16 @@ group_by in a panel, and none in a stat panel.
Never filter on a value that only one project has, such as a host name or an id.
- Give each panel one clear purpose. Two panels never show the same thing.
+Variables:
+- Add a dashboard variable when the metrics share an attribute that names one of several instances of the system, \
+such as a job, an instance, a namespace or a cluster. A user then picks one instance for the whole dashboard. \
+service_name, the service that sends the metric, is always available for a variable.
+- Add no variable for an attribute that splits one instance, such as a route, a status or a mode. Use group_by \
+for those. Use at most 2 variables.
+- Every panel whose metrics have the attribute refers to the variable with a filter: key is the attribute, op is \
+eq and value is $ and the variable name, for example {{"key": "service_name", "op": "eq", "value": "$service"}}.
+- A variable selects All when the dashboard opens. A user then picks a value from their own project.
+
Layout on the 12-column grid:
- Row 1: 3 or 4 stat panels with the key numbers, such as throughput, error ratio, latency and saturation. \
Use w 4 or w 3, h 2, y 0.
@@ -81,10 +91,11 @@ Look for problems that a person would notice:
- A title does not say what the panel shows.
- The layout has holes or overlaps, a half-width panel alone in a row, or stats outside the top row.
- Two panels show the same thing.
+- A variable that no panel refers to, or a panel that does not refer to a variable whose attribute its metrics have.
When there is no such problem, set looks_good to true. A small style preference is not a problem.
-When there are problems, set looks_good to false, list them, and return the complete corrected panel list. Keep \
-the panels that are fine as they are. Use only metric names from the catalog.
+When there are problems, set looks_good to false, list them, and return the complete corrected panel list and \
+variable list. Keep the panels and variables that are fine as they are. Use only metric names from the catalog.
{_DATA_NOTE}"""
@@ -151,6 +162,7 @@ def draft_input(
def critique_input(
*,
panels: Sequence[DraftPanel],
+ variables: Sequence[DraftVariable],
entries: Sequence[CatalogEntry],
attributes: dict[str, list[str]],
attribute_values: dict[str, list[str]],
@@ -158,6 +170,7 @@ def critique_input(
) -> str:
payload: dict[str, Any] = {
"panels": [panel.model_dump(mode="json", exclude_none=True) for panel in panels],
+ "variables": [variable.model_dump(mode="json") for variable in variables],
"metric_catalog": [{**_entry(entry), "attributes": attributes.get(entry.name, [])} for entry in entries],
"attribute_values": attribute_values,
}
diff --git a/products/metrics/backend/suggested_dashboards/review.py b/products/metrics/backend/suggested_dashboards/review.py
index b9dba2558d6e..9df56befbbdc 100644
--- a/products/metrics/backend/suggested_dashboards/review.py
+++ b/products/metrics/backend/suggested_dashboards/review.py
@@ -3,19 +3,34 @@
from __future__ import annotations
import uuid
+from collections.abc import Sequence
+from typing import Any
from django.db import transaction
from django.utils import timezone
+from pydantic import ValidationError
+
+from posthog.schema import MetricsDashboardVariable, MetricsDashboardVariableSelection, MetricsDashboardVariableSource
+
from posthog.event_usage import report_user_action
from posthog.models import Team, User
from products.dashboards.backend.facade.api import unknown_dashboard_ids
-from products.dashboards.backend.facade.dashboard_creation import delete_unlisted_dashboard, read_dashboard_tiles
+from products.dashboards.backend.facade.dashboard_creation import (
+ delete_unlisted_dashboard,
+ read_dashboard_metric_variables,
+ read_dashboard_tiles,
+)
from products.metrics.backend.dashboard_import.catalog import MetricCatalog
from products.metrics.backend.dashboard_import.spec import GridLayout
from products.metrics.backend.models import MetricsDashboardSuggestion, MetricsDashboardTemplate
-from products.metrics.backend.suggested_dashboards.dashboards import create_dashboard, drop_preview, panels_from_tiles
+from products.metrics.backend.suggested_dashboards.dashboards import (
+ create_dashboard,
+ drop_preview,
+ panels_from_tiles,
+ referenced_variables,
+)
from products.metrics.backend.suggested_dashboards.matching import template_metric_names
from products.metrics.backend.suggested_dashboards.spec import TemplatePanel, template_panels
@@ -66,6 +81,7 @@ def open_preview(*, template: MetricsDashboardTemplate, team: Team, user: User)
catalog=catalog,
idempotency_key=f"metrics-template-review:{template.id}:{uuid.uuid4()}",
unlisted=True,
+ variables=template.variables or [],
# The panels keep their boxes, so the ones that the preview leaves out can go back in place on approve.
refill=False,
)
@@ -114,6 +130,29 @@ def _restore_unbuilt(
return [panel.model_copy(update={"key": f"p{index + 1}"}) for index, panel in enumerate([*edited, *missing])]
+def _shared_variables(edited: Sequence[Any], original: Sequence[dict[str, Any]]) -> list[dict[str, Any]]:
+ """The variables of the preview, safe to give to every project, then the template's variables that it lacks.
+
+ A definition that does not parse is left out, so the template keeps its own. So is a custom list, because its
+ values come from the preview's project, not from the project that opens the dashboard. A selection can name a
+ host or a service of the preview's project, so every variable selects All.
+ """
+ shared: list[dict[str, Any]] = []
+ for item in edited:
+ try:
+ variable = MetricsDashboardVariable.model_validate(item)
+ except ValidationError:
+ continue
+ if variable.source == MetricsDashboardVariableSource.CUSTOM or variable.customValues:
+ continue
+ variable = variable.model_copy(
+ update={"includeAll": True, "current": MetricsDashboardVariableSelection(values=[], all=True)}
+ )
+ shared.append(variable.model_dump(mode="json", exclude_none=True))
+ names = {variable["name"] for variable in shared}
+ return [*shared, *(variable for variable in original if variable.get("name") not in names)]
+
+
def approve(*, template: MetricsDashboardTemplate, user: User) -> MetricsDashboardTemplate:
"""Approve the template. Changes made on its preview dashboard, by hand or with PostHog AI, become the template."""
with transaction.atomic():
@@ -133,6 +172,16 @@ def approve(*, template: MetricsDashboardTemplate, user: User) -> MetricsDashboa
panels = _restore_unbuilt(panels, template_panels(locked.panels), set(locked.preview_unbuilt_keys))
locked.panels = [panel.model_dump(mode="json") for panel in panels]
locked.metric_names = template_metric_names(panels)
+ # The preview has only the variables of the panels that it shows. The panels it left out keep theirs.
+ shared = _shared_variables(
+ read_dashboard_metric_variables(
+ team_id=locked.preview_team_id, dashboard_id=locked.preview_dashboard_id
+ ),
+ locked.variables or [],
+ )
+ locked.variables = referenced_variables(
+ (panel.query for panel in panels if panel.query is not None), shared
+ )
drop_preview(locked)
locked.status = MetricsDashboardTemplate.Status.APPROVED
locked.reviewed_by = user
@@ -191,6 +240,7 @@ def create_from_suggestion(*, suggestion: MetricsDashboardSuggestion, team: Team
catalog=catalog,
# A deleted dashboard keeps its insights' ids, so a new one needs a new key.
idempotency_key=f"metrics-suggestion:{suggestion.id}" + (f":after-{previous}" if previous else ""),
+ variables=template.variables or [],
)
if created is None:
raise NoPanelsWithData("This project no longer sends the metrics of this dashboard.")
@@ -205,6 +255,7 @@ def create_from_suggestion(*, suggestion: MetricsDashboardSuggestion, team: Team
"dashboard_id": created.id,
"coverage": suggestion.coverage,
"dropped_panel_count": len(built.dropped),
+ "variable_count": len(built.variable_names),
},
team=team,
)
diff --git a/products/metrics/backend/suggested_dashboards/spec.py b/products/metrics/backend/suggested_dashboards/spec.py
index 9744b61ec2bb..3095e43c3e8e 100644
--- a/products/metrics/backend/suggested_dashboards/spec.py
+++ b/products/metrics/backend/suggested_dashboards/spec.py
@@ -40,6 +40,8 @@ class TemplateDefinition(_Model):
name: str
description: str
panels: list[TemplatePanel]
+ # MetricsDashboardVariable JSON. A panel refers to a variable with a filter value such as `$service`.
+ variables: list[dict[str, Any]] = Field(default_factory=list)
# The AI answers. Field descriptions are part of the prompt, so they stay short and exact.
@@ -80,10 +82,20 @@ class DraftPanel(_Model):
layout: GridLayout
+class DraftVariable(_Model):
+ name: str = Field(description="The name that filters refer to as $name, for example service. Letters, digits, _.")
+ label: str = Field(description="A short label in sentence case, for example 'Service'.")
+ attribute: str = Field(description="The attribute whose values the variable lists.")
+ multi: bool = Field(default=False, description="True when a user can select more than one value.")
+
+
class DashboardDraft(_Model):
name: str = Field(description="A short dashboard name in sentence case.")
description: str = Field(description="One sentence that says what the dashboard shows. At most 120 characters.")
panels: list[DraftPanel]
+ variables: list[DraftVariable] = Field(
+ default_factory=list, description="The dashboard variables. Empty when no attribute suits a variable."
+ )
class PreviewCritique(_Model):
@@ -97,6 +109,10 @@ class PreviewCritique(_Model):
panels: list[DraftPanel] = Field(
description="The complete corrected panel list when looks_good is false. Empty when looks_good is true."
)
+ variables: list[DraftVariable] = Field(
+ default_factory=list,
+ description="The complete corrected variable list when looks_good is false. Empty when looks_good is true.",
+ )
class GenerationRound(_Model):
@@ -116,6 +132,7 @@ class GenerationRecord(_Model):
rounds: list[GenerationRound] = Field(default_factory=list)
# The panels in the shape that the model writes, so that a check round can show the model its own panels.
draft_panels: list[DraftPanel] = Field(default_factory=list)
+ draft_variables: list[DraftVariable] = Field(default_factory=list)
attributes: dict[str, list[str]] = Field(default_factory=dict)
attribute_values: dict[str, list[str]] = Field(default_factory=dict)
dropped_panels: list[str] = Field(default_factory=list)
diff --git a/products/metrics/backend/tests/test_suggested_dashboards.py b/products/metrics/backend/tests/test_suggested_dashboards.py
index bbb8804dec7f..3fb24fb1247f 100644
--- a/products/metrics/backend/tests/test_suggested_dashboards.py
+++ b/products/metrics/backend/tests/test_suggested_dashboards.py
@@ -6,20 +6,37 @@ from django.test import SimpleTestCase
from parameterized import parameterized
-from posthog.schema import MetricsHistogramQuery, MetricsQuery
+from posthog.schema import MetricsDashboardVariable, MetricsHistogramQuery, MetricsQuery, MetricsQueryFilter
from posthog.models import Team
from products.dashboards.backend.facade.dashboard_creation import DashboardTileSnapshot, NewInsightTile, TileLayout
from products.metrics.backend.dashboard_import.catalog import CatalogEntry, MetricCatalog
-from products.metrics.backend.dashboard_import.spec import BuilderClause, BuilderQuery, DisplaySpec, GridLayout
+from products.metrics.backend.dashboard_import.spec import (
+ BuilderClause,
+ BuilderFilter,
+ BuilderQuery,
+ DisplaySpec,
+ GridLayout,
+)
from products.metrics.backend.suggested_dashboards.bank import curated_templates
-from products.metrics.backend.suggested_dashboards.dashboards import build_tiles, panels_from_tiles
+from products.metrics.backend.suggested_dashboards.dashboards import (
+ attribute_variable,
+ build_tiles,
+ panels_from_tiles,
+ referenced_variables,
+)
from products.metrics.backend.suggested_dashboards.discovery import _signed
from products.metrics.backend.suggested_dashboards.generation import _convert
from products.metrics.backend.suggested_dashboards.llm import ModelFailed, ask_model
from products.metrics.backend.suggested_dashboards.matching import match_template
-from products.metrics.backend.suggested_dashboards.spec import DraftPanel, EvaluationAnswer, TemplatePanel
+from products.metrics.backend.suggested_dashboards.spec import (
+ DraftPanel,
+ DraftVariable,
+ EvaluationAnswer,
+ TemplatePanel,
+)
+from products.metrics.backend.variables import filter_referenced_names
CATALOG = MetricCatalog(
[
@@ -63,6 +80,10 @@ class TestSuggestedDashboards(SimpleTestCase):
cells = {(layout.x + dx, layout.y + dy) for dx in range(layout.w) for dy in range(layout.h)}
assert not boxes & cells, f"{panel.key} overlaps another panel"
boxes |= cells
+ for variable in template.definition.variables:
+ MetricsDashboardVariable.model_validate(variable)
+ queries = [panel.query for panel in template.definition.panels if panel.query is not None]
+ assert referenced_variables(queries, template.definition.variables) == template.definition.variables
@parameterized.expand(
[
@@ -188,6 +209,93 @@ class TestSuggestedDashboards(SimpleTestCase):
first, second = (panel.layout for panel in converted.panels)
assert first.x + first.w <= second.x or first.y + first.h <= second.y
+ def test_referenced_variables_keeps_the_variables_of_the_queries_and_of_their_options(self) -> None:
+ query = _query("orders_total")
+ query["clauses"][0]["filters"] = [{"key": "instance", "op": "eq", "value": "$instance"}]
+ instance = {
+ **attribute_variable(name="instance", label="Instance", attribute="instance"),
+ "query": "SELECT DISTINCT attribute_value FROM posthog.metric_attributes WHERE attribute_key = $job",
+ }
+ job = attribute_variable(name="job", label="Job", attribute="job")
+ unused = attribute_variable(name="unused", label="Unused", attribute="unused")
+
+ assert referenced_variables([query], [job, unused, instance]) == [job, instance]
+ assert referenced_variables([_query("orders_total")], [job, unused, instance]) == []
+
+ @parameterized.expand(
+ [
+ ("attribute", "instance", False),
+ ("several_values", "env", True),
+ ("service", "service_name", False),
+ ]
+ )
+ def test_a_drafted_variable_selects_all_and_lists_the_values_of_the_opening_project(
+ self, _name: str, attribute: str, multi: bool
+ ) -> None:
+ converted = _convert(
+ [self._draft("orders", "orders_total", 0, filters=[BuilderFilter(key=attribute, op="eq", value="$scope")])],
+ Team(id=1),
+ CATALOG,
+ variables=[DraftVariable(name="scope", label="Scope", attribute=attribute, multi=multi)],
+ attributes={"orders_total": ["instance", "env"]},
+ )
+
+ assert len(converted.variables) == 1
+ variable = MetricsDashboardVariable.model_validate(converted.variables[0])
+ assert variable.current is not None
+ assert (variable.current.values, variable.current.all) == ([], True)
+ assert (variable.includeAll, variable.multi) == (True, multi)
+ assert (variable.query or "").startswith(
+ "SELECT DISTINCT service_name FROM posthog.metric_names"
+ if attribute == "service_name"
+ else "SELECT DISTINCT attribute_value FROM posthog.metric_attributes"
+ )
+
+ def test_convert_drops_variables_with_unknown_attributes_or_no_panel(self) -> None:
+ converted = _convert(
+ [
+ self._draft(
+ "orders",
+ "orders_total",
+ 0,
+ filters=[
+ BuilderFilter(key="instance", op="eq", value="$instance"),
+ BuilderFilter(key="host", op="eq", value="$host"),
+ ],
+ ),
+ self._draft("queue", "queue_depth", 8),
+ ],
+ Team(id=1),
+ CATALOG,
+ variables=[
+ DraftVariable(name="instance", label="Instance", attribute="instance"),
+ DraftVariable(name="host", label="Host", attribute="host.name"),
+ DraftVariable(name="job", label="Job", attribute="job"),
+ DraftVariable(name="__interval", label="Interval", attribute="instance"),
+ ],
+ attributes={"orders_total": ["instance", "job"]},
+ )
+
+ assert [variable["name"] for variable in converted.variables] == ["instance"]
+ assert [variable.name for variable in converted.accepted_variables] == ["instance"]
+ orders = converted.panels[0].query
+ assert orders is not None
+ assert filter_referenced_names(
+ MetricsQueryFilter.model_validate(item) for item in orders["clauses"][0]["filters"]
+ ) == ["instance"]
+
+ @staticmethod
+ def _draft(key: str, metric: str, x: int, filters: list[BuilderFilter] | None = None) -> DraftPanel:
+ return DraftPanel(
+ key=key,
+ title=key,
+ query=BuilderQuery(
+ clauses=[BuilderClause(name="a", metric_name=metric, aggregation="rate", filters=filters or [])]
+ ),
+ display=DisplaySpec(type="line"),
+ layout=GridLayout(x=x, y=0, w=4, h=4),
+ )
+
@parameterized.expand([(0, 0), (2**63 - 1, 2**63 - 1), (2**63, -(2**63)), (2**64 - 1, -1)])
def test_unsigned_fingerprint_fits_a_signed_column(self, unsigned: int, signed: int) -> None:
assert _signed(unsigned) == signed
diff --git a/products/metrics/backend/tests/test_suggested_dashboards_api.py b/products/metrics/backend/tests/test_suggested_dashboards_api.py
index 1563cec1fe06..619380947f2e 100644
--- a/products/metrics/backend/tests/test_suggested_dashboards_api.py
+++ b/products/metrics/backend/tests/test_suggested_dashboards_api.py
@@ -1,12 +1,14 @@
import datetime as dt
from typing import Any
-from posthog.test.base import APIBaseTest
+from posthog.test.base import APIBaseTest, ClickhouseTestMixin
from unittest.mock import MagicMock, patch
from parameterized import parameterized
from rest_framework import status
+from posthog.schema import MetricsDashboardVariable
+
from posthog.models import Team
from products.dashboards.backend.models.dashboard import Dashboard
@@ -19,6 +21,7 @@ from products.metrics.backend.models import (
)
from products.metrics.backend.suggested_dashboards.analysis import analyze_team, bank_revision
from products.metrics.backend.suggested_dashboards.bank import sync_curated_templates
+from products.metrics.backend.suggested_dashboards.dashboards import attribute_variable
from products.metrics.backend.suggested_dashboards.discovery import record_opened, teams_to_analyze
from products.metrics.backend.suggested_dashboards.generation import (
fail_stale_generations,
@@ -27,6 +30,8 @@ from products.metrics.backend.suggested_dashboards.generation import (
)
from products.metrics.backend.suggested_dashboards.review import SuggestionGone, create_from_suggestion
from products.metrics.backend.suggested_dashboards.spec import EvaluationAnswer
+from products.metrics.backend.tests._seeder import seed_metric, truncate_metrics_tables
+from products.metrics.backend.variable_options import list_variable_options
QUEUE_METRICS = ["queue_depth", "queue_wait_seconds", "queue_dropped_total", "queue_retries_total"]
CATALOG = [
@@ -45,6 +50,11 @@ def _panel(key: str, metric: str, x: int) -> dict[str, Any]:
}
+def _with_variable_filter(panel: dict[str, Any], name: str) -> dict[str, Any]:
+ panel["query"]["clauses"][0]["filters"] = [{"key": name, "op": "eq", "value": f"${name}"}]
+ return panel
+
+
def _template(key: str, status: str, *metrics: str, source: str = "curated") -> MetricsDashboardTemplate:
return MetricsDashboardTemplate.objects.create(
key=key,
@@ -137,6 +147,27 @@ class TestSuggestedDashboardsAPI(APIBaseTest):
assert recreated.status_code == status.HTTP_201_CREATED
assert recreated.json()["dashboard_id"] not in (None, first.json()["dashboard_id"])
+ def test_a_suggested_dashboard_gets_the_variables_of_its_tiles(self) -> None:
+ template = _template("mixed", "approved", "orders_total", "unknown_total")
+ _with_variable_filter(template.panels[0], "instance")
+ # The project does not send unknown_total, so its panel and its variable stay out.
+ _with_variable_filter(template.panels[1], "job")
+ template.variables = [
+ attribute_variable(name="instance", label="Instance", attribute="instance"),
+ attribute_variable(name="job", label="Job", attribute="job"),
+ ]
+ template.save(update_fields=["panels", "variables"])
+ suggestion = MetricsDashboardSuggestion.objects.for_team(self.team.id).create(
+ team_id=self.team.id, template=template, coverage=0.5
+ )
+
+ response = self.client.post(f"{self.base_url}/suggested_dashboards/{suggestion.id}/dashboard/")
+
+ assert response.status_code == status.HTTP_201_CREATED
+ dashboard = Dashboard.objects.get(id=response.json()["dashboard_id"])
+ assert dashboard.filters["metricVariables"] == [template.variables[0]]
+ assert dashboard.filters["metricVariables"][0]["current"] == {"values": [], "all": True}
+
def test_suggestions_need_the_feature_flag(self) -> None:
self.flags.discard("metrics-suggested-dashboards")
@@ -186,6 +217,69 @@ class TestSuggestedDashboardsAPI(APIBaseTest):
assert Dashboard.objects_including_soft_deleted.get(id=preview_id).deleted
self.start_analysis.assert_called_once_with(self.team.id, force=True)
+ def test_approving_keeps_the_variables_of_the_preview_and_of_the_panels_it_left_out(self) -> None:
+ self.user.is_staff = True
+ self.user.save(update_fields=["is_staff"])
+ template = _template("generated-queue", "pending_review", "queue_depth", "unknown_total", source="generated")
+ _with_variable_filter(template.panels[0], "instance")
+ _with_variable_filter(template.panels[1], "job")
+ template.variables = [
+ attribute_variable(name="instance", label="Instance", attribute="instance"),
+ attribute_variable(name="job", label="Job", attribute="job"),
+ ]
+ template.source_team_id = self.team.id
+ template.save(update_fields=["panels", "variables", "source_team_id"])
+
+ preview = self.client.post(f"{self.base_url}/dashboard_templates/{template.id}/preview/")
+ dashboard = Dashboard.objects.get(id=preview.json()["dashboard_id"])
+ assert [variable["name"] for variable in dashboard.filters["metricVariables"]] == ["instance"]
+ # The reviewer renames the variable and selects an instance of this project, which other projects must not see.
+ edited = {
+ **attribute_variable(name="instance", label="Host", attribute="instance"),
+ "current": {"values": ["web-1"]},
+ }
+ dashboard.filters = {**dashboard.filters, "metricVariables": [edited]}
+ dashboard.save(update_fields=["filters"])
+ approved = self.client.post(f"{self.base_url}/dashboard_templates/{template.id}/approve/")
+
+ assert approved.status_code == status.HTTP_200_OK
+ template.refresh_from_db()
+ assert template.variables == [
+ attribute_variable(name="instance", label="Host", attribute="instance"),
+ attribute_variable(name="job", label="Job", attribute="job"),
+ ]
+
+ @parameterized.expand(
+ [
+ ("does_not_parse", {"name": "instance", "source": "invented"}),
+ (
+ "lists_values_of_the_preview_project",
+ {"name": "instance", "source": "custom", "customValues": ["web-1", "web-2"], "includeAll": True},
+ ),
+ ]
+ )
+ def test_approving_ignores_a_preview_variable_that_cannot_be_shared(
+ self, _name: str, preview_variable: dict[str, Any]
+ ) -> None:
+ self.user.is_staff = True
+ self.user.save(update_fields=["is_staff"])
+ template = _template("generated-queue", "pending_review", "queue_depth", source="generated")
+ _with_variable_filter(template.panels[0], "instance")
+ template.variables = [attribute_variable(name="instance", label="Instance", attribute="instance")]
+ template.source_team_id = self.team.id
+ template.save(update_fields=["panels", "variables", "source_team_id"])
+
+ preview = self.client.post(f"{self.base_url}/dashboard_templates/{template.id}/preview/")
+ dashboard = Dashboard.objects.get(id=preview.json()["dashboard_id"])
+ dashboard.filters = {**dashboard.filters, "metricVariables": [preview_variable]}
+ dashboard.save(update_fields=["filters"])
+ approved = self.client.post(f"{self.base_url}/dashboard_templates/{template.id}/approve/")
+
+ assert approved.status_code == status.HTTP_200_OK
+ template.refresh_from_db()
+ # The template keeps its own definition.
+ assert template.variables == [attribute_variable(name="instance", label="Instance", attribute="instance")]
+
@parameterized.expand(
[
("ready_for_review", ["queue_depth"], "pending_review", None),
@@ -376,3 +470,42 @@ class TestSuggestedDashboardsAPI(APIBaseTest):
assert isinstance(self.start_analysis, MagicMock)
assert self.start_analysis.call_count == 2
self.start_analysis.assert_called_with(self.team.id, force=True)
+
+
+class TestSuggestedDashboardVariableOptions(ClickhouseTestMixin, APIBaseTest):
+ def test_options_list_the_values_of_the_hour_that_holds_the_start_of_the_range(self) -> None:
+ truncate_metrics_tables()
+ # A range that starts after the hour still finds the samples in the bucket of that hour.
+ start = dt.datetime.now(dt.UTC).replace(minute=20, second=0, microsecond=0) - dt.timedelta(hours=2)
+ seed_metric(
+ team_id=self.team.id,
+ metric_name="queue_depth",
+ service_name="checkout",
+ points=[(start + dt.timedelta(minutes=5), 1.0)],
+ labels={"instance": "web-1"},
+ )
+ later = start + dt.timedelta(hours=1, minutes=10)
+ seed_metric(
+ team_id=self.team.id,
+ metric_name="queue_depth",
+ service_name="billing",
+ points=[(later, 1.0)],
+ labels={"instance": "web-2"},
+ )
+
+ def options(attribute: str) -> list[str]:
+ definition = MetricsDashboardVariable.model_validate(
+ attribute_variable(name="scope", label="Scope", attribute=attribute)
+ )
+ return list_variable_options(
+ team=self.team,
+ source=definition.source,
+ query=definition.query,
+ custom_values=None,
+ variables={},
+ date_from=start,
+ date_to=start + dt.timedelta(minutes=30),
+ )
+
+ assert options("instance") == ["web-1"]
+ assert options("service_name") == ["checkout"]