-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(llamaindex): instrument agents & tools
- Loading branch information
Showing
5 changed files
with
654 additions
and
595 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...umentation-llamaindex/opentelemetry/instrumentation/llamaindex/base_agent_instrumentor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from importlib.metadata import version as package_version, PackageNotFoundError | ||
|
||
from wrapt import wrap_function_wrapper | ||
|
||
from opentelemetry.instrumentation.llamaindex.utils import ( | ||
_with_tracer_wrapper, | ||
start_as_current_span_async, | ||
) | ||
from opentelemetry.semconv.ai import SpanAttributes, TraceloopSpanKindValues | ||
|
||
|
||
V9_MODULE_NAME = "llama_index.agent.types" | ||
V10_MODULE_NAME = "llama_index.core.agent.types" | ||
V10_LEGACY_MODULE_NAME = "llama_index.legacy.agent.types" | ||
|
||
CLASS_NAME = "BaseRetriever" | ||
|
||
|
||
class BaseAgentInstrumentor: | ||
def __init__(self, tracer): | ||
self._tracer = tracer | ||
|
||
def instrument(self): | ||
try: | ||
package_version("llama-index-core") | ||
self._instrument_module(V10_MODULE_NAME) | ||
self._instrument_module(V10_LEGACY_MODULE_NAME) | ||
|
||
except PackageNotFoundError: | ||
self._instrument_module(V9_MODULE_NAME) | ||
|
||
def _instrument_module(self, module_name): | ||
wrap_function_wrapper( | ||
module_name, f"{CLASS_NAME}._query", query_wrapper(self._tracer) | ||
) | ||
wrap_function_wrapper( | ||
module_name, f"{CLASS_NAME}._aquery", aquery_wrapper(self._tracer) | ||
) | ||
|
||
|
||
@_with_tracer_wrapper | ||
def query_wrapper(tracer, wrapped, instance, args, kwargs): | ||
with tracer.start_as_current_span(f"{instance.__class__.__name__}.agent") as span: | ||
span.set_attribute( | ||
SpanAttributes.TRACELOOP_SPAN_KIND, | ||
TraceloopSpanKindValues.AGENT.value, | ||
) | ||
|
||
return wrapped(*args, **kwargs) | ||
|
||
|
||
@_with_tracer_wrapper | ||
async def aquery_wrapper(tracer, wrapped, instance, args, kwargs): | ||
async with start_as_current_span_async( | ||
tracer=tracer, name=f"{instance.__class__.__name__}.agent" | ||
) as span: | ||
span.set_attribute( | ||
SpanAttributes.TRACELOOP_SPAN_KIND, | ||
TraceloopSpanKindValues.AGENT.value, | ||
) | ||
|
||
return await wrapped(*args, **kwargs) |
62 changes: 62 additions & 0 deletions
62
...rumentation-llamaindex/opentelemetry/instrumentation/llamaindex/base_tool_instrumentor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from importlib.metadata import version as package_version, PackageNotFoundError | ||
|
||
from wrapt import wrap_function_wrapper | ||
|
||
from opentelemetry.instrumentation.llamaindex.utils import ( | ||
_with_tracer_wrapper, | ||
start_as_current_span_async, | ||
) | ||
from opentelemetry.semconv.ai import SpanAttributes, TraceloopSpanKindValues | ||
|
||
|
||
V9_MODULE_NAME = "llama_index.tools.types" | ||
V10_MODULE_NAME = "llama_index.core.tools.types" | ||
V10_LEGACY_MODULE_NAME = "llama_index.legacy.tools.types" | ||
|
||
CLASS_NAME = "AsyncBaseTool" | ||
|
||
|
||
class BaseAgentInstrumentor: | ||
def __init__(self, tracer): | ||
self._tracer = tracer | ||
|
||
def instrument(self): | ||
try: | ||
package_version("llama-index-core") | ||
self._instrument_module(V10_MODULE_NAME) | ||
self._instrument_module(V10_LEGACY_MODULE_NAME) | ||
|
||
except PackageNotFoundError: | ||
self._instrument_module(V9_MODULE_NAME) | ||
|
||
def _instrument_module(self, module_name): | ||
wrap_function_wrapper( | ||
module_name, f"{CLASS_NAME}.call", query_wrapper(self._tracer) | ||
) | ||
wrap_function_wrapper( | ||
module_name, f"{CLASS_NAME}.acall", aquery_wrapper(self._tracer) | ||
) | ||
|
||
|
||
@_with_tracer_wrapper | ||
def query_wrapper(tracer, wrapped, instance, args, kwargs): | ||
with tracer.start_as_current_span(f"{instance.__class__.__name__}.tool") as span: | ||
span.set_attribute( | ||
SpanAttributes.TRACELOOP_SPAN_KIND, | ||
TraceloopSpanKindValues.TOOL.value, | ||
) | ||
|
||
return wrapped(*args, **kwargs) | ||
|
||
|
||
@_with_tracer_wrapper | ||
async def aquery_wrapper(tracer, wrapped, instance, args, kwargs): | ||
async with start_as_current_span_async( | ||
tracer=tracer, name=f"{instance.__class__.__name__}.tool" | ||
) as span: | ||
span.set_attribute( | ||
SpanAttributes.TRACELOOP_SPAN_KIND, | ||
TraceloopSpanKindValues.TOOL.value, | ||
) | ||
|
||
return await wrapped(*args, **kwargs) |
Oops, something went wrong.