Skip to main content

Core detectors for DetectK: threshold, statistical (MAD, Z-score, IQR)

Project description

detectk-detectors

Core detectors for DetectK: threshold-based, statistical (MAD, Z-score, IQR).

Installation

pip install detectk-detectors

Detectors

ThresholdDetector

Simple but powerful detector that compares metric values against static thresholds.

Features:

  • Multiple comparison operators
  • Absolute and percentage-based thresholds
  • Range checks (between/outside)
  • Auto-registration in DetectorRegistry

Operators:

  • greater_than: value > threshold
  • greater_equal: value >= threshold
  • less_than: value < threshold
  • less_equal: value <= threshold
  • equals: value == threshold (with tolerance)
  • not_equals: value != threshold (with tolerance)
  • between: threshold <= value <= upper_threshold (anomaly if INSIDE range)
  • outside: value < threshold OR value > upper_threshold (anomaly if OUTSIDE range)

Usage Examples

Simple Threshold (absolute value)

# config.yaml
name: "sessions_10min"

collector:
  type: "clickhouse"
  params:
    query: |
      SELECT
        toStartOfInterval(toDateTime('{{ period_finish }}'), INTERVAL 10 MINUTE) as period_time,
        count() as value
      FROM sessions
      WHERE timestamp >= toDateTime('{{ period_start }}')
        AND timestamp < toDateTime('{{ period_finish }}')

detector:
  type: "threshold"
  params:
    threshold: 1000
    operator: "greater_than"  # Alert if > 1000

alerter:
  type: "mattermost"
  params:
    webhook_url: "${MATTERMOST_WEBHOOK}"

Percentage Change Detection

Detect anomalies based on percentage change from baseline:

detector:
  type: "threshold"
  params:
    threshold: 10.0  # 10% increase
    operator: "greater_than"
    percent: true
    baseline: 1000  # Baseline value

Example: If sessions jump from 1000 to 1150 (15% increase), anomaly is detected.

Range Check (outside bounds)

Alert if value falls outside expected range:

detector:
  type: "threshold"
  params:
    threshold: 900        # Lower bound
    upper_threshold: 1100 # Upper bound
    operator: "outside"   # Anomaly if < 900 OR > 1100

Range Check (inside bounds)

Alert if value is INSIDE a specific range (opposite of outside):

detector:
  type: "threshold"
  params:
    threshold: 900
    upper_threshold: 1100
    operator: "between"  # Anomaly if 900 <= value <= 1100

Use case: Detect when metric is in a "bad" range (e.g., error rate between 5-10%).

Decrease Detection

Alert on significant decreases:

detector:
  type: "threshold"
  params:
    threshold: 800
    operator: "less_than"  # Alert if < 800

Or with percentage:

detector:
  type: "threshold"
  params:
    threshold: -10.0  # 10% decrease
    operator: "less_than"
    percent: true
    baseline: 1000

Equals Detection (with tolerance)

Alert when value equals specific number (useful for zero-value detection):

detector:
  type: "threshold"
  params:
    threshold: 0
    operator: "equals"
    tolerance: 0.1  # Within ±0.1

Multiple Detectors (A/B Testing)

Compare different threshold strategies:

detectors:
  # Conservative (fewer false positives)
  - id: "threshold_high"
    type: "threshold"
    params:
      threshold: 1500
      operator: "greater_than"

  # Aggressive (catch more anomalies)
  - id: "threshold_medium"
    type: "threshold"
    params:
      threshold: 1200
      operator: "greater_than"

  # Percentage-based
  - id: "percent_20"
    type: "threshold"
    params:
      threshold: 20.0
      operator: "greater_than"
      percent: true
      baseline: 1000

Configuration Parameters

Required

  • threshold (float): Threshold value (or lower bound for range operators)

Optional

  • operator (str): Comparison operator (default: "greater_than")

    • Options: "greater_than", "greater_equal", "less_than", "less_equal", "equals", "not_equals", "between", "outside"
  • upper_threshold (float): Upper bound for between/outside operators (required for these operators)

  • percent (bool): If true, threshold is percentage change from baseline (default: false)

  • baseline (float): Baseline value for percentage calculation (required if percent=true)

  • tolerance (float): Tolerance for equals/not_equals operators (default: 0.001)

Detection Result

ThresholdDetector returns DetectionResult with:

  • is_anomaly (bool): Whether value violates threshold
  • score (float): Distance from threshold (higher = more anomalous)
  • lower_bound / upper_bound (float | None): Expected bounds for visualization
  • direction (str | None): "up", "down", or None
  • percent_deviation (float | None): Percentage deviation from threshold (if anomaly)
  • metadata (dict): Detector configuration and comparison details

Edge Cases

Zero Threshold

detector:
  type: "threshold"
  params:
    threshold: 0
    operator: "greater_than"

Works correctly: any positive value triggers anomaly.

Negative Values

detector:
  type: "threshold"
  params:
    threshold: -100
    operator: "less_than"

Works correctly: -150 < -100 → anomaly.

Zero Baseline (percentage mode)

❌ Not allowed - raises ConfigurationError:

detector:
  type: "threshold"
  params:
    threshold: 10
    percent: true
    baseline: 0  # ERROR: Division by zero

Testing

cd packages/detectors/core
pytest tests/ -v

All 23 tests passing ✅

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

detectk_detectors-0.2.0.tar.gz (28.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

detectk_detectors-0.2.0-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file detectk_detectors-0.2.0.tar.gz.

File metadata

  • Download URL: detectk_detectors-0.2.0.tar.gz
  • Upload date:
  • Size: 28.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for detectk_detectors-0.2.0.tar.gz
Algorithm Hash digest
SHA256 dcf92fbc18592d0a042a571a1d16afe589318a0a49d0fd8c636ed4630460d0e8
MD5 c0f4120cc54d60b1856b5731a013cf1e
BLAKE2b-256 bb05a90952702551a70ce7a0670e789ea80938bcf057a078e392052f288c81d0

See more details on using hashes here.

File details

Details for the file detectk_detectors-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for detectk_detectors-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4909b2cc459543563f87f01cfc5b0da1b3a7a7cb1a8f689347ed9d93c7765987
MD5 dd89d02897c75e7ac83bdbfae761a2ca
BLAKE2b-256 67a6e133b0f30bbff1d4838331f36d202a674906390cfd0c285df68292a9b1a4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page