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

ref(alerts): Define anomaly detection connection pool once #78655

Closed
Closed
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
9 changes: 3 additions & 6 deletions src/sentry/seer/anomaly_detection/delete_rule.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import logging
from typing import TYPE_CHECKING, cast

from django.conf import settings
from urllib3.exceptions import MaxRetryError, TimeoutError

from sentry.conf.server import SEER_ALERT_DELETION_URL
from sentry.models.organization import Organization
from sentry.net.http import connection_from_url
from sentry.seer.anomaly_detection.types import DeleteAlertDataRequest
from sentry.seer.signed_seer_api import make_signed_seer_api_request
from sentry.utils import json
from sentry.utils.json import JSONDecodeError

logger = logging.getLogger(__name__)

seer_anomaly_detection_connection_pool = connection_from_url(
settings.SEER_ANOMALY_DETECTION_URL, timeout=settings.SEER_DEFAULT_TIMEOUT
)

if TYPE_CHECKING:
from sentry.incidents.models.alert_rule import AlertRule
Expand All @@ -26,6 +21,8 @@ def delete_rule_in_seer(alert_rule: "AlertRule") -> bool:
"""
Send a request to delete an alert rule from Seer. Returns True if the request was successful.
"""
from sentry.seer.anomaly_detection.utils import SEER_ANOMALY_DETECTION_CONNECTION_POOL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be a regular import instead of type checking only? (It looks like it's being used as a default value on line 36).


body = DeleteAlertDataRequest(
organization_id=cast(Organization, alert_rule.organization).id,
alert={"id": alert_rule.id},
Expand All @@ -36,7 +33,7 @@ def delete_rule_in_seer(alert_rule: "AlertRule") -> bool:

try:
response = make_signed_seer_api_request(
connection_pool=seer_anomaly_detection_connection_pool,
connection_pool=SEER_ANOMALY_DETECTION_CONNECTION_POOL,
path=SEER_ALERT_DELETION_URL,
body=json.dumps(body).encode("utf-8"),
)
Expand Down
12 changes: 4 additions & 8 deletions src/sentry/seer/anomaly_detection/get_anomaly_data.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
import logging

from django.conf import settings
from urllib3.exceptions import MaxRetryError, TimeoutError

from sentry.conf.server import SEER_ANOMALY_DETECTION_ENDPOINT_URL
from sentry.incidents.models.alert_rule import AlertRule
from sentry.net.http import connection_from_url
from sentry.seer.anomaly_detection.types import (
AlertInSeer,
AnomalyDetectionConfig,
DetectAnomaliesRequest,
DetectAnomaliesResponse,
TimeSeriesPoint,
)
from sentry.seer.anomaly_detection.utils import translate_direction
from sentry.seer.anomaly_detection.utils import (
SEER_ANOMALY_DETECTION_CONNECTION_POOL,
translate_direction,
)
from sentry.seer.signed_seer_api import make_signed_seer_api_request
from sentry.snuba.models import QuerySubscription
from sentry.utils import json
from sentry.utils.json import JSONDecodeError

logger = logging.getLogger(__name__)

SEER_ANOMALY_DETECTION_CONNECTION_POOL = connection_from_url(
settings.SEER_ANOMALY_DETECTION_URL,
timeout=settings.SEER_ANOMALY_DETECTION_TIMEOUT,
)


def get_anomaly_data_from_seer(
alert_rule: AlertRule,
Expand Down
12 changes: 3 additions & 9 deletions src/sentry/seer/anomaly_detection/get_historical_anomalies.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import logging
from datetime import datetime, timedelta

from django.conf import settings
from urllib3.exceptions import MaxRetryError, TimeoutError

from sentry.api.bases.organization_events import get_query_columns
from sentry.conf.server import SEER_ANOMALY_DETECTION_ENDPOINT_URL
from sentry.incidents.models.alert_rule import AlertRule, AlertRuleStatus
from sentry.models.project import Project
from sentry.net.http import connection_from_url
from sentry.seer.anomaly_detection.store_data import _get_start_and_end_indices
from sentry.seer.anomaly_detection.types import (
AnomalyDetectionConfig,
Expand All @@ -19,6 +17,7 @@
TimeSeriesPoint,
)
from sentry.seer.anomaly_detection.utils import (
SEER_ANOMALY_DETECTION_CONNECTION_POOL,
fetch_historical_data,
format_historical_data,
translate_direction,
Expand All @@ -31,11 +30,6 @@

logger = logging.getLogger(__name__)

seer_anomaly_detection_connection_pool = connection_from_url(
settings.SEER_ANOMALY_DETECTION_URL,
timeout=settings.SEER_ANOMALY_DETECTION_TIMEOUT,
)


def handle_seer_error_responses(response, config, context, log_params):
def log_statement(log_level, text, extra_data=None):
Expand Down Expand Up @@ -127,7 +121,7 @@ def get_historical_anomaly_data_from_seer_preview(
}
try:
response = make_signed_seer_api_request(
connection_pool=seer_anomaly_detection_connection_pool,
connection_pool=SEER_ANOMALY_DETECTION_CONNECTION_POOL,
path=SEER_ANOMALY_DETECTION_ENDPOINT_URL,
body=json.dumps(body).encode("utf-8"),
)
Expand Down Expand Up @@ -225,7 +219,7 @@ def get_historical_anomaly_data_from_seer(
)
try:
response = make_signed_seer_api_request(
seer_anomaly_detection_connection_pool,
SEER_ANOMALY_DETECTION_CONNECTION_POOL,
SEER_ANOMALY_DETECTION_ENDPOINT_URL,
json.dumps(body).encode("utf-8"),
)
Expand Down
10 changes: 2 additions & 8 deletions src/sentry/seer/anomaly_detection/store_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from enum import StrEnum
from typing import Any

from django.conf import settings
from django.core.exceptions import ValidationError
from parsimonious.exceptions import ParseError
from urllib3.exceptions import MaxRetryError, TimeoutError
Expand All @@ -12,7 +11,6 @@
from sentry.conf.server import SEER_ANOMALY_DETECTION_STORE_DATA_URL
from sentry.incidents.models.alert_rule import AlertRule, AlertRuleDetectionType, AlertRuleStatus
from sentry.models.project import Project
from sentry.net.http import connection_from_url
from sentry.seer.anomaly_detection.types import (
AlertInSeer,
AnomalyDetectionConfig,
Expand All @@ -21,6 +19,7 @@
TimeSeriesPoint,
)
from sentry.seer.anomaly_detection.utils import (
SEER_ANOMALY_DETECTION_CONNECTION_POOL,
fetch_historical_data,
format_historical_data,
get_dataset_from_label,
Expand All @@ -33,11 +32,6 @@
from sentry.utils.json import JSONDecodeError

logger = logging.getLogger(__name__)

seer_anomaly_detection_connection_pool = connection_from_url(
settings.SEER_ANOMALY_DETECTION_URL,
timeout=settings.SEER_ANOMALY_DETECTION_TIMEOUT,
)
MIN_DAYS = 7


Expand Down Expand Up @@ -218,7 +212,7 @@ def send_historical_data_to_seer(
)
try:
response = make_signed_seer_api_request(
connection_pool=seer_anomaly_detection_connection_pool,
connection_pool=SEER_ANOMALY_DETECTION_CONNECTION_POOL,
path=SEER_ANOMALY_DETECTION_STORE_DATA_URL,
body=json.dumps(body).encode("utf-8"),
)
Expand Down
7 changes: 7 additions & 0 deletions src/sentry/seer/anomaly_detection/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime, timedelta
from typing import Any

from django.conf import settings
from django.utils import timezone
from django.utils.datastructures import MultiValueDict
from rest_framework.exceptions import ParseError
Expand All @@ -11,6 +12,7 @@
from sentry.incidents.models.alert_rule import AlertRule, AlertRuleThresholdType
from sentry.models.organization import Organization
from sentry.models.project import Project
from sentry.net.http import connection_from_url
from sentry.search.events.types import SnubaParams
from sentry.seer.anomaly_detection.types import AnomalyType, TimeSeriesPoint
from sentry.snuba import metrics_performance
Expand All @@ -23,6 +25,11 @@

NUM_DAYS = 28

SEER_ANOMALY_DETECTION_CONNECTION_POOL = connection_from_url(
settings.SEER_ANOMALY_DETECTION_URL,
timeout=settings.SEER_ANOMALY_DETECTION_TIMEOUT,
)

SNUBA_QUERY_EVENT_TYPE_TO_STRING = {
SnubaQueryEventType.EventType.ERROR: "error",
SnubaQueryEventType.EventType.DEFAULT: "default",
Expand Down
Loading
Loading