Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a tracer.decorate decorator that work with async functions #3595

Closed
wants to merge 8 commits into from

Conversation

QuentinN42
Copy link
Contributor

@QuentinN42 QuentinN42 commented Dec 16, 2023

Description

As we discussed in #3270, we want to be able to decorate functions with a span.

Currently it's not raising any exceptions, but async functions always reports a near zero span duration due to the result not beeing awaited.

But as context managers can be sync OR async but not both (@contextlib.asynccontextmanager), I've added a small method Tracer.decorate that do this job.

I'm still not sure about the function name ... Any ideas are welcome :)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

Not yet tested, here is my two tests idea :

  • Test a sync function decorator.
  • Test an async function decorator.

Does This PR Require a Contrib Repo Change?

Answer the following question based on these examples of changes that would require a Contrib Repo Change:

  • The OTel specification has changed which prompted this PR to update the method interfaces of opentelemetry-api/ or opentelemetry-sdk/

I dont think so, I'm in the "this functionality MAY be offered additionally as a separate operation" of Span creation.

  • No.

Checklist:

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

Copy link

linux-foundation-easycla bot commented Dec 16, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

@QuentinN42 QuentinN42 marked this pull request as ready for review December 16, 2023 15:44
@QuentinN42 QuentinN42 requested a review from a team December 16, 2023 15:44
Copy link
Member

@aabmass aabmass left a comment

Choose a reason for hiding this comment

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

This doesn't seem to actually change the behavior of tracer.start_as_current_span() at all. It looks like this PR adds a new separate decorator

opentelemetry-api/src/opentelemetry/trace/__init__.py Outdated Show resolved Hide resolved
Signed-off-by: QuentinN42 <quentin@lieumont.fr>
Signed-off-by: QuentinN42 <quentin@lieumont.fr>
Signed-off-by: QuentinN42 <quentin@lieumont.fr>
Signed-off-by: QuentinN42 <quentin@lieumont.fr>
Signed-off-by: QuentinN42 <quentin@lieumont.fr>
Signed-off-by: QuentinN42 <quentin@lieumont.fr>
Signed-off-by: QuentinN42 <quentin@lieumont.fr>
@aabmass
Copy link
Member

aabmass commented Jan 10, 2024

@QuentinN42 This doesn't seem to actually change the behavior of tracer.start_as_current_span() at all. It looks like this PR adds a new separate decorator

@QuentinN42
Copy link
Contributor Author

@QuentinN42 This doesn't seem to actually change the behavior of tracer.start_as_current_span() at all. It looks like this PR adds a new separate decorator

Yes as sayed in the initial comment, do you think I should create a custom sync and async contrxtmanager ?

@aabmass
Copy link
Member

aabmass commented Jan 11, 2024

Do you have a backward compatible way to achieve this without adding to the API surface? I will bring this up in the Python SIG meeting tomorrow (you're welcome to attend if you're available).

edit I think I've figured out a way to make it behave as expected if you need some help, lmk

record_exception: bool = True,
set_status_on_exception: bool = True,
end_on_exit: bool = True,
span_arg_name: str | None = "span",
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense for this to be implemented by its own decorator? It could be a free function which is nice and could be stacked with other decorators if needed

@trace.start_as_current_span("foo")
@with_span_arg("the_span")
def foo(the_span: Span):
  ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can open an issue if you think it's a useful feature.

I have tested the get_current_span().set_attribute(...) and I think it's a better way to make it works without surcharging the function parameters that must be more business oriented...

Signed-off-by: QuentinN42 <quentin@lieumont.fr>
@QuentinN42 QuentinN42 changed the title Make tracer.start_as_current_span() decorator work with async functions Create a tracer.decorate decorator that work with async functions Jan 12, 2024
@QuentinN42
Copy link
Contributor Author

Closing this MR in favor of #3633

@QuentinN42 QuentinN42 closed this Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants