Skip to content

Commit a61dfdc

Browse files
authored
chore(seer): Add timing metric to requests to Seer (#74688)
This wraps our calls to Seer with a timing metric, tagged with the endpoint being called.
1 parent 4ae109a commit a61dfdc

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/sentry/seer/signed_seer_api.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
import hmac
33
from random import random
44
from typing import Any
5+
from urllib.parse import urlparse
56

67
from django.conf import settings
78
from urllib3 import BaseHTTPResponse, HTTPConnectionPool
89

910
from sentry import options
11+
from sentry.utils import metrics
1012

1113

1214
def make_signed_seer_api_request(
@@ -21,13 +23,19 @@ def make_signed_seer_api_request(
2123
if timeout:
2224
timeout_options["timeout"] = timeout
2325

24-
return connection_pool.urlopen(
25-
"POST",
26-
path,
27-
body=body,
28-
headers={"content-type": "application/json;charset=utf-8", **auth_headers},
29-
**timeout_options,
30-
)
26+
with metrics.timer(
27+
"seer.request_to_seer",
28+
sample_rate=1.0,
29+
# Pull off query params, if any
30+
tags={"endpoint": urlparse(path).path},
31+
):
32+
return connection_pool.urlopen(
33+
"POST",
34+
path,
35+
body=body,
36+
headers={"content-type": "application/json;charset=utf-8", **auth_headers},
37+
**timeout_options,
38+
)
3139

3240

3341
def sign_with_seer_secret(url: str, body: bytes):

tests/sentry/seer/test_signed_seer_api.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from unittest.mock import Mock
1+
from unittest.mock import MagicMock, Mock, patch
22

33
import pytest
44
from django.test import override_settings
@@ -67,3 +67,18 @@ def test_uses_shared_secret():
6767
"Authorization": "Rpcsignature rpc0:96f23d5b3df807a9dc91f090078a46c00e17fe8b0bc7ef08c9391fa8b37a66b5",
6868
},
6969
)
70+
71+
72+
@pytest.mark.django_db
73+
@pytest.mark.parametrize("path", [PATH, f"{PATH}?dogs=great"])
74+
@patch("sentry.seer.signed_seer_api.metrics.timer")
75+
def test_times_request(mock_metrics_timer: MagicMock, path: str):
76+
run_test_case(path=path)
77+
mock_metrics_timer.assert_called_with(
78+
"seer.request_to_seer",
79+
sample_rate=1.0,
80+
tags={
81+
# In both cases the path is the same, because query params are stripped
82+
"endpoint": PATH,
83+
},
84+
)

0 commit comments

Comments
 (0)