Files
magpie/internal/settings/otel.go
T
yetone e4197144a8 perf: every package's package-level regexps compile on first use
v0.1.3516 made gateway's and redact's patterns lazy. The other 299
package-level regexp.MustCompile calls, in 148 files across agent,
provider, gui, library, codexcat, sessions, usage, plugin and the rest,
now go through lazyre.New too, so a launch that never reaches them
(`magpie statusline`, run by Claude Code on every message) doesn't
compile them. Fields, maps and params that held them are *lazyre.RE.

Measured on mote (M5 Pro), sandbox HOME, v0.1.3516-equivalent build
(t42) against this one (t43):
- GODEBUG=inittrace=1 sum: 7.39 MB / 69,837 allocs -> 4.62 MB / 46,765
- `magpie statusline`, 3 runs each: instructions retired ~212M -> ~176M,
  peak memory footprint 19.2-19.4 MB -> 16.8-17.0 MB

Run: go vet ./... (darwin) ok; GOOS=windows go vet on each of the 30
changed packages ok; GOOS=linux go vet ok except ., internal/cli and
internal/gui, which stop in wails v3's cgo files when cross-compiled
(the same at main). New TestAllLazyPatternsCompile at the root compiles
all 574 registered patterns (ok), so a bad pattern fails a test rather
than a first use.
Not run before push: the changed packages' own test suites; they run
on mote after it.
2026-11-10 12:54:39 +08:00

92 lines
3.2 KiB
Go

package settings
import (
"fmt"
"net/url"
"os"
"strconv"
"strings"
"github.com/yetone/magpie/internal/lazyre"
)
// OTel is the optional OTLP/HTTP metadata export. Endpoint is the base URL,
// including /api/public/otel for Langfuse; signals append /v1/traces or metrics.
type OTel struct {
Enabled bool `json:"enabled,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Metrics bool `json:"metrics,omitempty"`
// Sessions follows newly recorded local Codex and Pi interactions.
Sessions bool `json:"sessions,omitempty"`
// Bodies adds each call's request and reply, as the gateway captured
// them (secrets masked, cut at 256 KiB), to its span as Langfuse's
// observation input and output (#538); off, only metadata leaves
Bodies bool `json:"bodies,omitempty"`
// BodiesWhole keeps those bodies entire, not cut at 256 KiB: the reply
// is written to a temporary file as it streams and read back for the
// export, so a long prompt or reply reaches the trace whole. Only
// meaningful with Bodies on
BodiesWhole bool `json:"bodiesWhole,omitempty"`
}
var otelHeaderName = lazyre.New("^[!#$%&'*+.^_`|~0-9A-Za-z-]+$")
func (o OTel) Check() error {
if o.Enabled && o.Endpoint == "" {
return fmt.Errorf("OTLP endpoint is required when export is enabled")
}
if o.Endpoint != "" {
u, err := url.Parse(o.Endpoint)
if err != nil || u.Hostname() == "" || (u.Scheme != "http" && u.Scheme != "https") || u.User != nil || u.RawQuery != "" || u.Fragment != "" {
return fmt.Errorf("OTLP endpoint must be an http or https base URL without credentials, query or fragment")
}
}
for k, v := range o.Headers {
if !otelHeaderName.MatchString(k) || strings.EqualFold(k, "Content-Type") || strings.EqualFold(k, "Content-Length") || strings.EqualFold(k, "Host") {
return fmt.Errorf("invalid OTLP header %q", k)
}
for _, c := range v {
if c < 32 && c != '\t' || c == 127 {
return fmt.Errorf("invalid OTLP header value for %q", k)
}
}
}
return nil
}
// OTelExport applies explicit MAGPIE_OTEL_* overrides without changing the
// saved preferences. Merely setting an endpoint never enables export.
func OTelExport() (OTel, error) {
o := Load().OTel
for _, x := range []struct {
name string
dst *bool
}{{"MAGPIE_OTEL_ENABLED", &o.Enabled}, {"MAGPIE_OTEL_METRICS", &o.Metrics}, {"MAGPIE_OTEL_BODIES", &o.Bodies}, {"MAGPIE_OTEL_BODIES_WHOLE", &o.BodiesWhole}, {"MAGPIE_OTEL_SESSIONS", &o.Sessions}} {
if v, ok := os.LookupEnv(x.name); ok {
b, err := strconv.ParseBool(v)
if err != nil {
return OTel{}, fmt.Errorf("%s must be true or false", x.name)
}
*x.dst = b
}
}
if v, ok := os.LookupEnv("MAGPIE_OTEL_ENDPOINT"); ok {
o.Endpoint = strings.TrimRight(strings.TrimSpace(v), "/")
}
if v, ok := os.LookupEnv("MAGPIE_OTEL_HEADERS"); ok {
o.Headers = map[string]string{}
if v != "" {
for _, h := range strings.Split(v, ",") {
k, value, ok := strings.Cut(h, "=")
value, err := url.PathUnescape(strings.TrimSpace(value))
if !ok || err != nil {
return OTel{}, fmt.Errorf("MAGPIE_OTEL_HEADERS must be comma-separated name=value pairs")
}
o.Headers[strings.TrimSpace(k)] = value
}
}
}
return o, o.Check()
}