Skip to content

feat(tracer): add OpenTelemetryProvider implementation (#7003) - #8366

Open
Alwaysgaurav1 wants to merge 3 commits into
aws-powertools:developfrom
Alwaysgaurav1:feature/opentelemetry-provider
Open

Alwaysgaurav1 wants to merge 3 commits into
aws-powertools:developfrom
Alwaysgaurav1:feature/opentelemetry-provider

Conversation

@Alwaysgaurav1

@Alwaysgaurav1 Alwaysgaurav1 commented Aug 5, 2026

Copy link
Copy Markdown

Issue number: closes #7003

Summary

Changes

  • Implemented OpenTelemetrySegment and OpenTelemetryProvider in aws_lambda_powertools.tracing.opentelemetry.
  • Exported OpenTelemetryProvider and OpenTelemetrySegment from aws_lambda_powertools.tracing.
  • Added unit tests covering segment attribute set, exception recording, subsegment context managers, and Tracer decorator integration in tests/unit/test_opentelemetry.py.

User experience

Users can now pass an OpenTelemetryProvider to Tracer to send trace data to OpenTelemetry backends:

from aws_lambda_powertools import Tracer
from aws_lambda_powertools.tracing import OpenTelemetryProvider

provider = OpenTelemetryProvider()
tracer = Tracer(service="my-service", provider=provider)

@tracer.capture_method
def handler(event, context):
    return "ok"

@Alwaysgaurav1
Alwaysgaurav1 requested a review from a team as a code owner August 5, 2026 10:30
@Alwaysgaurav1
Alwaysgaurav1 requested a review from hjgraca August 5, 2026 10:30
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 5, 2026
@boring-cyborg boring-cyborg Bot added tests tracer Tracer utility labels Aug 5, 2026
@boring-cyborg

boring-cyborg Bot commented Aug 5, 2026

Copy link
Copy Markdown

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@Alwaysgaurav1 Alwaysgaurav1 removed their assignment Aug 5, 2026
@powertools-for-aws-oss-automation

Copy link
Copy Markdown

No acknowledgement section found. Please make sure you used the template to open a PR and didn't remove the acknowledgment section. Check the template at .github/PULL_REQUEST_TEMPLATE.md#acknowledgment

@sonarqubecloud

Copy link
Copy Markdown

@iblancasa iblancasa left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The provider abstraction is a useful direction, but I think this needs more work before it is ready to merge.

Issue #7003 and the linked RFC describe a first-class OpenTelemetry experience that can work alongside Lambda autoinstrumentation. This PR currently provides basic child-span creation, but it does not yet define the expected behavior for dependencies, asynchronous functions, error status, metadata handling, or automatic instrumentation.

Could you please address the inline runtime issues and add tests using a real OpenTelemetry SDK/in-memory exporter rather than only mocks? In particular, we should verify async decorators, parent-child context, error status, metadata serialization and size limits and the behavior of patch/patch_all.

It would also help to document how customers select this provider and whether this is intended as a complete OpenTelemetry tracing solution or only a lower-level provider building block.

I know it has been a while since you created the PR and maybe you are not interested anymore in the feature, your priorities changed or something else. If you don't have bandwidth for working on this PR, let me know and I can take it.

def in_subsegment_async(self, name: str | None = None, **kwargs) -> Generator[BaseSegment, None, None]:
name = name or "subsegment"
if self._tracer is not None:
with self._tracer.start_as_current_span(name) as span:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

in_subsegment_async is decorated with @contextmanager, which creates a synchronous context manager. Powertools uses this method with async with, so every async @capture_method call will fail at runtime


def __init__(self, tracer: Any | None = None):
if tracer is None:
try:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When OpenTelemetry is not installed, this silently creates a no-op provider. Users can deploy successfully but receive no traces.
I guess something should be, at least, printed

def add_exception(
self,
exception: BaseException,
stack: list[traceback.StackSummary] | None = None,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Converting metadata with str(value) loses structured data and produces Python representations rather than stable telemetry values.
It can also create very large span attributes. Please use bounded JSON serialization or record metadata as a bounded span event. The same issue exists in put_metadata

attr_key = f"{namespace}.{key}" if namespace else key
span.set_attribute(attr_key, str(value))
except ImportError:
pass

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tracer enables automatic patching by default, but patch and patch_all silently do nothing for this provider. This means customers may assume HTTP, AWS SDK, and database calls are instrumented when they are not

stack: list[traceback.StackSummary] | None = None,
remote: bool = False,
):
if self.span and hasattr(self.span, "record_exception"):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Recording an exception does not set the OpenTelemetry span status to ERROR. Failed handlers and methods may therefore appear successful in tracing backends

This branch has not been deployed

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

Labels

size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests tracer Tracer utility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: OpenTelemetry Support

2 participants