|
5 | 5 | from fastapi import APIRouter, HTTPException, Request |
6 | 6 | from fastapi.params import Depends |
7 | 7 | from ogx_client import APIConnectionError, BadRequestError |
| 8 | +from opentelemetry import trace |
8 | 9 |
|
9 | 10 | from authentication import get_auth_dependency |
10 | 11 | from authentication.interface import AuthTuple |
|
28 | 29 | from utils.endpoints import check_configuration_loaded |
29 | 30 |
|
30 | 31 | logger = get_logger(__name__) |
| 32 | +tracer = trace.get_tracer(__name__) |
31 | 33 | router = APIRouter(tags=["rags"]) |
32 | 34 |
|
33 | 35 |
|
@@ -83,33 +85,35 @@ async def rags_endpoint_handler( |
83 | 85 | # Nothing interesting in the request |
84 | 86 | _ = request |
85 | 87 |
|
86 | | - # make sure that the configuration is loaded |
87 | | - check_configuration_loaded(configuration) |
| 88 | + with tracer.start_as_current_span("rags.list") as span: |
| 89 | + # make sure that the configuration is loaded |
| 90 | + check_configuration_loaded(configuration) |
88 | 91 |
|
89 | | - llama_stack_configuration = configuration.llama_stack_configuration |
90 | | - logger.info("Llama Stack config: %s", llama_stack_configuration) |
| 92 | + llama_stack_configuration = configuration.llama_stack_configuration |
| 93 | + logger.info("Llama Stack config: %s", llama_stack_configuration) |
91 | 94 |
|
92 | | - try: |
93 | | - # try to get Llama Stack client |
94 | | - client = AsyncOgxClientHolder().get_client() |
95 | | - # retrieve list of RAGs |
96 | | - rags = await client.vector_stores.list() |
97 | | - logger.info("List of rags: %d", len(rags.data)) |
| 95 | + try: |
| 96 | + # try to get Llama Stack client |
| 97 | + client = AsyncOgxClientHolder().get_client() |
| 98 | + # retrieve list of RAGs |
| 99 | + rags = await client.vector_stores.list() |
| 100 | + logger.info("List of rags: %d", len(rags.data)) |
98 | 101 |
|
99 | | - # Map llama-stack vector store IDs to user-facing rag_ids from config |
100 | | - rag_id_mapping = configuration.rag_id_mapping |
101 | | - rag_ids = [ |
102 | | - configuration.resolve_index_name(rag.id, rag_id_mapping) |
103 | | - for rag in rags.data |
104 | | - ] |
| 102 | + # Map llama-stack vector store IDs to user-facing rag_ids from config |
| 103 | + rag_id_mapping = configuration.rag_id_mapping |
| 104 | + rag_ids = [ |
| 105 | + configuration.resolve_index_name(rag.id, rag_id_mapping) |
| 106 | + for rag in rags.data |
| 107 | + ] |
105 | 108 |
|
106 | | - return RAGListResponse(rags=rag_ids) |
| 109 | + span.set_attribute("rags.count", len(rag_ids)) |
| 110 | + return RAGListResponse(rags=rag_ids) |
107 | 111 |
|
108 | | - # connection to Llama Stack server |
109 | | - except APIConnectionError as e: |
110 | | - logger.error("Unable to connect to Llama Stack: %s", e) |
111 | | - response = ServiceUnavailableResponse(backend_name="OGX", cause=str(e)) |
112 | | - raise HTTPException(**response.model_dump()) from e |
| 112 | + # connection to Llama Stack server |
| 113 | + except APIConnectionError as e: |
| 114 | + logger.error("Unable to connect to Llama Stack: %s", e) |
| 115 | + response = ServiceUnavailableResponse(backend_name="OGX", cause=str(e)) |
| 116 | + raise HTTPException(**response.model_dump()) from e |
113 | 117 |
|
114 | 118 |
|
115 | 119 | def _resolve_rag_id_to_vector_db_id(rag_id: str, byok_rags: list[RagStore]) -> str: |
@@ -171,42 +175,44 @@ async def get_rag_endpoint_handler( |
171 | 175 | # Nothing interesting in the request |
172 | 176 | _ = request |
173 | 177 |
|
174 | | - check_configuration_loaded(configuration) |
| 178 | + with tracer.start_as_current_span("rags.get") as span: |
| 179 | + check_configuration_loaded(configuration) |
175 | 180 |
|
176 | | - llama_stack_configuration = configuration.llama_stack_configuration |
177 | | - logger.info("Llama Stack config: %s", llama_stack_configuration) |
| 181 | + llama_stack_configuration = configuration.llama_stack_configuration |
| 182 | + logger.info("Llama Stack config: %s", llama_stack_configuration) |
178 | 183 |
|
179 | | - # Resolve user-facing rag_id to llama-stack vector_db_id |
180 | | - vector_db_id = _resolve_rag_id_to_vector_db_id( |
181 | | - rag_id, configuration.configuration.rag.byok.stores |
182 | | - ) |
183 | | - |
184 | | - try: |
185 | | - # try to get Llama Stack client |
186 | | - client = AsyncOgxClientHolder().get_client() |
187 | | - # retrieve info about RAG |
188 | | - rag_info = await client.vector_stores.retrieve(vector_db_id) |
189 | | - |
190 | | - # Return the user-facing ID (rag_id from config if mapped, otherwise as-is) |
191 | | - display_id = configuration.resolve_index_name( |
192 | | - rag_info.id, configuration.rag_id_mapping |
| 184 | + # Resolve user-facing rag_id to llama-stack vector_db_id |
| 185 | + vector_db_id = _resolve_rag_id_to_vector_db_id( |
| 186 | + rag_id, configuration.configuration.rag.byok.stores |
193 | 187 | ) |
194 | 188 |
|
195 | | - return RAGInfoResponse( |
196 | | - id=display_id, |
197 | | - name=rag_info.name, |
198 | | - created_at=rag_info.created_at, |
199 | | - last_active_at=rag_info.last_active_at, |
200 | | - expires_at=rag_info.expires_at, |
201 | | - object=rag_info.object or "vector_store", |
202 | | - status=rag_info.status or "unknown", |
203 | | - usage_bytes=rag_info.usage_bytes or 0, |
204 | | - ) |
205 | | - except APIConnectionError as e: |
206 | | - logger.error("Unable to connect to Llama Stack: %s", e) |
207 | | - response = ServiceUnavailableResponse(backend_name="OGX", cause=str(e)) |
208 | | - raise HTTPException(**response.model_dump()) from e |
209 | | - except BadRequestError as e: |
210 | | - logger.error("RAG not found: %s", e) |
211 | | - response = NotFoundResponse(resource="rag", resource_id=rag_id) |
212 | | - raise HTTPException(**response.model_dump()) from e |
| 189 | + try: |
| 190 | + # try to get Llama Stack client |
| 191 | + client = AsyncOgxClientHolder().get_client() |
| 192 | + # retrieve info about RAG |
| 193 | + rag_info = await client.vector_stores.retrieve(vector_db_id) |
| 194 | + |
| 195 | + # Return the user-facing ID (rag_id from config if mapped, otherwise as-is) |
| 196 | + display_id = configuration.resolve_index_name( |
| 197 | + rag_info.id, configuration.rag_id_mapping |
| 198 | + ) |
| 199 | + |
| 200 | + span.set_attribute("rags.found", True) |
| 201 | + return RAGInfoResponse( |
| 202 | + id=display_id, |
| 203 | + name=rag_info.name, |
| 204 | + created_at=rag_info.created_at, |
| 205 | + last_active_at=rag_info.last_active_at, |
| 206 | + expires_at=rag_info.expires_at, |
| 207 | + object=rag_info.object or "vector_store", |
| 208 | + status=rag_info.status or "unknown", |
| 209 | + usage_bytes=rag_info.usage_bytes or 0, |
| 210 | + ) |
| 211 | + except APIConnectionError as e: |
| 212 | + logger.error("Unable to connect to Llama Stack: %s", e) |
| 213 | + response = ServiceUnavailableResponse(backend_name="OGX", cause=str(e)) |
| 214 | + raise HTTPException(**response.model_dump()) from e |
| 215 | + except BadRequestError as e: |
| 216 | + logger.error("RAG not found: %s", e) |
| 217 | + response = NotFoundResponse(resource="rag", resource_id=rag_id) |
| 218 | + raise HTTPException(**response.model_dump()) from e |
0 commit comments