Unverified Commit 7bb6368f authored by Keiven C's avatar Keiven C Committed by GitHub
Browse files

perf: cache compiled regex patterns in prometheus metrics filtering (#3825)


Signed-off-by: default avatarKeiven Chang <keivenchang@users.noreply.github.com>
parent fa45ff1f
...@@ -13,7 +13,8 @@ while Dynamo runtime metrics are available immediately after component creation. ...@@ -13,7 +13,8 @@ while Dynamo runtime metrics are available immediately after component creation.
import logging import logging
import re import re
from typing import TYPE_CHECKING, Optional from functools import lru_cache
from typing import TYPE_CHECKING, Optional, Pattern
from prometheus_client import generate_latest from prometheus_client import generate_latest
...@@ -72,6 +73,30 @@ def register_engine_metrics_callback( ...@@ -72,6 +73,30 @@ def register_engine_metrics_callback(
endpoint.metrics.register_prometheus_expfmt_callback(get_expfmt) endpoint.metrics.register_prometheus_expfmt_callback(get_expfmt)
@lru_cache(maxsize=64)
def _compile_exclude_pattern(exclude_prefixes: tuple[str, ...]) -> Pattern:
"""Compile and cache regex for excluding metric prefixes.
Args take tuple not list - lru_cache requires hashable args (tuples are hashable, lists are not).
"""
escaped_prefixes = [re.escape(prefix) for prefix in exclude_prefixes]
prefixes_regex = "|".join(escaped_prefixes)
return re.compile(rf"^(# (HELP|TYPE) )?({prefixes_regex})")
@lru_cache(maxsize=64)
def _compile_include_pattern(metric_prefix: str) -> Pattern:
"""Compile and cache regex for including metrics by prefix."""
escaped_prefix = re.escape(metric_prefix)
return re.compile(rf"^(# (HELP|TYPE) )?{escaped_prefix}")
@lru_cache(maxsize=128)
def _compile_help_type_pattern() -> Pattern:
"""Compile and cache regex for extracting metric names from HELP/TYPE comment lines."""
return re.compile(r"^# (HELP|TYPE) (\S+)(.*)$")
def get_prometheus_expfmt( def get_prometheus_expfmt(
registry, registry,
metric_prefix_filter: Optional[str] = None, metric_prefix_filter: Optional[str] = None,
...@@ -107,21 +132,18 @@ def get_prometheus_expfmt( ...@@ -107,21 +132,18 @@ def get_prometheus_expfmt(
if metric_prefix_filter or exclude_prefixes or add_prefix: if metric_prefix_filter or exclude_prefixes or add_prefix:
lines = [] lines = []
# Build exclude pattern for lines to skip entirely # Get cached compiled patterns
exclude_line_pattern = None exclude_line_pattern = None
if exclude_prefixes: if exclude_prefixes:
escaped_prefixes = [re.escape(prefix) for prefix in exclude_prefixes] exclude_line_pattern = _compile_exclude_pattern(tuple(exclude_prefixes))
prefixes_regex = "|".join(escaped_prefixes)
# Match lines starting with: HELP/TYPE comments OR metric lines with excluded prefixes
exclude_line_pattern = re.compile(
rf"^(# (HELP|TYPE) )?({prefixes_regex})"
)
# Build include pattern if needed # Build include pattern if needed
include_pattern = None include_pattern = None
if metric_prefix_filter: if metric_prefix_filter:
escaped_prefix = re.escape(metric_prefix_filter) include_pattern = _compile_include_pattern(metric_prefix_filter)
include_pattern = re.compile(rf"^(# (HELP|TYPE) )?{escaped_prefix}")
# Get cached HELP/TYPE pattern
help_type_pattern = _compile_help_type_pattern()
for line in metrics_text.split("\n"): for line in metrics_text.split("\n"):
if not line.strip(): if not line.strip():
...@@ -139,7 +161,7 @@ def get_prometheus_expfmt( ...@@ -139,7 +161,7 @@ def get_prometheus_expfmt(
if add_prefix: if add_prefix:
# Handle HELP/TYPE comments # Handle HELP/TYPE comments
if line.startswith("# HELP ") or line.startswith("# TYPE "): if line.startswith("# HELP ") or line.startswith("# TYPE "):
match = re.match(r"^# (HELP|TYPE) (\S+)(.*)$", line) match = help_type_pattern.match(line)
if match: if match:
comment_type, metric_name, rest = match.groups() comment_type, metric_name, rest = match.groups()
# Remove existing prefix if present # Remove existing prefix if present
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment