Skip to main content

Metric monitoring with automatic anomaly detection

Project description

detectkit

PyPI version Python

Metric monitoring with automatic anomaly detection.

detectkit is a Python library for data analysts and engineers to monitor time-series metrics with automatic anomaly detection and alerting. dbt-like project structure and CLI.

Features

  • Pure numpy arrays — no pandas dependency in core logic
  • Statistical detectors — Z-Score, MAD, IQR, Manual Bounds
  • Multi-channel alerting — Mattermost, Slack, Telegram, Email, Webhook
  • @mentions — tag users/groups in alerts, each channel formats natively
  • Alert lifecycle — consecutive anomalies, cooldown, recovery notifications
  • Database agnostic — ClickHouse, PostgreSQL, MySQL
  • Idempotent — resume from interruptions, no duplicate processing
  • CLIdtk init, dtk run --select, tag-based selectors

Installation

pip install detectkit

With database drivers:

pip install detectkit[clickhouse]   # ClickHouse
pip install detectkit[all-db]       # All databases

Quick Start

CLI (Recommended)

# Create project
dtk init my_monitoring
cd my_monitoring

# Configure database in profiles.yml, then:
dtk run --select cpu_usage
dtk run --select tag:critical
dtk run --select cpu_usage --steps load,detect
dtk run --select cpu_usage --from 2024-01-01

Metric Configuration

# metrics/api_errors.yml
name: api_error_rate
interval: "5min"

query: |
  SELECT
    toStartOfInterval(timestamp, INTERVAL 5 MINUTE) AS timestamp,
    countIf(status_code >= 500) / count() * 100 AS value
  FROM http_requests
  WHERE timestamp >= %(from_date)s AND timestamp < %(to_date)s
  GROUP BY timestamp ORDER BY timestamp

detectors:
  - type: mad
    params:
      threshold: 3.0
      window_size: 2016    # 7 days

alerting:
  enabled: true
  channels: [mattermost_ops]
  consecutive_anomalies: 3
  direction: "up"
  mentions: [oncall_engineer, here]
  alert_cooldown: "30min"
  notify_on_recovery: true

Python API

import numpy as np
from detectkit.detectors.statistical import ZScoreDetector

detector = ZScoreDetector(threshold=3.0, window_size=100)
results = detector.detect({
    'timestamp': np.array([...], dtype='datetime64[ms]'),
    'value': np.array([1.0, 2.0, 1.5, 10.0, 1.8]),
})

for r in results:
    if r.is_anomaly:
        print(f"Anomaly at {r.timestamp}: {r.value}")

Documentation

Requirements

  • Python 3.10+
  • numpy >= 1.24.0
  • pydantic >= 2.0.0
  • click >= 8.0
  • PyYAML >= 6.0
  • Jinja2 >= 3.0

License

MIT License — see LICENSE for details.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

detectkit-0.3.13.tar.gz (81.5 kB view details)

Uploaded Source

Built Distribution

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

detectkit-0.3.13-py3-none-any.whl (105.9 kB view details)

Uploaded Python 3

File details

Details for the file detectkit-0.3.13.tar.gz.

File metadata

  • Download URL: detectkit-0.3.13.tar.gz
  • Upload date:
  • Size: 81.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for detectkit-0.3.13.tar.gz
Algorithm Hash digest
SHA256 2b3b3d8540ac33803139512eedc9b761df9145d4d11742c4c45f275b29957aaf
MD5 9a7db6eac968e94a7197deab7cbdef96
BLAKE2b-256 8cc257c1dd455024f728caf9a1d5bd82cc1eec8a6f7bae5d9afc8ccae93fa9d7

See more details on using hashes here.

File details

Details for the file detectkit-0.3.13-py3-none-any.whl.

File metadata

  • Download URL: detectkit-0.3.13-py3-none-any.whl
  • Upload date:
  • Size: 105.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for detectkit-0.3.13-py3-none-any.whl
Algorithm Hash digest
SHA256 f2a80fce4c06ac4f264c91f46331fd09c7f954ddaa682a13e6239953dacf58be
MD5 c98931213368aabee7e1e7cb69f1c5bd
BLAKE2b-256 d4d2fb60b58288608a90ab1b4e52336c9ff6e9ab2b11d2ad85070a8a37afd3b5

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