Observability
View SourceThis 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| event | when | measurements | metadata |
|---|---|---|---|
[wasm_tools, load, stop] | a load ends | duration, size | digest, class, kind |
[wasm_tools, call, start] | a call starts | system_time, monotonic_time | tool, export, registry |
[wasm_tools, call, stop] | the function returned, a value or an error value | duration | tool, export, registry, class, kind |
[wasm_tools, call, exception] | the call was stopped: deadline, cancel, heap limit, caller or registry gone | duration | tool, export, registry, class, kind |
[wasm_tools, http, denied] | the capabilities refused a wasi:http request of the call | system_time | tool, export, registry, scheme, host, port, and address when an address was refused |
durationis in native time units (erlang:convert_time_unit/3).classisokfor 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
httpandnetwork, every request is refused, and each one is reported. - With a
networkgrant and nohttp, 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.