Skip to main content

Python-based alerting, notification, and reliability monitoring runtime

Project description

Kanary

Kanary is a Python-based alerting, notification, and reliability monitoring runtime. You define three kinds of plugins in Python:

  • Source Reads values from a system, database, API, or device.
  • Rule Evaluates those values and decides whether an alert should fire.
  • Output Sends state changes to humans or other systems.

This separation keeps collection, evaluation, and notification independent, so monitoring definitions stay manageable as the system grows.

Installation

The normal installation method is PyPI:

pip install kanary

If you use uv, this also works:

uv tool install kanary

Installing from a source checkout is still supported, but it should be treated as a development workflow:

git clone https://github.com/mzks/kanary
cd kanary
uv sync
uv run python -m kanary ./demo

What To Do First

Start by running the smallest example from demo/.

kanary ./demo

Then move in this order:

  1. Read demo/basic_monitoring.py to understand the smallest possible Source, Rule, and Output.
  2. Read docs/getting_started.md and work through the examples in examples/getting_started.py.
  3. Browse examples/ for PostgreSQL, Discord, peer monitoring, and remote alert import.
  4. Create your own rules/ directory and start with one Source and one Rule.

Minimal Example

The smallest working example is in demo/basic_monitoring.py.

from datetime import datetime, timezone

import kanary


@kanary.source(source_id="demo", interval=10.0)
class DemoSource:
    def poll(self, ctx):
        return kanary.SourceResult(
            measurements=[
                kanary.Measurement(
                    name="temperature",
                    value=23.4,
                    timestamp=datetime.now(timezone.utc),
                )
            ],
            status="ok",
        )


@kanary.rule(
    rule_id="demo.temperature.high",
    source="demo",
    severity=kanary.WARN,
    tags=["demo"],
    owner="demo_owner",
)
class DemoTemperatureHigh:
    threshold = 25.0

    def evaluate(self, payload, ctx):
        temperature = ctx.value("temperature")
        if temperature is None:
            return kanary.Evaluation(
                state=kanary.AlertState.OK,
                payload=payload,
                message="temperature is missing",
            )
        if temperature > self.threshold:
            return kanary.Evaluation(
                state=kanary.AlertState.FIRING,
                payload=payload,
                message=f"temperature={temperature} is higher than {self.threshold}",
            )
        return kanary.Evaluation(
            state=kanary.AlertState.OK,
            payload=payload,
            message=f"temperature={temperature} is within limit",
        )


@kanary.output(output_id="console")
class ConsoleOutput:
    def emit(self, event, ctx):
        print(event.rule_id, event.current_state.value, event.alert.message)

In this example, you only implement the minimum interface:

  • a source that returns values
  • a rule that evaluates them
  • an output that reacts to state changes

Internally, Kanary handles plugin loading, periodic source polling, rule evaluation, alert state tracking, the HTTP API, and the Web viewer.

If you want shorter definitions later, you can switch to built-in helper classes such as RangeRule, StaleRule, and ThresholdRule.

Running Kanary

Basic run:

kanary ./demo

Change the API and Web viewer port:

kanary ./demo --api-port 8000

Expose the API and Web viewer on the LAN:

kanary ./demo --api-host 0.0.0.0 --api-port 8000

Persist history in SQLite:

kanary ./demo --state-db ./var/kanary.db

The Web viewer is available at:

http://<host>:8000/viewer

See all CLI options with:

kanary --help
kanaryctl help

Environment Variables

Kanary does not require any environment variables by default. You can use these when needed:

  • KANARY_SQLITE_PATH Alternative way to set the SQLite database path.
  • KANARY_API_URL Default API base URL for kanaryctl.
  • KANARY_API_HOST Bind host for the local API and Web viewer. The default is 0.0.0.0.
  • KANARY_NODE_ID Optional node identifier for peer export and import. If unset, Kanary uses the hostname.

Connection details for actual monitoring targets are defined by each Source implementation. For example, a PostgreSQL source may use KANARY_POSTGRES_DSN.

Demo And Examples

Smallest example:

More realistic examples:

demo/ is for the first working run. examples/ is closer to real deployments and includes helper classes, remote monitoring, PostgreSQL, and webhook outputs.

Web Viewer

Kanary includes a built-in Web viewer. The operational surface, however, is the HTTP API. The viewer is the standard UI built on top of that API, and you can replace it with your own tooling if needed.

Documentation

Japanese versions are available as _ja documents, for example README_ja.md.

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

kanary-0.1.0.tar.gz (61.1 kB view details)

Uploaded Source

Built Distribution

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

kanary-0.1.0-py3-none-any.whl (56.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kanary-0.1.0.tar.gz
  • Upload date:
  • Size: 61.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for kanary-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8d7b62daff195f44f15de1327a491b2ddff8cfab5efab0699edbc9efa0b01093
MD5 7f254f3ba7bfd146de53bb1e62c279f8
BLAKE2b-256 cfa9b0f87ad81f01f862074ddb0d32c0ac9484fa337746b8e48cf62881b49991

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kanary-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 56.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for kanary-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 953ef340701726055e8ef35e33dfd1564caaa06bc45e7342baacc13a961465c5
MD5 21ba757b101580e03b04d41852e92d66
BLAKE2b-256 bfe588865d7518f504a0a0e1602e13139d9789d9feb80f4b8993ab74ab0fa1f4

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