Skip to content

IGNORE: Standardize exception logging format and add enforcement cop#5583

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

IGNORE: Standardize exception logging format and add enforcement cop#5583
p-datadog wants to merge 8 commits intomasterfrom
custom-cop/exception-log-format-combined

Conversation

@p-datadog
Copy link
Copy Markdown
Member

What does this PR do?
Combines #5514 (standardize exception logging) with #5582 (custom cop to enforce it).

  • Standardizes all exception logging in lib/datadog/ to "#{e.class}: #{e}" (59 files)
  • Adds CustomCops::ExceptionMessageCop to prevent regressions

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:
Exception#to_s and Exception#message have different contracts in Ruby. Subclasses can override them independently, and to_s is the method Ruby calls during string interpolation. The codebase convention is "#{e.class}: #{e}". This PR brings all call sites in line and adds a cop to keep them there. Cop suggested by @vpellan in #5514 review.

Change log entry
None.

Additional Notes:
Supersedes #5514 and #5582 — those can be closed if this one merges.

Cop only applies to lib/**/* files (same scope as other custom cops).

How to test the change?

bundle exec rake spec:custom_cop
bundle exec rake standard

p-ddsign and others added 4 commits March 26, 2026 16:05
Replace `e.message` with `e` (via string interpolation) and
`e.class.name` with `e.class` in all logging contexts across
lib/datadog/.

The `to_s` and `message` methods on Exception have different contracts:
subclasses can override them independently, and `to_s` is the method
Ruby calls during string interpolation. Using `e.message` directly
can produce different output than `#{e}` when a subclass overrides
`to_s` without overriding `message`, or vice versa.

Similarly, `e.class` already returns a displayable class name via
`to_s`, making `.name` redundant in string interpolation contexts.

The codebase convention is `#{e.class}: #{e}`. This commit brings
all logging call sites in line with that convention.

Does not change:
- `e.message` used for constructing new exceptions (raise)
- `e.message` used in data structures or API payloads
- `e.message` in Core::Error.build_from (value object construction)
- `e.message` in conditionals or comparisons
- Test files

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The source code changed from `e.message` / `e.class.name e.message` to
`e.class: e` (which produces `ClassName: message`). Test expectations
that asserted on the old format now fail because the logged string
includes the exception class name with a colon separator.

- tags_spec.rb: expected "Oops..." → "StandardError: Oops..."
- component_spec.rb: expected /Test failure/ → /RuntimeError: Test failure/
  (3 tests: start, stop, update_on_fork)
- transport_spec.rb: expected /Timeout::Error Ooops/ → /Timeout::Error: Ooops/
  (space → colon+space between class and message)

Co-Authored-By: Claude <noreply@anthropic.com>
debug() with string interpolation always constructs the string even when
debug logging is off. Block form (debug { }) defers construction.

Also standardize e.message in open_feature/remote.rb which was missed
in the initial pass.

Co-Authored-By: Claude <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
@github-actions github-actions bot added core Involves Datadog core libraries integrations Involves tracing integrations profiling Involves Datadog profiling appsec Application Security monitoring product tracing labels Apr 10, 2026
@datadog-datadog-prod-us1
Copy link
Copy Markdown
Contributor

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

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 77.68%
Overall Coverage: 95.36% (-0.02%)

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

@p-datadog p-datadog changed the title Standardize exception logging format and add enforcement cop IGNORE: Standardize exception logging format and add enforcement cop Apr 10, 2026
p-ddsign and others added 4 commits April 10, 2026 17:32
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>
- 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>
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>
@p-datadog p-datadog force-pushed the custom-cop/exception-log-format-combined branch from c6a68a8 to e819a2d Compare April 10, 2026 21:32
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 appsec Application Security monitoring product core Involves Datadog core libraries integrations Involves tracing integrations profiling Involves Datadog profiling tracing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants