MCPcopy Create free account
hub / github.com/AgentOps-AI/agentops / wrapper

Function wrapper

agentops/sdk/decorators/factory.py:101–482  ·  view source on GitHub ↗
(
            wrapped_func: Callable[..., Any], instance: Optional[Any], args: tuple, kwargs: Dict[str, Any]
        )

Source from the content-addressed store, hash-verified

99
100 @wrapt.decorator
101 def wrapper(
102 wrapped_func: Callable[..., Any], instance: Optional[Any], args: tuple, kwargs: Dict[str, Any]
103 ) -> Any:
104 if not tracer.initialized:
105 return wrapped_func(*args, **kwargs)
106
107 operation_name = name or wrapped_func.__name__
108 is_async = asyncio.iscoroutinefunction(wrapped_func)
109 is_generator = inspect.isgeneratorfunction(wrapped_func)
110 is_async_generator = inspect.isasyncgenfunction(wrapped_func)
111
112 # Special handling for HTTP entity kind
113 if entity_kind == SpanKind.HTTP:
114 if is_generator or is_async_generator:
115 logger.warning(
116 f"@track_endpoint on generator '{operation_name}' is not supported. Use @trace instead."
117 )
118 return wrapped_func(*args, **kwargs)
119
120 if is_async:
121
122 async def _wrapped_http_async() -> Any:
123 trace_context: Optional[TraceContext] = None
124 try:
125 # Create main session span
126 trace_context = tracer.start_trace(trace_name=operation_name, tags=tags)
127 if not trace_context:
128 logger.error(
129 f"Failed to start trace for @track_endpoint '{operation_name}'. Executing without trace."
130 )
131 return await wrapped_func(*args, **kwargs)
132
133 # Create HTTP request span
134 if capture_request:
135 with _create_as_current_span(
136 f"{operation_name}.request",
137 SpanKind.HTTP,
138 version=version,
139 attributes={SpanAttributes.HTTP_METHOD: "REQUEST"}
140 if SpanAttributes.HTTP_METHOD
141 else None,
142 ) as request_span:
143 try:
144 request_data = _extract_request_data()
145 if request_data:
146 # Set HTTP attributes
147 if hasattr(SpanAttributes, "HTTP_METHOD") and request_data.get("method"):
148 request_span.set_attribute(
149 SpanAttributes.HTTP_METHOD, request_data["method"]
150 )
151 if hasattr(SpanAttributes, "HTTP_URL") and request_data.get("url"):
152 request_span.set_attribute(SpanAttributes.HTTP_URL, request_data["url"])
153
154 # Record the full request data
155 _record_entity_input(request_span, (request_data,), {})
156 except Exception as e:
157 logger.warning(f"Failed to record HTTP request for '{operation_name}': {e}")
158

Callers 1

decoratorFunction · 0.70

Calls 15

_create_as_current_spanFunction · 0.90
_extract_request_dataFunction · 0.90
_record_entity_inputFunction · 0.90
_extract_response_dataFunction · 0.90
_record_entity_outputFunction · 0.90
_process_sync_generatorFunction · 0.90
_process_async_generatorFunction · 0.90
_wrapped_http_asyncFunction · 0.85
_wrapped_session_asyncFunction · 0.85
_wrapped_asyncFunction · 0.85
start_traceMethod · 0.80
end_traceMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…