Skip to content

feat: Add stream_name to structured logs#3680

Draft
edgarrmondragon wants to merge 1 commit into
mainfrom
feat/stream_name
Draft

feat: Add stream_name to structured logs#3680
edgarrmondragon wants to merge 1 commit into
mainfrom
feat/stream_name

Conversation

@edgarrmondragon

@edgarrmondragon edgarrmondragon commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

SSIA

Summary by Sourcery

Propagate structured logging context by wrapping plugin and stream loggers in LoggerAdapter and updating logging utilities to handle optional stream-specific metadata.

Enhancements:

  • Attach stream_name context to stream loggers using LoggerAdapter for structured logging.
  • Change plugin-level logger to use LoggerAdapter to support contextual log metadata.
  • Update formatter and type hints to treat stream_name as optional and accept both Logger and LoggerAdapter instances.

Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
@edgarrmondragon edgarrmondragon added this to the v0.55 milestone Jun 17, 2026
@edgarrmondragon edgarrmondragon self-assigned this Jun 17, 2026
@sourcery-ai

sourcery-ai Bot commented Jun 17, 2026

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

Reviewer's Guide

Introduce structured logging support for streams by wrapping loggers in LoggerAdapter instances that inject a stream_name field, and adjust typing and log formatter behavior to handle the new context field safely.

File-Level Changes

Change Details Files
Wrap stream-level logger with LoggerAdapter to inject stream_name into log records.
  • Replace direct child logger retrieval with LoggerAdapter wrapping the tap's child logger.
  • Attach extra context containing the stream's name under the stream_name key.
  • Update the stream.logger property type hint to return LoggerAdapter instead of Logger.
singer_sdk/streams/core.py
Update plugin base logger to return a LoggerAdapter for consistency with structured logging.
  • Change PluginBase.logger class property return type hint from Logger to LoggerAdapter.
  • Wrap the class-based logger in a LoggerAdapter to support structured logging context.
singer_sdk/plugin_base.py
Adjust structured log formatter to conditionally include stream_name in output and avoid overwriting existing data fields.
  • Remove unconditional setting of stream_name in the base log record dictionary.
  • Only add stream_name to the log output if present in the record's extra data, preserving other payload fields.
singer_sdk/logging.py
Relax type hints for components that accept loggers to support both Logger and LoggerAdapter.
  • Update conform_record_data_types logger parameter type hint to accept either Logger or LoggerAdapter.
  • Update PluginMapper constructor logger parameter type hint to accept either Logger or LoggerAdapter.
singer_sdk/helpers/_typing.py
singer_sdk/mapper.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

@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 Meltano SDK | 🛠️ Build #33190194 | 📁 Comparing ad5f6e8 against latest (1b205c0)

  🔍 Preview build  

1 file changed
± classes/singer_sdk.Stream.html

@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:

  • In PluginBase.logger, logging.LoggerAdapter is constructed without the required extra argument, which will raise a TypeError at runtime; pass an explicit extra dict (e.g. extra={}) when instantiating the adapter.
  • In Stream.__init__, accessing tap.logger.logger.getChild(self.name) assumes tap.logger is a LoggerAdapter; it would be safer to rely on a documented interface or helper method rather than the .logger attribute to avoid coupling to the adapter’s internal structure.
  • Updating logger type hints to logging.Logger | logging.LoggerAdapter in some places but not others may cause inconsistencies for callers; consider centralizing a common logger protocol or alias to keep the logger interface uniform across the SDK.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `PluginBase.logger`, `logging.LoggerAdapter` is constructed without the required `extra` argument, which will raise a `TypeError` at runtime; pass an explicit `extra` dict (e.g. `extra={}`) when instantiating the adapter.
- In `Stream.__init__`, accessing `tap.logger.logger.getChild(self.name)` assumes `tap.logger` is a `LoggerAdapter`; it would be safer to rely on a documented interface or helper method rather than the `.logger` attribute to avoid coupling to the adapter’s internal structure.
- Updating `logger` type hints to `logging.Logger | logging.LoggerAdapter` in some places but not others may cause inconsistencies for callers; consider centralizing a common logger protocol or alias to keep the logger interface uniform across the SDK.

## Individual Comments

### Comment 1
<location path="singer_sdk/plugin_base.py" line_range="215-221" />
<code_context>

     @classproperty
-    def logger(cls) -> logging.Logger:  # noqa: N805
+    def logger(cls) -> logging.LoggerAdapter:  # noqa: N805
         """Get logger.

         Returns:
             Plugin logger.
         """
-        return logging.getLogger(cls.name)
+        return logging.LoggerAdapter(logging.getLogger(cls.name))

     # Constructor
</code_context>
<issue_to_address>
**issue (bug_risk):** LoggerAdapter is instantiated without the required `extra` mapping.

`logging.LoggerAdapter` requires both a logger and an `extra` mapping. This call (`logging.LoggerAdapter(logging.getLogger(cls.name))`) will raise `TypeError` at runtime. Please pass an `extra` dict-like mapping, e.g. `logging.LoggerAdapter(logging.getLogger(cls.name), extra={"app_name": cls.name})` (or other appropriate metadata).
</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/plugin_base.py
Comment on lines +215 to +221
def logger(cls) -> logging.LoggerAdapter: # noqa: N805
"""Get logger.

Returns:
Plugin logger.
"""
return logging.getLogger(cls.name)
return logging.LoggerAdapter(logging.getLogger(cls.name))

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.

issue (bug_risk): LoggerAdapter is instantiated without the required extra mapping.

logging.LoggerAdapter requires both a logger and an extra mapping. This call (logging.LoggerAdapter(logging.getLogger(cls.name))) will raise TypeError at runtime. Please pass an extra dict-like mapping, e.g. logging.LoggerAdapter(logging.getLogger(cls.name), extra={"app_name": cls.name}) (or other appropriate metadata).

@codecov

codecov Bot commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.39%. Comparing base (1b205c0) to head (ad5f6e8).
⚠️ Report is 15 commits behind head on main.

❗ There is a different number of reports uploaded between BASE (1b205c0) and HEAD (ad5f6e8). Click for more details.

HEAD has 39 uploads less than BASE
Flag BASE (1b205c0) HEAD (ad5f6e8)
core 14 1
optional-components 14 1
end-to-end 14 1
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3680      +/-   ##
==========================================
- Coverage   94.12%   88.39%   -5.74%     
==========================================
  Files          73       73              
  Lines        6200     6202       +2     
  Branches      762      763       +1     
==========================================
- Hits         5836     5482     -354     
- Misses        270      620     +350     
- Partials       94      100       +6     
Flag Coverage Δ
core 79.89% <100.00%> (-3.06%) ⬇️
end-to-end 69.09% <66.66%> (-6.95%) ⬇️
optional-components 44.80% <33.33%> (-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.

@edgarrmondragon edgarrmondragon marked this pull request as draft June 17, 2026 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant