The ecosystem · packages/otel
Metrics and logs, for any OpenTelemetry backend
Time every request by its route, count what your app cares about, and send it over OTLP to an OpenTelemetry Collector or a backend that accepts it (Grafana Cloud, Honeycomb, Datadog, and others). For logs, a collector reads the server's JSON log; the app adds nothing.
OpenTelemetry is the standard most monitoring tools accept. The uptime example uses this package: a histogram of each check's latency, and a gauge of whether each monitor is up.
Setting up#
- Name the package in the app's header:
otel: "…/packages/otel/main.roc". - List its plugin,
Otel.plugin({}), in the program'splugins, andimport otel.Otelwhere you record. Nothing is copied into the app. - Put
Otel.middleware({})first in the app's middleware,routes/+middleware.roc, so it times the rest too. - Start the server with
OTEL_EXPORTER_OTLP_ENDPOINTset to your collector, such ashttp://localhost:4318. In development, put it in the app's.env, whichrocx devreads.
Every request, timed#
# main.roc
program = App.program({
init!: |started| Ok({ facts: started }),
plugins: [Otel.plugin({})],
})
# routes/+middleware.roc: around every request, first, so it times the rest too
middleware = Otel.middleware({}).then(Middlewares.default)Otel.middleware({}) times every request through it as http.server.request.duration, a histogram in seconds, with the attributes of OpenTelemetry's HTTP conventions:
http.request.method, or_OTHERfor a method HTTP doesn't define;http.route, the route's pattern (/posts/[id], never/posts/42, so each post doesn't get a series of its own), left out when no route matched;http.response.status_code.
Measuring your own#
An instrument is something your app measures. Define one with Metric, and record values with Otel.record! anywhere on the server, with attributes that label each value:
signups = Metric.counter({ name: "app.signups", description: "Members who signed up.", unit: "{member}" })
Otel.record!(signups, 1, [("plan", "pro")])
checkout = Metric.histogram({ name: "app.checkout.duration", description: "Checkout.", unit: "s", buckets: Metric.seconds })
Otel.record!(checkout, seconds, [("method", "card")])
up = Metric.gauge({ name: "app.service.up", description: "Whether the service answered (1) or not (0).", unit: "1" })
Otel.record!(up, 1, [("service", "payments")])- A counter adds up what it is given (signups, emails sent). It never goes down: a negative value is dropped.
- A histogram counts values into buckets and sums them (how long a checkout took).
Metric.secondsis OpenTelemetry's buckets for durations, from 5 ms to 10 s. - A gauge keeps the last value set, negative too (whether a service is up, how many jobs wait), and is sent at each export until set again.
Each combination of attribute values is a separate series, so keep the values few: a user's id or a full URL would make a series per user or URL. An instrument keeps at most 2000 series and drops new ones past that. A name is one kind of instrument: a value recorded under a name another kind uses is dropped.
Where they go#
It reads the standard environment variables, as every OpenTelemetry SDK does:
| Variable | What it sets |
|---|---|
OTEL_EXPORTER_OTLP_ENDPOINT | the collector's address; /v1/metrics is added |
OTEL_EXPORTER_OTLP_HEADERS | headers to send, name=value,…, such as a backend's key |
OTEL_SERVICE_NAME, OTEL_RESOURCE_ATTRIBUTES | the service's name and attributes |
OTEL_METRIC_EXPORT_INTERVAL | milliseconds between sends (60000) |
OTEL_SDK_DISABLED=true | turns it off |
With no endpoint, metrics are off: nothing is measured, kept or sent, so it costs nothing while you develop. The package's README lists the other variables it reads (a metrics-only endpoint and headers, the send timeout, OTEL_METRICS_EXPORTER=none).
Logs#
Logs need nothing from this package. The server's log is JSON, one object a line (the platform's and the app's alike), when stderr isn't a terminal (as where logs are collected), with ROCX_LOG=json, or when the program says logs: { format: Json }. The Collector's filelog receiver turns each line into a log record: time, level as severity, message as body, and fields as attributes. The package's README has the collector's configuration.
How it works#
Recording a value puts it in the server's memory (Kv), so a request never waits. Each second, the plugin's loop adds what is there to its sums; every export interval, and once more as the server stops, it sends them all in one request (OTLP over HTTP, in JSON).
The sums are cumulative, since the loop started, so a failed send loses nothing: the next carries it all, and the failure is logged as a warning. Nothing goes to the database: a restart starts the sums again with a new start time, which a collector reads as a reset counter.
Not yet#
- Traces. They need to know which request a query or fetch runs for without the app passing it by hand; Roc has no such context yet.
- Up-down counters, exemplars, and Prometheus's
/metricsfor pulling. - A job's duration and outcome.