internal/gateway hit go test's default 10m timeout under a full ./... on
a loaded Mac. go test -json timed its 1583 top-level tests at ~306s
summed (they run one after another); the slowest few waited on real
clocks for things the tests don't check.
- TestQueuedStreamFailureIsTheStreamsError (27s -> 0.3s): it queued for
room in a 3s minute (MaxRPM 1), and each of the vendor's two retries
waited a minute more, 9s a protocol. It now queues for a slot
(MaxConcurrency 1, freed once the agent has heard a keepalive), which
goes through the same keepQueued, queuedAlive and watch around the
attempt, so its retries go at once. The assertions are unchanged.
- TestQueuedStreamKeptAliveOnceSentToItsVendor (15s -> 7.6s): the same
slot instead of a 4s minute; keepHeldAfter 1.5s, keepalive 100ms and
the vendor's 1s quiet stay. With the h.alive.sent clause in
holdWriter.watch removed by hand it goes red on all three protocols
("wrote nothing ... between the vendor being asked and its answer").
- TestQueuedStreamKeptAliveForTheMinute keeps the MaxRPM queue.
- A shared holdSlot helper holds the slot; it frees itself after 5s and
in cleanup, so a test that hears nothing fails instead of hanging.
- usage.OTelEvery / OTelSessionsEvery (5s / 2s, read once when StartOTel
starts an exporter) replace the hard-coded tickers; the gateway's OTel
tests set them to 200ms / 50ms through fastOTel.
TestOTelClaudeMainAndChildDedupRetainsHelper 10s -> 4.5s (the rest is
sessions' 2s settle before a Claude transcript counts as finished),
TestOTelSessionDedupOnlyUnkeyedLoopback 2.25s -> 0.4s. Production
intervals are unchanged.
Not cut: TestPluginStreamLargeReplies (22s, 24MB throttled at 3MB/s),
TestPluginStreamLongActiveReader (16s, sized to outlast a 10s stall
window the plugin host dropped in c38728145), TestLargeRequestEndpoints
(16s, a 64MB body the gateway parses many times over - product speed,
not test waits), and the many 1-3s tests.
Run: go test -count=3 -run 'TestQueued|TestOTel' ./internal/gateway (ok),
-race -count=2 of the same (ok), go test ./internal/usage (ok);
go test -count=1 -json ./internal/gateway: tests summed 305.9s before,
the changed ones ~41.6s less after; the whole run took 330.6s at load
average ~125 against 306.7s for the baseline, other tests slowing under
that load (e.g. TestWarmClaudeSaysWhyNot 0.26s -> 5.3s). gofmt -l clean;
GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gateway
./internal/usage clean.
172 lines
5.2 KiB
Go
172 lines
5.2 KiB
Go
package usage
|
|
|
|
import (
|
|
"context"
|
|
"reflect"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/redact"
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
type sessionAvailability struct {
|
|
config settings.OTel
|
|
agents map[string]time.Time
|
|
seen map[observedSession]time.Time
|
|
}
|
|
|
|
type observedSession struct {
|
|
agent, id string
|
|
}
|
|
|
|
// OTelSessionAgent reports recent observations for callers without a session ID.
|
|
func OTelSessionAgent(agent string) bool { return OTelSession(agent, "") }
|
|
|
|
// OTelSession reports whether this exact agent/session has recent local
|
|
// observations. Only requests without an ID fall back to agent-level readiness.
|
|
// Callers must also verify loopback and absence of a gateway key.
|
|
func OTelSession(agent, session string) bool {
|
|
e := otel.Load()
|
|
if e == nil || !sessions.TraceAgent(agent) {
|
|
return false
|
|
}
|
|
config, err := settings.OTelExport()
|
|
return err == nil && e.sessionObserved(agent, session, config)
|
|
}
|
|
|
|
// OTelSessionFresh resolves a cached miss once before the gateway exports its
|
|
// first attempt. Requests without a native ID retain observed-agent readiness.
|
|
func OTelSessionFresh(agent, session string) bool {
|
|
if OTelSession(agent, session) {
|
|
return true
|
|
}
|
|
e := otel.Load()
|
|
if e == nil || session == "" {
|
|
return false
|
|
}
|
|
config, err := settings.OTelExport()
|
|
return err == nil && config.Enabled && config.Sessions && e.identities.VisibleFresh(agent, session)
|
|
}
|
|
|
|
func (e *otelExporter) sessionObserved(agent, session string, config settings.OTel) bool {
|
|
if !config.Enabled || !config.Sessions {
|
|
return false
|
|
}
|
|
a := e.sessions.Load()
|
|
if a != nil && reflect.DeepEqual(a.config, config) {
|
|
at := a.agents[agent]
|
|
if session != "" {
|
|
at = a.seen[observedSession{agent, session}]
|
|
}
|
|
if time.Since(at) < 5*time.Minute {
|
|
return true
|
|
}
|
|
}
|
|
if session == "" {
|
|
return false
|
|
}
|
|
return e.identities.Visible(agent, session)
|
|
}
|
|
|
|
func sessionBody(body string, config settings.OTel) string {
|
|
if !config.Bodies {
|
|
return ""
|
|
}
|
|
body = string(redact.ScrubJSON([]byte(body)))
|
|
cut := false
|
|
if !config.BodiesWhole && len(body) > 256<<10 {
|
|
body = body[:256<<10]
|
|
cut = true
|
|
}
|
|
if cut {
|
|
body += BodyCut
|
|
}
|
|
return body
|
|
}
|
|
func sessionRecord(s sessions.TraceSpan, config settings.OTel) Record {
|
|
status := 200
|
|
if s.Error {
|
|
status = 500
|
|
}
|
|
typ := s.Kind
|
|
return Record{Agent: s.Agent, Model: s.Model, Served: s.Served, Provider: s.Provider, Time: s.Start, Millis: s.End.Sub(s.Start).Milliseconds(), Status: status,
|
|
Input: s.Tokens.Input, Output: s.Tokens.Output, CacheRead: s.Tokens.CacheRead, CacheWrite: s.Tokens.CacheWrite, CacheWrite1h: s.Tokens.CacheWrite1h, Reasoning: s.Reasoning,
|
|
BodyIn: sessionBody(s.Input, config), BodyOut: sessionBody(s.Output, config),
|
|
OTel: &OTelSpan{TraceID: sessions.TraceID(s.Agent, s.Session, s.Turn), SpanID: s.ID, ParentID: s.Parent, Root: s.Parent == "", End: s.End,
|
|
Name: s.Name, Type: typ, SessionID: s.Session, TraceName: s.Agent + " interaction", Session: true, Inferred: s.Inferred, Update: s.Update}}
|
|
}
|
|
|
|
func (e *otelExporter) offerSession(s sessions.TraceSpan, config settings.OTel) {
|
|
// OTLP spans are complete observations, not mutable start/end events.
|
|
// Sending the empty start root and later reusing its ID can leave Langfuse
|
|
// displaying the empty copy even though the completed copy has bodies.
|
|
if !s.Pending {
|
|
e.offer(otelItem{record: sessionRecord(s, config), config: config})
|
|
}
|
|
}
|
|
|
|
func (e *otelExporter) watchSessions(ctx context.Context, started time.Time, every time.Duration) {
|
|
ticker := time.NewTicker(every)
|
|
defer ticker.Stop()
|
|
var reader *sessions.TraceReader
|
|
var prior settings.OTel
|
|
if config, err := settings.OTelExport(); err == nil && config.Enabled && config.Sessions {
|
|
reader = sessions.NewTraceReader(started)
|
|
reader.SetSessionIndex(&e.identities)
|
|
prior = config
|
|
}
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
}
|
|
config, err := settings.OTelExport()
|
|
if err != nil || !config.Enabled || !config.Sessions {
|
|
reader = nil
|
|
e.sessions.Store(nil)
|
|
continue
|
|
}
|
|
if reader == nil || !reflect.DeepEqual(config, prior) {
|
|
reader = sessions.NewTraceReader(time.Now())
|
|
reader.SetSessionIndex(&e.identities)
|
|
prior = config
|
|
e.sessions.Store(nil)
|
|
}
|
|
spans := reader.Poll(config.Bodies)
|
|
available := &sessionAvailability{config: config, agents: map[string]time.Time{}, seen: map[observedSession]time.Time{}}
|
|
if previous := e.sessions.Load(); previous != nil {
|
|
for agent, at := range previous.agents {
|
|
if time.Since(at) < 5*time.Minute {
|
|
available.agents[agent] = at
|
|
}
|
|
}
|
|
for key, at := range previous.seen {
|
|
if time.Since(at) < 5*time.Minute {
|
|
available.seen[key] = at
|
|
}
|
|
}
|
|
}
|
|
for _, identity := range reader.VisibleSessions() {
|
|
available.seen[observedSession{identity.Agent, identity.ID}] = time.Now()
|
|
}
|
|
for _, s := range spans {
|
|
if s.End.After(available.agents[s.Agent]) {
|
|
available.agents[s.Agent] = s.End
|
|
}
|
|
key := observedSession{s.Agent, s.Session}
|
|
if s.Session != "" && s.End.After(available.seen[key]) {
|
|
available.seen[key] = s.End
|
|
}
|
|
}
|
|
e.sessions.Store(available)
|
|
for _, s := range spans {
|
|
if ctx.Err() != nil {
|
|
return
|
|
}
|
|
e.offerSession(s, config)
|
|
}
|
|
}
|
|
}
|