Skip to content

Commit b1ee566

Browse files
committed
Cleanup and add lifespan sync
1 parent 88defcd commit b1ee566

File tree

3 files changed

+10
-31
lines changed

3 files changed

+10
-31
lines changed

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

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import httpx
44
from fastapi import APIRouter, Header
55
from fastapp.models.sample import SampleRequest, SampleResponse
6-
from fastapp.utils import setup_logging, setup_tracer
7-
from opentelemetry.trace import SpanKind
6+
from fastapp.utils import setup_logging
87

98
logger = setup_logging(__name__)
10-
tracer = setup_tracer(__name__)
119

1210
router = APIRouter()
1311

@@ -17,26 +15,11 @@ async def post_predict(
1715
data: SampleRequest, x_forwarded_for: Annotated[str, Header()] = ""
1816
) -> SampleResponse:
1917
logger.info(f"Received request: {data}")
18+
logger.info(f"IP of sender: {x_forwarded_for}")
2019

2120
# Sample request
2221
async with httpx.AsyncClient() as client:
2322
response = await client.get("https://www.bing.com")
2423
logger.info(f"Received response status code: {response.status_code}")
2524

26-
# with httpx.Client() as client:
27-
# response = client.get("https://www.google.com")
28-
# response = httpx.get("https://www.google.de")
29-
30-
# tracer_attributes = {"http.client_ip": x_forwarded_for}
31-
# with tracer.start_as_current_span(
32-
# "dependency_span", attributes=tracer_attributes, kind=SpanKind.CLIENT
33-
# ) as span:
34-
# try:
35-
# async with httpx.AsyncClient() as client:
36-
# response = await client.get("https://www.bing.com")
37-
# logger.info(f"Received response status code: {response.status_code}")
38-
# except Exception as ex:
39-
# span.set_attribute("status", "exception")
40-
# span.record_exception(ex)
41-
4225
return SampleResponse(output=f"Hello {data.input}")

code/function/fastapp/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ async def lifespan(app: FastAPI) -> None:
1414
pass
1515

1616

17+
def lifespan_sync(app: FastAPI) -> None:
18+
"""Gracefully start the application before the server reports readiness."""
19+
setup_opentelemetry(app=app)
20+
21+
1722
def get_app() -> FastAPI:
1823
"""Setup the Fast API server.
1924

code/function/wrapper/__init__.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import azure.functions as func
2-
from fastapp.main import app
3-
from fastapp.utils import setup_opentelemetry, setup_tracer
4-
from opentelemetry.context import attach, detach
2+
from fastapp.main import app, lifespan_sync
3+
from fastapp.utils import setup_tracer
54
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
65

7-
# setup_opentelemetry(app=app)
6+
lifespan_sync(app=app)
87
tracer = setup_tracer(__name__)
98

109

@@ -24,12 +23,4 @@ async def main(req: func.HttpRequest, context: func.Context) -> func.HttpRespons
2423
req=req, # context=parent_context
2524
)
2625

27-
# token = attach(parent_context)
28-
# try:
29-
# with tracer.start_as_current_span("wrapper") as span:
30-
# response = await func.AsgiMiddleware(app).handle_async(req, parent_context)
31-
# finally:
32-
# # End distributed tracing
33-
# detach(token)
34-
3526
return response

0 commit comments

Comments
 (0)