Skip to main content

Generic SQL collector for DetectK (PostgreSQL, MySQL, SQLite)

Project description

DetectK SQL Collectors

Generic SQL collector for DetectK with support for PostgreSQL, MySQL, and SQLite.

Installation

# Core package (supports SQLite out of the box)
pip install detectk-collectors-sql

# With PostgreSQL support
pip install detectk-collectors-sql[postgres]

# With MySQL support
pip install detectk-collectors-sql[mysql]

# With all database drivers
pip install detectk-collectors-sql[all]

Supported Databases

  • PostgreSQL (9.6+)
  • MySQL (5.7+, 8.0+)
  • SQLite (3.x)

Usage

PostgreSQL

name: "user_sessions_postgres"
description: "Monitor active sessions in PostgreSQL"

collector:
  type: "sql"
  params:
    connection_string: "postgresql://user:password@localhost:5432/analytics"
    query: |
      SELECT
        date_trunc('minute', '{{ period_finish }}'::timestamp) as period_time,
        COUNT(DISTINCT user_id) as value
      FROM sessions
      WHERE created_at >= '{{ period_start }}'::timestamp
        AND created_at < '{{ period_finish }}'::timestamp

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

MySQL

name: "orders_mysql"
description: "Monitor order volume in MySQL"

collector:
  type: "sql"
  params:
    connection_string: "mysql://user:password@localhost:3306/ecommerce"
    query: |
      SELECT
        DATE_ADD('{{ period_start }}', INTERVAL 1 HOUR) as period_time,
        COUNT(*) as value
      FROM orders
      WHERE created_at >= '{{ period_start }}'
        AND created_at < '{{ period_finish }}'

SQLite

name: "local_metrics_sqlite"
description: "Monitor local database metrics"

collector:
  type: "sql"
  params:
    connection_string: "sqlite:///./metrics.db"
    query: |
      SELECT
        datetime('{{ period_start }}', '+10 minutes') as period_time,
        COUNT(*) as value
      FROM events
      WHERE timestamp >= datetime('{{ period_start }}')
        AND timestamp < datetime('{{ period_finish }}')

Configuration

Connection String

The collector uses SQLAlchemy connection strings:

  • PostgreSQL: postgresql://[user[:password]@][host][:port][/database]
  • MySQL: mysql://[user[:password]@][host][:port][/database]
  • SQLite: sqlite:///path/to/database.db

Environment Variables

export POSTGRES_URL="postgresql://user:password@localhost:5432/analytics"
export MYSQL_URL="mysql://user:password@localhost:3306/ecommerce"

Then in config:

collector:
  type: "sql"
  params:
    connection_string: "${POSTGRES_URL}"
    query: "SELECT ..."

Query Requirements

Query must return:

  • value column (float or int) - the metric value
  • timestamp column (optional) - timestamp of measurement

If timestamp is not provided, current time is used.

Storage

SQL collector can also be used as storage backend:

storage:
  enabled: true
  type: "sql"
  params:
    connection_string: "${POSTGRES_URL}"
    datapoints_retention_days: 90
    save_detections: false  # Optional

This creates tables:

  • dtk_datapoints - collected metric values
  • dtk_detections - detection results (if save_detections=true)

Examples

See examples/sql/ directory for complete configurations.

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_collectors_sql-0.1.1.tar.gz (11.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_collectors_sql-0.1.1-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file detectk_collectors_sql-0.1.1.tar.gz.

File metadata

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

File hashes

Hashes for detectk_collectors_sql-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e82241fc2b7b44ac23c90681321ba6b99632ba2226da7196670367b48e6e061e
MD5 5f27f49c579383c14c9752abedc437ed
BLAKE2b-256 b34adae4950fd02e8c06905b28d719cadef02f90d96e239f724e5f253b681c38

See more details on using hashes here.

File details

Details for the file detectk_collectors_sql-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for detectk_collectors_sql-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a9a6c121d359eb1842532674522607db9e1a7ed56aca153dad8b5d1cc7dd5507
MD5 6de1007cdffcdaf1328a52748528bd0f
BLAKE2b-256 6c411581bba43ac48a5df6d71279c7e5e1d86bf1df2441b83c95f551cc5548e0

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