Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions lib/hyper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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}
Expand All @@ -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}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/hyper/cluster/scheduler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 22 additions & 6 deletions lib/hyper/node/fire_vmm/agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
44 changes: 44 additions & 0 deletions lib/hyper/rpc.ex
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions lib/hyper/vm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions test/hyper/rpc_test.exs
Original file line number Diff line number Diff line change
@@ -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
Loading