Skip to content

feat(llama-index): add loongsuite-instrumentation-llama-index (#18) - #273

Open
RichardoMrMu wants to merge 13 commits into
alibaba:mainfrom
RichardoMrMu:feat/llama-index-instrumentation
Open

RichardoMrMu wants to merge 13 commits into
alibaba:mainfrom
RichardoMrMu:feat/llama-index-instrumentation

Conversation

@RichardoMrMu

Copy link
Copy Markdown
Contributor

What

Adds a new instrumentation package loongsuite-instrumentation-llama-index, providing automatic OpenTelemetry instrumentation for LlamaIndex (llama-index-core).

Closes #18 (roadmap #36 — "Add instrumentation for llama-index", contribution welcome).

How

Rather than monkey-patching call sites, this attaches to LlamaIndex's native instrumentation dispatcher (llama_index.core.instrumentation). LlamaIndex already emits a span/event stream through a root Dispatcher, assigning each instrumented call a span id_ and a parent_span_id that reflects the logical call tree (e.g. queryretrieve/synthesize, chatcomplete). The package registers a BaseSpanHandler + BaseEventHandler on that dispatcher and re-projects the stream onto OpenTelemetry spans following the ARMS gen-ai semantic conventions, consuming parent_span_id directly so the OTel trace preserves LlamaIndex's own parent/child structure.

Span-kind mapping (gen_ai.span.kind): LLM / EMBEDDING / RETRIEVER / RERANKER / TASK (synthesis) / CHAIN (query engine) / AGENT (chat engine, agent run). Classification is method-first so class names embedding a misleading keyword (e.g. RetrieverQueryEngine.query) are classified correctly (CHAIN, not RETRIEVER). LLM/embedding events fold request model, messages and provider token usage onto the span. Content capture can be disabled with OTEL_INSTRUMENTATION_LLAMA_INDEX_CAPTURE_CONTENT=false.

Testing

tests/test_instrumentor.py drives the real llama-index-core dispatcher with in-process MockLLM / MockEmbedding and asserts on spans exported to an InMemorySpanExporter (no network, no credentials). Each GREEN assertion is paired with a RED baseline:

  • RED: an LLM chat with no instrumentor active produces zero spans.
  • GREEN: chat produces an LLM span with correct gen_ai.* attributes; chatcomplete share one trace_id with complete nested under chat (proving parent_span_id mapping); embedding produces an EMBEDDING span.
  • RED-after-teardown: after uninstrument(), a subsequent chat produces zero spans.
  • Plus classification unit tests and idempotent-uninstrument.

15 tests pass (pytest), on llama-index-core 0.14.25 + opentelemetry-sdk.

Notes

  • Package layout, pyproject.toml, entry point, CHANGELOG and bootstrap-registry entry follow the existing loongsuite-instrumentation-* conventions (e.g. terminus2).
  • CHANGELOG.md updated (repo requirement).

…-index/src/opentelemetry/instrumentation/llama_index/package.py
…-index/src/opentelemetry/instrumentation/llama_index/version.py
…-index/src/opentelemetry/instrumentation/llama_index/__init__.py
…format)

CI precommit failed on the new package: E402 in tests/conftest.py (imports intentionally after sys.path setup + pytest_configure), PLC0415 for lazy imports in src, and I001/format. Follow the existing per-file-ignores convention used by every other loongsuite instrumentation package (PLC0415 for the package, E402+F811 for tests), and apply ruff import sorting + formatting. No behavior change; 15 tests still pass.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Structured prediction classification, active-span teardown, hierarchy verification, and CI registration need correction.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 3 Medium severity · 1 Low severity

Open (4)
What changed in this PR

Adds automatic OpenTelemetry instrumentation for LlamaIndex via its native dispatcher.

Changes:

  • Maps LlamaIndex spans and events to GenAI telemetry.
  • Adds lifecycle, hierarchy, classification, and embedding tests.
  • Registers packaging and bootstrap metadata.
File Description
pyproject.toml Adds Ruff exceptions.
loongsuite-distro/​.../​loongsuite_instrumentation_llama_index.py Adds bootstrap registry metadata.
.../​tests/​test_instrumentor.py Tests spans and lifecycle.
.../​tests/​conftest.py Configures test tracing.
.../​tests/​__init__.py Initializes tests.
.../​test-requirements.txt Adds test dependencies.
.../​version.py Defines package version.
.../​package.py Defines instrument dependency.
.../​llama_index/​__init__.py Implements instrumentation.
.../​README.md Documents usage.
.../​pyproject.toml Defines package metadata.
.../​LICENSE Adds Apache license.
.../​CHANGELOG.md Records initial release.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +166 to +167
if method in ("predict", "apredict", "structured_predict"):
return _SPAN_KIND_LLM, _OP_CHAT
Comment on lines +584 to +588
dispatcher.span_handlers = [
h
for h in dispatcher.span_handlers
if h is not self._span_handler
]
Comment thread pyproject.toml
Comment on lines +188 to +189
"instrumentation-loongsuite/loongsuite-instrumentation-llama-index/**/*.py" = ["PLC0415"]
"instrumentation-loongsuite/loongsuite-instrumentation-llama-index/tests/**/*.py" = ["E402", "F811"]
Comment on lines +135 to +142
by_span_id = {s.context.span_id: s for s in spans}
chat = next(s for s in spans if s.name.endswith(".chat"))
complete = next(s for s in spans if s.name.endswith(".complete"))

# complete's parent chain must reach the chat span within the same trace.
assert complete.parent is not None
assert complete.parent.span_id in by_span_id
assert complete.context.trace_id == chat.context.trace_id

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feat]Add instrumentation for llama-index

5 participants