Skip to content

Add custom RuboCop cop for consistent exception logging format#5582

Draft
p-datadog wants to merge 5 commits intomasterfrom
custom-cop/exception-log-format
Draft

Add custom RuboCop cop for consistent exception logging format#5582
p-datadog wants to merge 5 commits intomasterfrom
custom-cop/exception-log-format

Conversation

@p-datadog
Copy link
Copy Markdown
Member

@p-datadog p-datadog commented Apr 10, 2026

What does this PR do?
Adds CustomCops::ExceptionMessageCop that enforces consistent exception logging format inside rescue blocks.

The cop detects three patterns:

  • e.message → should be e (to_s and message have different contracts; #{e} calls to_s, which is the convention)
  • e.class.name → should be e.class (Class#to_s already returns the name)
  • #{e} without #{e.class} in the same string → the convention requires the class name

Auto-corrects e.message and e.class.name within string interpolation. The missing-class check flags but does not auto-correct.

Motivation:
Follow-up to #5514 which standardized #{e.class}: #{e} across the codebase. This cop prevents the old patterns from being reintroduced. Suggested by @vpellan during review.

Change log entry
None.

Additional Notes:
Only applies to lib/**/* files (same scope as other custom cops).

How to test the change?

bundle exec rake spec:custom_cop

Detects `e.message` and `e.class.name` inside rescue blocks and
auto-corrects them to `e` and `e.class` within string interpolation.

Follow-up to PR #5514 which standardized the format repo-wide.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@p-datadog p-datadog added the AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos label Apr 10, 2026
p-ddsign and others added 2 commits April 10, 2026 16:04
- Fix offense messages: to_s and message have different contracts,
  not the same value. The convention prefers to_s (via interpolation).
- Use <<~'RUBY' (non-interpolating) heredocs in specs so #{} is
  literal and caret column positions align correctly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Detect bare #{e} without #{e.class} in the same string inside rescue
blocks. The codebase convention is "#{e.class}: #{e}" — logging the
exception without its class loses context.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@datadog-prod-us1-4
Copy link
Copy Markdown

datadog-prod-us1-4 bot commented Apr 10, 2026

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 95.35% (+0.00%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 0c3dcaf | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

The e.message caret in the combined-patterns test was 1 column too far
right (col 26 instead of col 25). Verified all caret positions against
actual cop output.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@pr-commenter
Copy link
Copy Markdown

pr-commenter bot commented Apr 13, 2026

Benchmarks

Benchmark execution time: 2026-04-13 22:55:36

Comparing candidate commit 0c3dcaf in PR branch custom-cop/exception-log-format with baseline commit 3260714 in branch master.

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

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

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

Labels

AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants