Skip to content

Commit efbbd80

Browse files
committed
Test with span
1 parent 4f7de8f commit efbbd80

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

code/function/fastapp/api/v1/endpoints/sample.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from fastapi import APIRouter, Header
55
from fastapp.models.sample import SampleRequest, SampleResponse
66
from fastapp.utils import setup_logging, setup_tracer
7+
from opentelemetry.trace import SpanKind
78

89
logger = setup_logging(__name__)
910
tracer = setup_tracer(__name__)
@@ -15,11 +16,19 @@
1516
async def post_predict(
1617
data: SampleRequest, x_forwarded_for: Annotated[str, Header()] = ""
1718
) -> SampleResponse:
18-
logger.info(f"Received request: '{data}' from ip '{x_forwarded_for}'")
19+
logger.info(f"Received request: {data}")
1920

2021
# 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)
2433

2534
return SampleResponse(output=f"Hello {data.input}")

code/function/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
azure-functions~=1.17.0
77
fastapi~=0.106.0
88
pydantic-settings~=2.1.0
9-
httpx~=0.25.2
9+
httpx~=0.26.0
1010
azure-monitor-opentelemetry~=1.1.1
1111
opentelemetry-instrumentation-httpx~=0.43b0
1212
opentelemetry-instrumentation-system-metrics~=0.43b0

0 commit comments

Comments
 (0)