Skip to content

[NO-TICKET] Improve tracing Rails route computation #4688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Strech
Copy link
Member

@Strech Strech commented May 27, 2025

What does this PR do?

Drastically improves performance of the code (less allocations, better performance)

Motivation:

In AppSec we need to compute http.route-like value of the request and I found that code I can improve.

Change log entry

Yes. Tracing: Improve performance of http.route tag computation for rails contrib.

Additional Notes:

I've replaced 1 string allocation with mutation and have used faster suffix removal method.

w: - there is a match in the string
w/o - there is no match in the string

ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [arm64-darwin23] Warming up --------------------------------------
             w: gsub    72.280k i/100ms
              w: sub    81.689k i/100ms
    w: delete_suffix   502.700k i/100ms
           w/o: gsub   116.580k i/100ms
            w/o: sub   195.557k i/100ms
  w/o: delete_suffix   225.558k i/100ms
Calculating -------------------------------------
             w: gsub      1.609M (±68.8%) i/s  (621.46 ns/i) -     10.914M in  30.723566s
              w: sub      3.185M (±65.6%) i/s  (313.95 ns/i) -     17.481M in  30.198337s
    w: delete_suffix      4.903M (±118.8%) i/s  (203.97 ns/i) -     45.746M in  31.428342s
           w/o: gsub      4.935M (±64.8%) i/s  (202.61 ns/i) -     46.632M in  30.271821s
            w/o: sub      4.876M (±63.5%) i/s  (205.09 ns/i) -     46.934M in  30.233153s
  w/o: delete_suffix     10.564M (±78.8%) i/s   (94.66 ns/i) -     48.495M in  30.639072s

How to test the change?

CI + ST

@Strech Strech requested a review from a team as a code owner May 27, 2025 16:05
@github-actions github-actions bot added integrations Involves tracing integrations tracing labels May 27, 2025
@Strech Strech changed the title Add request env to tracing ActionDispatch contrib [APPSEC-57809] Add request env to tracing ActionDispatch contrib May 27, 2025
@datadog-datadog-prod-us1
Copy link
Contributor

datadog-datadog-prod-us1 bot commented May 27, 2025

Datadog Report

Branch report: appsec-57809-make-http-route-available-within-request
Commit report: af17fe4
Test service: dd-trace-rb

✅ 0 Failed, 21681 Passed, 1302 Skipped, 4m 52.24s Total Time

@pr-commenter
Copy link

pr-commenter bot commented May 27, 2025

Benchmarks

Benchmark execution time: 2025-06-14 12:34:05

Comparing candidate commit af17fe4 in PR branch appsec-57809-make-http-route-available-within-request with baseline commit 482c04e in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 41 metrics, 6 unstable metrics.

@Strech Strech force-pushed the appsec-57809-make-http-route-available-within-request branch from 0380fca to 4fa9ef9 Compare May 28, 2025 07:49
@Strech Strech requested a review from marcotc May 28, 2025 08:00
@codecov-commenter
Copy link

codecov-commenter commented May 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.63%. Comparing base (482c04e) to head (af17fe4).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4688      +/-   ##
==========================================
- Coverage   97.63%   97.63%   -0.01%     
==========================================
  Files        1474     1474              
  Lines       88022    88026       +4     
  Branches     4569     4568       -1     
==========================================
+ Hits        85942    85944       +2     
- Misses       2080     2082       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Strech
Copy link
Member Author

Strech commented May 28, 2025

I'm going to withdraw the part with environment variable, but keep the performance part intact. I've found that we can leverage a little bit of hacks to achieve the same

@Strech Strech changed the title [APPSEC-57809] Add request env to tracing ActionDispatch contrib [NO-TICKET] Improve tracing rails contrib route computation May 28, 2025
@Strech Strech changed the title [NO-TICKET] Improve tracing rails contrib route computation [NO-TICKET] Improve tracing Rails route computation May 28, 2025
Strech added 2 commits May 28, 2025 14:22
ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [arm64-darwin23]
Warming up --------------------------------------
             w: gsub    72.280k i/100ms
              w: sub    81.689k i/100ms
    w: delete_suffix   502.700k i/100ms
           w/o: gsub   116.580k i/100ms
            w/o: sub   195.557k i/100ms
  w/o: delete_suffix   225.558k i/100ms
Calculating -------------------------------------
             w: gsub      1.609M (±68.8%) i/s  (621.46 ns/i) -     10.914M in  30.723566s
              w: sub      3.185M (±65.6%) i/s  (313.95 ns/i) -     17.481M in  30.198337s
    w: delete_suffix      4.903M (±118.8%) i/s  (203.97 ns/i) -     45.746M in  31.428342s
           w/o: gsub      4.935M (±64.8%) i/s  (202.61 ns/i) -     46.632M in  30.271821s
            w/o: sub      4.876M (±63.5%) i/s  (205.09 ns/i) -     46.934M in  30.233153s
  w/o: delete_suffix     10.564M (±78.8%) i/s   (94.66 ns/i) -     48.495M in  30.639072s
@Strech Strech force-pushed the appsec-57809-make-http-route-available-within-request branch from 4fa9ef9 to 56643b3 Compare May 28, 2025 12:22
@Strech
Copy link
Member Author

Strech commented Jun 12, 2025

@marcotc Will be happy to merge it. I've found tests that covers the change. 🤲🏼

@TonyCTHsu TonyCTHsu merged commit 748314c into master Jun 23, 2025
444 checks passed
@TonyCTHsu TonyCTHsu deleted the appsec-57809-make-http-route-available-within-request branch June 23, 2025 09:45
@github-actions github-actions bot added this to the 2.18.0 milestone Jun 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrations Involves tracing integrations tracing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants