Skip to content

perf(tags): cache tag strings; hoist per-call regexes to LazyLock#1278

Draft
duncanista wants to merge 1 commit into
jordan.gonzalez/cold-start-instrumentation/featurefrom
jordan.gonzalez/tag-regex-cleanup/feature
Draft

perf(tags): cache tag strings; hoist per-call regexes to LazyLock#1278
duncanista wants to merge 1 commit into
jordan.gonzalez/cold-start-instrumentation/featurefrom
jordan.gonzalez/tag-regex-cleanup/feature

Conversation

@duncanista

Copy link
Copy Markdown
Contributor

Jira: none yet — add before marking ready.

DRAFT — stacked on #1271 (jordan.gonzalez/cold-start-instrumentation/feature). Review/merge the base PR first; this targets that branch, not main.

Overview

Two small cold-start / hot-path cleanups. Behavior is unchanged — same tag set, order, format, and values; same metrics. Only redundant work is removed.

1. Cache Lambda tag representations (tags/lambda/tags.rs, tags/provider.rs)

Lambda::get_tags_vec, get_tags_string, and get_function_tags_map previously re-iterated the tag map and re-ran format!("{k}:{v}") (and join(",")) on every call. These are called repeatedly during init (start_dogstatsd builds the enrichment-tag string/vec) and per-trace (trace_processor, log processor), so the same string was rebuilt many times over.

The tag set is immutable after construction, so the Vec<String>, joined String, and _dd.tags.function map are now computed once in Lambda::new_from_config and stored. The getters return the cached values, making repeated reads O(1). A get_tags_string() returning &str was added so Provider::get_tags_string() clones the cached string instead of re-joining the vec.

2. Hoist per-call regexes to LazyLock (proc/mod.rs)

get_fd_max_data_from_path and get_threads_max_data_from_path called Regex::new(...) on every invocation (every fd/threads metrics sample). Both patterns are static literals, so they are now compiled once via LazyLock<Regex> (using .expect("valid static regex")).

trace_processor::span_matches_tag_regex — left unchanged. Its pattern comes from per-call user config (apm_filter_tags_regex_reject), not a static literal, so it cannot be hoisted to a LazyLock without changing behavior. Left as-is intentionally.

Testing

  • cargo fmt clean; cargo clippy --bin bottlecap --no-deps clean (only the pre-existing buf_redux/multipart future-incompat warning remains; pedantic/unwrap_used denied — satisfied, only .expect() used).
  • cargo test --lib for proc::tests, tags::lambda::tags::tests, tags::provider::tests passes. (Two test_get_function_tags_map* cases are a pre-existing multi-threaded env-var-race flake — they also fail on the unmodified base branch and pass single-threaded / in isolation; not caused by this change.)

Compute the Lambda tag vec/string/function-tags-map once in Lambda::new_from_config and return the cached values from the getters, so repeated init- and per-trace-time calls to get_tags_vec/get_tags_string/get_function_tags_map are O(1) reads instead of re-iterating the tag map and re-running format!/join on every call.

Hoist the two static limits-file regexes in proc/mod.rs (Max open files, Max processes) to LazyLock<Regex> so they compile once instead of on every fd/threads metrics sample.

The trace_processor span_matches_tag_regex pattern is left as-is: its value comes from per-call user config (apm_filter_tags_regex_reject), not a static literal, so it cannot be hoisted to a LazyLock without changing behavior.

Output (tag set, format, values) is unchanged.
@datadog-prod-us1-6

datadog-prod-us1-6 Bot commented Jun 24, 2026

Copy link
Copy Markdown

Pipelines

Fix all issues with BitsAI

⚠️ Warnings

🚦 8 Pipeline jobs failed

DataDog/datadog-lambda-extension | integration-suite: [on-demand]   View in Datadog   GitLab

DataDog/datadog-lambda-extension | integration-suite: [payload-size]   View in Datadog   GitLab

DataDog/datadog-lambda-extension | integration-suite: [snapstart]   View in Datadog   GitLab

View all 8 failed jobs.

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: f6ec9db | Docs | Datadog PR Page | Give us feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant