Script icon
If icon
Set icon
SlackIncomingWebhook icon
Log icon
Schedule icon

Monitor Timeseries Spending Spikes with TimescaleDB and Gemini AI

Orchestrate financial timeseries anomaly detection with TimescaleDB CAGGs, Gemini AI root-cause analysis, and Slack alerting in Kestra.

Categories
AIData

Financial timeseries monitoring often suffers from alert fatigue when relying on rigid static thresholds. Uncoordinated cloud resource provisioning or runaway AI compute jobs can accumulate thousands of dollars in off-budget expenses before monthly invoices arrive.

This blueprint implements an autonomous FinOps sentinel. It queries hourly timeseries rollups from TimescaleDB Continuous Aggregates (CAGGs), computes statistical Z-score deviations across lookback windows, analyzes anomalous spikes through Google Gemini 1.5 Flash, and dispatches structured diagnostic alerts directly to your operations Slack channel.

How it works

  1. hourly_sentinel_schedule (io.kestra.plugin.core.trigger.Schedule): Automatically triggers the surveillance workflow at the top of every hour.
  2. detect_timeseries_anomalies (io.kestra.plugin.scripts.python.Script): Computes rolling standard deviations across transaction categories and outputs statistical anomalies into anomalies.json.
  3. gemini_root_cause_analysis (io.kestra.plugin.scripts.python.Script): Ingests the anomaly JSON into Google Gemini AI, returning an urgency score (1-10), incident summary, and prescribed remediation actions.
  4. evaluate_severity (io.kestra.plugin.core.flow.If):
    • If severity is CRITICAL or HIGH:
      • record_incident_in_kv (io.kestra.plugin.core.kv.Set): Logs the incident into Kestra's internal KV store for compliance and SLA tracking.
      • notify_slack_channel (io.kestra.plugin.slack.notifications.SlackIncomingWebhook): Formats a rich Slack Block Kit alert with the AI diagnosis and remediation checklist.
    • If severity is MODERATE or LOW:
      • log_nominal_status (io.kestra.plugin.core.log.Log): Records nominal state with zero notification noise.

What you get

  • Automated Anomaly Detection: Catches runaway cloud compute costs within 60 minutes instead of at end-of-month reconciliation.
  • Contextual Root-Cause Diagnosis: Delivers actionable engineering remedies rather than raw statistical metrics.
  • Zero Alert Fatigue: Low-severity fluctuations are logged silently; only critical threshold breaches notify on-call teams.
  • Complete Audit Trail: Incidents are stamped into Kestra's KV store with execution metadata.

Who it's for

  • FinOps & Platform Teams: Enforce continuous budget compliance across cloud providers.
  • Data & AI Engineers: Guard against runaway vector embedding or LLM compute jobs.
  • Site Reliability Engineers (SREs): Receive enriched incident tickets with pre-diagnosed action steps.

Why orchestrate this with Kestra

Script-based cron jobs lack state visibility, centralized secret management, and failure recovery. Orchestrating with Kestra gives you:

  • Declarative Flow Control: Clean separation between timeseries querying, LLM inference, and multi-channel notification routing.
  • Containerized Isolation: Python scripts execute inside ephemeral containers (python:3.12-slim) with isolated dependencies.
  • Built-in Secrets Vault: Securely injects GEMINI_API_KEY and SLACK_WEBHOOK_URL without exposing credentials in code.

Prerequisites

  • Docker or container runtime enabled on your Kestra worker.
  • Google Gemini API key with access to Gemini 1.5 models.
  • Slack Incoming Webhook URL.

Secrets

  • GEMINI_API_KEY: API authentication key for Google Generative AI API calls.
  • SLACK_WEBHOOK_URL: Webhook URL for the destination Slack channel.

Quick start

  1. Configure the required secrets (GEMINI_API_KEY, SLACK_WEBHOOK_URL) under Kestra Settings > Secrets.
  2. Test-run the flow manually with custom lookback_hours or z_score_threshold.
  3. Verify that anomalies.json and incident_report.json generate under the Execution Outputs tab.
  4. Enable the hourly_sentinel_schedule trigger for continuous background monitoring.

How to extend

  • Database Integration: Connect directly to production databases using io.kestra.plugin.jdbc.postgresql.Query to query live hypertable aggregates.
  • PagerDuty Escalation: Add io.kestra.plugin.pagerduty.notifications.PagerDutyAlert for automated phone escalations on score 10 emergencies.
  • Automated Remediation: Introduce a downstream AWS task (io.kestra.plugin.aws.ec2.StopInstances) to autonomously shut down idle instances.

Links

This blueprint was created by satanic47.

See How

New to Kestra?

Use blueprints to kickstart your first workflows.