Skip to content

Commit aa8475f

Browse files
committed
Add GraphQL OpenTelemetry integration
1 parent ed5a51c commit aa8475f

File tree

4 files changed

+1258
-1142
lines changed

4 files changed

+1258
-1142
lines changed

datajunction-server/datajunction_server/api/graphql/main.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import strawberry
77
from fastapi import Depends
8+
from strawberry.extensions.tracing import OpenTelemetryExtension
89
from strawberry.fastapi import GraphQLRouter
910
from strawberry.types import Info
1011
from datajunction_server.api.graphql.queries.catalogs import list_catalogs
@@ -128,6 +129,11 @@ class Query:
128129
)
129130

130131

131-
schema = strawberry.Schema(query=Query)
132+
schema = strawberry.Schema(
133+
query=Query,
134+
extensions=[
135+
OpenTelemetryExtension,
136+
],
137+
)
132138

133139
graphql_app = GraphQLRouter(schema, context_getter=get_context)

datajunction-server/datajunction_server/api/main.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Main DJ server app.
33
"""
44

5+
from contextlib import asynccontextmanager
56
import logging
67
from datajunction_server.api import setup_logging # noqa
78

@@ -54,6 +55,17 @@
5455

5556
dependencies = [Depends(default_attribute_types), Depends(default_catalog)]
5657

58+
59+
@asynccontextmanager
60+
async def lifespan(app: FastAPI):
61+
"""
62+
Lifespan context for initializing and tearing down app-wide resources, like the FastAPI cache
63+
"""
64+
FastAPICache.init(InMemoryBackend(), prefix="inmemory-cache") # pragma: no cover
65+
66+
yield
67+
68+
5769
app = FastAPI(
5870
title=settings.name,
5971
description=settings.description,
@@ -63,6 +75,7 @@
6375
"url": "https://mit-license.org/",
6476
},
6577
dependencies=dependencies,
78+
lifespan=lifespan,
6679
)
6780

6881
app.add_middleware(
@@ -98,14 +111,6 @@
98111
app.include_router(notifications.router)
99112

100113

101-
@app.on_event("startup")
102-
async def startup():
103-
"""
104-
Initialize FastAPI cache when the server starts up
105-
"""
106-
FastAPICache.init(InMemoryBackend(), prefix="inmemory-cache") # pragma: no cover
107-
108-
109114
@app.exception_handler(DJException)
110115
async def dj_exception_handler(
111116
request: Request,

0 commit comments

Comments
 (0)