Skip to content

Add Structlog Handler as Instrumentation#4286

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

Add Structlog Handler as Instrumentation#4286
JWinermaSplunk wants to merge 4 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
Comment on lines +129 to +132
def std_to_otel(levelno: int) -> SeverityNumber:
"""
Map python log levelno to OTel log severity number.
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

This duplicates some of logging instrumentation. What about moving reusable helpers to opentelemetry-instrumentation at some point?

Copy link
Author

Choose a reason for hiding this comment

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

Understood, would this be a good thing to expose in this PR or should we open a separate one?

Copy link
Contributor

Choose a reason for hiding this comment

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

This PR might be a good time to introduce fewer lines overall.

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 🙂 )?


class StructlogHandler:
"""
A structlog processor that translates structlog events into OpenTelemetry LogRecords.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: the docstrings could be more consistent in referring to this as a handler instead of a processor, the latter in my mind being something like SimpleLogRecordProcessor.

@tammy-baylis-swi
Copy link
Contributor

Thank you for undertaking this!

"instrumentation": "opentelemetry-instrumentation-starlette==0.62b0.dev",
},
{
"library": "structlog ~= 21.1",
Copy link
Member

Choose a reason for hiding this comment

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

>= 21.1 to match the version constraint in package.py?

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.

# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Add handler for structlog

3 participants