P3 of docs/subsystems/plugin-runtime.md opens the gateway to plugins. The
observer point is first: it only reads, so it carries the least risk.
With plugins/otel off, the gateway exports nothing and does no observing
work. It keeps no reply whole and adds no request context.
- usage.Observer:
- Methods: Serve, Bodies, Begin(Request) Watch and Ledger(Record). Watch
has Attempt and End.
- Observe(o) registers an observer until its remove is called.
- ServeObservers is counted, and only Server.ListenAndServe calls it.
- ObservedBodies takes the most any observer wants.
- Record.SkipOTel becomes Watched, set by appendUsage for a request an
observer watched.
- The gateway's otel.go becomes observe.go:
- observeRequest asks each observer through Begin.
- attempt and end call each Watch. Bodies are added only when an
observer asks for them, and the whole reply only for WholeBodies.
- internal/otlp: the exporter (otel*.go) moves out of internal/usage, and
the gateway's per-request span state moves into request.go. otlp.New()
is an Exporter, a usage.Observer. What it sends is unchanged.
- plugins/otel:
- Apply registers otlp.New() and disposes of it when turned off.
- InUse is 1 while settings.OTelExport says to send. A user who exports
only through MAGPIE_OTEL_* keeps the plugin on under Essentials.
- pluginrt.KeepFresh: the process serving the gateway refreshes every 2s
when plugins.yml or settings.json changes. A switch made in the window or
the CLI reaches a running gateway, or a headless serve, without a restart.
- docs: plugin-runtime.md gets a P3 section, a gateway runtime path and an
updated failure section; reference.md says the export is the otel plugin.
Run at this tree on origin/main 82f48f093 (rebased onto 52ee84761 after,
which touches only the Linux tray icon):
- gofmt -l is clean. go vet passes on darwin, GOOS=linux -tags nogui and
GOOS=windows.
- go test -count=1 -timeout 25m ./...: ok, 55 packages, none failed.
- New tests:
- usage: TestObserverServesWithTheGateway and TestObserverLedgerAndBodies.
- gateway: TestObserverPoint, covering unwatched, no bodies, captured,
whole and whole streamed.
- plugins/otel: TestOnExportsOffSendsNothing (on, off, on, with a real
collector) and TestInUseByEnvironment.
- pluginrt: TestKeepFreshFollowsPluginsYml.
- The OTel tests moved with their code to internal/otlp.
Broken by hand, each red, then restored:
- Apply without Observe, and without its dispose.
- KeepFresh refreshing only once.
- InUse reading settings.json only.
- Ledger keeping Watched rows: 3 gateway OTel tests red.
- ObservedBodies taking the last observer's answer.
- Observe not serving a late registration.
- appendUsage not setting Watched.
- Whole bodies never kept.
- Bodies always on with no observer asking.
133 lines
4.3 KiB
Go
133 lines
4.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// fakeObserver watches every request when watch is set, and keeps what it
|
|
// was told.
|
|
type fakeObserver struct {
|
|
watch bool
|
|
bodies usage.Bodies
|
|
|
|
mu sync.Mutex
|
|
begun []usage.Request
|
|
attempts []usage.Record
|
|
ends []usage.Record
|
|
rows []usage.Record
|
|
}
|
|
|
|
func (f *fakeObserver) Serve() func() { return func() {} }
|
|
func (f *fakeObserver) Bodies() usage.Bodies { return f.bodies }
|
|
func (f *fakeObserver) Begin(q usage.Request) usage.Watch {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.begun = append(f.begun, q)
|
|
if !f.watch {
|
|
return nil
|
|
}
|
|
return fakeWatch{f}
|
|
}
|
|
func (f *fakeObserver) Ledger(r usage.Record) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.rows = append(f.rows, r)
|
|
}
|
|
|
|
type fakeWatch struct{ f *fakeObserver }
|
|
|
|
func (w fakeWatch) Attempt(r usage.Record) {
|
|
w.f.mu.Lock()
|
|
defer w.f.mu.Unlock()
|
|
w.f.attempts = append(w.f.attempts, r)
|
|
}
|
|
func (w fakeWatch) End(r usage.Record) {
|
|
w.f.mu.Lock()
|
|
defer w.f.mu.Unlock()
|
|
w.f.ends = append(w.f.ends, r)
|
|
}
|
|
|
|
// The observer point: each observer is asked about each request with what
|
|
// the gateway knows of it; one that watches is told each attempt and the
|
|
// end, with the bodies it wants, and the ledger row says it was watched;
|
|
// one that doesn't is told nothing more, and its row isn't marked.
|
|
func TestObserverPoint(t *testing.T) {
|
|
big := strings.Repeat("A", 300<<10)
|
|
for _, c := range []struct {
|
|
name string
|
|
watch bool
|
|
bodies usage.Bodies
|
|
stream bool
|
|
}{
|
|
{"unwatched", false, usage.WholeBodies, false},
|
|
{"no bodies", true, usage.NoBodies, false},
|
|
{"captured", true, usage.CapturedBodies, false},
|
|
{"whole", true, usage.WholeBodies, false},
|
|
{"whole streamed", true, usage.WholeBodies, true},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
f := &fake{ctype: "application/json", reply: `{"id":"c1","model":"m1","choices":[{"message":{"role":"assistant","content":"` + big + `"},"finish_reason":"stop"}],"usage":{"prompt_tokens":10,"completion_tokens":5}}`}
|
|
if c.stream {
|
|
f.ctype = "text/event-stream"
|
|
f.reply = "data: {\"choices\":[{\"delta\":{\"content\":\"" + big + "\"}}]}\n\ndata: {\"choices\":[],\"usage\":{\"prompt_tokens\":10,\"completion_tokens\":5}}\n\ndata: [DONE]\n\n"
|
|
}
|
|
setup(t, provider.Chat, f)
|
|
o := &fakeObserver{watch: c.watch, bodies: c.bodies}
|
|
defer usage.Observe(o)()
|
|
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"m1","messages":[{"role":"user","content":"hello `+big+`"}]}`))
|
|
req.RemoteAddr = "127.0.0.1:1234"
|
|
req.Header.Set("User-Agent", "pi/1.0")
|
|
req.Header.Set("X-Session-Id", "s1")
|
|
w := httptest.NewRecorder()
|
|
New().Handler().ServeHTTP(w, req)
|
|
if w.Code != 200 {
|
|
t.Fatalf("gateway: %d", w.Code)
|
|
}
|
|
o.mu.Lock()
|
|
defer o.mu.Unlock()
|
|
if len(o.begun) != 1 {
|
|
t.Fatalf("begun %d, want 1", len(o.begun))
|
|
}
|
|
if q := o.begun[0]; q.Agent != "pi" || q.Session != "s1" || !q.Plain || q.Kind != "" {
|
|
t.Fatalf("request: %+v", q)
|
|
}
|
|
if len(o.rows) != 1 || o.rows[0].Watched != c.watch {
|
|
t.Fatalf("ledger rows: %d, watched %v", len(o.rows), len(o.rows) > 0 && o.rows[0].Watched)
|
|
}
|
|
if !c.watch {
|
|
if len(o.attempts)+len(o.ends) != 0 {
|
|
t.Fatal("an unwatched request was told on")
|
|
}
|
|
return
|
|
}
|
|
if len(o.attempts) != 1 || len(o.ends) != 1 {
|
|
t.Fatalf("attempts %d, ends %d; want 1, 1", len(o.attempts), len(o.ends))
|
|
}
|
|
a := o.attempts[0]
|
|
if a.Model != "m1" || a.Provider != "fake" || a.Input != 10 || a.Output != 5 || a.Status != 200 || o.ends[0].Status != 200 {
|
|
t.Fatalf("attempt %+v, end %+v", a, o.ends[0])
|
|
}
|
|
switch c.bodies {
|
|
case usage.NoBodies:
|
|
if a.BodyIn != "" || a.BodyOut != "" {
|
|
t.Fatal("bodies sent to an observer that wants none")
|
|
}
|
|
case usage.CapturedBodies:
|
|
if !strings.Contains(a.BodyIn, "hello") || !strings.HasSuffix(a.BodyOut, usage.BodyCut) {
|
|
t.Fatalf("captured bodies: in=%d out=%d", len(a.BodyIn), len(a.BodyOut))
|
|
}
|
|
case usage.WholeBodies:
|
|
if strings.Contains(a.BodyIn, usage.BodyCut) || strings.Contains(a.BodyOut, usage.BodyCut) || len(a.BodyIn) < len(big) || len(a.BodyOut) < len(big) {
|
|
t.Fatalf("whole bodies: in=%d out=%d", len(a.BodyIn), len(a.BodyOut))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|