Skip to content

Commit

Permalink
(dynamic alert thresholds) Force recompilation of Stumpy library duri…
Browse files Browse the repository at this point in the history
…ng bootup
  • Loading branch information
ram-senth committed Sep 7, 2024
1 parent 0fe6678 commit 53f0558
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ services:
- DEV=1
- SEVERITY_ENABLED=1
- GROUPING_ENABLED=1
- ANOMALY_DETECTION_ENABLED=1
- CELERY_WORKER_ENABLE=true
- PORT=9091
- SMOKE_TEST=1
Expand Down
59 changes: 45 additions & 14 deletions src/seer/anomaly_detection/anomaly_detection.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import logging
from typing import List, Tuple

import numpy as np
import sentry_sdk
import stumpy # type: ignore # mypy throws "missing library stubs"
from pydantic import BaseModel

from seer.anomaly_detection.accessors import AlertDataAccessor
from seer.anomaly_detection.anomaly_detection_di import anomaly_detection_module
from seer.anomaly_detection.detectors import MPBatchAnomalyDetector, MPStreamAnomalyDetector
from seer.anomaly_detection.detectors import (
MPBatchAnomalyDetector,
MPStreamAnomalyDetector,
SuSSWindowSizeSelector,
)
from seer.anomaly_detection.models import MPTimeSeriesAnomalies, TimeSeriesAnomalies
from seer.anomaly_detection.models.converters import convert_external_ts_to_internal
from seer.anomaly_detection.models.external import (
Expand All @@ -27,6 +33,29 @@


class AnomalyDetection(BaseModel):
@sentry_sdk.trace
def __init__(self):
"""
Force Stumpy compilation by making a dummy call to the library.
Note: compilation triggered here is very specific to the exact values of parameters ignore_trivial, normalize etc. A
future call with different values for one or more parameter will still trigger a recompilation.
"""
data = np.arange(10.0)
try:
SuSSWindowSizeSelector().optimal_window_size(data)
except Exception:
pass
mp = stumpy.stump(data, m=3, ignore_trivial=True, normalize=False)
stream = stumpy.stumpi(
data,
m=3,
mp=mp,
normalize=False,
egress=False,
)
stream.update(6.0)

@sentry_sdk.trace
def _batch_detect(
self, timeseries: List[TimeSeriesPoint], config: AnomalyDetectionConfig
Expand Down Expand Up @@ -181,6 +210,7 @@ def _update_anomalies(self, ts_external: List[TimeSeriesPoint], anomalies: TimeS
anomaly_type=anomalies.flags[i],
)

@sentry_sdk.trace
def detect_anomalies(self, request: DetectAnomaliesRequest) -> DetectAnomaliesResponse:
"""
Main entry point for anomaly detection.
Expand All @@ -190,21 +220,22 @@ def detect_anomalies(self, request: DetectAnomaliesRequest) -> DetectAnomaliesRe
Anomaly detection request that has either a complete time series or an alert reference.
"""
if isinstance(request.context, AlertInSeer):
transaction_name = "Stream AD for alert"
mode = "streaming.alert"
elif isinstance(request.context, TimeSeriesWithHistory):
mode = "streaming.ts_with_history"
else:
mode = "batch.ts_full"

sentry_sdk.set_tag("ad_mode", mode)

if isinstance(request.context, AlertInSeer):
ts, anomalies = self._online_detect(request.context, request.config)
elif isinstance(request.context, TimeSeriesWithHistory):
transaction_name = "Stream AD for timeseries with history"
ts, anomalies = self._combo_detect(request.context, request.config)
else:
transaction_name = "Batch AD for timeseries"

with sentry_sdk.start_transaction(op="task", name=transaction_name):
if isinstance(request.context, AlertInSeer):
ts, anomalies = self._online_detect(request.context, request.config)
elif isinstance(request.context, TimeSeriesWithHistory):
ts, anomalies = self._combo_detect(request.context, request.config)
else:
ts, anomalies = self._batch_detect(request.context, request.config)
self._update_anomalies(ts, anomalies)
return DetectAnomaliesResponse(timeseries=ts)
ts, anomalies = self._batch_detect(request.context, request.config)
self._update_anomalies(ts, anomalies)
return DetectAnomaliesResponse(timeseries=ts)

@inject
def store_data(
Expand Down

0 comments on commit 53f0558

Please sign in to comment.