Skip to content

Commit b3ac8b9

Browse files
WillNilgesWill Nilges
andauthored
Set up custom metrics and dogstatsd (#729)
* Add some metrics to the tasks * lint * linter doesn't like datadog documentation * Add metric to sync panoramas command This ensures that statsd won't interrupt normal operation * add stats to join form * add metric to index for testing * update endpoint * lint --------- Co-authored-by: Will Nilges <willard.nilges@gmail.com>
1 parent 6b1420d commit b3ac8b9

File tree

6 files changed

+29
-1
lines changed

6 files changed

+29
-1
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ dependencies = [
4545
"matplotlib==3.9.*",
4646
"django-ipware==7.0.1",
4747
"django-admin-site-search==1.1.*",
48+
"datadog==0.50.*",
49+
4850
]
4951

5052
[project.optional-dependencies]

src/meshapi/management/commands/sync_panoramas.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from argparse import ArgumentParser
22
from typing import Any
33

4+
from datadog import statsd
45
from django.core.management.base import BaseCommand
56

67
from meshapi.util.panoramas import sync_github_panoramas
@@ -13,6 +14,7 @@ def add_arguments(self, parser: ArgumentParser) -> None:
1314
pass
1415

1516
def handle(self, *args: Any, **options: Any) -> None:
17+
statsd.increment("meshdb.commands.sync_panoramas", tags=[])
1618
print("Syncing panoramas from GitHub...")
1719
panoramas_saved, warnings = sync_github_panoramas()
1820
print(f"Saved {panoramas_saved} panoramas. Got {len(warnings)} warnings.")

src/meshapi/tasks.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33

44
from celery.schedules import crontab
5+
from datadog import statsd
56
from django.core import management
67
from flags.state import disable_flag, enable_flag
78

@@ -32,8 +33,11 @@ def run_database_backup() -> None:
3233
management.call_command("dbbackup")
3334
except Exception as e:
3435
logging.exception(e)
36+
statsd.increment("meshdb.tasks.run_database_backup", tags=["status:failure"])
3537
raise e
3638

39+
statsd.increment("meshdb.tasks.run_database_backup", tags=["status:success"])
40+
3741

3842
@celery_app.task
3943
@skip_if_flag_disabled("TASK_ENABLED_RESET_DEV_DATABASE")
@@ -54,8 +58,11 @@ def reset_dev_database() -> None:
5458
disable_flag("MAINTENANCE_MODE")
5559
except Exception as e:
5660
logging.exception(e)
61+
statsd.increment("meshdb.tasks.reset_dev_database", tags=["status:failure"])
5762
raise e
5863

64+
statsd.increment("meshdb.tasks.reset_dev_database", tags=["status:success"])
65+
5966

6067
@celery_app.task
6168
@skip_if_flag_disabled("TASK_ENABLED_UPDATE_PANORAMAS")
@@ -68,8 +75,11 @@ def run_update_panoramas() -> None:
6875
except Exception as e:
6976
# Make sure the failure gets logged.
7077
logging.exception(e)
78+
statsd.increment("meshdb.tasks.run_update_panoramas", tags=["status:failure"])
7179
raise e
7280

81+
statsd.increment("meshdb.tasks.run_update_panoramas", tags=["status:success"])
82+
7383

7484
@celery_app.task
7585
@skip_if_flag_disabled("TASK_ENABLED_SYNC_WITH_UISP")
@@ -82,8 +92,11 @@ def run_update_from_uisp() -> None:
8292
except Exception as e:
8393
# Make sure the failure gets logged.
8494
logging.exception(e)
95+
statsd.increment("meshdb.tasks.run_update_from_uisp", tags=["status:failure"])
8596
raise e
8697

98+
statsd.increment("meshdb.tasks.run_update_from_uisp", tags=["status:success"])
99+
87100

88101
jitter_minutes = 0 if MESHDB_ENVIRONMENT == "prod2" else 2
89102

src/meshapi/views/forms.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from json.decoder import JSONDecodeError
77
from typing import Optional
88

9+
from datadog import statsd
910
from django.db import IntegrityError, transaction
1011
from django.db.models import Q
1112
from drf_spectacular.utils import OpenApiResponse, extend_schema, extend_schema_view, inline_serializer
@@ -100,6 +101,7 @@ class Meta:
100101
@permission_classes([permissions.AllowAny])
101102
@advisory_lock("join_form_lock")
102103
def join_form(request: Request) -> Response:
104+
statsd.increment("meshdb.join_form.request", tags=[])
103105
request_json = json.loads(request.body)
104106
try:
105107
r = JoinFormRequest(**request_json)
@@ -126,7 +128,9 @@ def join_form(request: Request) -> Response:
126128
logging.exception("Captcha validation failed")
127129
return Response({"detail": "Captcha verification failed"}, status=status.HTTP_401_UNAUTHORIZED)
128130

129-
return process_join_form(r, request)
131+
response = process_join_form(r, request)
132+
statsd.increment("meshdb.join_form.response", tags=[f"status:{response.status_code}"])
133+
return response
130134

131135

132136
def process_join_form(r: JoinFormRequest, request: Optional[Request] = None) -> Response:

src/meshdb/wsgi.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99

1010
import os
1111

12+
# Initialize dogstatsd
13+
from datadog import initialize
1214
from django.core.wsgi import get_wsgi_application
1315

16+
initialize(statsd_host="datadog-agent.datadog.svc.cluster.local", statsd_port=8125)
17+
18+
1419
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meshdb.settings")
1520

1621
application = get_wsgi_application()

src/meshweb/views/index.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from datadog import statsd
12
from django.conf import settings
23
from django.http import HttpRequest, HttpResponse
34
from django.template import loader
45

56

67
def index(request: HttpRequest) -> HttpResponse:
8+
statsd.increment("meshdb.views.index", tags=[])
79
template = loader.get_template("meshweb/index.html")
810
links = {
911
"Member Tools": [

0 commit comments

Comments
 (0)