Skip to content

refactor(otel): replace Any with Tracer and [T] generics#34883

Merged
asukaminato0721 merged 5 commits intolanggenius:mainfrom
corevibe555:refactor/otel-handlers-use-generics
Apr 10, 2026
Merged

refactor(otel): replace Any with Tracer and [T] generics#34883
asukaminato0721 merged 5 commits intolanggenius:mainfrom
corevibe555:refactor/otel-handlers-use-generics

Conversation

@corevibe555
Copy link
Copy Markdown
Contributor

Part of: #34878

Summary

  • Type tracer parameter as Tracer instead of Any in SpanHandler.wrapper and both handler subclasses
  • Add [T] generic to WorkflowAppRunnerHandler.wrapper to match base class signature — was erasing return type to Any
  • Use tuple[object, ...] and Mapping[str, object] instead of Any for args/kwargs in WorkflowAppRunnerHandler
  • Narrow _signature_cache and _build_span_name to Callable[..., object]
  • Remove unused Any import from workflow and generate handlers

Why this change

WorkflowAppRunnerHandler.wrapper overrode the base class [T] generic with Any, erasing the return type that the base class preserves. The tracer parameter was typed as Any across all handlers despite always receiving an opentelemetry.trace.Tracer instance from get_tracer().

Changes

  • extensions/otel/decorators/handler.py: Tracer type, Callable[..., object]
  • extensions/otel/decorators/handlers/workflow_app_runner_handler.py: [T] generic, Tracer, remove Any
  • extensions/otel/decorators/handlers/generate_handler.py: Tracer, remove Any

Test plan

  • basedpyright passes
  • ruff check passes
  • 35 otel unit tests pass

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. refactor labels Apr 10, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Apr 10, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

Comment thread api/extensions/otel/decorators/handlers/workflow_app_runner_handler.py Outdated
Comment thread api/extensions/otel/decorators/handler.py
Comment thread api/extensions/otel/decorators/handler.py Outdated
@corevibe555 corevibe555 requested a review from laipz8200 as a code owner April 10, 2026 03:41
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

1 similar comment
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

…lers

- Type tracer parameter as Tracer instead of Any across all handlers
- Add [T] generic to WorkflowAppRunnerHandler.wrapper to match base class
- Use tuple[object, ...] and Mapping[str, object] instead of Any in args
- Narrow _signature_cache and _build_span_name to Callable[..., object]
- Remove unused Any import from workflow and generate handlers
….wrapper

- Change wrapper signature from (wrapped, args, kwargs) to
  (wrapped, *args: P.args, **kwargs: P.kwargs) using ParamSpec
- Update base.py caller to forward args variadically
- Update all tests to match new variadic call convention
@corevibe555 corevibe555 force-pushed the refactor/otel-handlers-use-generics branch from bfed08e to 43a5640 Compare April 10, 2026 04:03
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@corevibe555 corevibe555 changed the title refactor(otel): replace Any with Tracer and [T] generics in span hand… refactor(otel): replace Any with Tracer and [T] generics Apr 10, 2026
Comment thread api/extensions/otel/decorators/handlers/generate_handler.py Outdated
Comment thread api/extensions/otel/decorators/handler.py Outdated
Comment thread api/extensions/otel/decorators/handler.py Outdated
Comment thread api/extensions/otel/decorators/handlers/workflow_app_runner_handler.py Outdated
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Apr 10, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@corevibe555
Copy link
Copy Markdown
Contributor Author

@asukaminato0721 resolved comments.
As we work types recently, I think this PR can be taken care of as well: #34754

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Apr 10, 2026
@asukaminato0721 asukaminato0721 requested a review from Copilot April 10, 2026 07:37
@asukaminato0721 asukaminato0721 added this pull request to the merge queue Apr 10, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors the OpenTelemetry span handler APIs to preserve wrapped function return types via PEP 695 generics and to tighten typing around the tracer parameter and argument passing.

Changes:

  • Update SpanHandler.wrapper / _extract_arguments to accept *args/**kwargs (ParamSpec) and return R, preserving wrapped return types.
  • Type tracer as opentelemetry.trace.Tracer across handlers and narrow signature cache typing.
  • Update unit tests to match the new calling conventions for _extract_arguments and wrapper.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
api/extensions/otel/decorators/handler.py Refactors core handler APIs to ParamSpec-based *args/**kwargs, preserves return type R, and types tracer as Tracer.
api/extensions/otel/decorators/base.py Updates the decorator to forward arguments via handler.wrapper(tracer, func, *args, **kwargs).
api/extensions/otel/decorators/handlers/generate_handler.py Aligns handler override with the new generic wrapper signature and typed Tracer.
api/extensions/otel/decorators/handlers/workflow_app_runner_handler.py Aligns handler override with the new generic wrapper signature and typed Tracer.
api/tests/unit_tests/extensions/otel/decorators/test_handler.py Updates SpanHandler unit tests to call _extract_arguments/wrapper using *args/**kwargs.
api/tests/unit_tests/extensions/otel/decorators/handlers/test_generate_handler.py Updates generate handler tests for the new _extract_arguments/wrapper signatures.
api/tests/unit_tests/extensions/otel/decorators/handlers/test_workflow_app_runner_handler.py Updates workflow runner handler test to call wrapper with positional args.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Merged via the queue into langgenius:main with commit af55665 Apr 10, 2026
31 checks passed
HanqingZ pushed a commit to HanqingZ/dify that referenced this pull request Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer refactor size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants