Skip to content

feat(python-sdk): add extension instrumentation#2096

Draft
pilartomas wants to merge 1 commit intomainfrom
feat(python-sdk)--extension-instrumentation
Draft

feat(python-sdk): add extension instrumentation#2096
pilartomas wants to merge 1 commit intomainfrom
feat(python-sdk)--extension-instrumentation

Conversation

@pilartomas
Copy link
Contributor

@pilartomas pilartomas commented Feb 13, 2026

Signed-off-by: Tomas Pilar thomas7pilar@gmail.com

Summary

Linked Issues

closes #1975

Documentation

  • No Docs Needed:

If this PR adds new feature or changes existing. Make sure documentation is adjusted accordingly. If the docs is not needed, please explain why.

Signed-off-by: Tomas Pilar <thomas7pilar@gmail.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @pilartomas, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the observability of the Python SDK by introducing OpenTelemetry-based instrumentation, specifically targeting the A2A extension components. It provides a robust framework for tracing functions and class methods, allowing for automatic generation of spans that capture the flow and performance of operations within agent extensions.

Highlights

  • New Telemetry Utility Module: A new telemetry.py module was introduced to centralize OpenTelemetry tracing logic, providing reusable decorators for instrumentation.
  • Function Tracing Decorator: Implemented trace_function decorator to enable flexible tracing of both synchronous and asynchronous Python functions, capturing execution details and exceptions.
  • Class Method Tracing Decorator: Developed trace_class decorator for automatically applying tracing to specified methods within a class, supporting configurable include/exclude lists and OpenTelemetry SpanKinds.
  • Extension Instrumentation: Integrated automatic tracing into BaseExtensionServer and BaseExtensionClient classes, marking their methods with SpanKind.SERVER and SpanKind.CLIENT respectively for enhanced observability of agent extensions.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • apps/agentstack-sdk-py/src/agentstack_sdk/a2a/extensions/base.py
    • Imported necessary OpenTelemetry tracing modules and the new trace_class decorator.
    • Applied the trace_class decorator to BaseExtensionServer to enable server-side tracing for its methods.
    • Applied the trace_class decorator to BaseExtensionClient to enable client-side tracing for its methods.
  • apps/agentstack-sdk-py/src/agentstack_sdk/util/telemetry.py
    • Added a new file containing OpenTelemetry tracing helper functions and decorators.
    • Implemented trace_function for decorating individual functions with tracing capabilities, handling both sync and async functions.
    • Implemented trace_class for decorating entire classes to automatically trace their methods based on include/exclude lists.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces OpenTelemetry-based instrumentation for extensions, which is a great addition for observability. The core of this is the new agentstack_sdk/util/telemetry.py file, which provides trace_function and trace_class decorators.

My review focuses on the correctness and robustness of these new utilities. I've identified a couple of important issues:

  • A bug in how asyncio.CancelledError is handled, which could lead to incomplete trace data.
  • An issue with tracing methods decorated with @asynccontextmanager, like lifespan, which would result in incorrect trace spans.

I've provided suggestions to fix these issues. Additionally, I've pointed out a couple of areas for improvement in code quality. Overall, this is a solid feature, and with these adjustments, it will be even more robust.


def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
trace_class(kind=SpanKind.SERVER)(cls)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The trace_class decorator is being applied to BaseExtensionServer, which includes the lifespan method decorated with @asynccontextmanager. The current trace_function implementation does not correctly handle async context managers, which will lead to incorrect tracing (the span will only cover the creation of the context manager, not its execution). To prevent issues, you should exclude lifespan from tracing. It's also good practice to exclude internal methods like _fork.

Suggested change
trace_class(kind=SpanKind.SERVER)(cls)
trace_class(kind=SpanKind.SERVER, exclude_list=["lifespan", "_fork"])(cls)

Comment on lines +103 to +106
except asyncio.CancelledError as ce:
exception = None
span.record_exception(ce)
raise
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The attribute_extractor will not receive the asyncio.CancelledError exception because exception is explicitly set to None. This can lead to incomplete telemetry data. The exception should be captured and passed to the extractor.

Suggested change
except asyncio.CancelledError as ce:
exception = None
span.record_exception(ce)
raise
except asyncio.CancelledError as ce:
exception = ce
span.record_exception(ce)
raise


from agentstack_sdk.util.telemetry import trace_class

tracer = trace.get_tracer(__name__)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This tracer instance appears to be unused and can be removed to improve code clarity.

@functools.wraps(func)
async def async_wrapper(*args, **kwargs) -> Any:
"""Async Wrapper for the decorator."""
tracer = trace.get_tracer(INSTRUMENTING_MODULE_NAME, INSTRUMENTING_MODULE_VERSION)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Getting the tracer on every function call is inefficient and leads to code duplication with the sync_wrapper (line 126). The opentelemetry.trace.get_tracer call is idempotent, but best practice is to call it once per module. Consider getting the tracer once, either at the module level or within trace_function but outside the async_wrapper and sync_wrapper definitions.

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 instrumentation to SDK extensions

1 participant