diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f15f6d3..031f901e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] ### Added +- Spans from calls that cross nodes join the caller's trace. `Hyper.Rpc.call/5` + carries the caller's OpenTelemetry context over `:erpc`, and every one of + Hyper's own `:erpc` hops goes through it; callers outside Hyper do the same + by calling `Hyper.Rpc.run/4` with `:otel_propagator_text_map.inject([])`. + `exec` gains spans too: `Hyper.exec` and `Hyper.Node.FireVMM.Agent.exec`, + which records the call's `outcome` (`ok`, `agent_unavailable`, `timeout`, + ...) but never its output. - `Hyper.Img.delete/1` removes an image no VM leases, along with the layer files and blob rows no other image shares. Recording an image now checks, under a lock, that its blob's file is still in the media store, so a diff --git a/lib/hyper.ex b/lib/hyper.ex index 9e960c57..e6e52266 100644 --- a/lib/hyper.ex +++ b/lib/hyper.ex @@ -3,6 +3,8 @@ defmodule Hyper do `Hyper` is a distrubuted elixir virtual machine orchestrator. """ + use OpenTelemetryDecorator + defmodule Layer do @moduledoc "A content-addressed image layer." @type id :: String.t() @@ -106,8 +108,9 @@ defmodule Hyper do @spec route(node(), Hyper.Vm.Id.t(), [String.t()], keyword()) :: {:ok, %{stdout: binary(), stderr: binary(), exit_code: integer()}} | {:error, term()} + @decorate with_span("Hyper.exec", include: [:node, :vm_id]) defp route(node, vm_id, argv, opts) do - :erpc.call(node, Hyper.Node, :exec, [vm_id, argv, opts]) + Hyper.Rpc.call(node, Hyper.Node, :exec, [vm_id, argv, opts]) catch :error, {:erpc, _} -> {:error, :node_unreachable} end @@ -147,7 +150,7 @@ defmodule Hyper do defp unflushed_on(nil, _vm_id), do: Unit.Time.zero() defp unflushed_on(node, vm_id) do - :erpc.call(node, Hyper.Node, :unflushed_usage, [vm_id]) + Hyper.Rpc.call(node, Hyper.Node, :unflushed_usage, [vm_id]) catch :error, {:erpc, _} -> Unit.Time.zero() end @@ -185,7 +188,7 @@ defmodule Hyper do @spec address_on(node(), Hyper.Vm.Id.t()) :: {:ok, :inet.ip6_address()} | {:error, :not_found | :node_unreachable} defp address_on(node, vm_id) do - :erpc.call(node, Hyper.Node, :address, [vm_id]) + Hyper.Rpc.call(node, Hyper.Node, :address, [vm_id]) catch :error, {:erpc, _} -> {:error, :node_unreachable} end @@ -210,7 +213,7 @@ defmodule Hyper do def stop_vm(vm_id) when is_binary(vm_id) do case Hyper.Cluster.Routing.supervisor_of(vm_id) do nil -> {:error, :not_found} - pid -> :erpc.call(node(pid), Hyper.Node, :stop_image_vm, [pid]) + pid -> Hyper.Rpc.call(node(pid), Hyper.Node, :stop_image_vm, [pid]) end catch :error, {:erpc, _} -> {:error, :machine_unreachable} @@ -228,7 +231,7 @@ defmodule Hyper do def set_vm_firewall(vm_id, %Hyper.Vm.Firewall{} = firewall) when is_binary(vm_id) do case Hyper.Cluster.Routing.supervisor_of(vm_id) do nil -> {:error, :not_found} - pid -> :erpc.call(node(pid), Hyper.Node.FireVMM.State, :set_firewall, [vm_id, firewall]) + pid -> Hyper.Rpc.call(node(pid), Hyper.Node.FireVMM.State, :set_firewall, [vm_id, firewall]) end catch :error, {:erpc, _} -> {:error, :machine_unreachable} @@ -260,7 +263,7 @@ defmodule Hyper do """ @spec id(Hyper.Vm.t()) :: Hyper.Vm.Id.t() | nil def id(pid) when is_pid(pid) do - :erpc.call(node(pid), Hyper.Cluster.Routing, :id_for, [pid]) + Hyper.Rpc.call(node(pid), Hyper.Cluster.Routing, :id_for, [pid]) catch :error, {:erpc, _} -> nil end diff --git a/lib/hyper/cluster/scheduler.ex b/lib/hyper/cluster/scheduler.ex index f18dd8d2..e978e272 100644 --- a/lib/hyper/cluster/scheduler.ex +++ b/lib/hyper/cluster/scheduler.ex @@ -91,7 +91,7 @@ defmodule Hyper.Cluster.Scheduler do {:ok, pid()} | {:error, term()} defp try_on(target, spec, start_fun, stop_fun) do if target == node() or target in Node.list() do - :erpc.call(target, Hyper.Node, :try_run, [spec, start_fun, stop_fun]) + Hyper.Rpc.call(target, Hyper.Node, :try_run, [spec, start_fun, stop_fun]) else {:error, :node_unreachable} end diff --git a/lib/hyper/node/fire_vmm/agent.ex b/lib/hyper/node/fire_vmm/agent.ex index 0d0181e6..76a5226f 100644 --- a/lib/hyper/node/fire_vmm/agent.ex +++ b/lib/hyper/node/fire_vmm/agent.ex @@ -7,7 +7,10 @@ defmodule Hyper.Node.FireVMM.Agent do path from it, so both sides agree without a process-registry lookup. """ + use OpenTelemetryDecorator + alias Hyper.Agent.V1.{ExecRequest, ExecResponse, GuestAgent} + alias OpenTelemetry.Tracer # GRPC.Status codes stored as module attributes so they can be used in # guard-equivalent pattern matches without calling functions at match time. @@ -58,6 +61,7 @@ defmodule Hyper.Node.FireVMM.Agent do - `:timeout` — deadline for the whole call in milliseconds (default: #{@default_timeout}) """ @spec exec(Hyper.Vm.Id.t(), [String.t()], keyword()) :: result() + @decorate with_span("Hyper.Node.FireVMM.Agent.exec", include: [:vm_id, :timeout]) def exec(vm_id, argv, opts \\ []) do timeout = Keyword.get(opts, :timeout, @default_timeout) deadline = System.monotonic_time(:millisecond) + timeout @@ -81,14 +85,26 @@ defmodule Hyper.Node.FireVMM.Agent do http2_opts: %{closing_timeout: 1} }) - try do - call(conn, req, timeout, deadline) - after - _ = :gun.close(conn) - :gun.flush(conn) - end + result = + try do + call(conn, req, timeout, deadline) + after + _ = :gun.close(conn) + :gun.flush(conn) + end + + Tracer.set_attribute(:outcome, outcome(result)) + result end + # What the span records of a call: never its output, which can hold anything. + @spec outcome(result()) :: atom() + defp outcome({:ok, _executed}), do: :ok + defp outcome({:error, reason}) when is_atom(reason), do: reason + defp outcome({:error, {reason, _detail}}) when is_atom(reason), do: reason + defp outcome({:error, %GRPC.RPCError{}}), do: :grpc_error + defp outcome({:error, _reason}), do: :error + @spec call(pid(), ExecRequest.t(), non_neg_integer(), integer()) :: result() defp call(conn, req, timeout, deadline) do case :gun.await_up(conn, remaining(deadline)) do diff --git a/lib/hyper/rpc.ex b/lib/hyper/rpc.ex new file mode 100644 index 00000000..4ca64a4c --- /dev/null +++ b/lib/hyper/rpc.ex @@ -0,0 +1,44 @@ +defmodule Hyper.Rpc do + @moduledoc """ + `:erpc` calls that stay in the caller's trace. + + `:erpc.call/5` runs the function on another node in a fresh process, which + starts with an empty OpenTelemetry context, so anything it traces opens a new + trace instead of continuing the caller's. `call/5` carries the caller's trace + context across as W3C trace-context headers, and `run/4` adopts it on the + target before applying the function. + + `run/4` is the entry point for callers outside Hyper that reach it over + `:erpc`: they pass `:otel_propagator_text_map.inject([])` as the carrier and + call `run/4` in place of the function itself. A carrier with no trace context + in it changes nothing: the function traces as it would have on its own. + """ + + @typedoc "W3C trace-context headers, as `:otel_propagator_text_map.inject/1` writes them." + @type carrier :: [{String.t(), String.t()}] + + @doc """ + `:erpc.call(node, module, fun, args, timeout)`, run in the caller's trace. + Returns and raises exactly as `:erpc.call/5` does. + """ + @spec call(node(), module(), atom(), [term()], timeout()) :: term() + def call(node, module, fun, args, timeout \\ :infinity) do + carrier = :otel_propagator_text_map.inject([]) + :erpc.call(node, __MODULE__, :run, [carrier, module, fun, args], timeout) + end + + @doc """ + Apply `module.fun(args)` in the trace `carrier` names, so its spans are + children of the span that injected it. + """ + @spec run(carrier(), module(), atom(), [term()]) :: term() + def run(carrier, module, fun, args) do + token = :otel_propagator_text_map.extract(carrier) + + try do + apply(module, fun, args) + after + :otel_ctx.detach(token) + end + end +end diff --git a/lib/hyper/vm.ex b/lib/hyper/vm.ex index 63a72c91..41fc1344 100644 --- a/lib/hyper/vm.ex +++ b/lib/hyper/vm.ex @@ -39,7 +39,7 @@ defmodule Hyper.Vm do vm_id -> try do - :erpc.call(node(vm), Hyper.Node, :fork_vm_local, [vm_id]) + Hyper.Rpc.call(node(vm), Hyper.Node, :fork_vm_local, [vm_id]) catch :error, {:erpc, _} -> {:error, :node_unreachable} end @@ -116,7 +116,7 @@ defmodule Hyper.Vm do # :erpc's default (infinity) is correct for a transfer proportional to writes. @spec publish_on_owner(node(), Hyper.Vm.Id.t()) :: {:ok, map()} | {:error, term()} defp publish_on_owner(node, vm_id) do - :erpc.call(node, Hyper.Node, :publish_fork_image, [vm_id]) + Hyper.Rpc.call(node, Hyper.Node, :publish_fork_image, [vm_id]) catch :error, {:erpc, _} -> {:error, :node_unreachable} end diff --git a/test/hyper/rpc_test.exs b/test/hyper/rpc_test.exs new file mode 100644 index 00000000..70badd72 --- /dev/null +++ b/test/hyper/rpc_test.exs @@ -0,0 +1,56 @@ +defmodule Hyper.RpcTest do + @moduledoc """ + `Hyper.Rpc` promises three things: the function it runs over `:erpc` joins + the caller's trace under the caller's span; `run/4` leaves the context of + the process it runs in as it found it; and failures surface exactly as + `:erpc.call/5` raises them, which every caller's `catch` matches on. + + `:erpc` runs a call to the local node with no timeout in the calling process, + context and all, so these calls give one: that runs them in a process of their + own, as a call to another node always is. + """ + use ExUnit.Case, async: true + + require OpenTelemetry.Tracer, as: Tracer + + @timeout_ms 5_000 + + setup_all do + {:ok, _apps} = Application.ensure_all_started(:opentelemetry) + :ok + end + + test "the function runs in the caller's trace, under the caller's span" do + Tracer.with_span "caller" do + caller = Tracer.current_span_ctx() + callee = Hyper.Rpc.call(node(), :otel_tracer, :current_span_ctx, [], @timeout_ms) + + assert :otel_span.is_valid(caller) + assert :otel_span.trace_id(callee) == :otel_span.trace_id(caller) + assert :otel_span.span_id(callee) == :otel_span.span_id(caller) + end + end + + test "without a trace to join, the function starts none" do + callee = Hyper.Rpc.call(node(), :otel_tracer, :current_span_ctx, [], @timeout_ms) + + refute :otel_span.is_valid(callee) + end + + test "run/4 leaves the calling process's context as it found it" do + carrier = Tracer.with_span("elsewhere", do: :otel_propagator_text_map.inject([])) + before = :otel_ctx.get_current() + + Hyper.Rpc.run(carrier, :otel_tracer, :current_span_ctx, []) + + assert :otel_ctx.get_current() == before + end + + test "a raise in the function reaches the caller as :erpc.call/5 raises it" do + expected = catch_error(:erpc.call(node(), :erlang, :error, [:boom], @timeout_ms)) + got = catch_error(Hyper.Rpc.call(node(), :erlang, :error, [:boom], @timeout_ms)) + + assert {:exception, :boom, _stack} = expected + assert {:exception, :boom, _stack} = got + end +end