← Send a metric

How to send a metric from PHP

This uses file_get_contents with a timeout, which works on almost any PHP 8 host without cURL or Composer packages. It fits Laravel, Symfony, WordPress and plain PHP.

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

It logs problems with error_log instead of throwing, so a metric can never break a page.

<?php

function send_metric(string $key, float $value = 1, ?string $timestamp = null): void
{
    $params = ['key' => $key, 'value' => $value];
    if ($timestamp !== null) {
        $params['timestamp'] = $timestamp;
    }
    $url = 'https://data.umlogger.com/api/m?' . http_build_query($params);
    $context = stream_context_create(['http' => ['timeout' => 5, 'ignore_errors' => true]]);
    $body = @file_get_contents($url, false, $context);
    if ($body === false || !str_contains($body, '"ok":true')) {
        error_log('umlogger: ' . ($body ?: 'request failed'));
    }
}

Count an event

Each call adds 1, for example after an order is placed.

send_metric(getenv('UMLOGGER_ORDERS_KEY'));

Record a value

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

$start = microtime(true);
build_report();
send_metric(getenv('UMLOGGER_REPORT_TIME_KEY'), round((microtime(true) - $start) * 1000));

Backfill old data

Add a timestamp with a time zone. gmdate('c') gives one.

send_metric(getenv('UMLOGGER_ORDERS_KEY'), 37, gmdate('c', strtotime('2026-11-30 00:00 UTC')));

Tips

  • In Laravel, Http::timeout(5)->get($url, $params) does the same; call it from a queued job if you don’t want to wait.
  • In WordPress, wp_remote_get($url, ['timeout' => 5]) works too.
  • 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

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

Start free trial →