Hi team, QQ - Is there a way to programmatically export the Project ID of a Phoenix project? We are trying to put a link to a remote Phoenix env on our chatbot so that our users can investigate the traces as they ask questions. In order to do this, need to provide a link to the specific project and get the ID from Phoenix once the project is created.
Does the project need to be created dynamically? If the project is already created, you can grab the ID from the slug.
Project will already be created but as soon as it is created, I need a way to programmatically pull the ID so that I can construct a link to the right tracing project and put it on the frontend of my application/chatbot. I do not want to grab it from the URL. I tried grabbing from active sessions but did not work
You can construct a GraphQL query if needed. Although maybe I am missing something, is the project also being programmatically created?
Yes
So it's like a dynamically created project based on a session or something?
Oh yeah actually it would be dynamic based on the session you are right
This sounds like a workaround since Phoenix does not natively support sessions in the UI?
Its not necessarily 1 user-specific sessions though. We are just trying to standardize and create a central approach where any chatbot we onboard automatically has Phoenix tracing and also a link to the right Phoenix project on the frontend. So the project is dynamically created whenever we create the chatbot but that is it.
I see.
One sec/
from typing import Optional
from httpx import Client
client = Client(base_url="http://localhost:6006")
query = """
query ($after: String = null) {
projects(after: $after) {
edges {
project: node {
id
name
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
"""
def get_id_for_project_name(name: str) -> Optional[str]:
has_next_page = True
after = None
while has_next_page:
response = client.post(
"/graphql", json={"query": query, "variables": {"after": after}}
)
if (status_code := response.status_code) != 200:
raise Exception(f"Failed with status code: {status_code}")
data = response.json()["data"]
edges = data["projects"]["edges"]
projects = map(lambda edge: edge["project"], edges)
for project in projects:
if project["name"] == name:
return project["id"]
page_info = data["projects"]["pageInfo"]
has_next_page = page_info["hasNextPage"]
after = page_info["endCursor"]
return None
print(f"{get_id_for_project_name("default")=}")
print(f"{get_id_for_project_name("non-existent")=}")
