Replies: 4 comments 1 reply
-
@adam-moss did you ever get this working? I'm in the same boat, wanting to instrument all CLI commands (also for OTEL). |
Beta Was this translation helpful? Give feedback.
-
Have you considered using functools.wraps in your decorator? https://docs.python.org/3/library/functools.html#functools.wraps |
Beta Was this translation helpful? Give feedback.
-
I've had a chance to test this and I concur that putting the decorator above the from functools import wraps
def test_wrap(func: Callable[..., Any]) -> Callable[..., Any]:
@wraps(func)
def wrapper(*args: tuple, **kwargs):
print("Starting wrapper")
return func(*args, **kwargs)
return wrapper |
Beta Was this translation helpful? Give feedback.
-
I concur on using However, I may suggest a different approach in case you have many commands you need to decorate: you may subclass class TyperWithTelemetry(typer.Typer):
@log_execution_time
def __call__(self, *args, **kwargs):
return super().__call__(*args, **kwargs) |
Beta Was this translation helpful? Give feedback.
-
First Check
Commit to Help
Example Code
Description
I am wondering what the recommended / better practice patterns are for including telemetry within a typer based cli?
For example, if I had a decorator like:
How could like be applied to a method in a typer cli?
I've tried
This doesn't fail but equally doesn't log anything. Doing it the opposite way:
Raises error
RuntimeError: Type not yet supported: <class 'tuple'>
. If I remove thetuple
type annotation I instead get the errorNo such command 'do_something'
Operating System
macOS
Operating System Details
Monterey 12.6.3
Typer Version
0.7.0
Python Version
3.11.2
Additional Context
I'm currently testing it by using the logging module but ultimately it'll use OpenTelemetry.
Beta Was this translation helpful? Give feedback.
All reactions