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

fix(llamaindex): instrument agents & tools #533

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

from opentelemetry.instrumentation.instrumentor import BaseInstrumentor

from opentelemetry.instrumentation.llamaindex.base_agent_instrumentor import (
BaseAgentInstrumentor,
)
from opentelemetry.instrumentation.llamaindex.retriever_query_engine_instrumentor import (
RetrieverQueryEngineInstrumentor,
)
Expand All @@ -16,6 +19,9 @@
from opentelemetry.instrumentation.llamaindex.base_synthesizer_instrumentor import (
BaseSynthesizerInstrumentor,
)
from opentelemetry.instrumentation.llamaindex.base_tool_instrumentor import (
BaseToolInstrumentor,
)
from opentelemetry.instrumentation.llamaindex.base_embedding_instrumentor import (
BaseEmbeddingInstrumentor,
)
Expand Down Expand Up @@ -48,6 +54,8 @@ def _instrument(self, **kwargs):
BaseEmbeddingInstrumentor(tracer).instrument()
CustomLLMInstrumentor(tracer).instrument()
QueryPipelineInstrumentor(tracer).instrument()
BaseAgentInstrumentor(tracer).instrument()
BaseToolInstrumentor(tracer).instrument()

def _uninstrument(self, **kwargs):
pass
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"
nirga marked this conversation as resolved.
Show resolved Hide resolved
V10_MODULE_NAME = "llama_index.core.agent.types"
V10_LEGACY_MODULE_NAME = "llama_index.legacy.agent.types"

CLASS_NAME = "BaseAgent"


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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from importlib.metadata import version as package_version, PackageNotFoundError
Copy link
Contributor

Choose a reason for hiding this comment

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

🙃 (NIT) There is a lot of duplication here. Maybe it could be refactored sometime.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I know :/ I need to refactor this


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 BaseToolInstrumentor:
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)
Loading
Loading