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
        COUNT(DISTINCT user_id) as value,
        NOW() as timestamp
      FROM sessions
      WHERE created_at >= NOW() - INTERVAL '10 minutes'

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
        COUNT(*) as value,
        NOW() as timestamp
      FROM orders
      WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 HOUR)

SQLite

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

collector:
  type: "sql"
  params:
    connection_string: "sqlite:///./metrics.db"
    query: |
      SELECT
        COUNT(*) as value,
        datetime('now') as timestamp
      FROM events
      WHERE timestamp >= datetime('now', '-10 minutes')

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.0.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.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: detectk_collectors_sql-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 819c71837397a709a5a129a6bd9752a44e4a44b755624334b84d78f6a581629a
MD5 b9223f7bdcf38241dab1486bbe025a2d
BLAKE2b-256 01682a13b37995b4e7306a7170d70f9ee4094119d0d3dd953283ce288084950c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for detectk_collectors_sql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9564a39500ce4e0302ad8b3673830808f11e26f6356079c1510c2a65d0e8c90b
MD5 bc43be81855a0259bcb4cfa413f5438b
BLAKE2b-256 79367ada85787803b813ba5d36df2c348c0cec5488d652a525750cbb2f77ab65

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