-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
44 lines (33 loc) · 1.08 KB
/
app.py
File metadata and controls
44 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import uvicorn
from datadog import initialize, statsd
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from timing_asgi import TimingMiddleware, TimingClient
from timing_asgi.integrations import StarletteScopeToName
initialize({'api_key': 'datadog api key', 'app_key': 'datadog app key'})
app = Starlette()
class StatsdClient(TimingClient):
def __init__(self, datadog_client, tags=None):
if tags is None:
tags = []
self.tags = tags
self.datadog_client = datadog_client
def timing(self, metric_name, timing, tags):
self.datadog_client.timing(metric_name, timing, tags + self.tags)
@app.route("/")
def homepage(request):
return PlainTextResponse("hello world")
app.add_middleware(
TimingMiddleware,
client=StatsdClient(
datadog_client=statsd,
tags=['app_version:'.format(os.environ.get('GITHASH'), 'unknown')],
),
metric_namer=StarletteScopeToName(
prefix="myapp",
starlette_app=app
)
)
if __name__ == "__main__":
uvicorn.run(app)