mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 16:04:01 +00:00
Move the command to cmd/opencode2api and group production code into configuration, gateway, protocol, model, admin, telemetry and helper packages. Keep shared state private behind context, metrics and runtime interfaces. Remove the requested test files and replace test-only CI jobs with vet/build checks. Update Docker and release build paths, preserve version injection and make the WebUI an explicit embedded asset package. Add directory trees and source reading guides to both READMEs. Anchor binary ignore rules so the command package is tracked.
116 lines
3.3 KiB
Go
116 lines
3.3 KiB
Go
// Package telemetry records request outcomes, metrics, and structured logs.
|
|
package telemetry
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
|
|
"opencode2api/internal/protocol"
|
|
)
|
|
|
|
type RequestMeta struct {
|
|
Model string
|
|
Tier string
|
|
Protocol protocol.Protocol
|
|
Request string
|
|
KeyID string
|
|
Channel string
|
|
Anonymous bool
|
|
Proxy string
|
|
Attempts int
|
|
Stream bool
|
|
Usage protocol.Usage
|
|
UsageReported bool
|
|
AttemptOutcome string
|
|
Outcome string
|
|
}
|
|
|
|
type requestMetaKey struct{}
|
|
|
|
// WithRequestMeta shares request-local outcomes across HTTP and gateway layers.
|
|
func WithRequestMeta(ctx context.Context, meta *RequestMeta) context.Context {
|
|
return context.WithValue(ctx, requestMetaKey{}, meta)
|
|
}
|
|
|
|
// MetaFromContext returns the request-local trace, or nil when it is absent.
|
|
func MetaFromContext(ctx context.Context) *RequestMeta {
|
|
meta, _ := ctx.Value(requestMetaKey{}).(*RequestMeta)
|
|
return meta
|
|
}
|
|
|
|
func MetaFromRequest(r *http.Request) *RequestMeta {
|
|
return MetaFromContext(r.Context())
|
|
}
|
|
|
|
type statusWriter struct {
|
|
http.ResponseWriter
|
|
status int
|
|
bytes int64
|
|
}
|
|
|
|
func (w *statusWriter) WriteHeader(status int) {
|
|
if w.status != 0 {
|
|
return
|
|
}
|
|
w.status = status
|
|
w.ResponseWriter.WriteHeader(status)
|
|
}
|
|
|
|
func (w *statusWriter) Write(data []byte) (int, error) {
|
|
if w.status == 0 {
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
n, err := w.ResponseWriter.Write(data)
|
|
w.bytes += int64(n)
|
|
return n, err
|
|
}
|
|
|
|
func (w *statusWriter) Flush() {
|
|
_ = http.NewResponseController(w.ResponseWriter).Flush()
|
|
}
|
|
|
|
func (w *statusWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter }
|
|
|
|
func Middleware(monitor *Monitor, logger *slog.Logger, next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/healthz" {
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
started := time.Now()
|
|
meta := MetaFromRequest(r)
|
|
if meta == nil {
|
|
meta = &RequestMeta{}
|
|
r = r.WithContext(WithRequestMeta(r.Context(), meta))
|
|
}
|
|
writer := &statusWriter{ResponseWriter: w}
|
|
monitor.active.Add(1)
|
|
defer func() {
|
|
monitor.active.Add(-1)
|
|
status := writer.status
|
|
if status == 0 {
|
|
status = http.StatusOK
|
|
}
|
|
duration := time.Since(started)
|
|
monitor.Record(r.URL.Path, status, duration, meta)
|
|
outcome := meta.Outcome
|
|
if outcome == "" {
|
|
outcome = requestOutcome(status, meta.Channel)
|
|
}
|
|
if meta.Channel != "" {
|
|
logger.Info("request routed", "component", "http", "event", "request_routed", "method", r.Method,
|
|
"path", r.URL.Path, "status", status, "duration_ms", duration.Milliseconds(), "request_id", meta.Request,
|
|
"model", meta.Model, "tier", meta.Tier, "key_id", meta.KeyID, "channel", meta.Channel,
|
|
"anonymous", meta.Anonymous, "attempts", meta.Attempts, "stream", meta.Stream, "outcome", outcome)
|
|
}
|
|
logger.Debug("request completed", "component", "http", "event", "request_complete", "method", r.Method,
|
|
"path", r.URL.Path, "status", status, "duration_ms", duration.Milliseconds(), "bytes", writer.bytes,
|
|
"request_id", meta.Request, "model", meta.Model, "tier", meta.Tier, "key_id", meta.KeyID,
|
|
"channel", meta.Channel, "anonymous", meta.Anonymous, "attempts", meta.Attempts, "stream", meta.Stream, "outcome", outcome)
|
|
}()
|
|
next.ServeHTTP(writer, r)
|
|
})
|
|
}
|