Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up custom metrics and dogstatsd #729

Merged
merged 10 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ dependencies = [
"matplotlib==3.9.*",
"django-ipware==7.0.1",
"django-admin-site-search==1.1.*",
"datadog==0.50.*",

]

[project.optional-dependencies]
Expand Down
2 changes: 2 additions & 0 deletions src/meshapi/management/commands/sync_panoramas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from argparse import ArgumentParser
from typing import Any

from datadog import statsd
from django.core.management.base import BaseCommand

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

def handle(self, *args: Any, **options: Any) -> None:
statsd.increment("meshdb.commands.sync_panoramas", tags=[])
print("Syncing panoramas from GitHub...")
panoramas_saved, warnings = sync_github_panoramas()
print(f"Saved {panoramas_saved} panoramas. Got {len(warnings)} warnings.")
Expand Down
13 changes: 13 additions & 0 deletions src/meshapi/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os

from celery.schedules import crontab
from datadog import statsd
from django.core import management
from flags.state import disable_flag, enable_flag

Expand Down Expand Up @@ -32,8 +33,11 @@
management.call_command("dbbackup")
except Exception as e:
logging.exception(e)
statsd.increment("meshdb.tasks.run_database_backup", tags=["status:failure"])

Check warning on line 36 in src/meshapi/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/meshapi/tasks.py#L36

Added line #L36 was not covered by tests
raise e

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


@celery_app.task
@skip_if_flag_disabled("TASK_ENABLED_RESET_DEV_DATABASE")
Expand All @@ -54,8 +58,11 @@
disable_flag("MAINTENANCE_MODE")
except Exception as e:
logging.exception(e)
statsd.increment("meshdb.tasks.reset_dev_database", tags=["status:failure"])

Check warning on line 61 in src/meshapi/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/meshapi/tasks.py#L61

Added line #L61 was not covered by tests
raise e

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


@celery_app.task
@skip_if_flag_disabled("TASK_ENABLED_UPDATE_PANORAMAS")
Expand All @@ -68,8 +75,11 @@
except Exception as e:
# Make sure the failure gets logged.
logging.exception(e)
statsd.increment("meshdb.tasks.run_update_panoramas", tags=["status:failure"])

Check warning on line 78 in src/meshapi/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/meshapi/tasks.py#L78

Added line #L78 was not covered by tests
raise e

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

Check warning on line 81 in src/meshapi/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/meshapi/tasks.py#L81

Added line #L81 was not covered by tests


@celery_app.task
@skip_if_flag_disabled("TASK_ENABLED_SYNC_WITH_UISP")
Expand All @@ -82,8 +92,11 @@
except Exception as e:
# Make sure the failure gets logged.
logging.exception(e)
statsd.increment("meshdb.tasks.run_update_from_uisp", tags=["status:failure"])

Check warning on line 95 in src/meshapi/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/meshapi/tasks.py#L95

Added line #L95 was not covered by tests
raise e

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

Check warning on line 98 in src/meshapi/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/meshapi/tasks.py#L98

Added line #L98 was not covered by tests


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

Expand Down
6 changes: 5 additions & 1 deletion src/meshapi/views/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from json.decoder import JSONDecodeError
from typing import Optional

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

return process_join_form(r, request)
response = process_join_form(r, request)
statsd.increment("meshdb.join_form.response", tags=[f"status:{response.status_code}"])
return response


def process_join_form(r: JoinFormRequest, request: Optional[Request] = None) -> Response:
Expand Down
5 changes: 5 additions & 0 deletions src/meshdb/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@

import os

# Initialize dogstatsd
from datadog import initialize
from django.core.wsgi import get_wsgi_application

initialize(statsd_host="datadog-agent.datadog.svc.cluster.local", statsd_port=8125)


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meshdb.settings")

application = get_wsgi_application()
2 changes: 2 additions & 0 deletions src/meshweb/views/index.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from datadog import statsd
from django.conf import settings
from django.http import HttpRequest, HttpResponse
from django.template import loader


def index(request: HttpRequest) -> HttpResponse:
statsd.increment("meshdb.views.index", tags=[])
template = loader.get_template("meshweb/index.html")
links = {
"Member Tools": [
Expand Down
Loading