Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(logs): mask ISO-8601 timestamps in pattern miner templates (#81752)

Jon McCallumc50cfa47b681
diff --git a/products/logs/backend/log_patterns.py b/products/logs/backend/log_patterns.py
index eb71fd2fa9f7..25477611f7a9 100644
--- a/products/logs/backend/log_patterns.py
+++ b/products/logs/backend/log_patterns.py
@@ -15,6 +15,9 @@ ERROR_SEVERITIES = {"error", "fatal"}
# one cluster per distinct value. Order matters — Drain applies these in sequence, so
# more-specific patterns (uuid, ip, hex) run before the catch-all number mask.
_MASKING_INSTRUCTIONS = [
+ # Timestamp must precede the number catch-all: \b never matches between a digit and
+ # a letter, so "12T08" and "397557Z" in ISO-8601 bodies would survive \b\d+\b intact.
+ MaskingInstruction(r"\b\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:[.,]\d+)?(?:Z|[+-]\d{2}:?\d{2})?", "timestamp"),
MaskingInstruction(r"\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b", "uuid"),
MaskingInstruction(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", "ip"),
MaskingInstruction(r"\b0x[0-9a-fA-F]+\b", "hex"),
@@ -31,6 +34,7 @@ _WHITESPACE_RE = re.compile(r"\s+")
_PLACEHOLDER_PATTERNS = {
"<*>": r"\S+",
"<num>": r"\d+",
+ "<timestamp>": r"\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:[.,]\d+)?(?:Z|[+-]\d{2}:?\d{2})?",
"<uuid>": r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}",
"<ip>": r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",
"<hex>": r"(?:0x[0-9a-fA-F]+|[0-9a-fA-F]{16,})",
diff --git a/products/logs/backend/presentation/views/api.py b/products/logs/backend/presentation/views/api.py
index 431bf7d0b1a6..35bd5c5358a1 100644
--- a/products/logs/backend/presentation/views/api.py
+++ b/products/logs/backend/presentation/views/api.py
@@ -760,7 +760,7 @@ class _LogPatternSerializer(serializers.Serializer):
pattern = serializers.CharField(
help_text=(
'Mined log template with variable tokens masked, e.g. "Connected to <ip> in <num>ms". '
- "Tokens: <uuid>, <ip>, <hex>, <num>, plus <*> for word positions Drain found to vary."
+ "Tokens: <timestamp>, <uuid>, <ip>, <hex>, <num>, plus <*> for word positions Drain found to vary."
),
)
count = serializers.IntegerField(
diff --git a/products/logs/backend/test/test_log_patterns.py b/products/logs/backend/test/test_log_patterns.py
index 12270f3cd573..212909e66c04 100644
--- a/products/logs/backend/test/test_log_patterns.py
+++ b/products/logs/backend/test/test_log_patterns.py
@@ -5,7 +5,13 @@ from unittest import TestCase
from parameterized import parameterized
-from products.logs.backend.log_patterns import LogSample, compile_match_regex, extract_match_literal, mine_patterns
+from products.logs.backend.log_patterns import (
+ LogSample,
+ compile_match_regex,
+ extract_match_literal,
+ mine_patterns,
+ pattern_fingerprint,
+)
def _sample(
@@ -53,6 +59,33 @@ class TestMinePatterns(TestCase):
"<uuid>",
"446655440000",
),
+ (
+ "timestamp_iso_t",
+ [
+ "job 2026-08-12T08:10:43.397557Z retried",
+ "job 2026-08-13T09:11:44.123456Z retried",
+ ],
+ "<timestamp>",
+ "397557Z",
+ ),
+ (
+ "timestamp_space_separated",
+ [
+ "job 2026-08-12 08:10:43.397557 retried",
+ "job 2026-08-13 09:11:44.123456 retried",
+ ],
+ "<timestamp>",
+ "397557",
+ ),
+ (
+ "timestamp_utc_offset",
+ [
+ "job 2026-08-12T08:10:43+00:00 retried",
+ "job 2026-08-13T09:11:44+02:00 retried",
+ ],
+ "<timestamp>",
+ "12T08",
+ ),
]
)
def test_masking_collapses_variable_tokens(
@@ -152,6 +185,24 @@ class TestMinePatterns(TestCase):
for example in patterns[0].examples:
assert compiled.search(example.body)
+ def test_same_statement_on_different_dates_shares_fingerprint(self) -> None:
+ # The patterns diff compares fingerprints across two windows (default: one week
+ # apart). A timestamp fragment surviving masking becomes a literal run, so the
+ # same log statement would fingerprint differently and show up as a false
+ # new/gone pair.
+ monday = mine_patterns([_sample("2026-08-12T08:10:43.397557Z task_retrying attempt=3")])
+ week_later = mine_patterns([_sample("2026-08-19T09:04:17.112233Z task_retrying attempt=7")])
+
+ assert pattern_fingerprint(monday[0].pattern) == pattern_fingerprint(week_later[0].pattern)
+
+ def test_match_regex_matches_siblings_with_different_timestamps(self) -> None:
+ # An unmasked timestamp baked into match_regex narrows the "view matching logs"
+ # pivot to the single line the pattern was mined from.
+ patterns = mine_patterns([_sample("task_retrying at 2026-08-12T08:10:43.397557Z scheduled")])
+
+ assert patterns[0].match_regex is not None
+ assert re.search(patterns[0].match_regex, "task_retrying at 2026-08-19T14:02:11.000001Z scheduled")
+
class TestCompileMatchRegex(TestCase):
@parameterized.expand(
@@ -164,6 +215,7 @@ class TestCompileMatchRegex(TestCase):
("peer <ip> disconnected", "peer 10.32.243.94 disconnected"),
("token <hex> rejected", "token 0xdeadbeef rejected"),
("path /api/v1/users?id=<num> hit", "path /api/v1/users?id=42 hit"),
+ ("job <timestamp> finished", "job 2026-08-12T08:10:43.397557Z finished"),
]
)
def test_compiled_regex_matches_raw_bodies(self, template: str, raw_body: str) -> None:
diff --git a/products/logs/frontend/generated/api.schemas.ts b/products/logs/frontend/generated/api.schemas.ts
index 9275c5165685..c652ac6b260e 100644
--- a/products/logs/frontend/generated/api.schemas.ts
+++ b/products/logs/frontend/generated/api.schemas.ts
@@ -1557,7 +1557,7 @@ export interface _LogPatternExampleApi {
export type _LogPatternApiSeverityCounts = { [key: string]: number }
export interface _LogPatternApi {
- /** Mined log template with variable tokens masked, e.g. "Connected to <ip> in <num>ms". Tokens: <uuid>, <ip>, <hex>, <num>, plus <*> for word positions Drain found to vary. */
+ /** Mined log template with variable tokens masked, e.g. "Connected to <ip> in <num>ms". Tokens: <timestamp>, <uuid>, <ip>, <hex>, <num>, plus <*> for word positions Drain found to vary. */
pattern: string
/** Occurrences of this pattern within the sample. When `sampled` is true this is a sample count, not the full-window total — prefer `estimated_count` for display. */
count: number
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index f56685bf13da..1bb32b4454c5 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -78851,7 +78851,7 @@ export namespace Schemas {
}
export interface _LogPattern {
- /** Mined log template with variable tokens masked, e.g. "Connected to <ip> in <num>ms". Tokens: <uuid>, <ip>, <hex>, <num>, plus <*> for word positions Drain found to vary. */
+ /** Mined log template with variable tokens masked, e.g. "Connected to <ip> in <num>ms". Tokens: <timestamp>, <uuid>, <ip>, <hex>, <num>, plus <*> for word positions Drain found to vary. */
pattern: string;
/** Occurrences of this pattern within the sample. When `sampled` is true this is a sample count, not the full-window total — prefer `estimated_count` for display. */
count: number;