|
| 1 | +// Package diag implements the agent-side debug surface that runs over |
| 2 | +// the existing QUIC control channel to tunnelproxy. |
| 3 | +package diag |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "sort" |
| 10 | +) |
| 11 | + |
| 12 | +func sortSpecs(s []Spec) { |
| 13 | + sort.Slice(s, func(i, j int) bool { return s[i].Name < s[j].Name }) |
| 14 | +} |
| 15 | + |
| 16 | +// ArgType is the wire-format token for an argument's type. |
| 17 | +type ArgType string |
| 18 | + |
| 19 | +const ( |
| 20 | + ArgTypeString ArgType = "string" |
| 21 | + ArgTypeInt ArgType = "int" |
| 22 | + ArgTypeBool ArgType = "bool" |
| 23 | + ArgTypeDuration ArgType = "duration" |
| 24 | +) |
| 25 | + |
| 26 | +// ArgSpec describes one argument of a Command. It is serialized into |
| 27 | +// the manifest returned by the built-in `agent` command so an operator |
| 28 | +// can discover the surface with a single GET. |
| 29 | +type ArgSpec struct { |
| 30 | + Type ArgType `json:"type"` |
| 31 | + Required bool `json:"required,omitempty"` |
| 32 | + Default any `json:"default,omitempty"` |
| 33 | + Max any `json:"max,omitempty"` |
| 34 | + Min any `json:"min,omitempty"` |
| 35 | + Desc string `json:"description,omitempty"` |
| 36 | +} |
| 37 | + |
| 38 | +// Spec is the manifest entry for a single Command. |
| 39 | +type Spec struct { |
| 40 | + Name string `json:"name"` |
| 41 | + Desc string `json:"description,omitempty"` |
| 42 | + Args map[string]ArgSpec `json:"args,omitempty"` |
| 43 | + Streams bool `json:"streams,omitempty"` |
| 44 | + CeilingMs int `json:"ceiling_ms"` |
| 45 | +} |
| 46 | + |
| 47 | +// Emitter is what a streaming Command writes its chunks to. It is |
| 48 | +// supplied by the dispatcher; Commands MUST NOT retain a reference |
| 49 | +// past their Run call. |
| 50 | +type Emitter interface { |
| 51 | + // Chunk emits one streaming frame upstream. |
| 52 | + Chunk(v any) error |
| 53 | +} |
| 54 | + |
| 55 | +// Command is the contract every probe implements. Non-streaming |
| 56 | +// commands return a JSON-serializable Result and ignore the Emitter. |
| 57 | +// Streaming commands return Result == nil and emit chunks via e until |
| 58 | +// Run returns. |
| 59 | +type Command interface { |
| 60 | + // Spec returns the manifest entry for this command. It is called |
| 61 | + // once at registration; the result must be safe to share. |
| 62 | + Spec() Spec |
| 63 | + |
| 64 | + // Run executes the command. args is the raw `args` field from the |
| 65 | + // Request; implementations decode it as needed. ctx carries the |
| 66 | + // dispatcher-imposed wall-clock ceiling and is cancelled when the |
| 67 | + // stream goes away. |
| 68 | + Run(ctx context.Context, args json.RawMessage, e Emitter) (result any, err error) |
| 69 | +} |
| 70 | + |
| 71 | +// Registry holds the set of registered commands. It is built once at |
| 72 | +// startup and treated as immutable thereafter. |
| 73 | +type Registry struct { |
| 74 | + cmds map[string]Command |
| 75 | +} |
| 76 | + |
| 77 | +// NewRegistry returns an empty Registry. |
| 78 | +func NewRegistry() *Registry { return &Registry{cmds: map[string]Command{}} } |
| 79 | + |
| 80 | +// Register adds c to r. Panics on duplicate name — registration is a |
| 81 | +// startup-time operation. |
| 82 | +func (r *Registry) Register(c Command) { |
| 83 | + name := c.Spec().Name |
| 84 | + if _, ok := r.cmds[name]; ok { |
| 85 | + panic(fmt.Sprintf("diag: duplicate command %q", name)) |
| 86 | + } |
| 87 | + r.cmds[name] = c |
| 88 | +} |
| 89 | + |
| 90 | +// Lookup returns the command with the given name and whether it |
| 91 | +// exists. |
| 92 | +func (r *Registry) Lookup(name string) (Command, bool) { |
| 93 | + c, ok := r.cmds[name] |
| 94 | + return c, ok |
| 95 | +} |
| 96 | + |
| 97 | +// Specs returns the manifest entries for every registered command, |
| 98 | +// sorted by name. Used by the built-in `agent` command. |
| 99 | +func (r *Registry) Specs() []Spec { |
| 100 | + out := make([]Spec, 0, len(r.cmds)) |
| 101 | + for _, c := range r.cmds { |
| 102 | + out = append(out, c.Spec()) |
| 103 | + } |
| 104 | + // Stable order so manifest diffs are reviewable. |
| 105 | + sortSpecs(out) |
| 106 | + return out |
| 107 | +} |
0 commit comments