Skip to content

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

feat(logs): mask hostnames in mined log patterns (#82018)

Jon McCallum84cce233435f
diff --git a/products/logs/backend/log_patterns.py b/products/logs/backend/log_patterns.py
index 4b264bee7c14..44df159ae261 100644
--- a/products/logs/backend/log_patterns.py
+++ b/products/logs/backend/log_patterns.py
@@ -14,6 +14,48 @@ ERROR_SEVERITIES = {"error", "fatal"}
# clustering, so templates stay readable ("<ip>", "<num>") instead of fragmenting into
# 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.
+# Suffixes that make a dotted token a hostname. Deliberately a fixed list rather than
+# "any trailing alphabetic label": dotted module paths, logger names, and source files
+# ("products.logs.backend", "django_structlog.celery.receivers", "Handler.cpp") are
+# otherwise indistinguishable from an FQDN, and masking those eats real literal content.
+_HOST_SUFFIXES = (
+ "ai",
+ "app",
+ "bot",
+ "cloud",
+ "co",
+ "com",
+ "de",
+ "dev",
+ "eu",
+ "fr",
+ "gg",
+ "internal",
+ "io",
+ "jp",
+ "local",
+ "me",
+ "net",
+ "nl",
+ "org",
+ "sh",
+ "so",
+ "tv",
+ "uk",
+ "us",
+ "xyz",
+)
+_HOST_LABEL = r"[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?"
+# The repeat is bounded to hold down backtracking, not to enforce a DNS limit. Under an
+# unbounded repeat, a dotted run that ends in a non-suffix ("a.a.a.<...>.invalid") makes the
+# engine retry the whole suffix alternation at every label boundary, from every start position
+# the run offers, so the cost grows with the square of the run's length. Every sampled row is
+# masked, and only LOGS_PATTERNS_BODY_TRUNCATE bounds how long a run can get, so a body built
+# out of single-character labels turns into real CPU. A dotted run with more labels than this
+# masks its tail and keeps its head literal, which no real hostname is long enough to reach.
+_HOST_MAX_LABELS_BEFORE_SUFFIX = 8
+_HOST_PATTERN = rf"\b(?:{_HOST_LABEL}\.){{1,{_HOST_MAX_LABELS_BEFORE_SUFFIX}}}(?:{'|'.join(_HOST_SUFFIXES)})\b"
+
_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.
@@ -32,6 +74,9 @@ _MASKING_INSTRUCTIONS = [
# whole, instead of leaving a "<version>.<num>" tail on a template of its own.
# Runs after ip so an address in a URL is already masked and cannot look like a version.
MaskingInstruction(r"(?<=/)\d+(?:\.\d+)+", "version"),
+ # Host runs after ip (a dotted quad has no alphabetic suffix, so it stays an <ip>) and
+ # before hex, which would otherwise claim a long hex-looking label.
+ MaskingInstruction(_HOST_PATTERN, "host"),
MaskingInstruction(r"\b0x[0-9a-fA-F]+\b", "hex"),
MaskingInstruction(r"\b[0-9a-fA-F]{16,}\b", "hex"),
MaskingInstruction(r"\b\d+\b", "num"),
@@ -50,6 +95,9 @@ _PLACEHOLDER_PATTERNS = {
"<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}",
"<version>": r"\d+(?:\.\d+)+",
+ # Reuses the masking pattern so the pivot cannot claim dotted code paths the mask
+ # deliberately left literal. \b is RE2-safe; only lookaround is not.
+ "<host>": _HOST_PATTERN,
"<hex>": r"(?:0x[0-9a-fA-F]+|[0-9a-fA-F]{16,})",
}
_PLACEHOLDER_RE = re.compile("|".join(re.escape(p) for p in _PLACEHOLDER_PATTERNS))
diff --git a/products/logs/backend/test/test_log_patterns.py b/products/logs/backend/test/test_log_patterns.py
index 1c051c855695..2085046f13c7 100644
--- a/products/logs/backend/test/test_log_patterns.py
+++ b/products/logs/backend/test/test_log_patterns.py
@@ -5,6 +5,7 @@ from itertools import zip_longest
from unittest import TestCase
from hypothesis import (
+ assume,
given,
settings,
strategies as st,
@@ -12,6 +13,7 @@ from hypothesis import (
from parameterized import parameterized
from products.logs.backend.log_patterns import (
+ _HOST_SUFFIXES,
_MASKING_INSTRUCTIONS,
_PLACEHOLDER_PATTERNS,
LogSample,
@@ -122,6 +124,18 @@ class TestMinePatterns(TestCase):
"<timestamp>",
"12T08",
),
+ (
+ "hostname",
+ ["upstream ingest.example.com refused", "upstream ingest.example.net refused"],
+ "<host>",
+ "example",
+ ),
+ (
+ "subdomains",
+ ["proxied to eu.i.example.com ok", "proxied to us.i.example.com ok"],
+ "<host>",
+ "example",
+ ),
]
)
def test_masking_collapses_variable_tokens(
@@ -163,6 +177,39 @@ class TestMinePatterns(TestCase):
assert "<version>" not in patterns[0].pattern
+ @parameterized.expand(
+ [
+ ("module_path", "handler resolved in products.logs.backend module"),
+ ("logger_name", "logger django_structlog.celery.receivers ready"),
+ ("source_file", "raised from MergeFromLogEntryTask.cpp while merging"),
+ ]
+ )
+ def test_dotted_code_paths_are_not_masked_as_hosts(self, _name: str, line: str) -> None:
+ # A hostname mask keyed on "any trailing alphabetic label" swallows module paths,
+ # logger names, and source files, which is the literal content the template is for.
+ patterns = mine_patterns([_sample(line)])
+
+ assert "<host>" not in patterns[0].pattern
+
+ def test_a_name_that_starts_with_an_address_masks_the_address_first(self) -> None:
+ # Wildcard-DNS names carry an address in their leading labels ("10.0.0.1.nip.io"), and
+ # ip runs before host by design, so the address masks and the domain masks after it.
+ # Both halves are variable and both are covered; the order is what this pins, because
+ # letting host win would bury the address a reader opened the pattern for.
+ patterns = mine_patterns([_sample("upstream 10.0.0.1.nip.io refused")])
+
+ assert patterns[0].pattern == "upstream <ip>.<host> refused"
+
+ def test_a_dotted_run_longer_than_any_hostname_keeps_its_head_literal(self) -> None:
+ # The label repeat is capped because an uncapped one retries the suffix alternation at
+ # every boundary of a long dotted run, at a cost that grows with the square of its
+ # length. Masking only the tail is the visible price of that cap, so a crafted run of
+ # single-character labels must leave its head in the template.
+ patterns = mine_patterns([_sample("upstream " + "a." * 40 + "example.com refused")])
+
+ assert "<host>" in patterns[0].pattern
+ assert "a.a.a." in patterns[0].pattern
+
def test_error_count_includes_only_error_and_fatal(self) -> None:
samples = [
_sample("db connection dropped", severity="error"),
@@ -278,6 +325,7 @@ class TestCompileMatchRegex(TestCase):
("took <num> ms", "took 12345 ms"),
("request <uuid> failed", "request 93fce79d-6926-4b08-8fa5-00ffd8e65f4e failed"),
("peer <ip> disconnected", "peer 10.32.243.94 disconnected"),
+ ("upstream <host> refused", "upstream eu.i.example.com refused"),
("token <hex> rejected", "token 0xdeadbeef rejected"),
("agent Chrome/<version> connected", "agent Chrome/139.0.0.0 connected"),
("path /api/v1/users?id=<num> hit", "path /api/v1/users?id=42 hit"),
@@ -374,9 +422,41 @@ _version_st = st.lists(st.integers(min_value=0, max_value=9999), min_size=2, max
lambda parts: ".".join(map(str, parts))
)
_decimal_context_st = st.sampled_from(["duration_ms=", "ratio=", "load ", "p95="])
+_label_st = st.text("abcdefghijklmnopqrstuvwxyz0123456789", min_size=1, max_size=12)
+# A label that is itself a host suffix would make a "code path" strategy generate a real
+# hostname, so it is excluded from both strategies below.
+_non_suffix_label_st = _label_st.filter(lambda label: label not in _HOST_SUFFIXES)
+
+
+# A name whose first four labels are a dotted quad ("0.0.0.0.ai", "10.0.0.1.nip.io") is an
+# address with a domain after it, and ip runs before host by design, so it masks as "<ip>.<host>"
+# rather than a single placeholder. That precedence has its own case above; excluding the shape
+# here keeps this strategy to names the host mask alone owns.
+# The trailing guards mirror the ip mask's own: a fifth label that starts with a digit makes the
+# leading quad the head of a longer dotted run, which the mask leaves alone.
+_LEADING_ADDRESS_RE = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?!\d)(?!\.\d)")
+
+
+# Up to five labels before the suffix, past the deepest names that show up in real logs
+# ("pod.ns.svc.cluster.local", "a.b.c.example.co.uk"). The mask caps how many labels it will
+# consume, and this range is deliberately not derived from that cap: tightening the cap below
+# what real hostnames need has to fail the collapse property, not narrow the strategy with it.
+def _fqdn_st(draw: st.DrawFn) -> str:
+ labels = draw(st.lists(_non_suffix_label_st, min_size=1, max_size=5))
+ name = ".".join([*labels, draw(st.sampled_from(_HOST_SUFFIXES))])
+ assume(not _LEADING_ADDRESS_RE.match(name))
+ return name
+
+
+def _dotted_code_path_st(draw: st.DrawFn) -> str:
+ return ".".join(draw(st.lists(_non_suffix_label_st, min_size=2, max_size=4)))
+
+
_slash_version_st = st.tuples(_product_st, _version_st).map(lambda t: f"{t[0]}/{t[1]}")
_variable_token_st = st.one_of(
- _uuid_st, _hex_0x_st, _hex_bare_st, _num_st, _timestamp_st(), _ipv4_st, _slash_version_st
+ _uuid_st, _hex_0x_st, _hex_bare_st, _num_st, _timestamp_st(), _ipv4_st, _slash_version_st, _fqdn_st()
)
@@ -474,6 +554,7 @@ _CONFUSABLE_PAIRS = [
("ip_vs_five_octets", _ipv4_st, _five_octet_st),
("ip_vs_versioned_quad", _ipv4_st, _versioned_quad_st),
("version_vs_plain_decimal", _slash_version_st, _plain_decimal_st),
+ ("host_vs_dotted_code_path", _fqdn_st(), _dotted_code_path_st()),
]
@@ -588,3 +669,38 @@ class TestVersionMaskProperties(TestCase):
second = mine_patterns([_sample(line.format(version_b))])
assert pattern_fingerprint(first[0].pattern) == pattern_fingerprint(second[0].pattern)
+
+
+class TestHostMaskProperties(TestCase):
+ """The cases above pin specific hostnames; these hold the rule over every shape of name.
+
+ The host mask is a trade: it has to catch any real domain while leaving dotted code
+ paths alone, and only the whole input space shows whether the suffix list draws that
+ line in the right place.
+ """
+
+ @given(fqdn=_fqdn_st())
+ @settings(max_examples=300, deadline=None)
+ def test_any_hostname_collapses_to_one_placeholder(self, fqdn: str) -> None:
+ patterns = mine_patterns([_sample(f"upstream {fqdn} refused")])
+
+ # exact template: the whole name is consumed, not partly masked and partly literal
+ assert patterns[0].pattern == "upstream <host> refused"
+
+ @given(path=_dotted_code_path_st())
+ @settings(max_examples=300, deadline=None)
+ def test_dotted_paths_without_a_host_suffix_stay_literal(self, path: str) -> None:
+ patterns = mine_patterns([_sample(f"handler in {path} module")])
+
+ assert "<host>" not in patterns[0].pattern
+
+ @given(host_a=_fqdn_st(), host_b=_fqdn_st())
+ @settings(max_examples=300, deadline=None)
+ def test_lines_differing_only_by_hostname_share_a_fingerprint(self, host_a: str, host_b: str) -> None:
+ # This is what the mask is for. The patterns diff and the pattern list both key on
+ # the fingerprint, so two hostnames that fingerprint apart show up as two templates.
+ line = '{{"authority":"{}","upstream_cluster":"capture"}}'
+ a = mine_patterns([_sample(line.format(host_a))])
+ b = mine_patterns([_sample(line.format(host_b))])
+
+ assert pattern_fingerprint(a[0].pattern) == pattern_fingerprint(b[0].pattern)