Skip to content

[awsxray] Prevent regex on hot path#2879

Open
jaydeluca wants to merge 2 commits into
open-telemetry:mainfrom
jaydeluca:aws-regex
Open

[awsxray] Prevent regex on hot path#2879
jaydeluca wants to merge 2 commits into
open-telemetry:mainfrom
jaydeluca:aws-regex

Conversation

@jaydeluca
Copy link
Copy Markdown
Member

Description:

Small fixes

  • Remove use of Random()
  • Pre-compiling the regex pattern once when the adaptive sampling config is applied so it doesn't need to re-parse and compile the same regex string on every span that passes through the sampler.

Copilot AI review requested due to automatic review settings May 27, 2026 20:36
@jaydeluca jaydeluca requested a review from a team as a code owner May 27, 2026 20:36
@github-actions github-actions Bot requested review from srprash and wangzlei May 27, 2026 20:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Small performance fixes in the AWS X-Ray sampler: replaces new Random() with ThreadLocalRandom for client ID generation, and pre-compiles anomaly-condition error code regexes once when the adaptive sampling config is applied rather than recompiling them on every span.

Changes:

  • Use ThreadLocalRandom.current() instead of allocating a new Random per generateClientId() call.
  • Pre-compile a List<Pattern> of error code regexes alongside the adaptive sampling config, and switch the anomaly check loop to index-based iteration so the compiled Pattern aligns with each AnomalyConditions.
  • Reuse the cached compiled patterns in isAnomaly instead of calling String.matches(regex) per span.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
aws-xray/src/main/java/io/opentelemetry/contrib/awsxray/AwsXrayRemoteSampler.java Drops java.util.Random import and uses ThreadLocalRandom in generateClientId().
aws-xray/src/main/java/io/opentelemetry/contrib/awsxray/XrayRulesSampler.java Adds errorCodePatterns field populated via new compileErrorCodePatterns helper at construction and when the adaptive sampling config is set; uses pre-compiled Pattern in isAnomaly.

@jaydeluca jaydeluca changed the title awsxray: Prevent regex on hot path [awsxray] Prevent regex on hot path May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants