TestOTelFastRequestTranscriptDiscovery/true wrote the Pi transcript
inside the upstream call and then checked that the cached lookup still
missed it. Two things could find the transcript first, both wall time:
- the exporter's transcript watcher, which fastOTel ticks every 50ms.
Its poll refreshes the session index and marks the session seen, so
a pause of more than ~50ms between the write and the check turned the
test red. This is the failure seen under several concurrent suites.
- the session index's 2s discovery cache window. While the watcher
ticks it keeps the snapshot fresh, so this showed only once the
watcher was held.
sessions.TraceIdentityTTL now names the 2s window (behaviour unchanged).
The test holds both for the request: OTelSessionsEvery and the window
are an hour, so only SessionFresh (the code under test) can find the
transcript, however slow the machine is.
Siblings with the same assumption:
- otlp TestOTelSessionDiscoveredDuringFirstRequest: same cached-miss
check after writing the transcript; same hold.
- sessions TestTraceSessionIndexSharesMissesAndDoesNotQueueRequests:
100 lookups had to finish inside the 2s window; the window is held for
that loop, and the "not queued" wait is 30s instead of 1s (a queued
lookup blocks until Unlock, so the bound only delays a real failure).
- gateway: the wait-until deadlines for transcript readiness and the
Claude child generations go from 6s/8s to 30s; they return as soon as
the condition holds.
Broken by hand (injected pauses, then removed):
- old gateway test, 100ms pause after the write: red ("request must
still have a cached discovery miss"); 2100ms: red. New: green at 0,
100ms, 2100ms after the write and 2100ms before it.
- new gateway test with only the watcher held, 2100ms before the write:
red, so the window hold is needed too.
- new gateway test with SessionFresh calling Visible instead of
VisibleFresh: red ("gateway spans=2 want=0"), so it still guards the
fresh lookup.
- old otlp sibling, 100ms and 2100ms pause: red; new: green at 0,
100ms, 2100ms.
- old sessions sibling with 25ms per lookup (2.5s): red ("each unknown
session repeated discovery"); new: green.
Run:
- go test -race -count=20 -cpu 1,4 -timeout 60m -run OTel ./internal/gateway: ok (1515s)
- go test -race -count=20 -cpu 1,4 -run 'OTel|TraceSession' ./internal/otlp ./internal/sessions: ok
- gofmt -l: clean; go vet ./... (darwin); CGO_ENABLED=0 GOOS=windows|linux go vet -tags nogui ./...: clean
- Full suite: SUITE_LINE
85 lines
3.1 KiB
Go
85 lines
3.1 KiB
Go
package otlp
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
func TestOTelSessionDiscoveredDuringFirstRequest(t *testing.T) {
|
|
t.Setenv("PI_CODING_AGENT_DIR", t.TempDir())
|
|
t.Setenv("PI_CODING_AGENT_SESSION_DIR", "")
|
|
t.Setenv("CODEX_HOME", t.TempDir())
|
|
t.Setenv("MAGPIE_OTEL_ENABLED", "true")
|
|
t.Setenv("MAGPIE_OTEL_ENDPOINT", "http://127.0.0.1:1")
|
|
t.Setenv("MAGPIE_OTEL_HEADERS", "")
|
|
t.Setenv("MAGPIE_OTEL_SESSIONS", "true")
|
|
fastOTel(t)
|
|
// Begin's cached miss must outlast the test however slow the machine
|
|
// is (#1811): neither the transcript watcher's poll nor the end of the
|
|
// discovery cache window may find the transcript before skip() does.
|
|
sessionsEvery, ttl := OTelSessionsEvery, sessions.TraceIdentityTTL
|
|
OTelSessionsEvery, sessions.TraceIdentityTTL = time.Hour, time.Hour
|
|
t.Cleanup(func() { OTelSessionsEvery, sessions.TraceIdentityTTL = sessionsEvery, ttl })
|
|
stop := StartOTel()
|
|
t.Cleanup(stop)
|
|
// a local pi turn with no key, as the gateway describes it
|
|
q := usage.Request{Header: http.Header{}, Agent: "pi", Session: "late-session", Plain: true}
|
|
pending, _ := testX.Begin(q).(*request)
|
|
exported, _ := testX.Begin(q).(*request)
|
|
if pending == nil || exported == nil || !pending.sessionCandidate {
|
|
t.Fatal("unseen session must retain gateway fallback")
|
|
}
|
|
if exported.skip() {
|
|
t.Fatal("unseen session suppressed")
|
|
}
|
|
dir := filepath.Join(os.Getenv("PI_CODING_AGENT_DIR"), "sessions", "work")
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "2026-10-03T00-00-00_late-session.jsonl"), []byte("{\"type\":\"session\",\"id\":\"late-session\"}\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if OTelSession("pi", "late-session") {
|
|
t.Fatal("test must exercise a fresh cached miss")
|
|
}
|
|
if !pending.skip() {
|
|
t.Fatal("newly discovered transcript duplicated gateway trace")
|
|
}
|
|
if exported.skip() {
|
|
t.Fatal("discovery after an exported attempt must retain its root")
|
|
}
|
|
remote := &request{x: testX, agent: "pi", session: "late-session"}
|
|
if remote.skip() {
|
|
t.Fatal("remote/keyed request must retain gateway tracing")
|
|
}
|
|
}
|
|
|
|
func TestOTelParentValidation(t *testing.T) {
|
|
valid := "00-" + strings.Repeat("a", 32) + "-" + strings.Repeat("b", 16) + "-01"
|
|
for _, value := range []string{valid, "", strings.Replace(valid, "00-", "ff-", 1), strings.ToUpper(valid), strings.Replace(valid, strings.Repeat("a", 32), strings.Repeat("0", 32), 1), valid + "-extra", valid[:len(valid)-1] + "z"} {
|
|
trace, parent := otelParent(value)
|
|
if value == valid {
|
|
if trace == "" || parent == "" {
|
|
t.Fatal("valid parent rejected")
|
|
}
|
|
} else if trace != "" || parent != "" {
|
|
t.Fatalf("accepted malformed parent: %q", value)
|
|
}
|
|
}
|
|
}
|
|
|
|
// fastOTel has the exporter read the agents' transcripts and send what it
|
|
// holds within a fraction of a second, not every 2s and 5s.
|
|
func fastOTel(t *testing.T) {
|
|
every, sessionsEvery := OTelEvery, OTelSessionsEvery
|
|
OTelEvery, OTelSessionsEvery = 200*time.Millisecond, 50*time.Millisecond
|
|
t.Cleanup(func() { OTelEvery, OTelSessionsEvery = every, sessionsEvery })
|
|
}
|