Observability

View Source

This page connects wasm_tools to your monitoring: how long calls take, how they end, how many run. wasm_tools has no dependency for it: it calls a function you give, with the same arguments a telemetry handler gets, and keeps counters you can read.

Give an observer

An observer is a fun of three arguments: the event, its measurements, its metadata. Give it to a registry, to load/2, or to one call:

{ok, _} = application:ensure_all_started(wasm_tools),
Test = self(),
Observer = fun(Event, Measurements, Meta) -> Test ! {event, Event, Measurements, Meta} end,
{ok, Calc} = wasm_tools:load({file, "test/fixtures/component/calculator.component.wasm"},
                             #{observer => Observer}),
{ok, 3} = wasm_tools:call(Calc, <<"add">>, #{<<"a">> => 1, <<"b">> => 2}),
receive {event, [wasm_tools, call, stop], #{duration := D}, #{class := ok}} -> D > 0 end.
%% => true
eventwhenmeasurementsmetadata
[wasm_tools, load, stop]a load endsduration, sizedigest, class, kind
[wasm_tools, call, start]a call startssystem_time, monotonic_timetool, export, registry
[wasm_tools, call, stop]the function returned, a value or an error valuedurationtool, export, registry, class, kind
[wasm_tools, call, exception]the call was stopped: deadline, cancel, heap limit, caller or registry gonedurationtool, export, registry, class, kind
[wasm_tools, http, denied]the capabilities refused a wasi:http request of the callsystem_timetool, export, registry, scheme, host, port, and address when an address was refused
  • duration is in native time units (erlang:convert_time_unit/3).
  • class is ok for a value, or the error class.
  • Arguments and results are never in the metadata.
  • The observer runs in the process of the call or of its watchdog: keep it quick, and send work elsewhere. An exception in it is caught and logged once.

See what a component was refused

A component that tries to reach a host you did not grant gets an error from wasi:http, and your observer gets [wasm_tools, http, denied], with where the request went:

{ok, Http} = wasm_tools:load({file, "test/fixtures/component/http_client.component.wasm"},
                             #{observer => Observer}),
{ok, {error, _}} = wasm_tools:call(Http, <<"fetch">>, #{<<"url">> => <<"http://127.0.0.1:9/">>}),
ok = wasm_tools:unload(Http),
receive {event, [wasm_tools, http, denied], _, #{host := Host, port := Port}} -> {Host, Port} end.
%% => {<<"127.0.0.1">>, 9}
  • A request is refused by its host name before the name is looked up, and by its address when a name you granted resolves to a loopback, private or other non-public address. Without http and network, every request is refused, and each one is reported.
  • With a network grant and no http, that grant decides wasi:http requests on its own, and a request it refuses sends no event.
  • The path, the headers and the body of the request are never in the metadata.

Send to telemetry

The arguments are telemetry's, so telemetry:execute/3 is an observer:

{ok, Reg} = wasm_tools:start_link(#{observer => fun telemetry:execute/3}).

Send to instrument

Meter = instrument_meter:get_meter(<<"wasm_tools">>),
Calls = instrument_meter:create_histogram(Meter, <<"wasm_tools_call_duration_seconds">>, #{}),
Observer = fun([wasm_tools, call, End], #{duration := D}, #{tool := Tool, class := Class})
                 when End =:= stop; End =:= exception ->
                   Seconds = erlang:convert_time_unit(D, native, microsecond) / 1.0e6,
                   instrument_meter:record(Calls, Seconds, #{tool => Tool, outcome => Class});
              (_, _, _) ->
                   ok
           end,
{ok, Reg} = wasm_tools:start_link(#{observer => Observer}).

Read a registry's counters

{ok, Reg} = wasm_tools:start_link(#{}),
unlink(Reg),
{ok, _} = wasm_tools:add(Reg, Calc, #{}),
{ok, 3} = wasm_tools:call(Reg, <<"add">>, #{<<"a">> => 1, <<"b">> => 2}),
maps:with([active, calls, ok, rejected], wasm_tools:stats(Reg)).
%% => #{active := 0, calls := 1, ok := 1, rejected := 0}

stats/1 also has tools and errors, a count per error class. The counters are OTP counters: reading them costs nothing to the calls.

ok = wasm_tools:stop(Reg), ok = wasm_tools:unload(Calc).

Notes

  • erlang_wasm accounts the memory of every component in the node: wasm_engine:stats/0.
  • Calls through MCP produce the same events.