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

Trace decorator #1553

Merged
merged 6 commits into from
Dec 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix decorator
  • Loading branch information
korgan00 committed Dec 20, 2024
commit 585c202d4f6f680486d53355f4b9ee64acfdc148
2 changes: 1 addition & 1 deletion client/qiskit_serverless/core/clients/serverless_client.py
Original file line number Diff line number Diff line change
@@ -382,7 +382,7 @@ def function(
timeout=REQUESTS_TIMEOUT,
)
)

return RunnableQiskitFunction(
client=self,
title=response_data.get("title"),
17 changes: 10 additions & 7 deletions client/qiskit_serverless/core/decorators.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@
import inspect
import os
import shutil
from types import FunctionType
import warnings
from dataclasses import dataclass
from typing import Optional, Dict, Any, Union, List, Callable, Sequence
@@ -456,29 +457,31 @@ def distribute_program(
def trace_decorator_factory(traced_feature: str):
"""Factory for generate decorators for classes or features."""

def generated_decorator(
_func: Optional[Callable] = None, *, traced_function: Optional[str] = None
):
def generated_decorator(traced_function: Union[FunctionType, str]):
"""
The decorator wrapper to generate optional arguments
if traced_function is nullable it will be replaced by the decorated function name.
"""

def decorator_trace(func: Callable):
def decorator_trace(func: FunctionType):
"""The decorator that python call"""

def wrapper(*args, **kwargs):
"""The wrapper"""
tracer = trace.get_tracer("client.tracer")
function_name = traced_function if traced_function else func.__name__
function_name = (
traced_function
if isinstance(traced_function, str)
else func.__name__
)
with tracer.start_as_current_span(f"{traced_feature}.${function_name}"):
result = func(*args, **kwargs)
return result

return wrapper

if _func:
return decorator_trace(_func)
if callable(traced_function):
return decorator_trace(traced_function)
return decorator_trace

return generated_decorator