Skip to content

feat: Include more lines of code for additional context in structured logging output#3667

Open
edgarrmondragon wants to merge 3 commits into
mainfrom
feat/structured-logging-exception-multiple-lines
Open

feat: Include more lines of code for additional context in structured logging output#3667
edgarrmondragon wants to merge 3 commits into
mainfrom
feat/structured-logging-exception-multiple-lines

Conversation

@edgarrmondragon

@edgarrmondragon edgarrmondragon commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Related

Summary by Sourcery

Include additional source code context lines in structured log exception frames.

New Features:

  • Add a multi-line source context field to structured logging exception frame data.

Enhancements:

  • Refine exception formatting to capture both the exact error line and surrounding lines for improved debugging context in logs.

Tests:

  • Extend structured formatter tests to assert the presence and content of the additional context lines in exception frames.

@edgarrmondragon edgarrmondragon added this to the v0.55 milestone Jun 4, 2026
@edgarrmondragon edgarrmondragon added the Logging Logging & Monitoring with Meltano label Jun 4, 2026
@edgarrmondragon edgarrmondragon self-assigned this Jun 4, 2026
@sourcery-ai

sourcery-ai Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Extend structured exception logging to include surrounding source lines for each traceback frame, and update tests to validate the new contextual data in the structured formatter output.

File-Level Changes

Change Details Files
Add surrounding source lines to structured exception frame data.
  • Extend the _FrameData TypedDict to include a new lines list of strings for contextual source lines.
  • Refactor exception formatting to capture filename and lineno variables for reuse when building frame data.
  • Populate the new lines field using linecache.getlines(filename)[lineno - 2 : lineno + 1] to include the line before, the error line, and the following line, while keeping line as the single error line via linecache.getline.
singer_sdk/logging.py
Update structured formatter tests to assert presence and correctness of contextual source lines.
  • Extend the structured formatter exception test to assert that the new lines field exists in each frame and contains the expected three-line context around the exception site.
  • Use dedent and string joining for stable comparison of the collected contextual source lines against the expected snippet.
tests/core/test_structured_formatter.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.13%. Comparing base (1bf73b3) to head (caa0a3c).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3667   +/-   ##
=======================================
  Coverage   94.13%   94.13%           
=======================================
  Files          73       73           
  Lines        6203     6207    +4     
  Branches      763      763           
=======================================
+ Hits         5839     5843    +4     
  Misses        270      270           
  Partials       94       94           
Flag Coverage Δ
core 82.97% <100.00%> (+0.01%) ⬆️
end-to-end 75.97% <25.00%> (-0.04%) ⬇️
optional-components 44.80% <25.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

@codspeed-hq

codspeed-hq Bot commented Jun 4, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 8 untouched benchmarks


Comparing feat/structured-logging-exception-multiple-lines (caa0a3c) with main (1bf73b3)

Open in CodSpeed

@edgarrmondragon edgarrmondragon force-pushed the feat/structured-logging-exception-multiple-lines branch 3 times, most recently from 1ff649a to cb60fe8 Compare June 10, 2026 19:40
@read-the-docs-community

read-the-docs-community Bot commented Jun 10, 2026

Copy link
Copy Markdown

@edgarrmondragon edgarrmondragon marked this pull request as ready for review June 10, 2026 20:11

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • When building the lines context slice, consider making the context window size a named constant and using explicit bounds (e.g., start = max(lineno - 2, 1), end = lineno + 1) so the intent is clearer and edge cases (top-of-file frames) are handled more transparently than relying on negative slice indices.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- When building the `lines` context slice, consider making the context window size a named constant and using explicit bounds (e.g., `start = max(lineno - 2, 1)`, `end = lineno + 1`) so the intent is clearer and edge cases (top-of-file frames) are handled more transparently than relying on negative slice indices.

## Individual Comments

### Comment 1
<location path="singer_sdk/logging.py" line_range="121" />
<code_context>
-                        tb.tb_lineno,
-                    ),
+                    "line": linecache.getline(filename, lineno),
+                    "lines": linecache.getlines(filename)[lineno - 2 : lineno + 1],
                 }
                 frames.append(frame_info)
</code_context>
<issue_to_address>
**issue:** Guard against negative slice indices when lineno is 1 or 2.

For `lineno` 1 or 2, `lineno - 2` is negative, so the slice pulls lines from the *end* of the file instead of just the first lines. Clamp the start index to 0 (e.g. `start = max(0, lineno - 2)` and slice `[start:lineno + 1]`) so early frames only include valid leading context.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread singer_sdk/logging.py Outdated
@edgarrmondragon edgarrmondragon force-pushed the feat/structured-logging-exception-multiple-lines branch 2 times, most recently from d4c1576 to 24f3285 Compare June 20, 2026 02:23
… logging output

Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
…bove and below

Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
@edgarrmondragon edgarrmondragon force-pushed the feat/structured-logging-exception-multiple-lines branch from 24f3285 to caa0a3c Compare June 25, 2026 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Logging Logging & Monitoring with Meltano

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant