Commit Graph
6 Commits
Author SHA1 Message Date
yetone ee20a1511b gateway: the package's tests wait ~42s less for queues and OTel exports (#1654)
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.
2026-10-12 01:16:36 +08:00
mofelee 7d7175cfb1 fix: preserve Langfuse coverage for first and auxiliary calls (#599) 2026-10-03 11:21:10 +08:00
mofelee 38b4a9d984 feat: export gateway and agent conversation waterfalls to Langfuse (#581) 2026-10-03 01:43:32 +08:00
mofelee c2222d17bc otel: bodies can go whole to the trace, not cut at 256 KB (#538) (#555)
* otel: bodies can go whole to the trace, not cut at 256 KB (#538)

The OTLP export sends a call's request and reply as the trace's Langfuse
observation input and output, but took them from the Recent-calls capture,
which keeps the first 256 KiB of each: a longer prompt or reply reached the
trace cut, ending in "… (cut here by magpie)".

Bodies can now go whole, behind a switch: Settings → Observability →
"Include the whole bodies" (otel.bodiesWhole, MAGPIE_OTEL_BODIES_WHOLE for
magpie serve, which needs Include request and response bodies on too). The
gateway keeps the whole request (already in memory for the call) and spools
the reply to a temporary file as it streams, read back for the export.
Nothing changes for Recent calls (still 256 KiB), the request archive or
usage.jsonl.

The export holds queued bodies within a byte budget and splits an OTLP batch
by body size, so a whole body can neither fill memory nor blow up one
collector request. Secrets are masked as before.

TestOTelExportsWholeBodiesWhenOn (a 300 KiB request and reply reach the span
whole with the switch on, cut and marked with it off), TestOTelQueueBodiesBudget,
and the settings/env and otel.test.cjs switches.

* otel: release recent-call bodies and enforce queue budget
2026-10-02 20:39:07 +08:00
yetone 2f02a767b7 otel: request and response bodies can go with a call's span, as Langfuse's input and output, behind a switch that is off by default (#538: Langfuse 似乎无法看到请求体,如果技术上好实现的话,希望可以加一个开关) — the OTLP export sent only an allowlist of metadata (model, tokens, status, timing), never a body, so a Langfuse trace had no Input or Output. Settings → Observability has a new row, "Include request and response bodies" (otel.bodies, MAGPIE_OTEL_BODIES overriding it as the other MAGPIE_OTEL_* do, and counted in the "environment variables override" note). When it and the export are on, the gateway hands the call's captured request and reply (the Recent-calls capture, first 256 KiB each, "… (cut here by magpie)" added when cut) to its usage record as BodyIn/BodyOut, json:"-" so they never reach usage.jsonl, scrubbed with redact.ScrubJSON as the request archive's are (on top of Mask secrets' placeholders); the span carries them as langfuse.observation.input and langfuse.observation.output. A streamed reply's output is its text put together from the events (Chat's choices[].delta.content, Responses' response.output_text.delta, Anthropic's content_block_delta), a reply that isn't a stream or has no text (tool calls only) going as it came. This is filled for the final record, a locally turned-away request, and an attempt refused before another account was tried (its refusal as output); draw/video/decide and Codex's own-login passthrough still send metadata only. Queued records keep the config they were queued under, so turning bodies off drops any not yet sent, and spans with bodies go four to a request so a batch stays well inside a collector's size limit. The OTLP export row's text now says credentials stay local and prompts and replies too unless bodies are included. Nothing was sent to a real collector or Langfuse. TestOTelExportsBodiesWhenOn (off: no input/output and no prompt in the export; on: the request as input, the reply as output, a streamed reply's text as output, a key in the request scrubbed) fails without the gateway change, and without the scrub; TestBodiesStayOutOfTheLog, TestOTelReplyText, TestOTelSettingsAndOverrides (MAGPIE_OTEL_BODIES) and otel.test.cjs (the row off at first, en and zh, a click saves bodies:true without scrolling, kept through another preference's save; WebKit) pass. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass. 2026-10-02 16:48:26 +08:00
Aliceandraper 0260716c9d feat(usage): 可选的 OTLP 元数据导出(Trace/Metrics,支持 Langfuse) (#412)
* usage: add opt-in OTLP HTTP metadata export

Export gateway traces and optional duration/token histograms asynchronously with a bounded queue, retries, proxy support and a bounded shutdown. Add bilingual Settings controls and MAGPIE_OTEL_* overrides. Keep prompts, replies, sessions and provider credentials local; use the same OTLP path for Langfuse.

Validated: full Go tests (nogui), usage race tests, focused go vet, and Chromium/WebKit settings tests in English and Chinese.

Refs #213

* otel: protect collector credentials in backups and settings

Remove all OTLP headers from keyless backups and restrict both new and existing settings files to 0600 before saving. Clarify SIGTERM export behavior. Addresses the two blocking review findings in #412.

* otel: preserve local credentials and read-only settings on restore

---------

Co-authored-by: raper <raper@raperdeMacBook-Air.local>
2026-10-02 01:51:04 +08:00