Skip to content

Add Structlog Handler as Instrumentation#4286

Open
JWinermaSplunk wants to merge 7 commits intoopen-telemetry:mainfrom
JWinermaSplunk:structlog-handler
Open

Add Structlog Handler as Instrumentation#4286
JWinermaSplunk wants to merge 7 commits intoopen-telemetry:mainfrom
JWinermaSplunk:structlog-handler

Conversation

@JWinermaSplunk
Copy link

@JWinermaSplunk JWinermaSplunk commented Mar 3, 2026

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes open-telemetry/opentelemetry-python#2993

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • test_structlog.py

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@JWinermaSplunk JWinermaSplunk marked this pull request as ready for review March 13, 2026 17:14
@JWinermaSplunk JWinermaSplunk requested a review from a team as a code owner March 13, 2026 17:14
@JWinermaSplunk JWinermaSplunk changed the title [WIP] Add Structlog Handler as Instrumentation Add Structlog Handler as Instrumentation Mar 13, 2026
return _STD_TO_OTEL[levelno]


class StructlogHandler:
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be simplified by subclassing an existing type in structlog (which I don't know that well 🙂 )?

@tammy-baylis-swi
Copy link
Contributor

Thank you for undertaking this!

current_processors.insert(insert_position, processor)

# Reconfigure structlog with the new processor chain
structlog.configure(processors=current_processors)
Copy link
Member

Choose a reason for hiding this comment

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

During auto-instrumentation, instrumentors will run first, then the user's code will run, and if that code calls structlog.configure, looks like it will overwrite this config. The solution may be to monkeypatch structlog.configure.

Copy link
Author

Choose a reason for hiding this comment

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

Believe I resolved this by intercepting the original call, injecting the handler into the processors list, then calling the original once. But, I will leave this comment unresolved for context just in case I did not.

@xrmx xrmx moved this to Reviewed PRs that need fixes in Python PR digest Mar 25, 2026
@tammy-baylis-swi
Copy link
Contributor

I think a rebase is needed. This PR is now reverting some unrelated docker-test fixes (this other PR)

# Conflicts:
#	.github/workflows/test_0.yml
#	.github/workflows/test_1.yml
#	.github/workflows/test_2.yml
#	opentelemetry-contrib-instrumentations/pyproject.toml
# Conflicts:
#	.github/workflows/test_1.yml
#	.github/workflows/test_2.yml
#	.github/workflows/test_3.yml
@JWinermaSplunk
Copy link
Author

I think a rebase is needed. This PR is now reverting some unrelated docker-test fixes (this other PR)

Believe I went ahead and rebased, but feel free to let me know if it still looks off!


### Added

- `opentelemetry-instrumentation-structlog`: Add new package providing `StructlogHandler` and `StructlogInstrumentor` to bridge structlog into the OpenTelemetry Logs SDK pipeline with trace context correlation and exception capture.
Copy link
Contributor

Choose a reason for hiding this comment

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

Something like this:

Suggested change
- `opentelemetry-instrumentation-structlog`: Add new package providing `StructlogHandler` and `StructlogInstrumentor` to bridge structlog into the OpenTelemetry Logs SDK pipeline with trace context correlation and exception capture.
- `opentelemetry-instrumentation-structlog`: Add new package providing `StructlogHandler` and `StructlogInstrumentor` to bridge structlog into the OpenTelemetry Logs SDK pipeline with trace context correlation and exception capture. `opentelemetry-instrumentation`: adds shared `log_utils`

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

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

Add handler for structlog

4 participants