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): stop saving variable values on metrics insights (#128350)

Frank Hamandb9b0722d8bc5
diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.json
index b680bd7d7027..02a05f471968 100644
--- a/frontend/src/queries/schema.json
+++ b/frontend/src/queries/schema.json
@@ -39734,6 +39734,13 @@
"type": {
"const": "MetricsAlertConfig",
"type": "string"
+ },
+ "variables": {
+ "additionalProperties": {
+ "$ref": "#/definitions/MetricsDashboardVariableSelection"
+ },
+ "description": "Values for the `$name` variables in the insight's query.",
+ "type": "object"
}
},
"required": ["type"],
diff --git a/frontend/src/queries/schema/schema-general.ts b/frontend/src/queries/schema/schema-general.ts
index bb460da26f3c..44bd08e61b41 100644
--- a/frontend/src/queries/schema/schema-general.ts
+++ b/frontend/src/queries/schema/schema-general.ts
@@ -7219,6 +7219,8 @@ export interface MetricsAlertConfig {
type: 'MetricsAlertConfig'
/** When true, anchor on the trailing (possibly still accumulating) bucket instead of the last complete one. */
check_ongoing_interval?: boolean
+ /** Values for the `$name` variables in the insight's query. */
+ variables?: Record<string, MetricsDashboardVariableSelection>
}
/** One blocked period for quiet hours: 24-hour HH:MM in the project timezone; interval is half-open [start, end). */
diff --git a/posthog/schema.py b/posthog/schema.py
index c86533913bb5..14ec800ee3fb 100644
--- a/posthog/schema.py
+++ b/posthog/schema.py
@@ -2792,19 +2792,6 @@ class MetricChartSettings(BaseModel):
)
-class MetricsAlertConfig(BaseModel):
- model_config = ConfigDict(
- extra="forbid",
- )
- check_ongoing_interval: bool | None = Field(
- default=None,
- description=(
- "When true, anchor on the trailing (possibly still accumulating) bucket instead of the last complete one."
- ),
- )
- type: Literal["MetricsAlertConfig"] = "MetricsAlertConfig"
-
-
class MetricsDashboardVariableSelection(BaseModel):
model_config = ConfigDict(
extra="forbid",
@@ -7190,6 +7177,23 @@ class MetricPropertyFilter(BaseModel):
value: list[str | float | bool] | str | float | bool | None = None
+class MetricsAlertConfig(BaseModel):
+ model_config = ConfigDict(
+ extra="forbid",
+ )
+ check_ongoing_interval: bool | None = Field(
+ default=None,
+ description=(
+ "When true, anchor on the trailing (possibly still accumulating) bucket instead of the last complete one."
+ ),
+ )
+ type: Literal["MetricsAlertConfig"] = "MetricsAlertConfig"
+ variables: dict[str, MetricsDashboardVariableSelection] | None = Field(
+ default=None,
+ description="Values for the `$name` variables in the insight's query.",
+ )
+
+
class MetricsDashboardVariable(BaseModel):
model_config = ConfigDict(
extra="forbid",
diff --git a/posthog/tasks/alerts/test/test_metrics_alerts.py b/posthog/tasks/alerts/test/test_metrics_alerts.py
index 1a90f1259c50..fbd3fa1caae3 100644
--- a/posthog/tasks/alerts/test/test_metrics_alerts.py
+++ b/posthog/tasks/alerts/test/test_metrics_alerts.py
@@ -255,6 +255,31 @@ class TestMetricsAlerts(APIBaseTest, ClickhouseTestMixin):
breach_messages = mock_send_breaches.call_args.args[1]
assert any("big" in message for message in breach_messages), breach_messages
+ def test_alert_checks_with_its_own_variable_values(
+ self, mock_send_breaches: MagicMock, mock_send_errors: MagicMock, mock_feature_enabled: MagicMock
+ ) -> None:
+ self.seed_gauge({6: 5.0, 7: 5.0}, labels={"container": "small"})
+ self.seed_gauge({6: 50.0, 7: 50.0}, labels={"container": "big"})
+ insight = self.create_metrics_insight(
+ clauses=[
+ {
+ "name": "a",
+ "metricName": self.metric_name,
+ "aggregation": "avg",
+ "filters": [{"key": "container", "op": "eq", "value": "$container"}],
+ }
+ ]
+ )
+ alert = self.create_alert(
+ insight, upper=20.0, config={"type": "MetricsAlertConfig", "variables": {"container": {"values": ["big"]}}}
+ )
+
+ run_alert_check(alert["id"])
+
+ assert AlertConfiguration.objects.get(pk=alert["id"]).state == AlertState.FIRING
+ alert_check = AlertCheck.objects.filter(alert_configuration=alert["id"]).latest("created_at")
+ assert alert_check.calculated_value == 50.0
+
def test_formula_alert_evaluates_only_the_formula_series(
self, mock_send_breaches: MagicMock, mock_send_errors: MagicMock, mock_feature_enabled: MagicMock
) -> None:
diff --git a/products/alerts/backend/evaluation/metrics.py b/products/alerts/backend/evaluation/metrics.py
index b0f98e002511..75bdf3528f12 100644
--- a/products/alerts/backend/evaluation/metrics.py
+++ b/products/alerts/backend/evaluation/metrics.py
@@ -61,10 +61,19 @@ class MetricsExtractor:
check_ongoing_interval = bool(config.check_ongoing_interval)
+ query_override = (
+ {
+ **query,
+ "variables": {name: value.model_dump(exclude_none=True) for name, value in config.variables.items()},
+ }
+ if config.variables
+ else None
+ )
calculation_result = calculate_for_query_based_insight(
insight,
team=alert.team,
execution_mode=execution_mode,
+ query_override=query_override,
cache_age_seconds=cache_age_seconds,
# Scheduled alert check (no request user); attribute the read to the alert owner.
user=alert.created_by,
diff --git a/products/alerts/frontend/generated/api.schemas.ts b/products/alerts/frontend/generated/api.schemas.ts
index 4304bf55fb4a..2fe14da370cc 100644
--- a/products/alerts/frontend/generated/api.schemas.ts
+++ b/products/alerts/frontend/generated/api.schemas.ts
@@ -284,10 +284,23 @@ export const MetricsAlertConfigApiType = {
MetricsAlertConfig: 'MetricsAlertConfig',
} as const
+export interface MetricsDashboardVariableSelectionApi {
+ all?: boolean | null
+ /** The selected values. Empty when `all` is true: All stands for every option of the variable. */
+ values: string[]
+}
+
+/**
+ * Values for the `$name` variables in the insight's query.
+ */
+export type MetricsAlertConfigApiVariables = { [key: string]: MetricsDashboardVariableSelectionApi } | null
+
export interface MetricsAlertConfigApi {
/** When true, anchor on the trailing (possibly still accumulating) bucket instead of the last complete one. */
check_ongoing_interval?: boolean | null
type: MetricsAlertConfigApiType
+ /** Values for the `$name` variables in the insight's query. */
+ variables?: MetricsAlertConfigApiVariables
}
/**
diff --git a/products/alerts/frontend/generated/api.zod.ts b/products/alerts/frontend/generated/api.zod.ts
index 7d2405035d2d..a2913f5bca9e 100644
--- a/products/alerts/frontend/generated/api.zod.ts
+++ b/products/alerts/frontend/generated/api.zod.ts
@@ -1195,6 +1195,23 @@ export const AlertsSimulateCreateBody = /* @__PURE__ */ zod.object({
type: zod
.enum(['MetricsAlertConfig'])
.default(alertsSimulateCreateBodyConfigOneFourTypeDefault),
+ variables: zod
+ .union([
+ zod.record(
+ zod.string(),
+ zod.object({
+ all: zod.union([zod.boolean(), zod.null()]).optional(),
+ values: zod
+ .array(zod.string())
+ .describe(
+ 'The selected values. Empty when `all` is true: All stands for every option of the variable.'
+ ),
+ })
+ ),
+ zod.null(),
+ ])
+ .optional()
+ .describe("Values for the `$name` variables in the insight's query."),
}),
])
.describe(
diff --git a/products/metrics/backend/tests/test_metrics_query_runner.py b/products/metrics/backend/tests/test_metrics_query_runner.py
index 66637b8e1611..b4d3e507b186 100644
--- a/products/metrics/backend/tests/test_metrics_query_runner.py
+++ b/products/metrics/backend/tests/test_metrics_query_runner.py
@@ -233,6 +233,24 @@ class TestMetricsQueryRunner(ClickhouseTestMixin, APIBaseTest):
assert update_response.status_code == 200, update_response.json()
assert update_response.json()["query"] == response.json()["query"]
+ with_values = {**query, "variables": {"job": {"values": ["api"]}}}
+ rejected_create = self.client.post(
+ f"/api/projects/{self.team.pk}/insights/",
+ {"name": "queue depth", "saved": True, "query": with_values},
+ format="json",
+ headers=client_headers,
+ )
+ rejected_update = self.client.patch(
+ f"/api/projects/{self.team.pk}/insights/{response.json()['id']}/",
+ {"query": with_values},
+ format="json",
+ headers=client_headers,
+ )
+
+ for rejected in (rejected_create, rejected_update):
+ assert rejected.status_code == 400, rejected.json()
+ assert "can't store variable values" in str(rejected.json())
+
def test_validate_query_runner_access_granted(self) -> None:
query = MetricsQuery(clauses=[MetricsQueryClause(name="a", metricName="queue_depth", aggregation="sum")])
diff --git a/products/product_analytics/backend/presentation/insight.py b/products/product_analytics/backend/presentation/insight.py
index df596c71333b..5860e8a92765 100644
--- a/products/product_analytics/backend/presentation/insight.py
+++ b/products/product_analytics/backend/presentation/insight.py
@@ -521,6 +521,18 @@ class _InsightQuerySchema(RootModel):
) = PydanticField(discriminator="kind")
+_METRICS_QUERY_KINDS = frozenset({"MetricsQuery", "MetricsHistogramQuery"})
+METRICS_VARIABLE_VALUES_MESSAGE = (
+ "A saved metrics insight can't store variable values. Keep the $name references in the query, "
+ "and set the values with a dashboard variable or on the alert."
+)
+
+
+def _reject_metrics_variable_values(query: Any) -> None:
+ if isinstance(query, dict) and query.get("kind") in _METRICS_QUERY_KINDS and query.get("variables"):
+ raise ValidationError({"query": [METRICS_VARIABLE_VALUES_MESSAGE]})
+
+
@extend_schema_field(_InsightQuerySchema) # type: ignore[arg-type]
class QueryFieldSerializer(serializers.Serializer):
def to_representation(self, value):
@@ -723,6 +735,7 @@ class InsightSerializer(InsightBasicSerializer):
def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
query = attrs.get("query") if "query" in attrs else None
+ _reject_metrics_variable_values(query)
# `filters` is read-only, so DRF drops it before validation. The raw payload is the only
# place a legacy write still shows up, and it has to be answered rather than ignored.
sent_filters = self.initial_data.get("filters")
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index af563bb8f893..152af6ef701e 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -13170,10 +13170,17 @@ export namespace Schemas {
MetricsAlertConfig: 'MetricsAlertConfig',
} as const;
+ /**
+ * Values for the `$name` variables in the insight's query.
+ */
+ export type MetricsAlertConfigVariables = {[key: string]: MetricsDashboardVariableSelection} | null;
+
export interface MetricsAlertConfig {
/** When true, anchor on the trailing (possibly still accumulating) bucket instead of the last complete one. */
check_ongoing_interval?: boolean | null;
type: MetricsAlertConfigType;
+ /** Values for the `$name` variables in the insight's query. */
+ variables?: MetricsAlertConfigVariables;
}
/**
diff --git a/services/mcp/src/generated/alerts/api.ts b/services/mcp/src/generated/alerts/api.ts
index 056c13505463..8af79b813ac0 100644
--- a/services/mcp/src/generated/alerts/api.ts
+++ b/services/mcp/src/generated/alerts/api.ts
@@ -203,6 +203,23 @@ export const AlertsCreateBody = () => zod.object({
'When true, anchor on the trailing (possibly still accumulating) bucket instead of the last complete one.'
),
type: zod.enum(['MetricsAlertConfig']).default(alertsCreateBodyConfigOneFourTypeDefault),
+ variables: zod
+ .union([
+ zod.record(
+ zod.string(),
+ zod.object({
+ all: zod.union([zod.boolean(), zod.null()]).optional(),
+ values: zod
+ .array(zod.string())
+ .describe(
+ 'The selected values. Empty when `all` is true: All stands for every option of the variable.'
+ ),
+ })
+ ),
+ zod.null(),
+ ])
+ .optional()
+ .describe("Values for the `$name` variables in the insight's query."),
}),
])
.describe(
@@ -1543,6 +1560,23 @@ export const AlertsPartialUpdateBody = () => zod.object({
'When true, anchor on the trailing (possibly still accumulating) bucket instead of the last complete one.'
),
type: zod.enum(['MetricsAlertConfig']).default(alertsPartialUpdateBodyConfigOneFourTypeDefault),
+ variables: zod
+ .union([
+ zod.record(
+ zod.string(),
+ zod.object({
+ all: zod.union([zod.boolean(), zod.null()]).optional(),
+ values: zod
+ .array(zod.string())
+ .describe(
+ 'The selected values. Empty when `all` is true: All stands for every option of the variable.'
+ ),
+ })
+ ),
+ zod.null(),
+ ])
+ .optional()
+ .describe("Values for the `$name` variables in the insight's query."),
}),
])
.describe(
@@ -3908,6 +3942,23 @@ export const AlertsSimulateCreateBody = () => zod.object({
type: zod
.enum(['MetricsAlertConfig'])
.default(alertsSimulateCreateBodyConfigOneFourTypeDefault),
+ variables: zod
+ .union([
+ zod.record(
+ zod.string(),
+ zod.object({
+ all: zod.union([zod.boolean(), zod.null()]).optional(),
+ values: zod
+ .array(zod.string())
+ .describe(
+ 'The selected values. Empty when `all` is true: All stands for every option of the variable.'
+ ),
+ })
+ ),
+ zod.null(),
+ ])
+ .optional()
+ .describe("Values for the `$name` variables in the insight's query."),
}),
])
.describe(
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-create.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-create.json
index 292d563a30f0..5f0b37bab8ae 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-create.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-create.json
@@ -146,6 +146,43 @@
"default": "MetricsAlertConfig",
"enum": ["MetricsAlertConfig"],
"type": "string"
+ },
+ "variables": {
+ "anyOf": [
+ {
+ "additionalProperties": {
+ "properties": {
+ "all": {
+ "anyOf": [
+ {
+ "type": "boolean"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "values": {
+ "description": "The selected values. Empty when `all` is true: All stands for every option of the variable.",
+ "items": {
+ "type": "string"
+ },
+ "type": "array"
+ }
+ },
+ "required": ["values"],
+ "type": "object"
+ },
+ "propertyNames": {
+ "type": "string"
+ },
+ "type": "object"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "description": "Values for the `$name` variables in the insight's query."
}
},
"type": "object"
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-simulate.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-simulate.json
index 6fec5b55a0a2..4a37fe0fde3a 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-simulate.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-simulate.json
@@ -134,6 +134,43 @@
"default": "MetricsAlertConfig",
"enum": ["MetricsAlertConfig"],
"type": "string"
+ },
+ "variables": {
+ "anyOf": [
+ {
+ "additionalProperties": {
+ "properties": {
+ "all": {
+ "anyOf": [
+ {
+ "type": "boolean"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "values": {
+ "description": "The selected values. Empty when `all` is true: All stands for every option of the variable.",
+ "items": {
+ "type": "string"
+ },
+ "type": "array"
+ }
+ },
+ "required": ["values"],
+ "type": "object"
+ },
+ "propertyNames": {
+ "type": "string"
+ },
+ "type": "object"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "description": "Values for the `$name` variables in the insight's query."
}
},
"type": "object"
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-update.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-update.json
index 62fc65c27c9f..75bfb5636c6a 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-update.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-update.json
@@ -146,6 +146,43 @@
"default": "MetricsAlertConfig",
"enum": ["MetricsAlertConfig"],
"type": "string"
+ },
+ "variables": {
+ "anyOf": [
+ {
+ "additionalProperties": {
+ "properties": {
+ "all": {
+ "anyOf": [
+ {
+ "type": "boolean"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "values": {
+ "description": "The selected values. Empty when `all` is true: All stands for every option of the variable.",
+ "items": {
+ "type": "string"
+ },
+ "type": "array"
+ }
+ },
+ "required": ["values"],
+ "type": "object"
+ },
+ "propertyNames": {
+ "type": "string"
+ },
+ "type": "object"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "description": "Values for the `$name` variables in the insight's query."
}
},
"type": "object"