← Send a metric

How to send a metric from Go

Use the standard library: net/http with a timeout and url.Values for the query. Nothing to add to go.mod.

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 instead of returning them, so a metric can never fail your request. Pass a zero time.Time to use the current time.

package main

import (
	"log"
	"net/http"
	"net/url"
	"os"
	"strconv"
	"time"
)

var metricsClient = &http.Client{Timeout: 5 * time.Second}

func sendMetric(key string, value float64, at time.Time) {
	q := url.Values{}
	q.Set("key", key)
	q.Set("value", strconv.FormatFloat(value, 'f', -1, 64))
	if !at.IsZero() {
		q.Set("timestamp", at.UTC().Format(time.RFC3339))
	}
	resp, err := metricsClient.Get("https://data.umlogger.com/api/m?" + q.Encode())
	if err != nil {
		log.Printf("umlogger: %v", err)
		return
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		log.Printf("umlogger: status %d", resp.StatusCode)
	}
}

Count an event

Each call adds 1. Run it in a goroutine if you don’t want to wait for it.

go sendMetric(os.Getenv("UMLOGGER_SIGNUPS_KEY"), 1, time.Time{})

Record a value

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

start := time.Now()
buildReport()
sendMetric(os.Getenv("UMLOGGER_REPORT_TIME_KEY"), float64(time.Since(start).Milliseconds()), time.Time{})

Backfill old data

Pass the time the sample belongs to. The helper sends it as RFC 3339 in UTC.

day := time.Date(2026, 11, 30, 0, 0, 0, 0, time.UTC)
sendMetric(os.Getenv("UMLOGGER_SIGNUPS_KEY"), 37, day)

Tips

  • Don’t use http.Get directly: the default client has no timeout.
  • If the program exits right after sending, call sendMetric without go, or the request may never leave.
  • 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 · PHP · Bash and curl · GitHub Actions · Full API reference

Start free trial →