trevrpc_opentelemetry

OpenTelemetry instrumentation for TrevRPC’s Erlang servers and clients and its Gleam binding, following the semantic conventions for RPC with rpc.system.name trevrpc. It is a Hex package of its own so that trevrpc itself does not depend on OpenTelemetry.

Versioning: this package has a version of its own. Each release depends on the trevrpc release it was built with, and every trevrpc release is followed by one of this package. The TrevRPC runtimes share one version, and runtimes of the same version are compatible with each other; compatibility across different versions isn’t guaranteed yet, a milestone for 2.0.

%% rebar.config
{deps, [trevrpc, trevrpc_opentelemetry, opentelemetry, opentelemetry_experimental]}.
ok = trevrpc_opentelemetry:attach_metrics(),
{ok, Server} = trevrpc_server:start(#{
    %% ...
    telemetry => true,
    %% First, so that its span covers the interceptors after it.
    interceptors => [trevrpc_opentelemetry:interceptor()]
}),

%% On clients, and in handlers that call other servers:
{ok, Reply, _} = trevrpc_client:unary(Channel, Method, Request,
    trevrpc_opentelemetry:propagate(#{timeout => 5000})).

Each uses this package’s tracer and meter from the global providers, and the global propagator, unless the tracer, meter or propagator option gives another. The metrics use opentelemetry_api_experimental, as OpenTelemetry for Erlang has no stable metrics API yet; an application exports them with the opentelemetry_experimental SDK. Start the SDKs before a server takes calls and before attach_metrics runs. The Gleam module trevrpc/opentelemetry has the same three. See OpenTelemetry in the Erlang guide.

Development

Gleam builds and tests the package, with trevrpc and qmux from its path dependencies; there is no rebar3 build. gleam test runs the Erlang tests in test/trevrpc_opentelemetry_tests.erl and the Gleam ones, with the SDKs exporting spans and metrics to the tests, including a server and client on loopback. nix build .#trevrpc-erlang-otel builds the package as gleam publish does and runs them, the trevrpc-erlang-otel-hex-tarball check runs gleam export hex-tarball, as flake-release does before it publishes, and the trevrpc-erlang-otel-lint check runs erlfmt and gleam format.

gleam export hex-tarball builds the package for production, test modules included, without the SDKs, which are dev dependencies, so the test modules must not include the SDKs’ headers. The Erlang tests read the SDKs’ records from the headers when they run instead, as maps of their fields.

✨ Search Document