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.

fix(metrics): scope generated dashboard variables to the dashboard's metrics (#122059)

Frank Hamanddcbe7047a3e9
diff --git a/products/metrics/backend/suggested_dashboards/dashboards.py b/products/metrics/backend/suggested_dashboards/dashboards.py
index 3d8e52604548..b3653e267717 100644
--- a/products/metrics/backend/suggested_dashboards/dashboards.py
+++ b/products/metrics/backend/suggested_dashboards/dashboards.py
@@ -146,19 +146,27 @@ def build_tiles(panels: Sequence[TemplatePanel], catalog: MetricCatalog, *, refi
)
-def attribute_variable(*, name: str, label: str, attribute: str, multi: bool = False) -> dict[str, Any]:
+def _sql_string(value: str) -> str:
+ return "'" + value.replace("\\", "\\\\").replace("'", "\\'") + "'"
+
+
+def attribute_variable(
+ *, name: str, label: str, attribute: str, multi: bool = False, metric_names: Iterable[str] = ()
+) -> 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.
+ With `metric_names`, the options are only the values of those metrics, so the variable does not list the values
+ of other systems in the team. It selects All, so its filters match everything until the user picks a value.
"""
+ names = sorted(set(metric_names))
+ metric_filter = f" AND metric_name IN ({', '.join(map(_sql_string, names))})" if names else ""
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}"
+ query = f"SELECT DISTINCT service_name FROM posthog.metric_names WHERE {OPTIONS_RANGE}{metric_filter}"
else:
- key = attribute.replace("\\", "\\\\").replace("'", "\\'")
query = (
"SELECT DISTINCT attribute_value FROM posthog.metric_attributes "
- f"WHERE attribute_key = '{key}' AND {OPTIONS_RANGE}"
+ f"WHERE attribute_key = {_sql_string(attribute)} AND {OPTIONS_RANGE}{metric_filter}"
)
return MetricsDashboardVariable(
name=name,
diff --git a/products/metrics/backend/suggested_dashboards/generation.py b/products/metrics/backend/suggested_dashboards/generation.py
index edad7c177a1e..b66f459b8ba1 100644
--- a/products/metrics/backend/suggested_dashboards/generation.py
+++ b/products/metrics/backend/suggested_dashboards/generation.py
@@ -49,7 +49,7 @@ from products.metrics.backend.suggested_dashboards.dashboards import (
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.matching import query_metric_names, template_metric_names
from products.metrics.backend.suggested_dashboards.spec import (
DashboardDraft,
DraftPanel,
@@ -276,6 +276,33 @@ def _with_known_references(draft: DraftPanel, valid: _ValidVariables) -> DraftPa
return draft.model_copy(update={"query": draft.query.model_copy(update={"clauses": clauses})})
+def _variable_definitions(
+ variables: Sequence[DraftVariable], panels: Sequence[DraftPanel], queries: Mapping[str, dict[str, Any]]
+) -> dict[str, dict[str, Any]]:
+ """MetricsDashboardVariable JSON for each variable that a panel refers to.
+
+ Its options are only the values of the metrics of the panels that refer to it, not every value in the project.
+ """
+ metric_names: dict[str, set[str]] = {}
+ for panel in panels:
+ names = query_metric_names(queries[panel.key])
+ for clause in panel.query.clauses:
+ for item in clause.filters:
+ for name in referenced_names(item.value):
+ metric_names.setdefault(name, set()).update(names)
+ return {
+ variable.name: attribute_variable(
+ name=variable.name,
+ label=" ".join(variable.label.split())[:100],
+ attribute=variable.attribute,
+ multi=variable.multi,
+ metric_names=metric_names[variable.name],
+ )
+ for variable in variables
+ if variable.name in metric_names
+ }
+
+
def _convert(
drafts: Sequence[DraftPanel],
team: Team,
@@ -293,15 +320,6 @@ def _convert(
"""
valid = _valid_variables(variables, attributes or {}, checks)
valid_variables = valid.variables
- definitions = {
- variable.name: attribute_variable(
- name=variable.name,
- label=" ".join(variable.label.split())[:100],
- attribute=variable.attribute,
- multi=variable.multi,
- )
- for variable in valid_variables
- }
drafts = [_with_known_references(draft, valid) 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}
@@ -324,7 +342,8 @@ def _convert(
accepted.append(draft)
if checks is not None and accepted:
errors = checks.run_errors(
- {draft.key: queries_by_key[draft.key] for draft in accepted}, list(definitions.values())
+ {draft.key: queries_by_key[draft.key] for draft in accepted},
+ list(_variable_definitions(valid_variables, accepted, queries_by_key).values()),
)
dropped.extend(f"{draft.title}: {errors[draft.key]}" for draft in accepted if draft.key in errors)
accepted = [draft for draft in accepted if draft.key not in errors]
@@ -335,14 +354,8 @@ def _convert(
TemplatePanel(key=draft.key, title=draft.title[:200], query=queries_by_key[draft.key], layout=boxes[draft.key])
for draft in accepted
)
- 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)
+ definitions = _variable_definitions(valid_variables, accepted, queries_by_key)
+ kept_variables = tuple(variable for variable in valid_variables if variable.name in definitions)
return _Converted(
panels=panels,
accepted=tuple(accepted),
diff --git a/products/metrics/backend/tests/test_suggested_dashboards.py b/products/metrics/backend/tests/test_suggested_dashboards.py
index ec96d77a912b..b2563fa54d3b 100644
--- a/products/metrics/backend/tests/test_suggested_dashboards.py
+++ b/products/metrics/backend/tests/test_suggested_dashboards.py
@@ -269,6 +269,7 @@ class TestSuggestedDashboards(SimpleTestCase):
if attribute == "service_name"
else "SELECT DISTINCT attribute_value FROM posthog.metric_attributes"
)
+ assert (variable.query or "").endswith(" AND metric_name IN ('orders_total')")
def test_convert_drops_variables_with_unknown_attributes_or_no_panel(self) -> None:
converted = _convert(
@@ -397,6 +398,34 @@ class TestSuggestedDashboards(SimpleTestCase):
assert [panel.key for panel in converted.panels] == ["orders"]
assert converted.dropped == ("queue: Bad regex",)
+ def test_convert_lists_only_the_values_of_the_metrics_of_the_panels_that_refer_to_a_variable(self) -> None:
+ scoped = [BuilderFilter(key="instance", op="eq", value="$instance")]
+ converted = _convert(
+ [
+ self._draft("orders", "orders_total", 0, filters=scoped),
+ self._draft("queue", "queue_depth", 4),
+ self._draft("latency", "request_duration_seconds", 8, filters=scoped),
+ ],
+ Team(id=1),
+ CATALOG,
+ variables=[DraftVariable(name="instance", label="Instance", attribute="instance")],
+ attributes={"orders_total": ["instance"]},
+ checks=_Checks({"instance": 3}, errors={"latency": "Bad regex"}),
+ )
+
+ assert [panel.key for panel in converted.panels] == ["orders", "queue"]
+ assert converted.dropped == ("latency: Bad regex",)
+ assert converted.variables[0]["query"].endswith(" AND metric_name IN ('orders_total')")
+
+ def test_attribute_variable_quotes_the_attribute_and_the_metric_names(self) -> None:
+ variable = attribute_variable(name="scope", label="", attribute="it's", metric_names=["b\\'x", "a", "a"])
+
+ assert variable["query"] == (
+ "SELECT DISTINCT attribute_value FROM posthog.metric_attributes WHERE attribute_key = 'it\\'s' "
+ "AND time_bucket >= toStartOfHour($__from) AND time_bucket < $__to "
+ "AND metric_name IN ('a', 'b\\\\\\'x')"
+ )
+
@parameterized.expand(
[
("one_value", 1, 3, False),