Skip to main content

A tiny, dependency-free telemetry library: events, counters, gauges, and timing spans.

Project description

nanotelemetry

A tiny, dependency-free telemetry library for Python. Record events, counters, gauges, and timing spans, then flush them to one or more pluggable exporters.

  • Zero dependencies — pure standard library.
  • Thread-safe — share one client across threads.
  • Pluggable exporters — console, in-memory, or your own.
  • Typed — ships with py.typed.

Install

pip install nanotelemetry

Quickstart

from nanotelemetry import Telemetry, ConsoleExporter

tel = Telemetry(
    service="my-app",
    exporters=[ConsoleExporter()],
    tags={"env": "prod"},
)

# Discrete events
tel.event("user.signup", plan="pro")

# Counters and gauges
tel.count("requests", 1, route="/home")
tel.gauge("queue.depth", 42)

# Time a block of code
with tel.span("db.query", table="users") as attrs:
    rows = run_query()
    attrs["rows"] = len(rows)

# ...or a whole function
@tel.timed("compute")
def compute():
    ...

tel.flush()   # send buffered events to exporters

Use it as a context manager to flush and close automatically:

with Telemetry(service="my-app", exporters=[ConsoleExporter()]) as tel:
    tel.event("startup")

Event kinds

Method Kind value means
event(name, **kw) event (none)
count(name, n) count the increment
gauge(name, x) gauge the reading
span(name) span duration in seconds

Every event carries the client's service name plus any tags you configured, merged with the per-call attributes.

System info

Pass include_system_info=True to automatically tag every event with details about the host it's running on — hostname, OS, architecture, Python version, and PID:

tel = Telemetry(service="my-app", include_system_info=True)
tel.event("startup")
# attributes now include: host, os, os_release, arch, python, python_impl, pid

It's collected once at construction and off by default. Any keys you set explicitly in tags take precedence. You can also call the collector directly:

from nanotelemetry import sysinfo
print(sysinfo())
# {'host': 'laptop', 'os': 'Darwin', 'arch': 'arm64', 'python': '3.12.1', ...}

Batching

The client buffers events and auto-flushes once batch_size (default 100) is reached. Set batch_size=0 to disable auto-flush and call flush() yourself.

Custom exporters

Subclass Exporter to send events anywhere:

import json
import urllib.request
from nanotelemetry import Exporter

class HTTPExporter(Exporter):
    def __init__(self, url):
        self.url = url

    def export(self, events):
        payload = json.dumps([e.to_dict() for e in events]).encode()
        req = urllib.request.Request(
            self.url, data=payload, headers={"Content-Type": "application/json"}
        )
        urllib.request.urlopen(req)

Development

pip install -e ".[dev]"
pytest

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

nanotelemetry-0.2.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

nanotelemetry-0.2.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file nanotelemetry-0.2.0.tar.gz.

File metadata

  • Download URL: nanotelemetry-0.2.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for nanotelemetry-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e68d4a692ea90f1aac52525398cd96d894f633a498ca13872833f9696077df81
MD5 3313a36d8275888f3921462a1f8cc8b9
BLAKE2b-256 bee3fdd153dbe3e57923e23cc5a95effc7d9cf258331524f70431d995c646ad9

See more details on using hashes here.

File details

Details for the file nanotelemetry-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: nanotelemetry-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for nanotelemetry-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 34ae54781dff5ccc8b9faba40d74e10f1f40106dd4f386ee7557b1679e4e027b
MD5 7a77d5ef2fe34c0834206b9873cd59c4
BLAKE2b-256 aa9c663a46bf8c2c6bc773ab79d93dbe58310e292c70ab24289e306d36e0761e

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