-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add get_current_span helper function for llama-index (#1165)
- Loading branch information
1 parent
64d5ac6
commit b46931c
Showing
3 changed files
with
64 additions
and
1 deletion.
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
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
51 changes: 51 additions & 0 deletions
51
...tion-llama-index/tests/openinference/instrumentation/llama_index/test_get_current_span.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,51 @@ | ||
from asyncio import create_task, gather, sleep | ||
from random import random | ||
from typing import Iterator | ||
|
||
import pytest | ||
from llama_index.core.instrumentation import get_dispatcher | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter | ||
|
||
from openinference.instrumentation.llama_index import LlamaIndexInstrumentor, get_current_span | ||
from openinference.semconv.trace import SpanAttributes | ||
|
||
dispatcher = get_dispatcher(__name__) | ||
|
||
|
||
@dispatcher.span # type: ignore[misc,unused-ignore] | ||
async def foo(k: int) -> str: | ||
child = create_task(foo(k - 1)) if k > 1 else None | ||
await sleep(random() / 100) | ||
span = get_current_span() | ||
if child: | ||
await child | ||
return str(span.get_span_context().span_id) if span else "" | ||
|
||
|
||
async def test_get_current_span( | ||
in_memory_span_exporter: InMemorySpanExporter, | ||
) -> None: | ||
n, k = 10, 5 | ||
await gather(*(foo(k) for _ in range(n))) | ||
spans = in_memory_span_exporter.get_finished_spans() | ||
assert len(spans) == n * k | ||
seen = set() | ||
for span in spans: | ||
assert span.attributes and span.context | ||
assert (expected := str(span.context.span_id)) not in seen | ||
seen.add(expected) | ||
assert span.attributes.get(OUTPUT_VALUE) == expected | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def instrument( | ||
tracer_provider: TracerProvider, | ||
in_memory_span_exporter: InMemorySpanExporter, | ||
) -> Iterator[None]: | ||
LlamaIndexInstrumentor().instrument(tracer_provider=tracer_provider) | ||
yield | ||
LlamaIndexInstrumentor().uninstrument() | ||
|
||
|
||
OUTPUT_VALUE = SpanAttributes.OUTPUT_VALUE |