Skip to main content

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

Metadata

Release files for detectk-collectors-sql 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for detectk-collectors-sql 0.1.1
File Size Uploaded
detectk_collectors_sql-0.1.1.tar.gz 11.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for detectk-collectors-sql 0.1.1
File Interpreter ABI Platform
detectk_collectors_sql-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 22.0 kB

Release files / detectk_collectors_sql-0.1.1.tar.gz

Download URL detectk_collectors_sql-0.1.1.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e82241fc2b7b44ac23c90681321ba6b99632ba2226da7196670367b48e6e061e
BLAKE2b-256 checksum
How to use checksums
b34adae4950fd02e8c06905b28d719cadef02f90d96e239f724e5f253b681c38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / detectk_collectors_sql-0.1.1-py3-none-any.whl

Download URL detectk_collectors_sql-0.1.1-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a9a6c121d359eb1842532674522607db9e1a7ed56aca153dad8b5d1cc7dd5507
BLAKE2b-256 checksum
How to use checksums
6c411581bba43ac48a5df6d71279c7e5e1d86bf1df2441b83c95f551cc5548e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page