|
4 | 4 | from fastapi import APIRouter, Header
|
5 | 5 | from fastapp.models.sample import SampleRequest, SampleResponse
|
6 | 6 | from fastapp.utils import setup_logging, setup_tracer
|
| 7 | +from opentelemetry.trace import SpanKind |
7 | 8 |
|
8 | 9 | logger = setup_logging(__name__)
|
9 | 10 | tracer = setup_tracer(__name__)
|
|
15 | 16 | async def post_predict(
|
16 | 17 | data: SampleRequest, x_forwarded_for: Annotated[str, Header()] = ""
|
17 | 18 | ) -> SampleResponse:
|
18 |
| - logger.info(f"Received request: '{data}' from ip '{x_forwarded_for}'") |
| 19 | + logger.info(f"Received request: {data}") |
19 | 20 |
|
20 | 21 | # Sample request
|
21 |
| - async with httpx.AsyncClient() as client: |
22 |
| - response = await client.get("https://www.bing.com") |
23 |
| - logger.info(f"Received response status code: {response.status_code}") |
| 22 | + tracer_attributes = {"http.client_ip": x_forwarded_for} |
| 23 | + with tracer.start_as_current_span( |
| 24 | + "dependency_span", attributes=tracer_attributes, kind=SpanKind.CLIENT |
| 25 | + ) as span: |
| 26 | + try: |
| 27 | + async with httpx.AsyncClient() as client: |
| 28 | + response = await client.get("https://www.bing.com") |
| 29 | + logger.info(f"Received response status code: {response.status_code}") |
| 30 | + except Exception as ex: |
| 31 | + span.set_attribute("status", "exception") |
| 32 | + span.record_exception(ex) |
24 | 33 |
|
25 | 34 | return SampleResponse(output=f"Hello {data.input}")
|
0 commit comments