: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(logs): add logs-group-by and service health compare for agents (#119978)
Daniel Visca04326b8ecae8
diff --git a/products/logs/backend/agent_window.py b/products/logs/backend/agent_window.pynew file mode 100644index 000000000000..586c9711dd4a--- /dev/null+++ b/products/logs/backend/agent_window.py@@ -0,0 +1,19 @@+import datetime as dt+++def utc_iso(value: dt.datetime) -> str:+ if value.tzinfo is None:+ value = value.replace(tzinfo=dt.UTC)+ value = value.astimezone(dt.UTC)+ timespec = "seconds" if value.microsecond == 0 else "milliseconds"+ return value.replace(tzinfo=None).isoformat(timespec=timespec) + "Z"+++def window_echo(+ current: tuple[dt.datetime, dt.datetime], baseline: tuple[dt.datetime, dt.datetime] | None = None+) -> dict:+ window: dict = {"current": {"from": utc_iso(current[0]), "to": utc_iso(current[1])}}+ if baseline is not None:+ window["baseline"] = {"from": utc_iso(baseline[0]), "to": utc_iso(baseline[1])}+ window["timezone"] = "UTC"+ return windowdiff --git a/products/logs/backend/compare_windows.py b/products/logs/backend/compare_windows.pynew file mode 100644index 000000000000..df3cb38bb8ce--- /dev/null+++ b/products/logs/backend/compare_windows.py@@ -0,0 +1,18 @@+import datetime as dt++from posthog.dataclasses import frozen++_SHIFTS = {"previous_day": dt.timedelta(days=1), "previous_week": dt.timedelta(days=7)}+++@frozen+class Window:+ date_from: dt.datetime+ date_to: dt.datetime+++def baseline_bounds(compare: str, date_from: dt.datetime, date_to: dt.datetime) -> Window | None:+ if compare == "previous_period":+ return Window(date_from=date_from - (date_to - date_from), date_to=date_from)+ shift = _SHIFTS.get(compare)+ return Window(date_from=date_from - shift, date_to=date_to - shift) if shift else Nonediff --git a/products/logs/backend/environment_filter.py b/products/logs/backend/environment_filter.pynew file mode 100644index 000000000000..3e45789691fa--- /dev/null+++ b/products/logs/backend/environment_filter.py@@ -0,0 +1,33 @@+from posthog.hogql import ast++ENVIRONMENT_KEYS: tuple[str, ...] = ("deployment.environment.name", "deployment.environment", "env")+UNLABELED_ENVIRONMENT = "(none)"+++def _resource_attribute(key: str) -> ast.Expr:+ return ast.Call(name="arrayElement", args=[ast.Field(chain=["resource_attributes"]), ast.Constant(value=key)])+++def environment_expr() -> ast.Expr:+ expr = _resource_attribute(ENVIRONMENT_KEYS[-1])+ for key in reversed(ENVIRONMENT_KEYS[:-1]):+ is_set = ast.CompareOperation(+ op=ast.CompareOperationOp.NotEq, left=_resource_attribute(key), right=ast.Constant(value="")+ )+ expr = ast.Call(name="if", args=[is_set, _resource_attribute(key), expr])+ return expr+++def _matches(column: ast.Expr, environment: str) -> ast.Expr:+ value = "" if environment.strip() == UNLABELED_ENVIRONMENT else environment.strip().lower()+ return ast.CompareOperation(+ op=ast.CompareOperationOp.Eq, left=ast.Call(name="lower", args=[column]), right=ast.Constant(value=value)+ )+++def environment_filter_expr(environment: str) -> ast.Expr:+ return _matches(environment_expr(), environment)+++def rollup_environment_filter_expr(environment: str) -> ast.Expr:+ return _matches(ast.Field(chain=["environment"]), environment)diff --git a/products/logs/backend/group_by_query_runner.py b/products/logs/backend/group_by_query_runner.pyindex a5ca4c4c18a9..291b60aae6b9 100644--- a/products/logs/backend/group_by_query_runner.py+++ b/products/logs/backend/group_by_query_runner.py@@ -1,6 +1,7 @@from collections.abc import Sequence+from datetime import UTC, datetime, timedeltafrom functools import cached_property-from typing import TYPE_CHECKING, NamedTuple+from typing import TYPE_CHECKING, Any, NamedTuplefrom zoneinfo import ZoneInfofrom django.db import models@@ -31,6 +32,8 @@ MAX_EXECUTION_TIME = 60DEFAULT_GROUP_LIMIT = 100MAX_GROUP_LIMIT = 500+RESULT_CACHE_SECONDS = 60+# Cap on combined group-by dimensions: each extra dimension multiplies group cardinality# and widens the GROUP BY tuple, while the scan cost stays roughly flat.MAX_GROUP_DIMENSIONS = 4@@ -40,8 +43,10 @@ MAX_GROUP_DIMENSIONS = 4# base64-encoded; hex is what users see in trace UIs.GROUPABLE_COLUMNS: dict[str, str] = {"severity_level": "severity_text",+ "service_name": "service_name","trace_id": "hex(tryBase64Decode(trace_id))","span_id": "hex(tryBase64Decode(span_id))",+ "pattern": "pattern",}@@ -83,9 +88,9 @@ class LogsGroupByQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRcollecting `groupArray(N)` + `count()` + `sum()` in the outer select — the sameshape LogAttributesQueryRunner uses for its keys-only path.- The group-by parameters are runner constructor args, not query-model fields, so the- runner must be invoked via `calculate()` (which the logs API does) — never through- the caching `run()` path, where the cache key is derived from the query model alone.+ The group-by parameters are runner constructor args, not query-model fields, so+ `get_cache_payload` adds them to the cache key. Cached results go stale after+ RESULT_CACHE_SECONDS, whatever the window."""query: LogsQuery@@ -127,6 +132,24 @@ class LogsGroupByQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRraise UserAccessControlError("logs", "viewer")+ def get_cache_payload(self) -> dict[str, Any]:+ return {+ **super().get_cache_payload(),+ "group_bys": [list(dimension) for dimension in self.group_bys],+ "order_groups_by": self.order_groups_by,+ "group_limit": self.group_limit,+ }++ def cache_target_age(self, last_refresh: datetime | None, lazy: bool = False) -> datetime | None:+ if last_refresh is None:+ return None+ return last_refresh + timedelta(seconds=RESULT_CACHE_SECONDS)++ def _is_stale(self, last_refresh: datetime | None, lazy: bool = False) -> bool:+ if last_refresh is None:+ return True+ return datetime.now(UTC) > last_refresh + timedelta(seconds=RESULT_CACHE_SECONDS)+@cached_propertydef settings(self) -> HogQLGlobalSettings:return HogQLGlobalSettings(@@ -194,6 +217,7 @@ class LogsGroupByQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryR{", ".join(scan_cols)},count() AS log_count,countIf(lower(severity_text) IN ('error', 'fatal')) AS error_count,+ min(timestamp) AS first_seen,max(timestamp) AS last_seenFROM logsWHERE {{where}}@@ -241,6 +265,7 @@ class LogsGroupByQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryR{", ".join(grouped_cols)},sum(agg.log_count) AS log_count,sum(agg.error_count) AS error_count,+ min(agg.first_seen) AS first_seen,max(agg.last_seen) AS last_seenFROM ({scan_sql}) AS agg{" ".join(join_sql_parts)}@@ -253,12 +278,12 @@ class LogsGroupByQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRquery = parse_select(f"""SELECT- groupArray({self.group_limit})((tuple({", ".join(aliases)}), log_count, error_count, last_seen)) AS groups,+ groupArray({self.group_limit})((tuple({", ".join(aliases)}), log_count, error_count, first_seen, last_seen)) AS groups,coalesce(any(group_count), 0) AS total_groups,coalesce(any(log_count_sum), 0) AS total_logsFROM (SELECT- {", ".join(aliases)}, log_count, error_count, last_seen,+ {", ".join(aliases)}, log_count, error_count, first_seen, last_seen,{_TOTALS_WINDOW}FROM ({grouped_sql})ORDER BY {{order_field}} DESC, {", ".join(f"{alias} ASC" for alias in aliases)}@@ -317,9 +342,10 @@ class LogsGroupByQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryR"values": list(values),"log_count": int(log_count),"error_count": int(error_count),+ "first_seen": first_seen.replace(tzinfo=ZoneInfo("UTC")).isoformat(),"last_seen": last_seen.replace(tzinfo=ZoneInfo("UTC")).isoformat(),}- for values, log_count, error_count, last_seen in groups_raw+ for values, log_count, error_count, first_seen, last_seen in groups_raw]return LogsQueryResponse(results={diff --git a/products/logs/backend/presentation/views/api.py b/products/logs/backend/presentation/views/api.pyindex cccab9acb285..7e894f31ee6b 100644--- a/products/logs/backend/presentation/views/api.py+++ b/products/logs/backend/presentation/views/api.py@@ -77,7 +77,13 @@ from products.logs.backend.presentation.views.metric_rules_api import LogsMetricfrom products.logs.backend.presentation.views.retention_api import LogsRetentionRuleViewSetfrom products.logs.backend.presentation.views.sampling_api import LogsSamplingRuleViewSetfrom products.logs.backend.presentation.views.views_api import LogsViewViewSet-from products.logs.backend.services_query_runner import ServicesQueryRunner+from products.logs.backend.services_query_runner import (+ COMPARE_MODES,+ LogsCompareMode,+ LogsServicesSource,+ LogsServiceStatus,+ ServicesQueryRunner,+)from products.logs.backend.sparkline_query_runner import SparklineQueryRunner__all__ = [@@ -282,7 +288,11 @@ class _LogsValuesQuerySerializer(serializers.Serializer):required=False,help_text='Type of attribute: "log" or "resource". Defaults to "log".',)- value = serializers.CharField(required=False, help_text="Search filter for attribute values")+ value = serializers.CharField(+ required=False,+ allow_blank=True,+ help_text="Search filter for attribute values. Omit it or pass an empty string to list all values.",+ )dateRange = _DateRangeSerializer(required=False,help_text="Date range to search within. Defaults to last hour.",@@ -695,10 +705,29 @@ class _LogsServicesBodySerializer(serializers.Serializer):default=list,help_text="Property filters for the query.",)+ environment = serializers.CharField(+ required=False,+ max_length=200,+ help_text=(+ "Deployment environment, matched without case against the first set resource attribute of "+ 'deployment.environment.name, deployment.environment and env. "(none)" matches logs without one.'+ ),+ )+ compare = serializers.ChoiceField(+ choices=LogsCompareMode.choices,+ default="none",+ help_text=(+ "Baseline window for per-service status: the window just before, the same window a day or a week "+ "earlier, or none. Silent services (logs in the baseline, none now) only appear with a baseline."+ ),+ )class _LogsServicesRequestSerializer(serializers.Serializer):query = _LogsServicesBodySerializer(help_text="The services aggregation query to execute.")+ baselineDateRange = _DateRangeSerializer(+ required=False, help_text="Explicit baseline window. Takes precedence over `query.compare`."+ )class _LogEntrySerializer(serializers.Serializer):@@ -808,6 +837,32 @@ class _LogsServiceAggregateSerializer(serializers.Serializer):service_name = serializers.CharField(help_text='Service name, or "(no value)" / "(no service)" placeholder for unset entries.',)+ status = serializers.ChoiceField(+ required=False,+ choices=LogsServiceStatus.choices,+ help_text=(+ "ok, degraded (error rate of 5% or more), regressed or improved (error rate up or down by 1 pp and 2x), "+ "recovered (back under 5%), new (no baseline logs) or silent (baseline logs, none in the window)."+ ),+ )+ reason = serializers.CharField(+ required=False, help_text='Short reason for the status, e.g. "error rate 0.4% → 3.1%".'+ )+ error_rate_pct = serializers.FloatField(required=False, help_text="Error rate in percent, rounded to 1 decimal.")+ baseline_log_count = serializers.IntegerField(+ required=False, allow_null=True, help_text="Logs in the baseline. Null without one."+ )+ error_rate_delta_pp = serializers.FloatField(+ required=False, allow_null=True, help_text="Error rate change against the baseline, in percentage points."+ )+ log_count_change_pct = serializers.FloatField(+ required=False, allow_null=True, help_text="Log volume change against the baseline, in percent."+ )+ last_seen = serializers.CharField(+ required=False,+ allow_null=True,+ help_text="Latest log time (UTC). From the rollup, and for silent services, the start of the last 5-minute bucket.",+ )log_count = serializers.IntegerField(help_text="Total log entries from this service in the window.")error_count = serializers.IntegerField(help_text='Count of logs at severity "error" or "fatal".',@@ -843,6 +898,31 @@ class _LogsServicesSummarySerializer(serializers.Serializer):top_services_volume_share_pct = serializers.FloatField(help_text="Combined volume share (percent) of the top services by log_count.",)+ status_counts = serializers.DictField(+ required=False, child=serializers.IntegerField(), help_text="Service rows per status."+ )+ truncated = serializers.BooleanField(+ required=False, help_text="True when more services matched than `services` holds."+ )+++class _LogsWindowSerializer(serializers.Serializer):+ current = serializers.DictField(+ child=serializers.CharField(), help_text="`from` and `to` of the queried window, ISO 8601 UTC."+ )+ baseline = serializers.DictField(+ child=serializers.CharField(), required=False, help_text="`from` and `to` of the baseline. Absent without one."+ )+ timezone = serializers.CharField(help_text="Always UTC.")+++class _LogsNextStepSerializer(serializers.Serializer):+ tool = serializers.CharField(help_text="MCP tool to call next.")+ why = serializers.CharField(help_text="What the call answers.")+ query = serializers.DictField(help_text="Prefilled `query` for that tool.")+ baselineDateRange = serializers.DictField(+ required=False, help_text="Prefilled sibling `baselineDateRange` argument, for tools that take one."+ )class _LogsServicesResponseSerializer(serializers.Serializer):@@ -868,6 +948,15 @@ class _LogsServicesResponseSerializer(serializers.Serializer):required=False,help_text="Roll-up stats for the Services tab header.",)+ window = _LogsWindowSerializer(required=False, help_text="Absolute UTC bounds of the window and the baseline.")+ source = serializers.ChoiceField( # type: ignore[assignment]+ required=False,+ choices=LogsServicesSource.choices,+ help_text="rollup: 5-minute rollup only. rollup+raw: rollup plus raw logs at unaligned window edges.",+ )+ next = _LogsNextStepSerializer(+ required=False, many=True, help_text="Prefilled follow-up calls for problem services."+ )class _LogsPatternsBodySerializer(serializers.Serializer):@@ -1198,7 +1287,7 @@ class _LogsGroupByDimensionSerializer(serializers.Serializer):help_text=('The key this dimension groups by — an attribute key (e.g. "session_id", "service.name") ''or, when source is "column", one of the top-level log fields: '- '"severity_level", "trace_id", "span_id".'+ '"severity_level", "service_name", "trace_id", "span_id", "pattern" (the masked message template).'),)source = serializers.ChoiceField( # type: ignore[assignment] # field named `source` shadows DRF Field.source@@ -1242,7 +1331,7 @@ class _LogsGroupByBodySerializer(serializers.Serializer):help_text=('The key to group logs by — an attribute key (e.g. "session_id", "service.name") or, ''when groupBySource is "column", one of the top-level log fields: '- '"severity_level", "trace_id", "span_id". Ignored when groupBys is provided.'+ '"severity_level", "service_name", "trace_id", "span_id", "pattern". Ignored when groupBys is provided.'),)groupBySource = serializers.ChoiceField(@@ -1299,6 +1388,9 @@ class _LogsGroupByGroupSerializer(serializers.Serializer):error_count = serializers.IntegerField(help_text='Number of matching logs in this group at severity "error" or "fatal".',)+ first_seen = serializers.CharField(+ required=False, help_text="ISO 8601 timestamp of the earliest matching log in this group."+ )last_seen = serializers.CharField(help_text="ISO 8601 timestamp of the most recent matching log in this group.")@@ -1782,10 +1874,21 @@ class LogsViewSet(TeamAndOrgViewSetMixin, PydanticModelMixin, viewsets.ViewSet):filterGroup=self._normalize_filter_group(query_data.get("filterGroup", None)),)+ compare = query_data.get("compare") or "none"+ if compare not in COMPARE_MODES:+ return Response(+ {"error": f"compare must be one of {', '.join(COMPARE_MODES)}"}, status=status.HTTP_400_BAD_REQUEST+ )+ if not isinstance(query_data.get("environment") or "", str):+ return Response({"error": "environment must be a string"}, status=status.HTTP_400_BAD_REQUEST)+ baseline_data = request.data.get("baselineDateRange")runner = ServicesQueryRunner(team=self.team,query=query,service_name_search=query_data.get("serviceNameSearch") or None,+ environment=query_data.get("environment") or None,+ compare=compare,+ baseline_date_range=self.get_model(baseline_data, DateRange) if baseline_data else None,)response = runner.run(ExecutionMode.CALCULATE_BLOCKING_ALWAYS,@@ -1919,9 +2022,11 @@ class LogsViewSet(TeamAndOrgViewSetMixin, PydanticModelMixin, viewsets.ViewSet):except (ValueError, TypeError) as e:return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)- # calculate() bypasses the query cache on purpose: the group-by parameters are runner- # args, not query-model fields, so they can't participate in the cache key.- response = runner.calculate()+ response = runner.run(+ ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE,+ analytics_props=get_request_analytics_properties(request),+ )+ assert isinstance(response, LogsQueryResponse | CachedLogsQueryResponse)results = response.results if isinstance(response.results, dict) else {}report_user_action(diff --git a/products/logs/backend/services_query_runner.py b/products/logs/backend/services_query_runner.pyindex c2352b88e535..a137be6d56df 100644--- a/products/logs/backend/services_query_runner.py+++ b/products/logs/backend/services_query_runner.py@@ -1,17 +1,25 @@+import datetime as dt+from collections.abc import Sequencefrom functools import cached_propertyfrom typing import Anyimport re2-from posthog.schema import CachedLogsQueryResponse, LogsQuery+from posthog.schema import CachedLogsQueryResponse, DateRange, LogsQueryfrom posthog.hogql import astfrom posthog.hogql.parser import parse_expr, parse_selectfrom posthog.hogql.query import execute_hogql_queryfrom posthog.clickhouse.client.connection import Workload+from posthog.dataclasses import frozen+from posthog.enums import LabeledStrEnumfrom posthog.hogql_queries.query_runner import AnalyticsQueryRunner+from posthog.hogql_queries.utils.caller_context import map_in_caller_context+from products.logs.backend.agent_window import utc_iso, window_echo+from products.logs.backend.compare_windows import baseline_bounds+from products.logs.backend.environment_filter import environment_filter_expr, rollup_environment_filter_exprfrom products.logs.backend.logs_query_runner import LogsQueryResponse, LogsQueryRunnerMixin, ilike_patternfrom products.logs.backend.models import LogsExclusionRule@@ -176,25 +184,151 @@ SERVICES_LIMIT = 10000# most recent buckets if scoped to all SERVICES_LIMIT names at once.SPARKLINE_SERVICES_LIMIT = 25+MIN_LOGS_FOR_RATE = 100+DEGRADED_ERROR_RATE_PCT = 5.0+CHANGE_MIN_DELTA_PP = 1.0+CHANGE_MIN_RATIO = 2.0+SILENT_MIN_BASELINE_LOGS = 10-class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunnerMixin):- """Returns per-service aggregates (volume, error count, error rate) and sparkline data."""+class LogsCompareMode(LabeledStrEnum):+ NONE = "none", "No baseline"+ PREVIOUS_PERIOD = "previous_period", "Previous period"+ PREVIOUS_DAY = "previous_day", "Same window a day earlier"+ PREVIOUS_WEEK = "previous_week", "Same window a week earlier"+++COMPARE_MODES = LogsCompareMode.values+++class LogsServiceStatus(LabeledStrEnum):+ OK = "ok", "OK"+ DEGRADED = "degraded", "Error rate 5% or more"+ REGRESSED = "regressed", "Error rate worse than the baseline"+ IMPROVED = "improved", "Error rate better than the baseline"+ RECOVERED = "recovered", "No longer degraded"+ NEW = "new", "No logs in the baseline"+ SILENT = "silent", "Logs stopped"+++class LogsServicesSource(LabeledStrEnum):+ ROLLUP = "rollup"+ ROLLUP_RAW = "rollup+raw"+ RAW = "raw"+++SERVICE_STATUSES = LogsServiceStatus.values+ROLLUP_BUCKET_SECONDS = 300+ROLLUP_RETENTION = dt.timedelta(days=42)++_COUNT_COLUMNS = ("log_count", "error_count", "debug", "info", "warn")+_NO_LOGS: dict = {**dict.fromkeys(_COUNT_COLUMNS, 0), "last_seen": None}+++def _error_rate_pct(counts: dict | None) -> float:+ return 100.0 * counts["error_count"] / counts["log_count"] if counts and counts["log_count"] else 0.0+++@frozen+class ServiceVerdict:+ status: str+ reason: str+++@frozen+class _WindowCounts:+ counts: dict[str, dict]+ total_services: int+ source: str+++def classify_service(current: dict | None, baseline: dict | None, compared: bool) -> ServiceVerdict:+ log_count = current["log_count"] if current else 0+ rate = _error_rate_pct(current)+ base_count = baseline["log_count"] if baseline else 0+ if compared and not base_count:+ return ServiceVerdict(status="new", reason=f"no logs in baseline, {log_count} now")+ if compared and not log_count:+ return ServiceVerdict(status="silent", reason=f"no logs in window (baseline {base_count})")+ if compared and min(log_count, base_count) >= MIN_LOGS_FOR_RATE:+ base_rate = _error_rate_pct(baseline)+ change = f"error rate {base_rate:.1f}% → {rate:.1f}%"+ if rate - base_rate >= CHANGE_MIN_DELTA_PP and rate >= CHANGE_MIN_RATIO * base_rate:+ return ServiceVerdict(status="regressed", reason=change)+ if base_rate - rate >= CHANGE_MIN_DELTA_PP and base_rate >= CHANGE_MIN_RATIO * rate:+ return ServiceVerdict(+ status="recovered" if base_rate >= DEGRADED_ERROR_RATE_PCT > rate else "improved", reason=change+ )+ if log_count >= MIN_LOGS_FOR_RATE and rate >= DEGRADED_ERROR_RATE_PCT:+ return ServiceVerdict(status="degraded", reason=f"error rate {rate:.1f}%")+ return ServiceVerdict(status="ok", reason=f"error rate {rate:.1f}%")+++def _floor_to_bucket(value: dt.datetime) -> dt.datetime:+ seconds = int(value.timestamp()) // ROLLUP_BUCKET_SECONDS * ROLLUP_BUCKET_SECONDS+ return dt.datetime.fromtimestamp(seconds, tz=dt.UTC)+++def _counts_by_service(rows: list) -> tuple[dict[str, dict], int]:+ counts = {+ row[0]: {**dict(zip(_COUNT_COLUMNS, (int(v) for v in row[1:6]))), "last_seen": row[7].replace(tzinfo=dt.UTC)}+ for row in rows+ }+ return counts, int(rows[0][6]) if rows else 0+++class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunnerMixin):query: LogsQuerycached_response: CachedLogsQueryResponse- def __init__(self, *args: Any, service_name_search: str | None = None, **kwargs: Any) -> None:+ def __init__(+ self,+ *args: Any,+ service_name_search: str | None = None,+ environment: str | None = None,+ compare: str = "none",+ baseline_date_range: DateRange | None = None,+ **kwargs: Any,+ ) -> None:super().__init__(*args, **kwargs)self.service_name_search = service_name_search.strip() if service_name_search else None+ self.environment = environment.strip() if environment and environment.strip() else None+ self.compare = compare+ self.baseline_date_range = baseline_date_range+ self._pristine_scope = self._scope()def get_cache_payload(self) -> dict[str, Any]:- # A runner argument, not a query field, so the base payload cannot see it.- return {**super().get_cache_payload(), "service_name_search": self.service_name_search}+ return {+ **super().get_cache_payload(),+ "service_name_search": self.service_name_search,+ "environment": self.environment,+ "compare": self.compare,+ "baseline_date_range": self.baseline_date_range.model_dump() if self.baseline_date_range else None,+ }- def _calculate(self) -> LogsQueryResponse:- aggregates_response = execute_hogql_query(+ @cached_property+ def _baseline_runner(self) -> "ServicesQueryRunner | None":+ date_range = self.baseline_date_range+ if date_range is None:+ bounds = baseline_bounds(self.compare, self.query_date_range.date_from(), self.query_date_range.date_to())+ if bounds is None:+ return None+ date_range = DateRange(+ date_from=bounds.date_from.isoformat(), date_to=bounds.date_to.isoformat(), explicitDate=True+ )+ return ServicesQueryRunner(+ team=self.team,+ query=self.query.model_copy(deep=True, update={"dateRange": date_range}),+ service_name_search=self.service_name_search,+ environment=self.environment,+ limit_context=self.limit_context,+ modifiers=self.modifiers,+ )++ def _execute(self, query: ast.SelectQuery) -> list:+ return execute_hogql_query(query_type="LogsQuery",- query=self._aggregates_query(),+ query=query,modifiers=self.modifiers,team=self.team,workload=Workload.LOGS,@@ -202,7 +336,51 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunnlimit_context=self.limit_context,filters=self.query_date_range.to_hogql_filters(),settings=self.settings,+ ).results++ def _use_rollup(self) -> bool:+ query = self.query+ groups = query.filterGroup.values if query.filterGroup else []+ if any(getattr(group, "values", None) for group in groups) or query.searchTerm or query.resourceFingerprint:+ return False+ if query.personId is not None or query.sessionId is not None:+ return False+ return self.query_date_range.date_from() >= dt.datetime.now(dt.UTC) - ROLLUP_RETENTION++ def _window_counts(self) -> _WindowCounts:+ date_from, date_to = self.query_date_range.date_from(), self.query_date_range.date_to()+ start = _floor_to_bucket(date_from)+ start = start if start == date_from else start + dt.timedelta(seconds=ROLLUP_BUCKET_SECONDS)+ end = _floor_to_bucket(date_to)+ if start >= end or not self._use_rollup():+ counts, total = _counts_by_service(self._execute(self._aggregates_query()))+ return _WindowCounts(counts=counts, total_services=total, source="raw")+ counts, total = _counts_by_service(self._execute(self._rollup_query(start, end)))+ edges = [(lower, upper) for lower, upper in ((date_from, start), (end, date_to)) if lower < upper]+ if not edges:+ return _WindowCounts(counts=counts, total_services=total, source="rollup")+ edge_counts, edge_total = _counts_by_service(self._execute(self._aggregates_query(edges)))+ if max(len(counts), len(edge_counts)) >= SERVICES_LIMIT:+ counts, total = _counts_by_service(self._execute(self._aggregates_query()))+ return _WindowCounts(counts=counts, total_services=total, source="raw")+ for name, edge in edge_counts.items():+ merged = counts.setdefault(name, {**edge, **dict.fromkeys(_COUNT_COLUMNS, 0)})+ for column in _COUNT_COLUMNS:+ merged[column] += edge[column]+ merged["last_seen"] = max(merged["last_seen"], edge["last_seen"])+ ordered = dict(sorted(counts.items(), key=lambda item: -item[1]["log_count"])[:SERVICES_LIMIT])+ return _WindowCounts(counts=ordered, total_services=max(total, edge_total, len(counts)), source="rollup+raw")++ def _calculate(self) -> LogsQueryResponse:+ baseline_runner = self._baseline_runner+ runners = [self, baseline_runner] if baseline_runner else [self]+ windows = map_in_caller_context(+ lambda runner: runner._window_counts(), runners, thread_name_prefix="logs_services")+ current, total_services, source = windows[0].counts, windows[0].total_services, windows[0].source+ baseline = windows[1].counts if baseline_runner else None+ current_cut = len(current) >= SERVICES_LIMIT+ baseline_cut = baseline is not None and len(baseline) >= SERVICES_LIMIT# Sparklines cover only the top services by volume; callers wanting# trends for other rows re-request with `serviceNames` scoped to the@@ -210,39 +388,27 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunn# filter prunes the scan, and the bounded name list keeps the grid of# time × service rows under the sparkline query's row LIMIT (which# truncates the most recent buckets first).- top_service_names = [row[0] for row in aggregates_response.results[:SPARKLINE_SERVICES_LIMIT]]- sparkline_rows: list[Any] = []- if top_service_names:- sparkline_response = execute_hogql_query(- query_type="LogsQuery",- query=self._sparkline_query(top_service_names),- modifiers=self.modifiers,- team=self.team,- workload=Workload.LOGS,- timings=self.timings,- limit_context=self.limit_context,- filters=self.query_date_range.to_hogql_filters(),- settings=self.settings,- )- sparkline_rows = sparkline_response.results+ top_service_names = list(current)[:SPARKLINE_SERVICES_LIMIT]+ sparkline_rows = self._execute(self._sparkline_query(top_service_names)) if top_service_names else []- # True distinct-service count, unaffected by SERVICES_LIMIT — the UI- # uses it to disclose truncation. The window function counts the groups- # before LIMIT applies, so every row carries the same total.- total_services = int(aggregates_response.results[0][6]) if aggregates_response.results else 0+ silent_names = [+ name+ for name, counts in (baseline or {}).items()+ if name not in current and counts["log_count"] >= SILENT_MIN_BASELINE_LOGS and not current_cut+ ]+ silent_last_seen = self._last_seen_in_rollup(silent_names) if silent_names and self._use_rollup() else {}enabled_rules = list(LogsExclusionRule.objects.filter(team_id=self.team.pk, enabled=True).order_by("priority", "created_at"))services = []- for row in aggregates_response.results:- service_name = row[0] if row[0] else "(no service)"- log_count = row[1]- error_count = row[2]- sev_debug = row[3]- sev_info = row[4]- sev_warn = row[5]+ for name in [*current, *silent_names]:+ counts = current.get(name, _NO_LOGS)+ base = baseline.get(name, _NO_LOGS) if baseline is not None else None+ service_name = name if name else "(no service)"+ log_count = counts["log_count"]+ error_count = counts["error_count"]error_rate = error_count / log_count if log_count > 0 else 0.0active_rules = []for rule in enabled_rules:@@ -263,17 +429,36 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunn"summary_string": _sampling_rule_summary(rule),})+ verdict = classify_service(+ counts, base, baseline is not None and not (baseline_cut and name not in baseline)+ )+ status, reason = verdict.status, verdict.reason+ last_seen = counts["last_seen"] or silent_last_seen.get(name)+ base_count = base["log_count"] if base else None+ if status == "silent" and last_seen:+ reason = f"no logs since {utc_iso(last_seen)[5:16]}Z (baseline {base_count})"services.append({"service_name": service_name,+ "status": status,+ "reason": reason,"log_count": log_count,"error_count": error_count,"error_rate": round(error_rate, 4),+ "error_rate_pct": round(100 * error_rate, 1),+ "baseline_log_count": base_count,+ "error_rate_delta_pp": round(_error_rate_pct(counts) - _error_rate_pct(base), 1)+ if base_count and log_count+ else None,+ "log_count_change_pct": round(100.0 * (log_count - base_count) / base_count, 1)+ if base_count+ else None,+ "last_seen": utc_iso(last_seen) if last_seen else None,"severity_breakdown": {- "debug": int(sev_debug),- "info": int(sev_info),- "warn": int(sev_warn),- "error": int(error_count),+ "debug": counts["debug"],+ "info": counts["info"],+ "warn": counts["warn"],+ "error": error_count,},"active_rules": active_rules,}@@ -284,7 +469,7 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunnlc = int(s["log_count"])s["volume_share_pct"] = round(100.0 * lc / total_logs, 2) if total_logs else 0.0- top_n = min(5, len(services))+ top_n = min(5, len(current))top_share = round(sum(float(s["volume_share_pct"]) for s in services[:top_n]), 2) if services else 0.0sparkline = []@@ -297,33 +482,64 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunn})+ current_window = (self.query_date_range.date_from(), self.query_date_range.date_to())+ baseline_window = (+ (baseline_runner.query_date_range.date_from(), baseline_runner.query_date_range.date_to())+ if baseline_runner+ else None+ )return LogsQueryResponse(results={+ "window": window_echo(current_window, baseline_window),"services": services,"sparkline": sparkline,"total_services": total_services,+ "source": source,"summary": {"top_services_count": top_n,"top_services_volume_share_pct": top_share,+ "status_counts": {+ status: sum(1 for s in services if s["status"] == status) for status in SERVICE_STATUSES+ },+ "truncated": total_services > len(current) or baseline_cut,},+ "next": []+ if self.environment+ else _next_steps(+ [(name, s["status"]) for name, s in zip([*current, *silent_names], services)],+ current_window,+ baseline_window,+ self._pristine_scope,+ ),})def to_query(self) -> ast.SelectQuery:return self._aggregates_query()+ def _search_exprs(self, *, rollup: bool) -> list[ast.Expr]:+ exprs: list[ast.Expr] = []+ if self.service_name_search:+ exprs.append(+ parse_expr(+ "service_name ILIKE {pattern}",+ placeholders={"pattern": ast.Constant(value=ilike_pattern(self.service_name_search))},+ )+ )+ if self.environment:+ env_filter = rollup_environment_filter_expr if rollup else environment_filter_expr+ exprs.append(env_filter(self.environment))+ return exprs+@cached_propertydef _where_with_search(self) -> ast.Expr:- base = self.where()- if not self.service_name_search:- return base- search = parse_expr(- "service_name ILIKE {pattern}",- placeholders={"pattern": ast.Constant(value=ilike_pattern(self.service_name_search))},- )- return ast.And(exprs=[base, search])+ exprs = [self.where(), *self._search_exprs(rollup=False)]+ return exprs[0] if len(exprs) == 1 else ast.And(exprs=exprs)- def _aggregates_query(self) -> ast.SelectQuery:+ def _aggregates_query(self, time_ranges: list[tuple[dt.datetime, dt.datetime]] | None = None) -> ast.SelectQuery:+ where = self._where_with_search+ if time_ranges:+ where = ast.And(exprs=[where, ast.Or(exprs=[_time_range_expr("timestamp", *r) for r in time_ranges])])query = parse_select("""SELECT@@ -335,12 +551,14 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunnsumIf(cnt, in(severity_text, tuple('warn', 'warning'))) AS severity_warn,-- Counts the grouped rows before LIMIT, so the caller learns the-- true service count without a second scan of the window.- count() OVER () AS total_services+ count() OVER () AS total_services,+ max(last_seen) AS last_seenFROM (SELECTservice_name,lower(severity_text) AS severity_text,count() AS cnt,+ max(timestamp) AS last_seenFROM logsWHERE {where}GROUP BY service_name, severity_text@@ -350,13 +568,73 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunnLIMIT {limit}""",placeholders={- "where": self._where_with_search,+ "where": where,"limit": ast.Constant(value=SERVICES_LIMIT),},)assert isinstance(query, ast.SelectQuery)return query+ def _rollup_where(self) -> list[ast.Expr]:+ exprs: list[ast.Expr] = []+ if self.query.serviceNames:+ exprs.append(parse_expr("service_name IN {names}", placeholders={"names": _tuple(self.query.serviceNames)}))+ if self.query.severityLevels:+ exprs.append(+ parse_expr("severity_text IN {levels}", placeholders={"levels": _tuple(self.query.severityLevels)})+ )+ return [*exprs, *self._search_exprs(rollup=True)]++ def _rollup_query(self, start: dt.datetime, end: dt.datetime) -> ast.SelectQuery:+ query = parse_select(+ """+ SELECT+ service_name,+ sum(log_count) AS total,+ sumIf(log_count, in(severity_text, tuple('error', 'fatal'))),+ sumIf(log_count, in(severity_text, tuple('trace', 'debug'))),+ sumIf(log_count, severity_text = 'info'),+ sumIf(log_count, in(severity_text, tuple('warn', 'warning'))),+ count() OVER () AS total_services,+ max(time_bucket)+ FROM posthog.logs_volume_buckets+ WHERE {where}+ GROUP BY service_name+ ORDER BY total DESC+ LIMIT {limit}+ """,+ placeholders={+ "where": ast.And(exprs=[_time_range_expr("time_bucket", start, end), *self._rollup_where()]),+ "limit": ast.Constant(value=SERVICES_LIMIT),+ },+ )+ assert isinstance(query, ast.SelectQuery)+ return query++ def _last_seen_in_rollup(self, service_names: list[str]) -> dict[str, dt.datetime]:+ until = self.query_date_range.date_to()+ exprs = [_time_range_expr("time_bucket", until - ROLLUP_RETENTION, until), *self._rollup_where()]+ exprs.append(parse_expr("service_name IN {names}", placeholders={"names": _tuple(service_names)}))+ query = parse_select(+ "SELECT service_name, max(time_bucket) FROM posthog.logs_volume_buckets WHERE {where} GROUP BY service_name LIMIT {limit}",+ placeholders={"where": ast.And(exprs=exprs), "limit": ast.Constant(value=len(service_names))},+ )+ assert isinstance(query, ast.SelectQuery)+ return {row[0]: row[1] for row in self._execute(query)}++ def _scope(self) -> dict[str, Any]:+ filters = [+ prop.model_dump(exclude_none=True)+ for group in (self.query.filterGroup.values if self.query.filterGroup else [])+ for prop in getattr(group, "values", [])+ ]+ scope = {+ "searchTerm": self.query.searchTerm,+ "filterGroup": filters,+ "severityLevels": self.query.severityLevels,+ }+ return {key: value for key, value in scope.items() if value}+def _sparkline_query(self, service_names: list[str]) -> ast.SelectQuery:query = parse_select("""@@ -381,3 +659,38 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunn)assert isinstance(query, ast.SelectQuery)return query+++def _tuple(values: Sequence[object]) -> ast.Tuple:+ return ast.Tuple(exprs=[ast.Constant(value=str(value)) for value in values])+++def _time_range_expr(field: str, lower: dt.datetime, upper: dt.datetime) -> ast.Expr:+ return parse_expr(+ f"{field} >= {{lower}} AND {field} < {{upper}}",+ placeholders={"lower": ast.Constant(value=lower), "upper": ast.Constant(value=upper)},+ )+++def _next_steps(statuses: list[tuple[str, str]], current: tuple, baseline: tuple | None, scope: dict) -> list[dict]:+ window = {"date_from": utc_iso(current[0]), "date_to": utc_iso(current[1])}+ steps: list[dict] = []+ silent = next((name for name, status in statuses if status == "silent"), None)+ if silent is not None and baseline:+ since_baseline = {"date_from": utc_iso(baseline[0]), "date_to": window["date_to"]}+ query = {**scope, "dateRange": since_baseline, "serviceNames": [silent], "targetBuckets": 24}+ steps.append({"tool": "logs-count-ranges", "why": f"when {silent} stopped logging", "query": query})+ worse = next((name for name, status in statuses if status in ("regressed", "degraded")), None)+ if worse is not None:+ errors = {**scope, "dateRange": window, "serviceNames": [worse], "severityLevels": ["error", "fatal"]}+ diff: dict = {+ "tool": "logs-patterns-diff",+ "why": f"error patterns behind {worse or '(no service)'}",+ "query": errors,+ }+ if baseline:+ diff["baselineDateRange"] = {"date_from": utc_iso(baseline[0]), "date_to": utc_iso(baseline[1])}+ steps.append(diff)+ spike = {**errors, "targetBuckets": 30}+ steps.append({"tool": "logs-count-ranges", "why": f"when errors in {worse} started", "query": spike})+ return steps[:3]diff --git a/products/logs/backend/test/test_environment_filter.py b/products/logs/backend/test/test_environment_filter.pynew file mode 100644index 000000000000..3e1edee61f88--- /dev/null+++ b/products/logs/backend/test/test_environment_filter.py@@ -0,0 +1,64 @@+import datetime as dt++from posthog.test.base import APIBaseTest, ClickhouseTestMixin++from parameterized import parameterized++from posthog.hogql import ast+from posthog.hogql.parser import parse_select+from posthog.hogql.query import execute_hogql_query++from products.logs.backend.environment_filter import (+ UNLABELED_ENVIRONMENT,+ environment_filter_expr,+ rollup_environment_filter_expr,+)+from products.logs.backend.test.volume_rollup_fixtures import insert_logs_and_rollups, log_row++_AT = dt.datetime(2026, 6, 23, 12, 1, tzinfo=dt.UTC)+++class TestEnvironmentFilter(ClickhouseTestMixin, APIBaseTest):+ CLASS_DATA_LEVEL_SETUP = True++ @classmethod+ def setUpTestData(cls):+ super().setUpTestData()+ resources = [+ ("name-key", {"deployment.environment.name": "production", "env": "staging"}),+ ("legacy-key", {"deployment.environment": "production"}),+ ("short-key", {"env": "Production"}),+ ("empty-name-falls-back", {"deployment.environment.name": "", "env": "staging"}),+ ("unlabeled", {"k8s.namespace.name": "default"}),+ ]+ insert_logs_and_rollups(+ [log_row(cls.team.id, _AT, service=service, resource_attributes=attrs) for service, attrs in resources]+ )++ def _raw_services(self, environment: str) -> set[str]:+ query = parse_select(+ "SELECT DISTINCT service_name FROM logs WHERE {env}",+ placeholders={"env": environment_filter_expr(environment)},+ )+ return {row[0] for row in execute_hogql_query(query=query, team=self.team).results}++ def _rollup_services(self, environment: str) -> set[str]:+ query = parse_select(+ "SELECT DISTINCT service_name FROM posthog.logs_volume_buckets WHERE {env}",+ placeholders={"env": rollup_environment_filter_expr(environment)},+ )+ assert isinstance(query, ast.SelectQuery)+ return {row[0] for row in execute_hogql_query(query=query, team=self.team).results}++ @parameterized.expand(+ [+ ("production", "production", {"name-key", "legacy-key", "short-key"}),+ ("case_insensitive", "PRODUCTION", {"name-key", "legacy-key", "short-key"}),+ ("staging_only_where_no_higher_key", "staging", {"empty-name-falls-back"}),+ ("unlabeled", UNLABELED_ENVIRONMENT, {"unlabeled"}),+ ("unknown", "qa", set()),+ ]+ )+ def test_raw_filter_follows_the_rollup_fallback_chain(self, _name: str, environment: str, expected: set[str]):+ assert self._raw_services(environment) == expected+ assert self._rollup_services(environment) == expecteddiff --git a/products/logs/backend/test/test_group_by_query_runner.py b/products/logs/backend/test/test_group_by_query_runner.pyindex e4bd4f288fe6..4ef23f3f7acf 100644--- a/products/logs/backend/test/test_group_by_query_runner.py+++ b/products/logs/backend/test/test_group_by_query_runner.py@@ -6,9 +6,10 @@ from posthog.test.base import APIBaseTest, ClickhouseTestMixinfrom parameterized import parameterizedfrom rest_framework import status-from posthog.schema import DateRange, FilterLogicalOperator, LogsQuery, PropertyGroupFilter+from posthog.schema import CachedLogsQueryResponse, DateRange, FilterLogicalOperator, LogsQuery, PropertyGroupFilterfrom posthog.clickhouse.client import sync_execute+from posthog.hogql_queries.query_runner import ExecutionModefrom products.logs.backend.group_by_query_runner import LogsGroupByQueryRunner@@ -29,6 +30,7 @@ class TestGroupByQueryRunner(ClickhouseTestMixin, APIBaseTest):minute: int = 0,attributes: dict | None = None,resource_attributes: dict | None = None,+ pattern: str = "",) -> dict:row: dict = {"timestamp": f"2026-06-23 {hour:02d}:{minute:02d}:00.000000",@@ -42,6 +44,9 @@ class TestGroupByQueryRunner(ClickhouseTestMixin, APIBaseTest):row["attributes_map_str"] = {f"{k}__str": v for k, v in attributes.items()}if resource_attributes:row["resource_attributes"] = resource_attributes+ if pattern:+ row["pattern"] = pattern+ row["pattern_version"] = 1return rowdef _run(@@ -96,6 +101,7 @@ class TestGroupByQueryRunner(ClickhouseTestMixin, APIBaseTest):"values": ["s1"],"log_count": 3,"error_count": 2,+ "first_seen": "2026-06-23T12:00:00+00:00","last_seen": "2026-06-23T12:02:00+00:00",}assert s2 == {@@ -103,6 +109,7 @@ class TestGroupByQueryRunner(ClickhouseTestMixin, APIBaseTest):"values": ["s2"],"log_count": 2,"error_count": 0,+ "first_seen": "2026-06-23T12:05:00+00:00","last_seen": "2026-06-23T12:06:00+00:00",}@@ -233,6 +240,83 @@ class TestGroupByQueryRunner(ClickhouseTestMixin, APIBaseTest):(["prod", "info"], 1),]+ @time_machine.travel(_FROZEN_NOW, tick=False)+ def test_groups_by_pattern_column_with_service(self) -> None:+ self._insert(+ [+ self._log(service="checkout", severity="error", minute=1, pattern="timeout after <*> ms"),+ self._log(service="checkout", severity="error", minute=4, pattern="timeout after <*> ms"),+ self._log(service="checkout", severity="info", minute=2, pattern="request done"),+ self._log(service="billing", severity="error", minute=3, pattern="timeout after <*> ms"),+ self._log(service="billing", severity="error", minute=5),+ ]+ )++ results = self._run(+ group_bys=[("service_name", "column"), ("pattern", "column")], order_groups_by="error_count"+ )++ assert results["total_logs"] == 4+ assert [+ (g["values"], g["log_count"], g["error_count"], g["first_seen"], g["last_seen"]) for g in results["groups"]+ ] == [+ (["checkout", "timeout after <*> ms"], 2, 2, "2026-06-23T12:01:00+00:00", "2026-06-23T12:04:00+00:00"),+ (["billing", "timeout after <*> ms"], 1, 1, "2026-06-23T12:03:00+00:00", "2026-06-23T12:03:00+00:00"),+ (["checkout", "request done"], 1, 0, "2026-06-23T12:02:00+00:00", "2026-06-23T12:02:00+00:00"),+ ]++ @time_machine.travel(_FROZEN_NOW, tick=False)+ def test_repeat_query_is_served_from_the_short_cache(self) -> None:+ self._insert([self._log(attributes={"session_id": "s1"})])+ query = LogsQuery(+ dateRange=_WINDOW,+ filterGroup=PropertyGroupFilter(type=FilterLogicalOperator.AND_, values=[]),+ severityLevels=[],+ serviceNames=[],+ )++ def runner(group_bys: list[tuple[str, str]]) -> LogsGroupByQueryRunner:+ return LogsGroupByQueryRunner(team=self.team, query=query, group_bys=group_bys)++ first = runner([("session_id", "log")]).run(ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE)+ second = runner([("session_id", "log")]).run(ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE)+ other = runner([("severity_level", "column")]).run(ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE)++ assert isinstance(first, CachedLogsQueryResponse) and not first.is_cached+ assert isinstance(second, CachedLogsQueryResponse) and second.is_cached+ assert second.results == first.results+ assert isinstance(other, CachedLogsQueryResponse) and not other.is_cached++ with time_machine.travel("2026-06-23T13:01:01Z", tick=False):+ stale = runner([("session_id", "log")]).run(ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE)+ assert isinstance(stale, CachedLogsQueryResponse) and not stale.is_cached++ @parameterized.expand(+ [+ ("group_bys", {"group_bys": [("session_id", "log")]}, {"group_bys": [("trace_id", "column")]}),+ (+ "order",+ {"group_bys": [("session_id", "log")], "order_groups_by": "log_count"},+ {"group_bys": [("session_id", "log")], "order_groups_by": "error_count"},+ ),+ (+ "limit",+ {"group_bys": [("session_id", "log")], "group_limit": 10},+ {"group_bys": [("session_id", "log")], "group_limit": 20},+ ),+ ]+ )+ def test_runner_arguments_reach_the_cache_key(self, _name: str, first: dict, second: dict) -> None:+ query = LogsQuery(+ dateRange=_WINDOW,+ filterGroup=PropertyGroupFilter(type=FilterLogicalOperator.AND_, values=[]),+ serviceNames=[],+ severityLevels=[],+ )+ first_key = LogsGroupByQueryRunner(team=self.team, query=query, **first).get_cache_key()+ second_key = LogsGroupByQueryRunner(team=self.team, query=query, **second).get_cache_key()+ assert first_key != second_key+@parameterized.expand([("too_many_dimensions", [("a", "log"), ("b", "log"), ("c", "log"), ("d", "log"), ("e", "log")]),@@ -315,6 +399,7 @@ class TestGroupByAPI(ClickhouseTestMixin, APIBaseTest):"values": ["s1"],"log_count": 1,"error_count": 1,+ "first_seen": "2026-06-23T12:00:00+00:00","last_seen": "2026-06-23T12:00:00+00:00",}]@@ -356,6 +441,42 @@ class TestGroupByAPI(ClickhouseTestMixin, APIBaseTest):data = response.json()assert [g["values"] for g in data["groups"]] == [["s1", "error"]]+ @time_machine.travel(_FROZEN_NOW, tick=False)+ def test_endpoint_accepts_pattern_column(self) -> None:+ sync_execute(+ "INSERT INTO logs FORMAT JSONEachRow\n"+ + "".join(+ json.dumps(+ {+ "team_id": self.team.id,+ "timestamp": "2026-06-23 12:00:00.000000",+ "body": "timeout after 30 ms",+ "severity_text": "error",+ "service_name": "checkout",+ "pattern": "timeout after <*> ms",+ "pattern_version": 1,+ }+ )+ + "\n"+ for _ in range(2)+ )+ )++ response = self.client.post(+ f"/api/projects/{self.team.id}/logs/group-by",+ data={+ "query": {+ "dateRange": {"date_from": "2026-06-23T12:00:00Z", "date_to": "2026-06-23T13:00:00Z"},+ "groupBys": [{"key": "pattern", "source": "column"}],+ }+ },+ format="json",+ )++ assert response.status_code == status.HTTP_200_OK, response.json()+ data = response.json()+ assert [(g["values"], g["log_count"]) for g in data["groups"]] == [(["timeout after <*> ms"], 2)]+def test_endpoint_rejects_too_many_dimensions(self) -> None:response = self.client.post(f"/api/projects/{self.team.id}/logs/group-by",diff --git a/products/logs/backend/test/test_log_values_attributes.py b/products/logs/backend/test/test_log_values_attributes.pyindex 1592ce9870c2..5e8cfc177f27 100644--- a/products/logs/backend/test/test_log_values_attributes.py+++ b/products/logs/backend/test/test_log_values_attributes.py@@ -12,6 +12,8 @@ from posthog.hogql.errors import QueryErrorfrom posthog.clickhouse.client import sync_executefrom posthog.errors import ExposedCHQueryError+from products.logs.backend.presentation.views.api import _LogsValuesQuerySerializer+class TestLogValuesAttributesTimezones(ClickhouseTestMixin, APIBaseTest):CLASS_DATA_LEVEL_SETUP = True@@ -547,3 +549,11 @@ class TestLogAttributesKeysFilter(ClickhouseTestMixin, APIBaseTest):def test_too_many_keys_returns_400(self):response = self._get({"keys": ",".join(f"key.{i}" for i in range(101))})self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+++class TestLogValuesQuerySerializer:+ def test_blank_value_is_accepted(self) -> None:+ serializer = _LogsValuesQuerySerializer(data={"key": "level", "value": ""})++ assert serializer.is_valid(), serializer.errors+ assert serializer.validated_data.get("value", "") == ""diff --git a/products/logs/backend/test/test_services_query_runner.py b/products/logs/backend/test/test_services_query_runner.pyindex 1ebf708e2980..c52a46ae126a 100644--- a/products/logs/backend/test/test_services_query_runner.py+++ b/products/logs/backend/test/test_services_query_runner.py@@ -1,5 +1,6 @@import osimport json+import datetime as dtfrom typing import Anyimport pytest@@ -10,10 +11,9 @@ from unittest.mock import patchfrom parameterized import parameterizedfrom rest_framework import status-from posthog.clickhouse.client import sync_execute-from products.logs.backend import services_query_runnerfrom products.logs.backend.services_query_runner import rule_could_apply_to_service+from products.logs.backend.test.volume_rollup_fixtures import insert_logs_and_rollups, log_rowdef _wrap(inner: dict) -> dict:@@ -169,16 +169,7 @@ class TestServicesQueryDateRange(ClickhouseTestMixin, APIBaseTest):def setUpTestData(cls):super().setUpTestData()with open(os.path.join(os.path.dirname(__file__), "test_logs.jsonnd")) as f:- sql = ""- for line in f:- log_item = json.loads(line)- log_item["team_id"] = cls.team.id- sql += json.dumps(log_item) + "\n"- sync_execute(f"""- INSERT INTO logs- FORMAT JSONEachRow- {sql}- """)+ insert_logs_and_rollups([{**json.loads(line), "team_id": cls.team.id} for line in f])def _services(self, date_from: str, date_to: str, service_name_search: str | None = None) -> dict:query: dict[str, Any] = {@@ -300,5 +291,221 @@ class TestServicesQueryDateRange(ClickhouseTestMixin, APIBaseTest):self.assertEqual(result["total_services"], len(expected_names))+_COMPARE_NOW = "2026-06-23T13:00:00Z"+_CURRENT = {"date_from": "2026-06-23T12:00:00Z", "date_to": "2026-06-23T13:00:00Z"}+_PRODUCTION = {"deployment.environment.name": "production"}+_STAGING = {"deployment.environment": "staging"}+_COMPARE_SERVICES: dict[str, tuple[dict[str, str], tuple[int, int], tuple[int, int]]] = {+ "checkout": (_PRODUCTION, (200, 2), (200, 20)),+ "billing": (_PRODUCTION, (50, 0), (0, 0)),+ "search": (_PRODUCTION, (0, 0), (30, 0)),+ "inventory": ({"env": "production"}, (150, 0), (150, 0)),+ "ledger": (_STAGING, (100, 10), (100, 10)),+ "payments": (_STAGING, (100, 20), (120, 0)),+}+++def _hour_rows(team_id: int, service: str, hour: int, info: int, errors: int, resource: dict[str, str]) -> list[dict]:+ total = info + errors+ start = dt.datetime(2026, 6, 23, hour, 0, 30, tzinfo=dt.UTC)+ rows = []+ for i in range(total):+ rows.append(+ log_row(+ team_id,+ start + dt.timedelta(seconds=i * 3540 / total),+ service=service,+ severity="error" if i < errors else "info",+ resource_attributes=resource,+ )+ )+ return rows+++class TestServicesCompare(ClickhouseTestMixin, APIBaseTest):+ CLASS_DATA_LEVEL_SETUP = True++ @classmethod+ def setUpTestData(cls):+ super().setUpTestData()+ rows: list[dict] = []+ for service, (resource, (base_info, base_errors), (cur_info, cur_errors)) in _COMPARE_SERVICES.items():+ rows += _hour_rows(cls.team.id, service, 11, base_info, base_errors, resource)+ rows += _hour_rows(cls.team.id, service, 12, cur_info, cur_errors, resource)+ insert_logs_and_rollups(rows)++ def _services(+ self, expected_status: int = status.HTTP_200_OK, baselineDateRange: dict | None = None, **query: Any+ ) -> dict:+ body: dict[str, Any] = {"query": {"dateRange": _CURRENT, **query}}+ if baselineDateRange is not None:+ body["baselineDateRange"] = baselineDateRange+ response = self.client.post(+ f"/api/projects/{self.team.id}/logs/services",+ data=body,+ format="json",+ )+ self.assertEqual(response.status_code, expected_status, response.content)+ return response.json()++ @staticmethod+ def _by_service(result: dict) -> dict[str, dict]:+ return {row["service_name"]: row for row in result["services"]}++ @time_machine.travel(_COMPARE_NOW, tick=False)+ def test_previous_period_classifies_each_service(self) -> None:+ result = self._services(compare="previous_period")+ rows = self._by_service(result)++ assert {name: row["status"] for name, row in rows.items()} == {+ "checkout": "regressed",+ "billing": "silent",+ "search": "new",+ "inventory": "ok",+ "ledger": "degraded",+ "payments": "recovered",+ }+ assert rows["checkout"]["reason"] == "error rate 1.0% → 9.1%"+ assert rows["checkout"]["error_rate_pct"] == 9.1+ assert rows["checkout"]["error_rate_delta_pp"] == 8.1+ assert rows["checkout"]["baseline_log_count"] == 202+ assert rows["billing"]["log_count"] == 0+ assert rows["billing"]["baseline_log_count"] == 50+ assert rows["billing"]["last_seen"] == "2026-06-23T11:55:00Z"+ assert result["window"] == {+ "current": {"from": "2026-06-23T12:00:00Z", "to": "2026-06-23T13:00:00Z"},+ "baseline": {"from": "2026-06-23T11:00:00Z", "to": "2026-06-23T12:00:00Z"},+ "timezone": "UTC",+ }+ assert result["summary"]["status_counts"] == {+ "ok": 1,+ "degraded": 1,+ "regressed": 1,+ "improved": 0,+ "recovered": 1,+ "new": 1,+ "silent": 1,+ }+ assert all(len(row["reason"]) <= 80 for row in result["services"])++ @time_machine.travel(_COMPARE_NOW, tick=False)+ def test_next_suggests_follow_ups_for_problem_services(self) -> None:+ result = self._services(compare="previous_period")++ suggestions = {(step["tool"], tuple(step["query"].get("serviceNames", []))) for step in result["next"]}+ assert ("logs-count-ranges", ("billing",)) in suggestions+ assert ("logs-patterns-diff", ("checkout",)) in suggestions+ assert len(result["next"]) <= 3++ @parameterized.expand(+ [+ ("aligned_window_reads_the_rollup", _CURRENT, "rollup"),+ (+ "unaligned_window_adds_raw_edges",+ {"date_from": "2026-06-23T12:02:10Z", "date_to": "2026-06-23T12:57:50Z"},+ "rollup+raw",+ ),+ ]+ )+ @time_machine.travel(_COMPARE_NOW, tick=False)+ def test_rollup_and_raw_paths_give_the_same_counts(self, _name: str, window: dict, expected_source: str) -> None:+ def counts(result: dict) -> dict[str, tuple]:+ return {+ row["service_name"]: (+ row["log_count"],+ row["error_count"],+ row["severity_breakdown"],+ row["baseline_log_count"],+ row["status"],+ )+ for row in result["services"]+ }++ fast = self._services(dateRange=window, compare="previous_period")+ with patch.object(services_query_runner.ServicesQueryRunner, "_use_rollup", return_value=False):+ raw = self._services(dateRange=window, compare="previous_period")++ assert fast["source"] == expected_source+ assert raw["source"] == "raw"+ assert counts(fast) == counts(raw)+ assert fast["total_services"] == raw["total_services"]++ @parameterized.expand([("rollup", True), ("raw", False)])+ @time_machine.travel(_COMPARE_NOW, tick=False)+ def test_environment_filter_follows_the_fallback_chain(self, _name: str, use_rollup: bool) -> None:+ with patch.object(services_query_runner.ServicesQueryRunner, "_use_rollup", return_value=use_rollup):+ result = self._services(compare="previous_period", environment="production")++ assert set(self._by_service(result)) == {"checkout", "billing", "search", "inventory"}++ @time_machine.travel(_COMPARE_NOW, tick=False)+ def test_no_compare_keeps_the_single_window_response(self) -> None:+ result = self._services()+ rows = self._by_service(result)++ assert set(rows) == {"checkout", "search", "inventory", "ledger", "payments"}+ assert rows["checkout"]["status"] == "degraded"+ assert rows["inventory"]["status"] == "ok"+ assert rows["inventory"]["baseline_log_count"] is None+ assert "baseline" not in result["window"]+ assert {"sparkline", "total_services", "summary"} <= set(result)++ @time_machine.travel(_COMPARE_NOW, tick=False)+ def test_explicit_baseline_matches_previous_period(self) -> None:+ explicit = self._services(+ baselineDateRange={"date_from": "2026-06-23T11:00:00Z", "date_to": "2026-06-23T12:00:00Z"}+ )+ previous = self._services(compare="previous_period")++ assert explicit["services"] == previous["services"]++ @time_machine.travel(_COMPARE_NOW, tick=False)+ def test_previous_day_without_history_marks_every_service_new(self) -> None:+ result = self._services(compare="previous_day")++ assert result["window"]["baseline"] == {"from": "2026-06-22T12:00:00Z", "to": "2026-06-22T13:00:00Z"}+ assert {row["status"] for row in result["services"]} == {"new"}++ @time_machine.travel(_COMPARE_NOW, tick=False)+ def test_next_keeps_the_original_scope(self) -> None:+ excluded = {+ "key": "k8s.namespace.name",+ "operator": "is_not",+ "value": ["staging"],+ "type": "log_resource_attribute",+ }+ result = self._services(compare="previous_period", searchTerm="log line", filterGroup=[excluded])++ assert result["next"]+ for step in result["next"]:+ assert step["query"]["searchTerm"] == "log line"+ assert step["query"]["filterGroup"] == [excluded]+ diff = next(step for step in result["next"] if step["tool"] == "logs-patterns-diff")+ assert diff["baselineDateRange"] == {"date_from": "2026-06-23T11:00:00Z", "date_to": "2026-06-23T12:00:00Z"}+ assert self._services(compare="previous_period", environment="production")["next"] == []++ def test_next_filters_on_the_stored_service_name(self) -> None:+ window = (dt.datetime(2026, 6, 23, 12, tzinfo=dt.UTC), dt.datetime(2026, 6, 23, 13, tzinfo=dt.UTC))++ steps = services_query_runner._next_steps([("", "degraded")], window, None, {})++ assert {tuple(step["query"]["serviceNames"]) for step in steps} == {("",)}++ @time_machine.travel(_COMPARE_NOW, tick=False)+ def test_a_capped_scan_does_not_invent_silent_or_new_services(self) -> None:+ with patch("products.logs.backend.services_query_runner.SERVICES_LIMIT", 2):+ result = self._services(compare="previous_period")++ assert not {row["status"] for row in result["services"]} & {"silent", "new"}+ assert result["summary"]["truncated"] is True++ def test_environment_must_be_a_string(self) -> None:+ self._services(expected_status=status.HTTP_400_BAD_REQUEST, environment=["production"])++ def test_unknown_compare_mode_is_rejected(self) -> None:+ response = self._services(expected_status=status.HTTP_400_BAD_REQUEST, compare="yesterday")+ assert "compare" in json.dumps(response)++if __name__ == "__main__":pytest.main([__file__, "-v"])diff --git a/products/logs/backend/test/volume_rollup_fixtures.py b/products/logs/backend/test/volume_rollup_fixtures.pynew file mode 100644index 000000000000..e1726c074ca2--- /dev/null+++ b/products/logs/backend/test/volume_rollup_fixtures.py@@ -0,0 +1,44 @@+import json+import datetime as dt++from posthog.clickhouse.client import sync_execute++# The test `logs` table reads logs32, which feeds no volume rollup. Writing the same rows to logs34+# runs the production materialized views, so logs_volume_buckets holds what ingestion would write.+_FAR_FUTURE_EXPIRY = "2099-01-01 00:00:00.000000"+++def log_row(+ team_id: int,+ timestamp: dt.datetime,+ *,+ service: str = "api",+ severity: str = "info",+ body: str = "log line",+ resource_attributes: dict[str, str] | None = None,+ attributes: dict[str, str] | None = None,+ pattern: str = "",+) -> dict:+ row: dict = {+ "team_id": team_id,+ "timestamp": timestamp.astimezone(dt.UTC).strftime("%Y-%m-%d %H:%M:%S.%f"),+ "observed_timestamp": timestamp.astimezone(dt.UTC).strftime("%Y-%m-%d %H:%M:%S.%f"),+ "body": body,+ "severity_text": severity,+ "service_name": service,+ "resource_attributes": resource_attributes or {},+ "attributes_map_str": {f"{k}__str": v for k, v in (attributes or {}).items()},+ }+ if pattern:+ row["pattern"] = pattern+ row["pattern_version"] = 1+ return row+++def insert_logs_and_rollups(rows: list[dict]) -> None:+ if not rows:+ return+ raw = "".join(json.dumps(row) + "\n" for row in rows)+ sync_execute(f"INSERT INTO logs FORMAT JSONEachRow\n{raw}")+ rollup_source = "".join(json.dumps({**row, "original_expiry_timestamp": _FAR_FUTURE_EXPIRY}) + "\n" for row in rows)+ sync_execute(f"INSERT INTO logs34 FORMAT JSONEachRow\n{rollup_source}")diff --git a/products/logs/frontend/components/LogsViewer/groupBySource.ts b/products/logs/frontend/components/LogsViewer/groupBySource.tsindex f0e6bf15efaa..0d40ca0a944a 100644--- a/products/logs/frontend/components/LogsViewer/groupBySource.ts+++ b/products/logs/frontend/components/LogsViewer/groupBySource.ts@@ -16,7 +16,13 @@ export const TAXONOMIC_GROUP_TO_SOURCE: Partial<Record<TaxonomicFilterGroupType,// attribute map entry and returns nothing. Pin them to `column` by key so a recent groups by the// real column, matching a fresh pick from the `Logs` group.// This mirrors the backend dict by hand; `groupBySource.test.ts` fails if the two drift apart.-export const GROUPABLE_COLUMN_KEYS = new Set<string>(['severity_level', 'trace_id', 'span_id'])+export const GROUPABLE_COLUMN_KEYS = new Set<string>([+ 'severity_level',+ 'trace_id',+ 'span_id',+ 'service_name',+ 'pattern',+])export function resolveGroupBySource(key: string, groupType: TaxonomicFilterGroupType): LogsGroupBySourceEnumApi {if (GROUPABLE_COLUMN_KEYS.has(key)) {diff --git a/products/logs/frontend/generated/api.schemas.ts b/products/logs/frontend/generated/api.schemas.tsindex 3b643ecaa50c..54b5c5c728ae 100644--- a/products/logs/frontend/generated/api.schemas.ts+++ b/products/logs/frontend/generated/api.schemas.ts@@ -1790,7 +1790,7 @@ export const LogsGroupBySourceEnumApi = {} as constexport interface _LogsGroupByDimensionApi {- /** The key this dimension groups by — an attribute key (e.g. "session_id", "service.name") or, when source is "column", one of the top-level log fields: "severity_level", "trace_id", "span_id". */+ /** The key this dimension groups by — an attribute key (e.g. "session_id", "service.name") or, when source is "column", one of the top-level log fields: "severity_level", "service_name", "trace_id", "span_id", "pattern" (the masked message template). */key: string/** Where this dimension's key lives: "log" for log-level attributes, "resource" for resource-level attributes, "column" for top-level log fields.*@@ -1824,7 +1824,7 @@ export interface _LogsGroupByBodyApi {searchTerm?: string/** Property filters applied before grouping. Same shape as the query-logs endpoint. */filterGroup?: _LogPropertyFilterApi[]- /** The key to group logs by — an attribute key (e.g. "session_id", "service.name") or, when groupBySource is "column", one of the top-level log fields: "severity_level", "trace_id", "span_id". Ignored when groupBys is provided. */+ /** The key to group logs by — an attribute key (e.g. "session_id", "service.name") or, when groupBySource is "column", one of the top-level log fields: "severity_level", "service_name", "trace_id", "span_id", "pattern". Ignored when groupBys is provided. */groupBy?: string/** Where the grouping key lives: "log" for log-level attributes, "resource" for resource-level attributes, "column" for top-level log fields. Ignored when groupBys is provided.*@@ -1870,6 +1870,8 @@ export interface _LogsGroupByGroupApi {log_count: number/** Number of matching logs in this group at severity "error" or "fatal". */error_count: number+ /** ISO 8601 timestamp of the earliest matching log in this group. */+ first_seen?: string/** ISO 8601 timestamp of the most recent matching log in this group. */last_seen: string}@@ -2764,6 +2766,21 @@ export interface LogsSamplingRuleReorderApi {ordered_ids: string[]}+/**+ * * `none` - No baseline+ * * `previous_period` - Previous period+ * * `previous_day` - Same window a day earlier+ * * `previous_week` - Same window a week earlier+ */+export type LogsCompareModeEnumApi = (typeof LogsCompareModeEnumApi)[keyof typeof LogsCompareModeEnumApi]++export const LogsCompareModeEnumApi = {+ None: 'none',+ PreviousPeriod: 'previous_period',+ PreviousDay: 'previous_day',+ PreviousWeek: 'previous_week',+} as const+export interface _LogsServicesBodyApi {/** Date range for the services aggregation. Defaults to last hour. */dateRange?: _DateRangeApi@@ -2780,13 +2797,48 @@ export interface _LogsServicesBodyApi {serviceNameSearch?: string/** Property filters for the query. */filterGroup?: _LogPropertyFilterApi[]+ /**+ * Deployment environment, matched without case against the first set resource attribute of deployment.environment.name, deployment.environment and env. "(none)" matches logs without one.+ * @maxLength 200+ */+ environment?: string+ /** Baseline window for per-service status: the window just before, the same window a day or a week earlier, or none. Silent services (logs in the baseline, none now) only appear with a baseline.+ *+ * * `none` - No baseline+ * * `previous_period` - Previous period+ * * `previous_day` - Same window a day earlier+ * * `previous_week` - Same window a week earlier */+ compare?: LogsCompareModeEnumApi}export interface _LogsServicesRequestApi {/** The services aggregation query to execute. */query: _LogsServicesBodyApi+ /** Explicit baseline window. Takes precedence over `query.compare`. */+ baselineDateRange?: _DateRangeApi}+/**+ * * `ok` - OK+ * * `degraded` - Error rate 5% or more+ * * `regressed` - Error rate worse than the baseline+ * * `improved` - Error rate better than the baseline+ * * `recovered` - No longer degraded+ * * `new` - No logs in the baseline+ * * `silent` - Logs stopped+ */+export type LogsServiceStatusEnumApi = (typeof LogsServiceStatusEnumApi)[keyof typeof LogsServiceStatusEnumApi]++export const LogsServiceStatusEnumApi = {+ Ok: 'ok',+ Degraded: 'degraded',+ Regressed: 'regressed',+ Improved: 'improved',+ Recovered: 'recovered',+ New: 'new',+ Silent: 'silent',+} as const+export interface _LogsServiceSeverityBreakdownApi {debug: numberinfo: number@@ -2803,6 +2855,40 @@ export interface _LogsServiceActiveRuleApi {export interface _LogsServiceAggregateApi {/** Service name, or "(no value)" / "(no service)" placeholder for unset entries. */service_name: string+ /** ok, degraded (error rate of 5% or more), regressed or improved (error rate up or down by 1 pp and 2x), recovered (back under 5%), new (no baseline logs) or silent (baseline logs, none in the window).+ *+ * * `ok` - OK+ * * `degraded` - Error rate 5% or more+ * * `regressed` - Error rate worse than the baseline+ * * `improved` - Error rate better than the baseline+ * * `recovered` - No longer degraded+ * * `new` - No logs in the baseline+ * * `silent` - Logs stopped */+ status?: LogsServiceStatusEnumApi+ /** Short reason for the status, e.g. "error rate 0.4% → 3.1%". */+ reason?: string+ /** Error rate in percent, rounded to 1 decimal. */+ error_rate_pct?: number+ /**+ * Logs in the baseline. Null without one.+ * @nullable+ */+ baseline_log_count?: number | null+ /**+ * Error rate change against the baseline, in percentage points.+ * @nullable+ */+ error_rate_delta_pp?: number | null+ /**+ * Log volume change against the baseline, in percent.+ * @nullable+ */+ log_count_change_pct?: number | null+ /**+ * Latest log time (UTC). From the rollup, and for silent services, the start of the last 5-minute bucket.+ * @nullable+ */+ last_seen?: string | null/** Total log entries from this service in the window. */log_count: number/** Count of logs at severity "error" or "fatal". */@@ -2824,11 +2910,73 @@ export interface _LogsServicesSparklineBucketApi {count: number}+/**+ * Service rows per status.+ */+export type _LogsServicesSummaryApiStatusCounts = { [key: string]: number }+export interface _LogsServicesSummaryApi {/** Number of top services included in the volume_share aggregate (up to 5). */top_services_count: number/** Combined volume share (percent) of the top services by log_count. */top_services_volume_share_pct: number+ /** Service rows per status. */+ status_counts?: _LogsServicesSummaryApiStatusCounts+ /** True when more services matched than `services` holds. */+ truncated?: boolean+}++/**+ * `from` and `to` of the queried window, ISO 8601 UTC.+ */+export type _LogsWindowApiCurrent = { [key: string]: string }++/**+ * `from` and `to` of the baseline. Absent without one.+ */+export type _LogsWindowApiBaseline = { [key: string]: string }++export interface _LogsWindowApi {+ /** `from` and `to` of the queried window, ISO 8601 UTC. */+ current: _LogsWindowApiCurrent+ /** `from` and `to` of the baseline. Absent without one. */+ baseline?: _LogsWindowApiBaseline+ /** Always UTC. */+ timezone: string+}++/**+ * * `rollup` - Rollup+ * * `rollup+raw` - Rollup Raw+ * * `raw` - Raw+ */+export type LogsServicesSourceEnumApi = (typeof LogsServicesSourceEnumApi)[keyof typeof LogsServicesSourceEnumApi]++export const LogsServicesSourceEnumApi = {+ Rollup: 'rollup',+ RollupRaw: 'rollup+raw',+ Raw: 'raw',+} as const++/**+ * Prefilled `query` for that tool.+ */+export type _LogsNextStepApiQuery = { [key: string]: unknown }++/**+ * Prefilled sibling `baselineDateRange` argument, for tools that take one.+ */+export type _LogsNextStepApiBaselineDateRange = { [key: string]: unknown }++export interface _LogsNextStepApi {+ /** MCP tool to call next. */+ tool: string+ /** What the call answers. */+ why: string+ /** Prefilled `query` for that tool. */+ query: _LogsNextStepApiQuery+ /** Prefilled sibling `baselineDateRange` argument, for tools that take one. */+ baselineDateRange?: _LogsNextStepApiBaselineDateRange}export interface _LogsServicesResponseApi {@@ -2840,6 +2988,16 @@ export interface _LogsServicesResponseApi {total_services: number/** Roll-up stats for the Services tab header. */summary?: _LogsServicesSummaryApi+ /** Absolute UTC bounds of the window and the baseline. */+ window?: _LogsWindowApi+ /** rollup: 5-minute rollup only. rollup+raw: rollup plus raw logs at unaligned window edges.+ *+ * * `rollup` - Rollup+ * * `rollup+raw` - Rollup Raw+ * * `raw` - Raw */+ source?: LogsServicesSourceEnumApi+ /** Prefilled follow-up calls for problem services. */+ next?: _LogsNextStepApi[]}/**@@ -3215,8 +3373,7 @@ export type LogsValuesRetrieveParams = {*/serviceNames?: string[]/**- * Search filter for attribute values- * @minLength 1+ * Search filter for attribute values. Omit it or pass an empty string to list all values.*/value?: string}diff --git a/products/logs/frontend/generated/api.zod.ts b/products/logs/frontend/generated/api.zod.tsindex 27f1eac640e3..013eeec07b62 100644--- a/products/logs/frontend/generated/api.zod.ts+++ b/products/logs/frontend/generated/api.zod.ts@@ -1043,7 +1043,7 @@ export const LogsGroupByCreateBody = /* @__PURE__ */ zod.object({.string().optional().describe(- 'The key to group logs by — an attribute key (e.g. \"session_id\", \"service.name\") or, when groupBySource is \"column\", one of the top-level log fields: \"severity_level\", \"trace_id\", \"span_id\". Ignored when groupBys is provided.'+ 'The key to group logs by — an attribute key (e.g. \"session_id\", \"service.name\") or, when groupBySource is \"column\", one of the top-level log fields: \"severity_level\", \"service_name\", \"trace_id\", \"span_id\", \"pattern\". Ignored when groupBys is provided.'),groupBySource: zod.enum(['log', 'resource', 'column'])@@ -1058,7 +1058,7 @@ export const LogsGroupByCreateBody = /* @__PURE__ */ zod.object({key: zod.string().describe(- 'The key this dimension groups by — an attribute key (e.g. \"session_id\", \"service.name\") or, when source is \"column\", one of the top-level log fields: \"severity_level\", \"trace_id\", \"span_id\".'+ 'The key this dimension groups by — an attribute key (e.g. \"session_id\", \"service.name\") or, when source is \"column\", one of the top-level log fields: \"severity_level\", \"service_name\", \"trace_id\", \"span_id\", \"pattern\" (the masked message template).'),source: zod.enum(['log', 'resource', 'column'])@@ -2093,6 +2093,10 @@ export const LogsSamplingRulesReorderCreateBody = /* @__PURE__ */ zod.object({export const logsServicesCreateBodyQueryOneServiceNameSearchMax = 200+export const logsServicesCreateBodyQueryOneEnvironmentMax = 200++export const logsServicesCreateBodyQueryOneCompareDefault = `none`+export const LogsServicesCreateBody = /* @__PURE__ */ zod.object({query: zod.object({@@ -2185,8 +2189,39 @@ export const LogsServicesCreateBody = /* @__PURE__ */ zod.object({).optional().describe('Property filters for the query.'),+ environment: zod+ .string()+ .max(logsServicesCreateBodyQueryOneEnvironmentMax)+ .optional()+ .describe(+ 'Deployment environment, matched without case against the first set resource attribute of deployment.environment.name, deployment.environment and env. \"(none)\" matches logs without one.'+ ),+ compare: zod+ .enum(['none', 'previous_period', 'previous_day', 'previous_week'])+ .describe(+ '\* `none` - No baseline\n\* `previous_period` - Previous period\n\* `previous_day` - Same window a day earlier\n\* `previous_week` - Same window a week earlier'+ )+ .default(logsServicesCreateBodyQueryOneCompareDefault)+ .describe(+ 'Baseline window for per-service status: the window just before, the same window a day or a week earlier, or none. Silent services (logs in the baseline, none now) only appear with a baseline.\n\n\* `none` - No baseline\n\* `previous_period` - Previous period\n\* `previous_day` - Same window a day earlier\n\* `previous_week` - Same window a week earlier'+ ),}).describe('The services aggregation query to execute.'),+ baselineDateRange: zod+ .object({+ date_from: zod+ .string()+ .nullish()+ .describe(+ 'Start of the date range. Accepts ISO 8601 timestamps or relative formats: -7d, -1h, -1mStart, etc.'+ ),+ date_to: zod+ .string()+ .nullish()+ .describe('End of the date range. Same format as date_from. Omit or null for \"now\".'),+ })+ .optional()+ .describe('Explicit baseline window. Takes precedence over `query.compare`.'),})export const logsSparklineCreateBodyQueryOneSeverityLevelsDefault = []diff --git a/products/logs/frontend/posthogAiToolRenderers.tsx b/products/logs/frontend/posthogAiToolRenderers.tsxindex c05c9c40d9b9..f59209745aee 100644--- a/products/logs/frontend/posthogAiToolRenderers.tsx+++ b/products/logs/frontend/posthogAiToolRenderers.tsx@@ -29,7 +29,11 @@ export const posthogAiToolRenderers: ToolRegistryEntry[] = [},...labelled(['logs-services-create'], 'Logs services', <IconServer />),...labelled(['logs-patterns', 'logs-patterns-diff'], 'Log patterns', <IconStack />),- ...labelled(['logs-count', 'logs-count-ranges', 'logs-sparkline-query'], 'Count logs', <IconPulse />),+ ...labelled(+ ['logs-count', 'logs-count-ranges', 'logs-sparkline-query', 'logs-group-by'],+ 'Count logs',+ <IconPulse />+ ),...labelled(['logs-attributes-list', 'logs-attribute-values-list', 'logs-facet-values-create'],'Log attributes',diff --git a/products/logs/mcp/prompts/logs-group-by.md b/products/logs/mcp/prompts/logs-group-by.mdnew file mode 100644index 000000000000..098578cbff6a--- /dev/null+++ b/products/logs/mcp/prompts/logs-group-by.md@@ -0,0 +1,17 @@+Count logs grouped by up to 4 dimensions in one call. All parameters go inside `query`:++```json+{ "query": { "dateRange": { "date_from": "-24h" }, "groupBys": [{ "key": "pattern", "source": "column" }] } }+```++Each group has `values` (one per dimension, in request order), `log_count`, `error_count`, `first_seen` and `last_seen`. `truncated` is true when more groups matched than `limit`.++Use it instead of SQL for "top errors by service", "which patterns fail most" or "which sessions are noisiest". Use `logs-services-create` for a per-service health summary against a baseline, `logs-patterns-diff` for new or growing patterns, and `query-logs` to read the rows of one group.++Dimensions in `groupBys`:++- `source: "column"`: `service_name`, `severity_level`, `pattern` (the masked message template), `trace_id`, `span_id`.+- `source: "log"`: a log attribute key, for example `session_id`.+- `source: "resource"`: a resource attribute key, for example `k8s.pod.name`.++Rows without a value for a dimension are not counted. `orderGroupsBy` is `log_count`, `error_count` or `last_seen` (descending). Filter with `severityLevels`, `serviceNames`, `searchTerm` and `filterGroup`. Results are cached for 60 seconds.diff --git a/products/logs/mcp/prompts/logs-services.md b/products/logs/mcp/prompts/logs-services.mdnew file mode 100644index 000000000000..215ad92a0cee--- /dev/null+++ b/products/logs/mcp/prompts/logs-services.md@@ -0,0 +1,15 @@+List every service that logged in the window, with its health against a baseline. All other parameters go inside `query`:++```json+{ "query": { "dateRange": { "date_from": "-24h" }, "compare": "previous_day", "environment": "production" } }+```++Each row has `log_count`, `error_count`, `error_rate_pct` and `last_seen`. With `compare`, each row also gets a `status` and a short `reason`.+The status is `ok`, `degraded` (5% errors or more), `regressed` or `improved` (error rate up or down by 1 pp and 2x), `recovered`, `new` (no baseline logs) or `silent` (baseline logs, none now).+Silent services appear only with a baseline.+This is the entry point for "which services are silent, new or worse".+Use `logs-group-by` for counts by pattern or attribute, `logs-patterns-diff` for the error patterns behind a regression, and `logs-count-ranges` for when a spike started.++`compare` is `none` (default), `previous_period`, `previous_day` or `previous_week`. `baselineDateRange`, next to `query`, sets an explicit baseline.+`environment` matches the first set resource attribute of `deployment.environment.name`, `deployment.environment` and `env`.+`window` gives the absolute UTC bounds. `next` holds prefilled follow-up calls. It is empty when `environment` is set, because those tools do not filter by environment.diff --git a/products/logs/mcp/tools.yaml b/products/logs/mcp/tools.yamlindex aad679817281..f3f3cb8c835d 100644--- a/products/logs/mcp/tools.yaml+++ b/products/logs/mcp/tools.yaml@@ -407,6 +407,21 @@ tools:response:include:- results+ logs-group-by:+ operation: logs_group_by_create+ enabled: true+ scopes:+ - logs:read+ annotations:+ readOnly: true+ destructive: false+ idempotent: true+ title: Group logs by attributes+ description_file: ./prompts/logs-group-by.md+ feature_flag: logs-agent-tools+ response:+ exclude:+ - groups.*.valuelogs-patterns:operation: logs_patterns_createenabled: true@@ -451,22 +466,15 @@ tools:readOnly: truedestructive: falseidempotent: true- title: List services with stats- description: |- Return the top-25 services by log volume in the window, each with log_count, error_count, and error_rate,- plus a per-service sparkline. Use this as the entry point when triaging which services are worth alerting on- — high volume × non-zero error_rate is the natural alert candidate. Far cheaper than walking- attribute-values + per-service counts.-- Put all parameters in `query`:-- ```json- { "query": { "dateRange": { "date_from": "-24h" } } }- ```+ title: List services with health status+ description_file: ./prompts/logs-services.mdresponse:include:- services- sparkline+ - window+ - summary+ - nextlogs-sparkline-query:operation: logs_sparkline_createenabled: truediff --git a/services/mcp/schema/generated-tool-definitions.json b/services/mcp/schema/generated-tool-definitions.jsonindex 63211a10f6e1..ea6cf183b53a 100644--- a/services/mcp/schema/generated-tool-definitions.json+++ b/services/mcp/schema/generated-tool-definitions.json@@ -9184,6 +9184,21 @@"readOnlyHint": true}},+ "logs-group-by": {+ "description": "Count logs grouped by up to 4 dimensions in one call. All parameters go inside `query`:\n\n```json\n{ \"query\": { \"dateRange\": { \"date_from\": \"-24h\" }, \"groupBys\": [{ \"key\": \"pattern\", \"source\": \"column\" }] } }\n```\n\nEach group has `values` (one per dimension, in request order), `log_count`, `error_count`, `first_seen` and `last_seen`. `truncated` is true when more groups matched than `limit`.\n\nUse it instead of SQL for \"top errors by service\", \"which patterns fail most\" or \"which sessions are noisiest\". Use `logs-services-create` for a per-service health summary against a baseline, `logs-patterns-diff` for new or growing patterns, and `query-logs` to read the rows of one group.\n\nDimensions in `groupBys`:\n\n- `source: \"column\"`: `service_name`, `severity_level`, `pattern` (the masked message template), `trace_id`, `span_id`.\n- `source: \"log\"`: a log attribute key, for example `session_id`.\n- `source: \"resource\"`: a resource attribute key, for example `k8s.pod.name`.\n\nRows without a value for a dimension are not counted. `orderGroupsBy` is `log_count`, `error_count` or `last_seen` (descending). Filter with `severityLevels`, `serviceNames`, `searchTerm` and `filterGroup`. Results are cached for 60 seconds.",+ "category": "Logs",+ "feature": "logs",+ "summary": "Group logs by attributes",+ "title": "Group logs by attributes",+ "required_scopes": ["logs:read"],+ "annotations": {+ "destructiveHint": false,+ "idempotentHint": true,+ "openWorldHint": true,+ "readOnlyHint": true+ },+ "feature_flag": "logs-agent-tools"+ },"logs-patterns": {"description": "Mine recurring log templates (\"patterns\") from the logs matching a filter set, ordered by frequency. Each pattern is a message template with the variable parts masked — e.g. `Connected to <ip> in <num>ms` — plus occurrence estimates, severity mix, the services it appears in, and a ready-made predicate for fetching its matching lines.\n\nAll parameters go inside `query` — top-level fields are rejected:\n\n```json\n{ \"query\": { \"serviceNames\": [\"api\"], \"dateRange\": { \"date_from\": \"-1h\" } } }\n```\n\nThis is the fastest way to understand what a log stream is _saying_ without reading raw rows: one call summarizes millions of lines into at most 200 templates.\n\n# When to use\n\n- To triage an unfamiliar or noisy stream: mine the last hour, scan the top templates by `estimated_count`, and look for anything with a non-zero error share in `severity_counts`.\n- To find what's new or dominant during an incident window: mine with `severityLevels: [\"error\", \"fatal\"]` and a `dateRange` covering the incident.\n- As the entry point of a drill-down loop: mine → pick a suspicious pattern → filter logs to exactly its lines using `match_patterns`, or `match_regex` and `match_literal` when it is empty (see below) → read the raw rows with `query-logs`.\n- To quantify repetition before proposing log sampling or cleanup: `volume_share_pct` tells you how much of the stream one template accounts for.\n\n## Pick the right tool\n\n- Raw log lines matching a filter → `query-logs`.\n- A single number (how many logs match) → `logs-count`; per-time-bucket counts → `logs-count-ranges`.\n- Distribution of a dimension (which services emit the errors?) → `logs-facet-values-create`.\n- Use **this** tool to summarize message _content_ — what distinct things the logs say and how often.\n\n# Reading the response\n\n- `source` reports what was used: `stored_patterns` for exact stored-pattern aggregation followed by Drain3 grouping, or `body_mining` for body masking and Drain3. Include this distinction in your answer.\n- `pattern_version` identifies the version used for stored patterns. `fallback_reason` explains body mining: flag disabled, insufficient version coverage, an empty window, or comparison mode.\n- The `logs_patterns_query_v2` flag enables the stored-pattern path only when one version supplies nonempty patterns for at least 99% of **all** matching rows. This is the dominant version, not necessarily the newest.\n- On the stored-pattern path, `represented_count` counts the rows in the returned groups. `remainder_count` accounts for other versions, unstamped rows, the long tail and groups outside the display limit. Report this remainder rather than implying the returned groups cover everything.\n- `pattern` — the template. Body mining masks `<uuid>`, `<ip>`, `<hex>`, `<num>`, and `<*>` for any word position that varied. Stored patterns use the ingestion vocabulary instead: `<N>`, `<TIMESTAMP>`, `<KLOGTIME>`, `<UUID>`, `<IP>`, `<HOST>`, `<HEX>`, `<ID>`, `<EMAIL>`, `<JSON_ARRAY>`, and `<JSON:keys>` for a JSON body reduced to its key set.\n- `estimated_count` / `estimated_error_count` — occurrences extrapolated to the full window. When `sampled` is false these are exact.\n- `severity_counts` — occurrences per severity, never extrapolated: sample counts when `sampled` is true, exact counts over every matching row otherwise. A template split across `info` and `error` often means the same code path logging both outcomes.\n- `services` — up to 4 service names the pattern was seen in.\n- `match_regex` — a regex over raw log bodies that matches this pattern's lines, pre-validated against the raw bodies of the pattern's own sampled rows. Always null on the stored-pattern path, which pivots with `match_patterns` instead, and null on the mining path when no trustworthy regex could be compiled. For JSON logs the pattern is mined from the extracted message field, so the regex may be unanchored, because the message is a substring of the raw line. It still targets the raw stored body.\n- `match_literal` — longest literal run of the template, a plain-text fallback when `match_regex` is null. Also null on the stored-pattern path.\n\nMining samples the window (`sampled: true` when it did): counts are estimates, and rare patterns (below roughly 1 in `scanned_count` of the volume) may be missing entirely. Narrow the `dateRange` or filters to mine a finer-grained sample.\n\n## Pivoting to a pattern's raw lines\n\nTo fetch the lines behind a pattern, call `query-logs` with the appropriate filters in `filterGroup`:\n\n- If `match_patterns` is nonempty, use **both** exact filters instead of a message predicate: `{ \"key\": \"pattern\", \"value\": [\"<canonical member>\", \"...\"], \"operator\": \"exact\", \"type\": \"log\" }` and `{ \"key\": \"pattern_version\", \"value\": 3, \"operator\": \"exact\", \"type\": \"log\" }`. Substitute the returned members and version, and combine the filters with AND. Keep the original time range and filters. Do not narrow these exact groups using example services or severities.\n- If `match_regex` is set: `{ \"key\": \"message\", \"value\": \"<match_regex>\", \"operator\": \"regex\", \"type\": \"log\" }`\n- Else if `match_literal` is set: `{ \"key\": \"message\", \"value\": \"<match_literal>\", \"operator\": \"icontains\", \"type\": \"log\" }`\n\nFor body-mined patterns, also pass the pattern's `services` as `serviceNames` and (when every entry is one of trace/debug/info/warn/error/fatal) the keys of `severity_counts` as `severityLevels` — both make the query dramatically cheaper.\n\n# Parameters\n\n## query.dateRange\n\nDate range to mine. Defaults to the last hour (`-1h`).\n\n- `date_from`: ISO 8601 timestamp or relative format: `-1h`, `-6h`, `-1d`, `-7d`.\n- `date_to`: Same format. Omit or null for \"now\".\n\n## query.severityLevels\n\nMine only these severities: `trace`, `debug`, `info`, `warn`, `error`, `fatal`. Omit to include all levels.\n\n## query.serviceNames\n\nRestrict mining to these services. Recommended once you know the target service — it prunes the scan and spends the whole sample budget on one service's templates.\n\n## query.searchTerm\n\nFull-text search over log bodies applied before mining. Useful to mine only the sub-stream around a keyword (e.g. `timeout`).\n\n## query.filterGroup\n\nProperty filters applied before mining. Same format as `query-logs` filters.\n\n# Examples\n\n## What is this stream saying? (last hour, everything)\n\n```json\n{ \"query\": { \"dateRange\": { \"date_from\": \"-1h\" } } }\n```\n\n## Dominant error templates during an incident\n\n```json\n{\n \"query\": {\n \"severityLevels\": [\"error\", \"fatal\"],\n \"dateRange\": { \"date_from\": \"2024-01-15T09:00:00Z\", \"date_to\": \"2024-01-15T11:00:00Z\" }\n }\n}\n```\n\n## Mine one service's logs around a keyword\n\n```json\n{\n \"query\": {\n \"serviceNames\": [\"checkout\"],\n \"searchTerm\": \"payment\",\n \"dateRange\": { \"date_from\": \"-6h\" }\n }\n}\n```","category": "Logs",@@ -9213,11 +9228,11 @@}},"logs-services-create": {- "description": "Return the top-25 services by log volume in the window, each with log_count, error_count, and error_rate,\nplus a per-service sparkline. Use this as the entry point when triaging which services are worth alerting on\n— high volume × non-zero error_rate is the natural alert candidate. Far cheaper than walking\nattribute-values + per-service counts.\n\nPut all parameters in `query`:\n\n```json\n{ \"query\": { \"dateRange\": { \"date_from\": \"-24h\" } } }\n```",+ "description": "List every service that logged in the window, with its health against a baseline. All other parameters go inside `query`:\n\n```json\n{ \"query\": { \"dateRange\": { \"date_from\": \"-24h\" }, \"compare\": \"previous_day\", \"environment\": \"production\" } }\n```\n\nEach row has `log_count`, `error_count`, `error_rate_pct` and `last_seen`. With `compare`, each row also gets a `status` and a short `reason`.\nThe status is `ok`, `degraded` (5% errors or more), `regressed` or `improved` (error rate up or down by 1 pp and 2x), `recovered`, `new` (no baseline logs) or `silent` (baseline logs, none now).\nSilent services appear only with a baseline.\nThis is the entry point for \"which services are silent, new or worse\".\nUse `logs-group-by` for counts by pattern or attribute, `logs-patterns-diff` for the error patterns behind a regression, and `logs-count-ranges` for when a spike started.\n\n`compare` is `none` (default), `previous_period`, `previous_day` or `previous_week`. `baselineDateRange`, next to `query`, sets an explicit baseline.\n`environment` matches the first set resource attribute of `deployment.environment.name`, `deployment.environment` and `env`.\n`window` gives the absolute UTC bounds. `next` holds prefilled follow-up calls. It is empty when `environment` is set, because those tools do not filter by environment.","category": "Logs","feature": "logs",- "summary": "List services with stats",- "title": "List services with stats",+ "summary": "List services with health status",+ "title": "List services with health status","required_scopes": ["logs:read"],"annotations": {"destructiveHint": false,diff --git a/services/mcp/schema/tool-definitions-all.json b/services/mcp/schema/tool-definitions-all.jsonindex e54615c393b0..47e699059d06 100644--- a/services/mcp/schema/tool-definitions-all.json+++ b/services/mcp/schema/tool-definitions-all.json@@ -9578,6 +9578,21 @@"readOnlyHint": true}},+ "logs-group-by": {+ "description": "Count logs grouped by up to 4 dimensions in one call. All parameters go inside `query`:\n\n```json\n{ \"query\": { \"dateRange\": { \"date_from\": \"-24h\" }, \"groupBys\": [{ \"key\": \"pattern\", \"source\": \"column\" }] } }\n```\n\nEach group has `values` (one per dimension, in request order), `log_count`, `error_count`, `first_seen` and `last_seen`. `truncated` is true when more groups matched than `limit`.\n\nUse it instead of SQL for \"top errors by service\", \"which patterns fail most\" or \"which sessions are noisiest\". Use `logs-services-create` for a per-service health summary against a baseline, `logs-patterns-diff` for new or growing patterns, and `query-logs` to read the rows of one group.\n\nDimensions in `groupBys`:\n\n- `source: \"column\"`: `service_name`, `severity_level`, `pattern` (the masked message template), `trace_id`, `span_id`.\n- `source: \"log\"`: a log attribute key, for example `session_id`.\n- `source: \"resource\"`: a resource attribute key, for example `k8s.pod.name`.\n\nRows without a value for a dimension are not counted. `orderGroupsBy` is `log_count`, `error_count` or `last_seen` (descending). Filter with `severityLevels`, `serviceNames`, `searchTerm` and `filterGroup`. Results are cached for 60 seconds.",+ "category": "Logs",+ "feature": "logs",+ "summary": "Group logs by attributes",+ "title": "Group logs by attributes",+ "required_scopes": ["logs:read"],+ "annotations": {+ "destructiveHint": false,+ "idempotentHint": true,+ "openWorldHint": true,+ "readOnlyHint": true+ },+ "feature_flag": "logs-agent-tools"+ },"logs-patterns": {"description": "Mine recurring log templates (\"patterns\") from the logs matching a filter set, ordered by frequency. Each pattern is a message template with the variable parts masked — e.g. `Connected to <ip> in <num>ms` — plus occurrence estimates, severity mix, the services it appears in, and a ready-made predicate for fetching its matching lines.\n\nAll parameters go inside `query` — top-level fields are rejected:\n\n```json\n{ \"query\": { \"serviceNames\": [\"api\"], \"dateRange\": { \"date_from\": \"-1h\" } } }\n```\n\nThis is the fastest way to understand what a log stream is _saying_ without reading raw rows: one call summarizes millions of lines into at most 200 templates.\n\n# When to use\n\n- To triage an unfamiliar or noisy stream: mine the last hour, scan the top templates by `estimated_count`, and look for anything with a non-zero error share in `severity_counts`.\n- To find what's new or dominant during an incident window: mine with `severityLevels: [\"error\", \"fatal\"]` and a `dateRange` covering the incident.\n- As the entry point of a drill-down loop: mine → pick a suspicious pattern → filter logs to exactly its lines using `match_patterns`, or `match_regex` and `match_literal` when it is empty (see below) → read the raw rows with `query-logs`.\n- To quantify repetition before proposing log sampling or cleanup: `volume_share_pct` tells you how much of the stream one template accounts for.\n\n## Pick the right tool\n\n- Raw log lines matching a filter → `query-logs`.\n- A single number (how many logs match) → `logs-count`; per-time-bucket counts → `logs-count-ranges`.\n- Distribution of a dimension (which services emit the errors?) → `logs-facet-values-create`.\n- Use **this** tool to summarize message _content_ — what distinct things the logs say and how often.\n\n# Reading the response\n\n- `source` reports what was used: `stored_patterns` for exact stored-pattern aggregation followed by Drain3 grouping, or `body_mining` for body masking and Drain3. Include this distinction in your answer.\n- `pattern_version` identifies the version used for stored patterns. `fallback_reason` explains body mining: flag disabled, insufficient version coverage, an empty window, or comparison mode.\n- The `logs_patterns_query_v2` flag enables the stored-pattern path only when one version supplies nonempty patterns for at least 99% of **all** matching rows. This is the dominant version, not necessarily the newest.\n- On the stored-pattern path, `represented_count` counts the rows in the returned groups. `remainder_count` accounts for other versions, unstamped rows, the long tail and groups outside the display limit. Report this remainder rather than implying the returned groups cover everything.\n- `pattern` — the template. Body mining masks `<uuid>`, `<ip>`, `<hex>`, `<num>`, and `<*>` for any word position that varied. Stored patterns use the ingestion vocabulary instead: `<N>`, `<TIMESTAMP>`, `<KLOGTIME>`, `<UUID>`, `<IP>`, `<HOST>`, `<HEX>`, `<ID>`, `<EMAIL>`, `<JSON_ARRAY>`, and `<JSON:keys>` for a JSON body reduced to its key set.\n- `estimated_count` / `estimated_error_count` — occurrences extrapolated to the full window. When `sampled` is false these are exact.\n- `severity_counts` — occurrences per severity, never extrapolated: sample counts when `sampled` is true, exact counts over every matching row otherwise. A template split across `info` and `error` often means the same code path logging both outcomes.\n- `services` — up to 4 service names the pattern was seen in.\n- `match_regex` — a regex over raw log bodies that matches this pattern's lines, pre-validated against the raw bodies of the pattern's own sampled rows. Always null on the stored-pattern path, which pivots with `match_patterns` instead, and null on the mining path when no trustworthy regex could be compiled. For JSON logs the pattern is mined from the extracted message field, so the regex may be unanchored, because the message is a substring of the raw line. It still targets the raw stored body.\n- `match_literal` — longest literal run of the template, a plain-text fallback when `match_regex` is null. Also null on the stored-pattern path.\n\nMining samples the window (`sampled: true` when it did): counts are estimates, and rare patterns (below roughly 1 in `scanned_count` of the volume) may be missing entirely. Narrow the `dateRange` or filters to mine a finer-grained sample.\n\n## Pivoting to a pattern's raw lines\n\nTo fetch the lines behind a pattern, call `query-logs` with the appropriate filters in `filterGroup`:\n\n- If `match_patterns` is nonempty, use **both** exact filters instead of a message predicate: `{ \"key\": \"pattern\", \"value\": [\"<canonical member>\", \"...\"], \"operator\": \"exact\", \"type\": \"log\" }` and `{ \"key\": \"pattern_version\", \"value\": 3, \"operator\": \"exact\", \"type\": \"log\" }`. Substitute the returned members and version, and combine the filters with AND. Keep the original time range and filters. Do not narrow these exact groups using example services or severities.\n- If `match_regex` is set: `{ \"key\": \"message\", \"value\": \"<match_regex>\", \"operator\": \"regex\", \"type\": \"log\" }`\n- Else if `match_literal` is set: `{ \"key\": \"message\", \"value\": \"<match_literal>\", \"operator\": \"icontains\", \"type\": \"log\" }`\n\nFor body-mined patterns, also pass the pattern's `services` as `serviceNames` and (when every entry is one of trace/debug/info/warn/error/fatal) the keys of `severity_counts` as `severityLevels` — both make the query dramatically cheaper.\n\n# Parameters\n\n## query.dateRange\n\nDate range to mine. Defaults to the last hour (`-1h`).\n\n- `date_from`: ISO 8601 timestamp or relative format: `-1h`, `-6h`, `-1d`, `-7d`.\n- `date_to`: Same format. Omit or null for \"now\".\n\n## query.severityLevels\n\nMine only these severities: `trace`, `debug`, `info`, `warn`, `error`, `fatal`. Omit to include all levels.\n\n## query.serviceNames\n\nRestrict mining to these services. Recommended once you know the target service — it prunes the scan and spends the whole sample budget on one service's templates.\n\n## query.searchTerm\n\nFull-text search over log bodies applied before mining. Useful to mine only the sub-stream around a keyword (e.g. `timeout`).\n\n## query.filterGroup\n\nProperty filters applied before mining. Same format as `query-logs` filters.\n\n# Examples\n\n## What is this stream saying? (last hour, everything)\n\n```json\n{ \"query\": { \"dateRange\": { \"date_from\": \"-1h\" } } }\n```\n\n## Dominant error templates during an incident\n\n```json\n{\n \"query\": {\n \"severityLevels\": [\"error\", \"fatal\"],\n \"dateRange\": { \"date_from\": \"2024-01-15T09:00:00Z\", \"date_to\": \"2024-01-15T11:00:00Z\" }\n }\n}\n```\n\n## Mine one service's logs around a keyword\n\n```json\n{\n \"query\": {\n \"serviceNames\": [\"checkout\"],\n \"searchTerm\": \"payment\",\n \"dateRange\": { \"date_from\": \"-6h\" }\n }\n}\n```","category": "Logs",@@ -9607,11 +9622,11 @@}},"logs-services-create": {- "description": "Return the top-25 services by log volume in the window, each with log_count, error_count, and error_rate,\nplus a per-service sparkline. Use this as the entry point when triaging which services are worth alerting on\n— high volume × non-zero error_rate is the natural alert candidate. Far cheaper than walking\nattribute-values + per-service counts.\n\nPut all parameters in `query`:\n\n```json\n{ \"query\": { \"dateRange\": { \"date_from\": \"-24h\" } } }\n```",+ "description": "List every service that logged in the window, with its health against a baseline. All other parameters go inside `query`:\n\n```json\n{ \"query\": { \"dateRange\": { \"date_from\": \"-24h\" }, \"compare\": \"previous_day\", \"environment\": \"production\" } }\n```\n\nEach row has `log_count`, `error_count`, `error_rate_pct` and `last_seen`. With `compare`, each row also gets a `status` and a short `reason`.\nThe status is `ok`, `degraded` (5% errors or more), `regressed` or `improved` (error rate up or down by 1 pp and 2x), `recovered`, `new` (no baseline logs) or `silent` (baseline logs, none now).\nSilent services appear only with a baseline.\nThis is the entry point for \"which services are silent, new or worse\".\nUse `logs-group-by` for counts by pattern or attribute, `logs-patterns-diff` for the error patterns behind a regression, and `logs-count-ranges` for when a spike started.\n\n`compare` is `none` (default), `previous_period`, `previous_day` or `previous_week`. `baselineDateRange`, next to `query`, sets an explicit baseline.\n`environment` matches the first set resource attribute of `deployment.environment.name`, `deployment.environment` and `env`.\n`window` gives the absolute UTC bounds. `next` holds prefilled follow-up calls. It is empty when `environment` is set, because those tools do not filter by environment.","category": "Logs","feature": "logs",- "summary": "List services with stats",- "title": "List services with stats",+ "summary": "List services with health status",+ "title": "List services with health status","required_scopes": ["logs:read"],"annotations": {"destructiveHint": false,diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 4ccbc69cc08e..e3c7af931991 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -64761,6 +64761,22 @@ export namespace Schemas {issues: LogsAnomalyScanIssue[];}+ /**+ * * `none` - No baseline+ * * `previous_period` - Previous period+ * * `previous_day` - Same window a day earlier+ * * `previous_week` - Same window a week earlier+ */+ export type LogsCompareModeEnum = typeof LogsCompareModeEnum[keyof typeof LogsCompareModeEnum];+++ export const LogsCompareModeEnum = {+ None: 'none',+ PreviousPeriod: 'previous_period',+ PreviousDay: 'previous_day',+ PreviousWeek: 'previous_week',+ } as const;+/*** * `severity_sampling` - Severity-based reduction* * `path_drop` - Path exclusion@@ -65444,6 +65460,42 @@ export namespace Schemas {series: LogsSeriesBandSeries[];}+ /**+ * * `ok` - OK+ * * `degraded` - Error rate 5% or more+ * * `regressed` - Error rate worse than the baseline+ * * `improved` - Error rate better than the baseline+ * * `recovered` - No longer degraded+ * * `new` - No logs in the baseline+ * * `silent` - Logs stopped+ */+ export type LogsServiceStatusEnum = typeof LogsServiceStatusEnum[keyof typeof LogsServiceStatusEnum];+++ export const LogsServiceStatusEnum = {+ Ok: 'ok',+ Degraded: 'degraded',+ Regressed: 'regressed',+ Improved: 'improved',+ Recovered: 'recovered',+ New: 'new',+ Silent: 'silent',+ } as const;++ /**+ * * `rollup` - Rollup+ * * `rollup+raw` - Rollup Raw+ * * `raw` - Raw+ */+ export type LogsServicesSourceEnum = typeof LogsServicesSourceEnum[keyof typeof LogsServicesSourceEnum];+++ export const LogsServicesSourceEnum = {+ Rollup: 'rollup',+ RollupRaw: 'rollup+raw',+ Raw: 'raw',+ } as const;+/*** Filter criteria — subset of LogsViewerFilters. May contain severityLevels, serviceNames, searchTerm, filterGroup, dateRange, and other keys.*/@@ -117583,7 +117635,7 @@ export namespace Schemas {}export interface _LogsGroupByDimension {- /** The key this dimension groups by — an attribute key (e.g. "session_id", "service.name") or, when source is "column", one of the top-level log fields: "severity_level", "trace_id", "span_id". */+ /** The key this dimension groups by — an attribute key (e.g. "session_id", "service.name") or, when source is "column", one of the top-level log fields: "severity_level", "service_name", "trace_id", "span_id", "pattern" (the masked message template). */key: string;/** Where this dimension's key lives: "log" for log-level attributes, "resource" for resource-level attributes, "column" for top-level log fields.*@@ -117604,7 +117656,7 @@ export namespace Schemas {searchTerm?: string;/** Property filters applied before grouping. Same shape as the query-logs endpoint. */filterGroup?: _LogPropertyFilter[];- /** The key to group logs by — an attribute key (e.g. "session_id", "service.name") or, when groupBySource is "column", one of the top-level log fields: "severity_level", "trace_id", "span_id". Ignored when groupBys is provided. */+ /** The key to group logs by — an attribute key (e.g. "session_id", "service.name") or, when groupBySource is "column", one of the top-level log fields: "severity_level", "service_name", "trace_id", "span_id", "pattern". Ignored when groupBys is provided. */groupBy?: string;/** Where the grouping key lives: "log" for log-level attributes, "resource" for resource-level attributes, "column" for top-level log fields. Ignored when groupBys is provided.*@@ -117645,6 +117697,8 @@ export namespace Schemas {log_count: number;/** Number of matching logs in this group at severity "error" or "fatal". */error_count: number;+ /** ISO 8601 timestamp of the earliest matching log in this group. */+ first_seen?: string;/** ISO 8601 timestamp of the most recent matching log in this group. */last_seen: string;}@@ -117714,6 +117768,27 @@ export namespace Schemas {error: string;}+ /**+ * Prefilled `query` for that tool.+ */+ export type _LogsNextStepQuery = { [key: string]: unknown };++ /**+ * Prefilled sibling `baselineDateRange` argument, for tools that take one.+ */+ export type _LogsNextStepBaselineDateRange = { [key: string]: unknown };++ export interface _LogsNextStep {+ /** MCP tool to call next. */+ tool: string;+ /** What the call answers. */+ why: string;+ /** Prefilled `query` for that tool. */+ query: _LogsNextStepQuery;+ /** Prefilled sibling `baselineDateRange` argument, for tools that take one. */+ baselineDateRange?: _LogsNextStepBaselineDateRange;+ }+export interface _LogsPatternsBody {/** Date range to mine patterns from. Defaults to last hour. */dateRange?: _DateRange;@@ -117930,6 +118005,40 @@ export namespace Schemas {export interface _LogsServiceAggregate {/** Service name, or "(no value)" / "(no service)" placeholder for unset entries. */service_name: string;+ /** ok, degraded (error rate of 5% or more), regressed or improved (error rate up or down by 1 pp and 2x), recovered (back under 5%), new (no baseline logs) or silent (baseline logs, none in the window).+ *+ * * `ok` - OK+ * * `degraded` - Error rate 5% or more+ * * `regressed` - Error rate worse than the baseline+ * * `improved` - Error rate better than the baseline+ * * `recovered` - No longer degraded+ * * `new` - No logs in the baseline+ * * `silent` - Logs stopped */+ status?: LogsServiceStatusEnum;+ /** Short reason for the status, e.g. "error rate 0.4% → 3.1%". */+ reason?: string;+ /** Error rate in percent, rounded to 1 decimal. */+ error_rate_pct?: number;+ /**+ * Logs in the baseline. Null without one.+ * @nullable+ */+ baseline_log_count?: number | null;+ /**+ * Error rate change against the baseline, in percentage points.+ * @nullable+ */+ error_rate_delta_pp?: number | null;+ /**+ * Log volume change against the baseline, in percent.+ * @nullable+ */+ log_count_change_pct?: number | null;+ /**+ * Latest log time (UTC). From the rollup, and for silent services, the start of the last 5-minute bucket.+ * @nullable+ */+ last_seen?: string | null;/** Total log entries from this service in the window. */log_count: number;/** Count of logs at severity "error" or "fatal". */@@ -117960,11 +118069,25 @@ export namespace Schemas {serviceNameSearch?: string;/** Property filters for the query. */filterGroup?: _LogPropertyFilter[];+ /**+ * Deployment environment, matched without case against the first set resource attribute of deployment.environment.name, deployment.environment and env. "(none)" matches logs without one.+ * @maxLength 200+ */+ environment?: string;+ /** Baseline window for per-service status: the window just before, the same window a day or a week earlier, or none. Silent services (logs in the baseline, none now) only appear with a baseline.+ *+ * * `none` - No baseline+ * * `previous_period` - Previous period+ * * `previous_day` - Same window a day earlier+ * * `previous_week` - Same window a week earlier */+ compare?: LogsCompareModeEnum;}export interface _LogsServicesRequest {/** The services aggregation query to execute. */query: _LogsServicesBody;+ /** Explicit baseline window. Takes precedence over `query.compare`. */+ baselineDateRange?: _DateRange;}export interface _LogsServicesSparklineBucket {@@ -117974,11 +118097,39 @@ export namespace Schemas {count: number;}+ /**+ * Service rows per status.+ */+ export type _LogsServicesSummaryStatusCounts = {[key: string]: number};+export interface _LogsServicesSummary {/** Number of top services included in the volume_share aggregate (up to 5). */top_services_count: number;/** Combined volume share (percent) of the top services by log_count. */top_services_volume_share_pct: number;+ /** Service rows per status. */+ status_counts?: _LogsServicesSummaryStatusCounts;+ /** True when more services matched than `services` holds. */+ truncated?: boolean;+ }++ /**+ * `from` and `to` of the queried window, ISO 8601 UTC.+ */+ export type _LogsWindowCurrent = {[key: string]: string};++ /**+ * `from` and `to` of the baseline. Absent without one.+ */+ export type _LogsWindowBaseline = {[key: string]: string};++ export interface _LogsWindow {+ /** `from` and `to` of the queried window, ISO 8601 UTC. */+ current: _LogsWindowCurrent;+ /** `from` and `to` of the baseline. Absent without one. */+ baseline?: _LogsWindowBaseline;+ /** Always UTC. */+ timezone: string;}export interface _LogsServicesResponse {@@ -117990,6 +118141,16 @@ export namespace Schemas {total_services: number;/** Roll-up stats for the Services tab header. */summary?: _LogsServicesSummary;+ /** Absolute UTC bounds of the window and the baseline. */+ window?: _LogsWindow;+ /** rollup: 5-minute rollup only. rollup+raw: rollup plus raw logs at unaligned window edges.+ *+ * * `rollup` - Rollup+ * * `rollup+raw` - Rollup Raw+ * * `raw` - Raw */+ source?: LogsServicesSourceEnum;+ /** Prefilled follow-up calls for problem services. */+ next?: _LogsNextStep[];}export interface _LogsSparklineBody {@@ -129638,8 +129799,7 @@ export namespace Schemas {*/serviceNames?: string[];/**- * Search filter for attribute values- * @minLength 1+ * Search filter for attribute values. Omit it or pass an empty string to list all values.*/value?: string;};diff --git a/services/mcp/src/generated/logs/api.ts b/services/mcp/src/generated/logs/api.tsindex 805b7a5cf80f..019aa0a7158c 100644--- a/services/mcp/src/generated/logs/api.ts+++ b/services/mcp/src/generated/logs/api.ts@@ -3,7 +3,7 @@* MCP service uses these Zod schemas for generated tool handlers.* To regenerate: hogli build:openapi*- * PostHog API - MCP 21 enabled ops+ * PostHog API - MCP 22 enabled ops* OpenAPI spec version: 1.0.0*/import * as zod from 'zod'@@ -6052,6 +6052,169 @@ export const LogsFacetValuesCreateBody = () => zod.object({.describe('The facet values query to execute.'),})+export const LogsGroupByCreateParams = () => zod.object({+ project_id: zod+ .string()+ .describe(+ "Project ID of the project you're trying to access. To find the ID of the project, make a call to \/api\/projects\/."+ ),+})++export const logsGroupByCreateBodyQueryOneGroupBySourceDefault = `log`+export const logsGroupByCreateBodyQueryOneGroupBysItemSourceDefault = `log`+export const logsGroupByCreateBodyQueryOneGroupBysMax = 4++export const logsGroupByCreateBodyQueryOneOrderGroupsByDefault = `log_count`+export const logsGroupByCreateBodyQueryOneLimitDefault = 100+export const logsGroupByCreateBodyQueryOneLimitMax = 500++export const LogsGroupByCreateBody = () => zod.object({+ query: zod+ .object({+ dateRange: zod+ .object({+ date_from: zod+ .string()+ .nullish()+ .describe(+ 'Start of the date range. Accepts ISO 8601 timestamps or relative formats: -7d, -1h, -1mStart, etc.'+ ),+ date_to: zod+ .string()+ .nullish()+ .describe('End of the date range. Same format as date_from. Omit or null for \"now\".'),+ })+ .optional()+ .describe('Date range to aggregate over. Defaults to last hour.'),+ severityLevels: zod+ .array(+ zod+ .enum(['trace', 'debug', 'info', 'warn', 'error', 'fatal'])+ .describe(+ '\* `trace` - trace\n\* `debug` - debug\n\* `info` - info\n\* `warn` - warn\n\* `error` - error\n\* `fatal` - fatal'+ )+ )+ .optional()+ .describe('Filter by log severity levels before grouping.'),+ serviceNames: zod.array(zod.string()).optional().describe('Restrict grouping to these service names.'),+ searchTerm: zod.string().optional().describe('Full-text search term to filter log bodies before grouping.'),+ filterGroup: zod+ .array(+ zod.object({+ key: zod+ .string()+ .describe(+ 'Attribute key. For type \"log\", use \"message\" for the body text, or a log column: \"pattern\" and \"pattern_version\" (the patterns pivot), \"severity_level\", \"service_name\", \"trace_id\", \"span_id\". For \"log_attribute\"\/\"log_resource_attribute\", use the attribute key (e.g. \"k8s.container.name\").'+ ),+ type: zod+ .enum(['log', 'log_attribute', 'log_resource_attribute'])+ .describe(+ '\* `log` - log\n\* `log_attribute` - log_attribute\n\* `log_resource_attribute` - log_resource_attribute'+ )+ .describe(+ '\"log\" filters the log body\/message. \"log_attribute\" filters log-level attributes. \"log_resource_attribute\" filters resource-level attributes.\n\n\* `log` - log\n\* `log_attribute` - log_attribute\n\* `log_resource_attribute` - log_resource_attribute'+ ),+ operator: zod+ .enum([+ 'exact',+ 'is_not',+ 'icontains',+ 'not_icontains',+ 'starts_with',+ 'not_starts_with',+ 'ends_with',+ 'not_ends_with',+ 'regex',+ 'not_regex',+ 'gt',+ 'lt',+ 'is_date_exact',+ 'is_date_before',+ 'is_date_after',+ 'is_set',+ 'is_not_set',+ ])+ .describe(+ '\* `exact` - exact\n\* `is_not` - is_not\n\* `icontains` - icontains\n\* `not_icontains` - not_icontains\n\* `starts_with` - starts_with\n\* `not_starts_with` - not_starts_with\n\* `ends_with` - ends_with\n\* `not_ends_with` - not_ends_with\n\* `regex` - regex\n\* `not_regex` - not_regex\n\* `gt` - gt\n\* `lt` - lt\n\* `is_date_exact` - is_date_exact\n\* `is_date_before` - is_date_before\n\* `is_date_after` - is_date_after\n\* `is_set` - is_set\n\* `is_not_set` - is_not_set'+ )+ .describe(+ 'Comparison operator.\n\n\* `exact` - exact\n\* `is_not` - is_not\n\* `icontains` - icontains\n\* `not_icontains` - not_icontains\n\* `starts_with` - starts_with\n\* `not_starts_with` - not_starts_with\n\* `ends_with` - ends_with\n\* `not_ends_with` - not_ends_with\n\* `regex` - regex\n\* `not_regex` - not_regex\n\* `gt` - gt\n\* `lt` - lt\n\* `is_date_exact` - is_date_exact\n\* `is_date_before` - is_date_before\n\* `is_date_after` - is_date_after\n\* `is_set` - is_set\n\* `is_not_set` - is_not_set'+ ),+ value: zod+ .unknown()+ .optional()+ .describe(+ 'Value to compare against. String, number, or array of strings. Omit for is_set\/is_not_set operators.'+ ),+ })+ )+ .optional()+ .describe('Property filters applied before grouping. Same shape as the query-logs endpoint.'),+ groupBy: zod+ .string()+ .optional()+ .describe(+ 'The key to group logs by — an attribute key (e.g. \"session_id\", \"service.name\") or, when groupBySource is \"column\", one of the top-level log fields: \"severity_level\", \"service_name\", \"trace_id\", \"span_id\", \"pattern\". Ignored when groupBys is provided.'+ ),+ groupBySource: zod+ .enum(['log', 'resource', 'column'])+ .describe('\* `log` - log\n\* `resource` - resource\n\* `column` - column')+ .default(logsGroupByCreateBodyQueryOneGroupBySourceDefault)+ .describe(+ 'Where the grouping key lives: \"log\" for log-level attributes, \"resource\" for resource-level attributes, \"column\" for top-level log fields. Ignored when groupBys is provided.\n\n\* `log` - log\n\* `resource` - resource\n\* `column` - column'+ ),+ groupBys: zod+ .array(+ zod.object({+ key: zod+ .string()+ .describe(+ 'The key this dimension groups by — an attribute key (e.g. \"session_id\", \"service.name\") or, when source is \"column\", one of the top-level log fields: \"severity_level\", \"service_name\", \"trace_id\", \"span_id\", \"pattern\" (the masked message template).'+ ),+ source: zod+ .enum(['log', 'resource', 'column'])+ .describe('\* `log` - log\n\* `resource` - resource\n\* `column` - column')+ .default(logsGroupByCreateBodyQueryOneGroupBysItemSourceDefault)+ .describe(+ 'Where this dimension\'s key lives: \"log\" for log-level attributes, \"resource\" for resource-level attributes, \"column\" for top-level log fields.\n\n\* `log` - log\n\* `resource` - resource\n\* `column` - column'+ ),+ })+ )+ .min(1)+ .max(logsGroupByCreateBodyQueryOneGroupBysMax)+ .optional()+ .describe(+ 'Ordered group-by dimensions to combine (a group is one combination of per-dimension values), up to 4. Takes precedence over groupBy\/groupBySource; one of the two must be provided.'+ ),+ orderGroupsBy: zod+ .enum(['log_count', 'error_count', 'last_seen'])+ .describe('\* `log_count` - log_count\n\* `error_count` - error_count\n\* `last_seen` - last_seen')+ .default(logsGroupByCreateBodyQueryOneOrderGroupsByDefault)+ .describe(+ 'Aggregate to rank groups by (descending): \"log_count\" for the noisiest groups, \"error_count\" for the most failing, \"last_seen\" for the most recent.\n\n\* `log_count` - log_count\n\* `error_count` - error_count\n\* `last_seen` - last_seen'+ ),+ limit: zod+ .number()+ .min(1)+ .max(logsGroupByCreateBodyQueryOneLimitMax)+ .default(logsGroupByCreateBodyQueryOneLimitDefault)+ .describe('Maximum number of groups to return (top-N by orderGroupsBy). Defaults to 100.'),+ personId: zod+ .string()+ .optional()+ .describe(+ "Scope grouping to one person (UUID or numeric ID). Expanded server-side to the person's distinct IDs and matched against the team's configured distinct-id log attribute keys."+ ),+ sessionId: zod+ .string()+ .optional()+ .describe(+ "Scope grouping to one session ID. Matched server-side against the team's configured session-id log attribute keys plus the built-in conventions, in both log attributes and resource attributes."+ ),+ })+ .describe('The group-by query to execute.'),+})+export const LogsPatternsCreateParams = () => zod.object({project_id: zod.string()@@ -6498,6 +6661,10 @@ export const LogsServicesCreateParams = () => zod.object({export const logsServicesCreateBodyQueryOneServiceNameSearchMax = 200+export const logsServicesCreateBodyQueryOneEnvironmentMax = 200++export const logsServicesCreateBodyQueryOneCompareDefault = `none`+export const LogsServicesCreateBody = () => zod.object({query: zod.object({@@ -6590,8 +6757,39 @@ export const LogsServicesCreateBody = () => zod.object({).optional().describe('Property filters for the query.'),+ environment: zod+ .string()+ .max(logsServicesCreateBodyQueryOneEnvironmentMax)+ .optional()+ .describe(+ 'Deployment environment, matched without case against the first set resource attribute of deployment.environment.name, deployment.environment and env. \"(none)\" matches logs without one.'+ ),+ compare: zod+ .enum(['none', 'previous_period', 'previous_day', 'previous_week'])+ .describe(+ '\* `none` - No baseline\n\* `previous_period` - Previous period\n\* `previous_day` - Same window a day earlier\n\* `previous_week` - Same window a week earlier'+ )+ .default(logsServicesCreateBodyQueryOneCompareDefault)+ .describe(+ 'Baseline window for per-service status: the window just before, the same window a day or a week earlier, or none. Silent services (logs in the baseline, none now) only appear with a baseline.\n\n\* `none` - No baseline\n\* `previous_period` - Previous period\n\* `previous_day` - Same window a day earlier\n\* `previous_week` - Same window a week earlier'+ ),}).describe('The services aggregation query to execute.'),+ baselineDateRange: zod+ .object({+ date_from: zod+ .string()+ .nullish()+ .describe(+ 'Start of the date range. Accepts ISO 8601 timestamps or relative formats: -7d, -1h, -1mStart, etc.'+ ),+ date_to: zod+ .string()+ .nullish()+ .describe('End of the date range. Same format as date_from. Omit or null for \"now\".'),+ })+ .optional()+ .describe('Explicit baseline window. Takes precedence over `query.compare`.'),})export const LogsSparklineCreateParams = () => zod.object({@@ -6881,5 +7079,8 @@ export const LogsValuesRetrieveQueryParams = () => zod.object({.array(zod.string()).default(logsValuesRetrieveQueryServiceNamesDefault).describe('Filter values to those appearing in logs from these services.'),- value: zod.string().min(1).optional().describe('Search filter for attribute values'),+ value: zod+ .string()+ .optional()+ .describe('Search filter for attribute values. Omit it or pass an empty string to list all values.'),})diff --git a/services/mcp/src/tools/generated/logs.ts b/services/mcp/src/tools/generated/logs.tsindex 242c703edfb7..95526c0edbfc 100644--- a/services/mcp/src/tools/generated/logs.ts+++ b/services/mcp/src/tools/generated/logs.ts@@ -672,6 +672,30 @@ const logsFacetValuesCreate = (): ToolBase<},})+const LogsGroupBySchema = () => {+ const LogsGroupByCreateBody = orvalSchemas.LogsGroupByCreateBody()+ return LogsGroupByCreateBody+}++const logsGroupBy = (): ToolBase<ReturnType<typeof LogsGroupBySchema>, Schemas._LogsGroupByResponse> => ({+ name: 'logs-group-by',+ schema: LogsGroupBySchema(),+ handler: async (context: Context, params: z.infer<ReturnType<typeof LogsGroupBySchema>>) => {+ const projectId = await context.stateManager.getProjectId()+ const body: Record<string, unknown> = {}+ if (params.query !== undefined) {+ body['query'] = params.query+ }+ const result = await context.api.request<Schemas._LogsGroupByResponse>({+ method: 'POST',+ path: `/api/projects/${encodeURIComponent(String(projectId))}/logs/group-by/`,+ body,+ })+ const filtered = omitResponseFields(result, ['groups.*.value']) as typeof result+ return filtered+ },+})+const LogsPatternsSchema = () => {const LogsPatternsCreateBody = orvalSchemas.LogsPatternsCreateBody()return LogsPatternsCreateBody@@ -754,12 +778,21 @@ const logsServicesCreate = (): ToolBase<if (params.query !== undefined) {body['query'] = params.query}+ if (params.baselineDateRange !== undefined) {+ body['baselineDateRange'] = params.baselineDateRange+ }const result = await context.api.request<Schemas._LogsServicesResponse>({method: 'POST',path: `/api/projects/${encodeURIComponent(String(projectId))}/logs/services/`,body,})- const filtered = pickResponseFields(result, ['services', 'sparkline']) as typeof result+ const filtered = pickResponseFields(result, [+ 'services',+ 'sparkline',+ 'window',+ 'summary',+ 'next',+ ]) as typeof resultreturn filtered},})@@ -831,6 +864,7 @@ export const GENERATED_TOOLS: Record<string, () => ToolBase<ZodObjectAny>> = {'logs-count': logsCount,'logs-count-ranges': logsCountRanges,'logs-facet-values-create': logsFacetValuesCreate,+ 'logs-group-by': logsGroupBy,'logs-patterns': logsPatterns,'logs-patterns-diff': logsPatternsDiff,'logs-services-create': logsServicesCreate,diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/logs-attribute-values-list.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/logs-attribute-values-list.jsonindex 21c0ba91ed62..77beba68deaa 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/logs-attribute-values-list.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/logs-attribute-values-list.json@@ -94,8 +94,7 @@"type": "array"},"value": {- "description": "Search filter for attribute values",- "minLength": 1,+ "description": "Search filter for attribute values. Omit it or pass an empty string to list all values.","type": "string"}},diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/logs-group-by.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/logs-group-by.jsonnew file mode 100644index 000000000000..1efec00365c1--- /dev/null+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/logs-group-by.json@@ -0,0 +1,159 @@+{+ "$schema": "https://json-schema.org/draft/2020-12/schema",+ "properties": {+ "query": {+ "description": "The group-by query to execute.",+ "properties": {+ "dateRange": {+ "description": "Date range to aggregate over. Defaults to last hour.",+ "properties": {+ "date_from": {+ "anyOf": [+ {+ "type": "string"+ },+ {+ "type": "null"+ }+ ],+ "description": "Start of the date range. Accepts ISO 8601 timestamps or relative formats: -7d, -1h, -1mStart, etc."+ },+ "date_to": {+ "anyOf": [+ {+ "type": "string"+ },+ {+ "type": "null"+ }+ ],+ "description": "End of the date range. Same format as date_from. Omit or null for \"now\"."+ }+ },+ "type": "object"+ },+ "filterGroup": {+ "description": "Property filters applied before grouping. Same shape as the query-logs endpoint.",+ "items": {+ "properties": {+ "key": {+ "description": "Attribute key. For type \"log\", use \"message\" for the body text, or a log column: \"pattern\" and \"pattern_version\" (the patterns pivot), \"severity_level\", \"service_name\", \"trace_id\", \"span_id\". For \"log_attribute\"/\"log_resource_attribute\", use the attribute key (e.g. \"k8s.container.name\").",+ "type": "string"+ },+ "operator": {+ "description": "Comparison operator.\n\n* `exact` - exact\n* `is_not` - is_not\n* `icontains` - icontains\n* `not_icontains` - not_icontains\n* `starts_with` - starts_with\n* `not_starts_with` - not_starts_with\n* `ends_with` - ends_with\n* `not_ends_with` - not_ends_with\n* `regex` - regex\n* `not_regex` - not_regex\n* `gt` - gt\n* `lt` - lt\n* `is_date_exact` - is_date_exact\n* `is_date_before` - is_date_before\n* `is_date_after` - is_date_after\n* `is_set` - is_set\n* `is_not_set` - is_not_set",+ "enum": [+ "exact",+ "is_not",+ "icontains",+ "not_icontains",+ "starts_with",+ "not_starts_with",+ "ends_with",+ "not_ends_with",+ "regex",+ "not_regex",+ "gt",+ "lt",+ "is_date_exact",+ "is_date_before",+ "is_date_after",+ "is_set",+ "is_not_set"+ ],+ "type": "string"+ },+ "type": {+ "description": "\"log\" filters the log body/message. \"log_attribute\" filters log-level attributes. \"log_resource_attribute\" filters resource-level attributes.\n\n* `log` - log\n* `log_attribute` - log_attribute\n* `log_resource_attribute` - log_resource_attribute",+ "enum": ["log", "log_attribute", "log_resource_attribute"],+ "type": "string"+ },+ "value": {+ "description": "Value to compare against. String, number, or array of strings. Omit for is_set/is_not_set operators."+ }+ },+ "required": ["key", "type", "operator"],+ "type": "object"+ },+ "type": "array"+ },+ "groupBy": {+ "description": "The key to group logs by — an attribute key (e.g. \"session_id\", \"service.name\") or, when groupBySource is \"column\", one of the top-level log fields: \"severity_level\", \"service_name\", \"trace_id\", \"span_id\", \"pattern\". Ignored when groupBys is provided.",+ "type": "string"+ },+ "groupBys": {+ "description": "Ordered group-by dimensions to combine (a group is one combination of per-dimension values), up to 4. Takes precedence over groupBy/groupBySource; one of the two must be provided.",+ "items": {+ "properties": {+ "key": {+ "description": "The key this dimension groups by — an attribute key (e.g. \"session_id\", \"service.name\") or, when source is \"column\", one of the top-level log fields: \"severity_level\", \"service_name\", \"trace_id\", \"span_id\", \"pattern\" (the masked message template).",+ "type": "string"+ },+ "source": {+ "default": "log",+ "description": "Where this dimension's key lives: \"log\" for log-level attributes, \"resource\" for resource-level attributes, \"column\" for top-level log fields.\n\n* `log` - log\n* `resource` - resource\n* `column` - column",+ "enum": ["log", "resource", "column"],+ "type": "string"+ }+ },+ "required": ["key"],+ "type": "object"+ },+ "maxItems": 4,+ "minItems": 1,+ "type": "array"+ },+ "groupBySource": {+ "default": "log",+ "description": "Where the grouping key lives: \"log\" for log-level attributes, \"resource\" for resource-level attributes, \"column\" for top-level log fields. Ignored when groupBys is provided.\n\n* `log` - log\n* `resource` - resource\n* `column` - column",+ "enum": ["log", "resource", "column"],+ "type": "string"+ },+ "limit": {+ "default": 100,+ "description": "Maximum number of groups to return (top-N by orderGroupsBy). Defaults to 100.",+ "maximum": 500,+ "minimum": 1,+ "type": "number"+ },+ "orderGroupsBy": {+ "default": "log_count",+ "description": "Aggregate to rank groups by (descending): \"log_count\" for the noisiest groups, \"error_count\" for the most failing, \"last_seen\" for the most recent.\n\n* `log_count` - log_count\n* `error_count` - error_count\n* `last_seen` - last_seen",+ "enum": ["log_count", "error_count", "last_seen"],+ "type": "string"+ },+ "personId": {+ "description": "Scope grouping to one person (UUID or numeric ID). Expanded server-side to the person's distinct IDs and matched against the team's configured distinct-id log attribute keys.",+ "type": "string"+ },+ "searchTerm": {+ "description": "Full-text search term to filter log bodies before grouping.",+ "type": "string"+ },+ "serviceNames": {+ "description": "Restrict grouping to these service names.",+ "items": {+ "type": "string"+ },+ "type": "array"+ },+ "sessionId": {+ "description": "Scope grouping to one session ID. Matched server-side against the team's configured session-id log attribute keys plus the built-in conventions, in both log attributes and resource attributes.",+ "type": "string"+ },+ "severityLevels": {+ "description": "Filter by log severity levels before grouping.",+ "items": {+ "description": "* `trace` - trace\n* `debug` - debug\n* `info` - info\n* `warn` - warn\n* `error` - error\n* `fatal` - fatal",+ "enum": ["trace", "debug", "info", "warn", "error", "fatal"],+ "type": "string"+ },+ "type": "array"+ }+ },+ "type": "object"+ }+ },+ "required": ["query"],+ "type": "object"+}diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/logs-services-create.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/logs-services-create.jsonindex db01bea830ce..db2c3be2bc75 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/logs-services-create.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/logs-services-create.json@@ -1,9 +1,43 @@{"$schema": "https://json-schema.org/draft/2020-12/schema","properties": {+ "baselineDateRange": {+ "description": "Explicit baseline window. Takes precedence over `query.compare`.",+ "properties": {+ "date_from": {+ "anyOf": [+ {+ "type": "string"+ },+ {+ "type": "null"+ }+ ],+ "description": "Start of the date range. Accepts ISO 8601 timestamps or relative formats: -7d, -1h, -1mStart, etc."+ },+ "date_to": {+ "anyOf": [+ {+ "type": "string"+ },+ {+ "type": "null"+ }+ ],+ "description": "End of the date range. Same format as date_from. Omit or null for \"now\"."+ }+ },+ "type": "object"+ },"query": {"description": "The services aggregation query to execute.","properties": {+ "compare": {+ "default": "none",+ "description": "Baseline window for per-service status: the window just before, the same window a day or a week earlier, or none. Silent services (logs in the baseline, none now) only appear with a baseline.\n\n* `none` - No baseline\n* `previous_period` - Previous period\n* `previous_day` - Same window a day earlier\n* `previous_week` - Same window a week earlier",+ "enum": ["none", "previous_period", "previous_day", "previous_week"],+ "type": "string"+ },"dateRange": {"description": "Date range for the services aggregation. Defaults to last hour.","properties": {@@ -32,6 +66,11 @@},"type": "object"},+ "environment": {+ "description": "Deployment environment, matched without case against the first set resource attribute of deployment.environment.name, deployment.environment and env. \"(none)\" matches logs without one.",+ "maxLength": 200,+ "type": "string"+ },"filterGroup": {"description": "Property filters for the query.","items": {diff --git a/services/mcp/tests/unit/tool-filtering.test.ts b/services/mcp/tests/unit/tool-filtering.test.tsindex bc954a23217c..4ee04b70fcb3 100644--- a/services/mcp/tests/unit/tool-filtering.test.ts+++ b/services/mcp/tests/unit/tool-filtering.test.ts@@ -1096,6 +1096,7 @@ describe('Tool Filtering - Feature Flags', () => {'managed-warehouse-trino-query','llm-analytics-tags','insight-data-checks',+ 'logs-agent-tools',]expect(allFlags).toEqual(expect.arrayContaining(branchFlags))// The flags branches add are asserted on the line above and held out of the list anddiff --git a/services/mcp/tests/unit/tool-schema-snapshots.test.ts b/services/mcp/tests/unit/tool-schema-snapshots.test.tsindex 7fe563329527..4755b4f7727a 100644--- a/services/mcp/tests/unit/tool-schema-snapshots.test.ts+++ b/services/mcp/tests/unit/tool-schema-snapshots.test.ts@@ -91,6 +91,7 @@ async function loadSnapshotTools(): Promise<Tool<ZodObjectAny>[]> {// agent-feedback is always_available and no longer flag-gated, so it appears regardless.const featureFlags = {tracing: true,+ 'logs-agent-tools': true,tasks: true,'tasks-mcp-agent-run-start': true,loops: true,