← Send a metric

How to send a metric from Python

You need no SDK or agent. A metric is one HTTP GET request, and this helper uses only the standard library, so it works in scripts, Django, Flask, FastAPI and cron jobs.

Before you start

Create a metric in Umlogger and copy its ingest key (pmk_…) from the metric's page. Choose count for things that happen, or value for measurements (which one?). The examples read the key from an environment variable.

The helper

Copy this into your project. It never raises, so a network problem can never break your app; it logs the problem instead.

import os
import urllib.parse
import urllib.request

UMLOGGER_URL = "https://data.umlogger.com/api/m"


def send_metric(key, value=1, timestamp=None):
    params = {"key": key, "value": value}
    if timestamp:
        params["timestamp"] = timestamp
    url = UMLOGGER_URL + "?" + urllib.parse.urlencode(params)
    try:
        urllib.request.urlopen(url, timeout=5).close()
    except OSError as error:  # includes HTTP errors and timeouts
        print(f"umlogger: {error}")

Count an event

Call it where the event happens, for example after a user signs up. Each call adds 1.

send_metric(os.environ["UMLOGGER_SIGNUPS_KEY"])

Record a value

For measurements, use a value metric and send the number. Umlogger averages the samples on the chart.

import time

start = time.perf_counter()
run_report()
elapsed_ms = (time.perf_counter() - start) * 1000
send_metric(os.environ["UMLOGGER_REPORT_TIME_KEY"], round(elapsed_ms, 1))

Backfill old data

Add a timestamp with a time zone (ISO 8601) to record when something happened, for example when importing history.

from datetime import datetime, timezone

day = datetime(2026, 11, 30, tzinfo=timezone.utc)
send_metric(os.environ["UMLOGGER_SIGNUPS_KEY"], 37, day.isoformat())

Tips

  • Already use requests? requests.get(UMLOGGER_URL, params=params, timeout=5) does the same.
  • In a web request, send the metric from a background task if you don’t want to wait the few milliseconds it takes.
  • Want to hear when the numbers stop arriving? Add an alert.

Questions

Which key do I use?

Use the metric's own ingest key (pmk_…), shown on the metric's page in Umlogger. It can only add samples to that one metric. Keep it in an environment variable or a secret store rather than in your code. If it leaks, replace it from the metric's page; the old key stops working immediately and your data stays.

How many samples can I send?

Up to 120 requests per minute per key. For busier events, add them up and send the total: a count metric adds up its samples, so one request with value=25 counts as 25 events.

Should my metric be a count or a value?

Use a count for things that happen (signups, orders, errors): Umlogger adds the samples up per hour or day. Use a value for measurements (response time, queue length, MRR): Umlogger averages them.

Other languages

Node.js · Go · PHP · Bash and curl · GitHub Actions · Full API reference

Start free trial →