Roc Solid
Markup in Roc · pages on a native server · components in WebAssembly

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#

  1. Name the package in the app's header: otel: "…/packages/otel/main.roc".
  2. List its plugin, Otel.plugin({}), in the program's plugins, and import otel.Otel where you record. Nothing is copied into the app.
  3. Put Otel.middleware({}) first in the app's middleware, routes/+middleware.roc, so it times the rest too.
  4. Start the server with OTEL_EXPORTER_OTLP_ENDPOINT set to your collector, such as http://localhost:4318. In development, put it in the app's .env, which rocx dev reads.

Every request, timed#

main.roc, in part
# 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:

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:

Anywhere on the server
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")])

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:

VariableWhat it sets
OTEL_EXPORTER_OTLP_ENDPOINTthe collector's address; /v1/metrics is added
OTEL_EXPORTER_OTLP_HEADERSheaders to send, name=value,…, such as a backend's key
OTEL_SERVICE_NAME, OTEL_RESOURCE_ATTRIBUTESthe service's name and attributes
OTEL_METRIC_EXPORT_INTERVALmilliseconds between sends (60000)
OTEL_SDK_DISABLED=trueturns 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#