hey folks, does anyone have a cheeky query on hand to get all of the descendants (not just immediate children) of a specific span_id? i've done the naive implementation of recessively querying children with separate queries to phoenix and it works but it is a wee bit slow
To efficiently retrieve all descendants of a specific span_id in Phoenix, you can use the SpanDescendantsDataLoader class, which is designed to load all descendant spans in a single query. This approach avoids the need for multiple recursive queries and should improve performance.Here's a concise example of how you might implement this: LBKSfrom phoenix.server.api.dataloaders.span_descendants import SpanDescendantsDataLoader from phoenix.db import DbSessionFactoryLBKS# Initialize the data loader with your database session factory span_descendants_loader = SpanDescendantsDataLoader(db=DbSessionFactory())LBKS# Load all descendants for a given span_id descendants = await span_descendants_loader.load(span_id)LBKSThis method uses a recursive common table expression (CTE) to efficiently gather all descendant spans in one go, which should be faster than making separate queries for each level of descendants.Would you like more detail?Sources:
- SpanDescendantsDataLoader
馃挕聽Hint: Mention RunLLM in the thread for followups.
here鈥檚 how the Ui does it
from httpx import Client
import pandas as pd
query = """
query ($spanId: GlobalID!) {
node(id: $spanId) {
... on Span {
descendants {
id
context {
spanId
}
}
}
}
}
"""
def get_descendants(span_id: str) -> pd.DataFrame:
response = Client(base_url="http://localhost:6006").post(
"graphql",
json={
"query": query,
"variables": {
"spanId": span_id,
},
},
)
spans = response.json()["data"]["node"]["descendants"]
return pd.json_normalize(spans)
span_id = "U3BhbjoyMQ==" # replace with your span id
df = get_descendants(span_id)
df
you need to get the span node id from the UI
can also use the graphql endpoint
oh wait -- is there any way to get span node id using python interfaces? sorry, i confused the span node id with the span id
Reviving this thread... Is there a way to do this using phoenix.Client and leveraging OpenTelemetry's span_id? I need to fetch all descendants of a root span.
Sorry I missed the follow-up question here. My apologies. Currently the only way to do this would be via the OTEL trace_id instead as shown below.
from httpx import Client
import pandas as pd
query = """
query($traceId: ID!, $projectId: GlobalID!) {
node(id: $projectId) {
... on Project {
trace(traceId: $traceId) {
spans {
edges {
node {
id
spanId
parentId
trace {
traceId
}
}
}
}
}
}
}
}
"""
def get_spans(trace_id: str, project_id: str) -> pd.DataFrame:
response = Client(base_url="http://localhost:6006").post(
"graphql",
json={
"query": query,
"variables": {
"projectId": project_id,
"traceId": trace_id,
},
},
)
spans = response.json()["data"]["node"]["trace"]["spans"]["edges"]
df = pd.json_normalize(spans)
df.columns = df.columns.str.replace('node.', '')
return df
project_id = "UHJvamVjdDox" # replace with your project node id
trace_id = "9f344b7a20422dd4989aaf01826bdfd9" # replace with your trace OTEL id
df = get_spans(trace_id, project_id)
df
