proxy,ui-svelte: add request/response capturing (#508)
Add saving request and response headers and bodies that go through llama-swap in memory. - captureBuffer added to configuration. Captures are enabled by default. - 5MB of memory is allocated for req/response captures in a ring buffer. Setting captureBuffer to 0 will disable captures. - UI elements to view captured data added to Activity page. Includes some QOL features like json formatting and recombining SSE chat streams - capture saving is done at the byte level and has minimal impact on llama-swap performance Fixes #464 Ref #503
This commit is contained in:
@@ -87,6 +87,12 @@
|
|||||||
"default": 1000,
|
"default": 1000,
|
||||||
"description": "Maximum number of metrics to keep in memory. Controls how many metrics are stored before older ones are discarded."
|
"description": "Maximum number of metrics to keep in memory. Controls how many metrics are stored before older ones are discarded."
|
||||||
},
|
},
|
||||||
|
"captureBuffer": {
|
||||||
|
"type": "integer",
|
||||||
|
"minimum": 0,
|
||||||
|
"default": 5,
|
||||||
|
"description": "Size in megabytes of the buffer for storing request/response captures. Set to 0 to disable captures."
|
||||||
|
},
|
||||||
"startPort": {
|
"startPort": {
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"default": 5800,
|
"default": 5800,
|
||||||
|
|||||||
@@ -50,6 +50,11 @@ logToStdout: "proxy"
|
|||||||
# - useful for limiting memory usage when processing large volumes of metrics
|
# - useful for limiting memory usage when processing large volumes of metrics
|
||||||
metricsMaxInMemory: 1000
|
metricsMaxInMemory: 1000
|
||||||
|
|
||||||
|
# captureBuffer: how many MBs to allocate for storing request/response captures
|
||||||
|
# - optional, default: 10
|
||||||
|
# - set to 0 to disable
|
||||||
|
captureBuffer: 15
|
||||||
|
|
||||||
# startPort: sets the starting port number for the automatic ${PORT} macro.
|
# startPort: sets the starting port number for the automatic ${PORT} macro.
|
||||||
# - optional, default: 5800
|
# - optional, default: 5800
|
||||||
# - the ${PORT} macro can be used in model.cmd and model.proxy settings
|
# - the ${PORT} macro can be used in model.cmd and model.proxy settings
|
||||||
|
|||||||
@@ -123,6 +123,7 @@ type Config struct {
|
|||||||
LogTimeFormat string `yaml:"logTimeFormat"`
|
LogTimeFormat string `yaml:"logTimeFormat"`
|
||||||
LogToStdout string `yaml:"logToStdout"`
|
LogToStdout string `yaml:"logToStdout"`
|
||||||
MetricsMaxInMemory int `yaml:"metricsMaxInMemory"`
|
MetricsMaxInMemory int `yaml:"metricsMaxInMemory"`
|
||||||
|
CaptureBuffer int `yaml:"captureBuffer"`
|
||||||
Models map[string]ModelConfig `yaml:"models"` /* key is model ID */
|
Models map[string]ModelConfig `yaml:"models"` /* key is model ID */
|
||||||
Profiles map[string][]string `yaml:"profiles"`
|
Profiles map[string][]string `yaml:"profiles"`
|
||||||
Groups map[string]GroupConfig `yaml:"groups"` /* key is group ID */
|
Groups map[string]GroupConfig `yaml:"groups"` /* key is group ID */
|
||||||
@@ -201,6 +202,7 @@ func LoadConfigFromReader(r io.Reader) (Config, error) {
|
|||||||
LogTimeFormat: "",
|
LogTimeFormat: "",
|
||||||
LogToStdout: LogToStdoutProxy,
|
LogToStdout: LogToStdoutProxy,
|
||||||
MetricsMaxInMemory: 1000,
|
MetricsMaxInMemory: 1000,
|
||||||
|
CaptureBuffer: 5,
|
||||||
}
|
}
|
||||||
if err = yaml.Unmarshal([]byte(yamlStr), &config); err != nil {
|
if err = yaml.Unmarshal([]byte(yamlStr), &config); err != nil {
|
||||||
return Config{}, err
|
return Config{}, err
|
||||||
|
|||||||
@@ -215,6 +215,7 @@ groups:
|
|||||||
},
|
},
|
||||||
HealthCheckTimeout: 15,
|
HealthCheckTimeout: 15,
|
||||||
MetricsMaxInMemory: 1000,
|
MetricsMaxInMemory: 1000,
|
||||||
|
CaptureBuffer: 5,
|
||||||
Profiles: map[string][]string{
|
Profiles: map[string][]string{
|
||||||
"test": {"model1", "model2"},
|
"test": {"model1", "model2"},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -204,6 +204,7 @@ groups:
|
|||||||
},
|
},
|
||||||
HealthCheckTimeout: 15,
|
HealthCheckTimeout: 15,
|
||||||
MetricsMaxInMemory: 1000,
|
MetricsMaxInMemory: 1000,
|
||||||
|
CaptureBuffer: 5,
|
||||||
Profiles: map[string][]string{
|
Profiles: map[string][]string{
|
||||||
"test": {"model1", "model2"},
|
"test": {"model1", "model2"},
|
||||||
},
|
},
|
||||||
|
|||||||
+158
-9
@@ -28,6 +28,28 @@ type TokenMetrics struct {
|
|||||||
PromptPerSecond float64 `json:"prompt_per_second"`
|
PromptPerSecond float64 `json:"prompt_per_second"`
|
||||||
TokensPerSecond float64 `json:"tokens_per_second"`
|
TokensPerSecond float64 `json:"tokens_per_second"`
|
||||||
DurationMs int `json:"duration_ms"`
|
DurationMs int `json:"duration_ms"`
|
||||||
|
HasCapture bool `json:"has_capture"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReqRespCapture struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
ReqPath string `json:"req_path"`
|
||||||
|
ReqHeaders map[string]string `json:"req_headers"`
|
||||||
|
ReqBody []byte `json:"req_body"`
|
||||||
|
RespHeaders map[string]string `json:"resp_headers"`
|
||||||
|
RespBody []byte `json:"resp_body"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size returns the approximate memory usage of this capture in bytes
|
||||||
|
func (c *ReqRespCapture) Size() int {
|
||||||
|
size := len(c.ReqPath) + len(c.ReqBody) + len(c.RespBody)
|
||||||
|
for k, v := range c.ReqHeaders {
|
||||||
|
size += len(k) + len(v)
|
||||||
|
}
|
||||||
|
for k, v := range c.RespHeaders {
|
||||||
|
size += len(k) + len(v)
|
||||||
|
}
|
||||||
|
return size
|
||||||
}
|
}
|
||||||
|
|
||||||
// TokenMetricsEvent represents a token metrics event
|
// TokenMetricsEvent represents a token metrics event
|
||||||
@@ -46,19 +68,32 @@ type metricsMonitor struct {
|
|||||||
maxMetrics int
|
maxMetrics int
|
||||||
nextID int
|
nextID int
|
||||||
logger *LogMonitor
|
logger *LogMonitor
|
||||||
|
|
||||||
|
// capture fields
|
||||||
|
enableCaptures bool
|
||||||
|
captures map[int]ReqRespCapture // map for O(1) lookup by ID
|
||||||
|
captureOrder []int // track insertion order for FIFO eviction
|
||||||
|
captureSize int // current total size in bytes
|
||||||
|
maxCaptureSize int // max bytes for captures
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMetricsMonitor(logger *LogMonitor, maxMetrics int) *metricsMonitor {
|
// newMetricsMonitor creates a new metricsMonitor. captureBufferMB is the
|
||||||
mp := &metricsMonitor{
|
// capture buffer size in megabytes; 0 disables captures.
|
||||||
logger: logger,
|
func newMetricsMonitor(logger *LogMonitor, maxMetrics int, captureBufferMB int) *metricsMonitor {
|
||||||
maxMetrics: maxMetrics,
|
return &metricsMonitor{
|
||||||
|
logger: logger,
|
||||||
|
maxMetrics: maxMetrics,
|
||||||
|
enableCaptures: captureBufferMB > 0,
|
||||||
|
captures: make(map[int]ReqRespCapture),
|
||||||
|
captureOrder: make([]int, 0),
|
||||||
|
captureSize: 0,
|
||||||
|
maxCaptureSize: captureBufferMB * 1024 * 1024,
|
||||||
}
|
}
|
||||||
|
|
||||||
return mp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// addMetrics adds a new metric to the collection and publishes an event
|
// addMetrics adds a new metric to the collection and publishes an event.
|
||||||
func (mp *metricsMonitor) addMetrics(metric TokenMetrics) {
|
// Returns the assigned metric ID.
|
||||||
|
func (mp *metricsMonitor) addMetrics(metric TokenMetrics) int {
|
||||||
mp.mu.Lock()
|
mp.mu.Lock()
|
||||||
defer mp.mu.Unlock()
|
defer mp.mu.Unlock()
|
||||||
|
|
||||||
@@ -69,6 +104,49 @@ func (mp *metricsMonitor) addMetrics(metric TokenMetrics) {
|
|||||||
mp.metrics = mp.metrics[len(mp.metrics)-mp.maxMetrics:]
|
mp.metrics = mp.metrics[len(mp.metrics)-mp.maxMetrics:]
|
||||||
}
|
}
|
||||||
event.Emit(TokenMetricsEvent{Metrics: metric})
|
event.Emit(TokenMetricsEvent{Metrics: metric})
|
||||||
|
return metric.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// addCapture adds a new capture to the buffer with size-based eviction.
|
||||||
|
// Captures are skipped if enableCaptures is false or if capture exceeds maxCaptureSize.
|
||||||
|
func (mp *metricsMonitor) addCapture(capture ReqRespCapture) {
|
||||||
|
if !mp.enableCaptures {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mp.mu.Lock()
|
||||||
|
defer mp.mu.Unlock()
|
||||||
|
|
||||||
|
captureSize := capture.Size()
|
||||||
|
if captureSize > mp.maxCaptureSize {
|
||||||
|
mp.logger.Warnf("capture size %d exceeds max %d, skipping", captureSize, mp.maxCaptureSize)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Evict oldest (FIFO) until room available
|
||||||
|
for mp.captureSize+captureSize > mp.maxCaptureSize && len(mp.captureOrder) > 0 {
|
||||||
|
oldestID := mp.captureOrder[0]
|
||||||
|
mp.captureOrder = mp.captureOrder[1:]
|
||||||
|
if evicted, exists := mp.captures[oldestID]; exists {
|
||||||
|
mp.captureSize -= evicted.Size()
|
||||||
|
delete(mp.captures, oldestID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mp.captures[capture.ID] = capture
|
||||||
|
mp.captureOrder = append(mp.captureOrder, capture.ID)
|
||||||
|
mp.captureSize += captureSize
|
||||||
|
}
|
||||||
|
|
||||||
|
// getCaptureByID returns a capture by its ID, or nil if not found.
|
||||||
|
func (mp *metricsMonitor) getCaptureByID(id int) *ReqRespCapture {
|
||||||
|
mp.mu.RLock()
|
||||||
|
defer mp.mu.RUnlock()
|
||||||
|
|
||||||
|
if capture, exists := mp.captures[id]; exists {
|
||||||
|
return &capture
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getMetrics returns a copy of the current metrics
|
// getMetrics returns a copy of the current metrics
|
||||||
@@ -97,6 +175,28 @@ func (mp *metricsMonitor) wrapHandler(
|
|||||||
request *http.Request,
|
request *http.Request,
|
||||||
next func(modelID string, w http.ResponseWriter, r *http.Request) error,
|
next func(modelID string, w http.ResponseWriter, r *http.Request) error,
|
||||||
) error {
|
) error {
|
||||||
|
// Capture request body and headers if captures enabled
|
||||||
|
var reqBody []byte
|
||||||
|
var reqHeaders map[string]string
|
||||||
|
if mp.enableCaptures {
|
||||||
|
if request.Body != nil {
|
||||||
|
var err error
|
||||||
|
reqBody, err = io.ReadAll(request.Body)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read request body for capture: %w", err)
|
||||||
|
}
|
||||||
|
request.Body.Close()
|
||||||
|
request.Body = io.NopCloser(bytes.NewBuffer(reqBody))
|
||||||
|
}
|
||||||
|
reqHeaders = make(map[string]string)
|
||||||
|
for key, values := range request.Header {
|
||||||
|
if len(values) > 0 {
|
||||||
|
reqHeaders[key] = values[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
redactHeaders(reqHeaders)
|
||||||
|
}
|
||||||
|
|
||||||
recorder := newBodyCopier(writer)
|
recorder := newBodyCopier(writer)
|
||||||
|
|
||||||
// Filter Accept-Encoding to only include encodings we can decompress for metrics
|
// Filter Accept-Encoding to only include encodings we can decompress for metrics
|
||||||
@@ -165,7 +265,38 @@ func (mp *metricsMonitor) wrapHandler(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mp.addMetrics(tm)
|
// Build capture if enabled and determine if it will be stored
|
||||||
|
var capture *ReqRespCapture
|
||||||
|
if mp.enableCaptures {
|
||||||
|
respHeaders := make(map[string]string)
|
||||||
|
for key, values := range recorder.Header() {
|
||||||
|
if len(values) > 0 {
|
||||||
|
respHeaders[key] = values[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
redactHeaders(respHeaders)
|
||||||
|
delete(respHeaders, "Content-Encoding")
|
||||||
|
capture = &ReqRespCapture{
|
||||||
|
ReqPath: request.URL.Path,
|
||||||
|
ReqHeaders: reqHeaders,
|
||||||
|
ReqBody: reqBody,
|
||||||
|
RespHeaders: respHeaders,
|
||||||
|
RespBody: body,
|
||||||
|
}
|
||||||
|
// Only set HasCapture if the capture will actually be stored (not too large)
|
||||||
|
if capture.Size() <= mp.maxCaptureSize {
|
||||||
|
tm.HasCapture = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
metricID := mp.addMetrics(tm)
|
||||||
|
|
||||||
|
// Store capture if enabled
|
||||||
|
if capture != nil {
|
||||||
|
capture.ID = metricID
|
||||||
|
mp.addCapture(*capture)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -336,6 +467,24 @@ func (w *responseBodyCopier) StartTime() time.Time {
|
|||||||
return w.start
|
return w.start
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sensitiveHeaders lists headers that should be redacted in captures
|
||||||
|
var sensitiveHeaders = map[string]bool{
|
||||||
|
"authorization": true,
|
||||||
|
"proxy-authorization": true,
|
||||||
|
"cookie": true,
|
||||||
|
"set-cookie": true,
|
||||||
|
"x-api-key": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// redactHeaders replaces sensitive header values in-place with "[REDACTED]"
|
||||||
|
func redactHeaders(headers map[string]string) {
|
||||||
|
for key := range headers {
|
||||||
|
if sensitiveHeaders[strings.ToLower(key)] {
|
||||||
|
headers[key] = "[REDACTED]"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// filterAcceptEncoding filters the Accept-Encoding header to only include
|
// filterAcceptEncoding filters the Accept-Encoding header to only include
|
||||||
// encodings we can decompress (gzip, deflate). This respects the client's
|
// encodings we can decompress (gzip, deflate). This respects the client's
|
||||||
// preferences while ensuring we can parse response bodies for metrics.
|
// preferences while ensuring we can parse response bodies for metrics.
|
||||||
|
|||||||
+254
-29
@@ -18,7 +18,7 @@ import (
|
|||||||
|
|
||||||
func TestMetricsMonitor_AddMetrics(t *testing.T) {
|
func TestMetricsMonitor_AddMetrics(t *testing.T) {
|
||||||
t.Run("adds metrics and assigns ID", func(t *testing.T) {
|
t.Run("adds metrics and assigns ID", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
metric := TokenMetrics{
|
metric := TokenMetrics{
|
||||||
Model: "test-model",
|
Model: "test-model",
|
||||||
@@ -37,7 +37,7 @@ func TestMetricsMonitor_AddMetrics(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("increments ID for each metric", func(t *testing.T) {
|
t.Run("increments ID for each metric", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
mm.addMetrics(TokenMetrics{Model: "model"})
|
mm.addMetrics(TokenMetrics{Model: "model"})
|
||||||
@@ -51,7 +51,7 @@ func TestMetricsMonitor_AddMetrics(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("respects max metrics limit", func(t *testing.T) {
|
t.Run("respects max metrics limit", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 3)
|
mm := newMetricsMonitor(testLogger, 3, 0)
|
||||||
|
|
||||||
// Add 5 metrics
|
// Add 5 metrics
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
@@ -71,7 +71,7 @@ func TestMetricsMonitor_AddMetrics(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("emits TokenMetricsEvent", func(t *testing.T) {
|
t.Run("emits TokenMetricsEvent", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
receivedEvent := make(chan TokenMetricsEvent, 1)
|
receivedEvent := make(chan TokenMetricsEvent, 1)
|
||||||
cancel := event.On(func(e TokenMetricsEvent) {
|
cancel := event.On(func(e TokenMetricsEvent) {
|
||||||
@@ -101,14 +101,14 @@ func TestMetricsMonitor_AddMetrics(t *testing.T) {
|
|||||||
|
|
||||||
func TestMetricsMonitor_GetMetrics(t *testing.T) {
|
func TestMetricsMonitor_GetMetrics(t *testing.T) {
|
||||||
t.Run("returns empty slice when no metrics", func(t *testing.T) {
|
t.Run("returns empty slice when no metrics", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
metrics := mm.getMetrics()
|
metrics := mm.getMetrics()
|
||||||
assert.NotNil(t, metrics)
|
assert.NotNil(t, metrics)
|
||||||
assert.Equal(t, 0, len(metrics))
|
assert.Equal(t, 0, len(metrics))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("returns copy of metrics", func(t *testing.T) {
|
t.Run("returns copy of metrics", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
mm.addMetrics(TokenMetrics{Model: "model1"})
|
mm.addMetrics(TokenMetrics{Model: "model1"})
|
||||||
mm.addMetrics(TokenMetrics{Model: "model2"})
|
mm.addMetrics(TokenMetrics{Model: "model2"})
|
||||||
|
|
||||||
@@ -128,7 +128,7 @@ func TestMetricsMonitor_GetMetrics(t *testing.T) {
|
|||||||
|
|
||||||
func TestMetricsMonitor_GetMetricsJSON(t *testing.T) {
|
func TestMetricsMonitor_GetMetricsJSON(t *testing.T) {
|
||||||
t.Run("returns valid JSON for empty metrics", func(t *testing.T) {
|
t.Run("returns valid JSON for empty metrics", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
jsonData, err := mm.getMetricsJSON()
|
jsonData, err := mm.getMetricsJSON()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotNil(t, jsonData)
|
assert.NotNil(t, jsonData)
|
||||||
@@ -140,7 +140,7 @@ func TestMetricsMonitor_GetMetricsJSON(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("returns valid JSON with metrics", func(t *testing.T) {
|
t.Run("returns valid JSON with metrics", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
mm.addMetrics(TokenMetrics{
|
mm.addMetrics(TokenMetrics{
|
||||||
Model: "model1",
|
Model: "model1",
|
||||||
InputTokens: 100,
|
InputTokens: 100,
|
||||||
@@ -168,7 +168,7 @@ func TestMetricsMonitor_GetMetricsJSON(t *testing.T) {
|
|||||||
|
|
||||||
func TestMetricsMonitor_WrapHandler(t *testing.T) {
|
func TestMetricsMonitor_WrapHandler(t *testing.T) {
|
||||||
t.Run("successful non-streaming request with usage data", func(t *testing.T) {
|
t.Run("successful non-streaming request with usage data", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
responseBody := `{
|
responseBody := `{
|
||||||
"usage": {
|
"usage": {
|
||||||
@@ -199,7 +199,7 @@ func TestMetricsMonitor_WrapHandler(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("successful request with timings data", func(t *testing.T) {
|
t.Run("successful request with timings data", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
responseBody := `{
|
responseBody := `{
|
||||||
"timings": {
|
"timings": {
|
||||||
@@ -239,7 +239,7 @@ func TestMetricsMonitor_WrapHandler(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("streaming request with SSE format", func(t *testing.T) {
|
t.Run("streaming request with SSE format", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
// Note: SSE format requires proper line breaks - each data line followed by blank line
|
// Note: SSE format requires proper line breaks - each data line followed by blank line
|
||||||
responseBody := `data: {"choices":[{"text":"Hello"}]}
|
responseBody := `data: {"choices":[{"text":"Hello"}]}
|
||||||
@@ -275,7 +275,7 @@ data: [DONE]
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("non-OK status code does not record metrics", func(t *testing.T) {
|
t.Run("non-OK status code does not record metrics", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
nextHandler := func(modelID string, w http.ResponseWriter, r *http.Request) error {
|
nextHandler := func(modelID string, w http.ResponseWriter, r *http.Request) error {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
@@ -295,7 +295,7 @@ data: [DONE]
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("empty response body records minimal metrics", func(t *testing.T) {
|
t.Run("empty response body records minimal metrics", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
nextHandler := func(modelID string, w http.ResponseWriter, r *http.Request) error {
|
nextHandler := func(modelID string, w http.ResponseWriter, r *http.Request) error {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
@@ -317,7 +317,7 @@ data: [DONE]
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("invalid JSON records minimal metrics", func(t *testing.T) {
|
t.Run("invalid JSON records minimal metrics", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
nextHandler := func(modelID string, w http.ResponseWriter, r *http.Request) error {
|
nextHandler := func(modelID string, w http.ResponseWriter, r *http.Request) error {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
@@ -341,7 +341,7 @@ data: [DONE]
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("next handler error is propagated", func(t *testing.T) {
|
t.Run("next handler error is propagated", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
expectedErr := assert.AnError
|
expectedErr := assert.AnError
|
||||||
nextHandler := func(modelID string, w http.ResponseWriter, r *http.Request) error {
|
nextHandler := func(modelID string, w http.ResponseWriter, r *http.Request) error {
|
||||||
@@ -360,7 +360,7 @@ data: [DONE]
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("response without usage or timings records minimal metrics", func(t *testing.T) {
|
t.Run("response without usage or timings records minimal metrics", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
responseBody := `{"result": "ok"}`
|
responseBody := `{"result": "ok"}`
|
||||||
|
|
||||||
@@ -437,7 +437,7 @@ func TestMetricsMonitor_ResponseBodyCopier(t *testing.T) {
|
|||||||
|
|
||||||
func TestMetricsMonitor_Concurrent(t *testing.T) {
|
func TestMetricsMonitor_Concurrent(t *testing.T) {
|
||||||
t.Run("concurrent addMetrics is safe", func(t *testing.T) {
|
t.Run("concurrent addMetrics is safe", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 1000)
|
mm := newMetricsMonitor(testLogger, 1000, 0)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
numGoroutines := 10
|
numGoroutines := 10
|
||||||
@@ -464,7 +464,7 @@ func TestMetricsMonitor_Concurrent(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("concurrent reads and writes are safe", func(t *testing.T) {
|
t.Run("concurrent reads and writes are safe", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 100)
|
mm := newMetricsMonitor(testLogger, 100, 0)
|
||||||
|
|
||||||
done := make(chan bool)
|
done := make(chan bool)
|
||||||
|
|
||||||
@@ -502,7 +502,7 @@ func TestMetricsMonitor_Concurrent(t *testing.T) {
|
|||||||
|
|
||||||
func TestMetricsMonitor_ParseMetrics(t *testing.T) {
|
func TestMetricsMonitor_ParseMetrics(t *testing.T) {
|
||||||
t.Run("prefers timings over usage data", func(t *testing.T) {
|
t.Run("prefers timings over usage data", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
// Timings should take precedence over usage
|
// Timings should take precedence over usage
|
||||||
responseBody := `{
|
responseBody := `{
|
||||||
@@ -542,7 +542,7 @@ func TestMetricsMonitor_ParseMetrics(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("handles missing cache_n in timings", func(t *testing.T) {
|
t.Run("handles missing cache_n in timings", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
responseBody := `{
|
responseBody := `{
|
||||||
"timings": {
|
"timings": {
|
||||||
@@ -577,7 +577,7 @@ func TestMetricsMonitor_ParseMetrics(t *testing.T) {
|
|||||||
|
|
||||||
func TestMetricsMonitor_StreamingResponse(t *testing.T) {
|
func TestMetricsMonitor_StreamingResponse(t *testing.T) {
|
||||||
t.Run("finds metrics in last valid SSE data", func(t *testing.T) {
|
t.Run("finds metrics in last valid SSE data", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
// Metrics should be found in the last data line before [DONE]
|
// Metrics should be found in the last data line before [DONE]
|
||||||
responseBody := `data: {"choices":[{"text":"First"}]}
|
responseBody := `data: {"choices":[{"text":"First"}]}
|
||||||
@@ -611,7 +611,7 @@ data: [DONE]
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("handles streaming with no valid JSON records minimal metrics", func(t *testing.T) {
|
t.Run("handles streaming with no valid JSON records minimal metrics", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
responseBody := `data: not json
|
responseBody := `data: not json
|
||||||
|
|
||||||
@@ -641,7 +641,7 @@ data: [DONE]
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("handles empty streaming response records minimal metrics", func(t *testing.T) {
|
t.Run("handles empty streaming response records minimal metrics", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
responseBody := ``
|
responseBody := ``
|
||||||
|
|
||||||
@@ -669,7 +669,7 @@ data: [DONE]
|
|||||||
|
|
||||||
// Benchmark tests
|
// Benchmark tests
|
||||||
func BenchmarkMetricsMonitor_AddMetrics(b *testing.B) {
|
func BenchmarkMetricsMonitor_AddMetrics(b *testing.B) {
|
||||||
mm := newMetricsMonitor(testLogger, 1000)
|
mm := newMetricsMonitor(testLogger, 1000, 0)
|
||||||
|
|
||||||
metric := TokenMetrics{
|
metric := TokenMetrics{
|
||||||
Model: "test-model",
|
Model: "test-model",
|
||||||
@@ -690,7 +690,7 @@ func BenchmarkMetricsMonitor_AddMetrics(b *testing.B) {
|
|||||||
|
|
||||||
func BenchmarkMetricsMonitor_AddMetrics_SmallBuffer(b *testing.B) {
|
func BenchmarkMetricsMonitor_AddMetrics_SmallBuffer(b *testing.B) {
|
||||||
// Test performance with a smaller buffer where wrapping occurs more frequently
|
// Test performance with a smaller buffer where wrapping occurs more frequently
|
||||||
mm := newMetricsMonitor(testLogger, 100)
|
mm := newMetricsMonitor(testLogger, 100, 0)
|
||||||
|
|
||||||
metric := TokenMetrics{
|
metric := TokenMetrics{
|
||||||
Model: "test-model",
|
Model: "test-model",
|
||||||
@@ -711,7 +711,7 @@ func BenchmarkMetricsMonitor_AddMetrics_SmallBuffer(b *testing.B) {
|
|||||||
|
|
||||||
func TestMetricsMonitor_WrapHandler_Compression(t *testing.T) {
|
func TestMetricsMonitor_WrapHandler_Compression(t *testing.T) {
|
||||||
t.Run("gzip encoded response", func(t *testing.T) {
|
t.Run("gzip encoded response", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
responseBody := `{"usage": {"prompt_tokens": 100, "completion_tokens": 50}}`
|
responseBody := `{"usage": {"prompt_tokens": 100, "completion_tokens": 50}}`
|
||||||
|
|
||||||
@@ -745,7 +745,7 @@ func TestMetricsMonitor_WrapHandler_Compression(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("deflate encoded response", func(t *testing.T) {
|
t.Run("deflate encoded response", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
responseBody := `{"usage": {"prompt_tokens": 200, "completion_tokens": 75}}`
|
responseBody := `{"usage": {"prompt_tokens": 200, "completion_tokens": 75}}`
|
||||||
|
|
||||||
@@ -779,7 +779,7 @@ func TestMetricsMonitor_WrapHandler_Compression(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("invalid gzip data records minimal metrics", func(t *testing.T) {
|
t.Run("invalid gzip data records minimal metrics", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
// Invalid compressed data
|
// Invalid compressed data
|
||||||
invalidData := []byte("this is not gzip data")
|
invalidData := []byte("this is not gzip data")
|
||||||
@@ -807,7 +807,7 @@ func TestMetricsMonitor_WrapHandler_Compression(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("unknown encoding treated as uncompressed", func(t *testing.T) {
|
t.Run("unknown encoding treated as uncompressed", func(t *testing.T) {
|
||||||
mm := newMetricsMonitor(testLogger, 10)
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
responseBody := `{"usage": {"prompt_tokens": 300, "completion_tokens": 100}}`
|
responseBody := `{"usage": {"prompt_tokens": 300, "completion_tokens": 100}}`
|
||||||
|
|
||||||
@@ -832,3 +832,228 @@ func TestMetricsMonitor_WrapHandler_Compression(t *testing.T) {
|
|||||||
assert.Equal(t, 100, metrics[0].OutputTokens)
|
assert.Equal(t, 100, metrics[0].OutputTokens)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReqRespCapture_Size(t *testing.T) {
|
||||||
|
t.Run("calculates size correctly", func(t *testing.T) {
|
||||||
|
capture := ReqRespCapture{
|
||||||
|
ID: 1,
|
||||||
|
ReqPath: "/v1/chat/completions", // 20 bytes
|
||||||
|
ReqHeaders: map[string]string{
|
||||||
|
"Content-Type": "application/json", // 12 + 16 = 28
|
||||||
|
},
|
||||||
|
ReqBody: []byte("request body"), // 12 bytes
|
||||||
|
RespHeaders: map[string]string{
|
||||||
|
"X-Test": "value", // 6 + 5 = 11
|
||||||
|
},
|
||||||
|
RespBody: []byte("response body"), // 13 bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
// Expected: 20 + 12 + 13 + 28 + 11 = 84
|
||||||
|
assert.Equal(t, 84, capture.Size())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("handles empty capture", func(t *testing.T) {
|
||||||
|
capture := ReqRespCapture{}
|
||||||
|
assert.Equal(t, 0, capture.Size())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMetricsMonitor_AddCapture(t *testing.T) {
|
||||||
|
t.Run("does nothing when captures disabled", func(t *testing.T) {
|
||||||
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
|
capture := ReqRespCapture{
|
||||||
|
ID: 0,
|
||||||
|
ReqBody: []byte("test"),
|
||||||
|
}
|
||||||
|
mm.addCapture(capture)
|
||||||
|
|
||||||
|
// Should not store capture
|
||||||
|
assert.Nil(t, mm.getCaptureByID(0))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("adds capture when enabled", func(t *testing.T) {
|
||||||
|
mm := newMetricsMonitor(testLogger, 10, 5)
|
||||||
|
|
||||||
|
capture := ReqRespCapture{
|
||||||
|
ID: 0,
|
||||||
|
ReqBody: []byte("test request"),
|
||||||
|
RespBody: []byte("test response"),
|
||||||
|
}
|
||||||
|
mm.addCapture(capture)
|
||||||
|
|
||||||
|
retrieved := mm.getCaptureByID(0)
|
||||||
|
assert.NotNil(t, retrieved)
|
||||||
|
assert.Equal(t, 0, retrieved.ID)
|
||||||
|
assert.Equal(t, []byte("test request"), retrieved.ReqBody)
|
||||||
|
assert.Equal(t, []byte("test response"), retrieved.RespBody)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("evicts oldest when exceeding max size", func(t *testing.T) {
|
||||||
|
mm := newMetricsMonitor(testLogger, 10, 5)
|
||||||
|
mm.maxCaptureSize = 100 // Set small limit for test
|
||||||
|
|
||||||
|
// Add captures that will exceed the limit
|
||||||
|
capture1 := ReqRespCapture{ID: 0, ReqBody: make([]byte, 40)}
|
||||||
|
capture2 := ReqRespCapture{ID: 1, ReqBody: make([]byte, 40)}
|
||||||
|
capture3 := ReqRespCapture{ID: 2, ReqBody: make([]byte, 40)}
|
||||||
|
|
||||||
|
mm.addCapture(capture1)
|
||||||
|
mm.addCapture(capture2)
|
||||||
|
// Adding capture3 should evict capture1
|
||||||
|
mm.addCapture(capture3)
|
||||||
|
|
||||||
|
assert.Nil(t, mm.getCaptureByID(0), "capture 0 should be evicted")
|
||||||
|
assert.NotNil(t, mm.getCaptureByID(1), "capture 1 should exist")
|
||||||
|
assert.NotNil(t, mm.getCaptureByID(2), "capture 2 should exist")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("skips capture larger than max size", func(t *testing.T) {
|
||||||
|
mm := newMetricsMonitor(testLogger, 10, 5)
|
||||||
|
mm.maxCaptureSize = 100
|
||||||
|
|
||||||
|
// Add a capture larger than max
|
||||||
|
largeCapture := ReqRespCapture{ID: 0, ReqBody: make([]byte, 200)}
|
||||||
|
mm.addCapture(largeCapture)
|
||||||
|
|
||||||
|
assert.Nil(t, mm.getCaptureByID(0), "oversized capture should not be stored")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMetricsMonitor_GetCaptureByID(t *testing.T) {
|
||||||
|
t.Run("returns nil for non-existent ID", func(t *testing.T) {
|
||||||
|
mm := newMetricsMonitor(testLogger, 10, 5)
|
||||||
|
|
||||||
|
assert.Nil(t, mm.getCaptureByID(999))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("returns capture by ID", func(t *testing.T) {
|
||||||
|
mm := newMetricsMonitor(testLogger, 10, 5)
|
||||||
|
|
||||||
|
capture := ReqRespCapture{
|
||||||
|
ID: 42,
|
||||||
|
ReqBody: []byte("test"),
|
||||||
|
}
|
||||||
|
mm.addCapture(capture)
|
||||||
|
|
||||||
|
retrieved := mm.getCaptureByID(42)
|
||||||
|
assert.NotNil(t, retrieved)
|
||||||
|
assert.Equal(t, 42, retrieved.ID)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedactHeaders(t *testing.T) {
|
||||||
|
t.Run("redacts sensitive headers", func(t *testing.T) {
|
||||||
|
headers := map[string]string{
|
||||||
|
"Authorization": "Bearer secret-token",
|
||||||
|
"Proxy-Authorization": "Basic creds",
|
||||||
|
"Cookie": "session=abc123",
|
||||||
|
"Set-Cookie": "session=xyz789",
|
||||||
|
"X-Api-Key": "sk-12345",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"X-Custom": "safe-value",
|
||||||
|
}
|
||||||
|
|
||||||
|
redactHeaders(headers)
|
||||||
|
|
||||||
|
assert.Equal(t, "[REDACTED]", headers["Authorization"])
|
||||||
|
assert.Equal(t, "[REDACTED]", headers["Proxy-Authorization"])
|
||||||
|
assert.Equal(t, "[REDACTED]", headers["Cookie"])
|
||||||
|
assert.Equal(t, "[REDACTED]", headers["Set-Cookie"])
|
||||||
|
assert.Equal(t, "[REDACTED]", headers["X-Api-Key"])
|
||||||
|
assert.Equal(t, "application/json", headers["Content-Type"])
|
||||||
|
assert.Equal(t, "safe-value", headers["X-Custom"])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("handles mixed case header names", func(t *testing.T) {
|
||||||
|
headers := map[string]string{
|
||||||
|
"authorization": "Bearer token",
|
||||||
|
"COOKIE": "session=abc",
|
||||||
|
"x-api-key": "key123",
|
||||||
|
}
|
||||||
|
|
||||||
|
redactHeaders(headers)
|
||||||
|
|
||||||
|
assert.Equal(t, "[REDACTED]", headers["authorization"])
|
||||||
|
assert.Equal(t, "[REDACTED]", headers["COOKIE"])
|
||||||
|
assert.Equal(t, "[REDACTED]", headers["x-api-key"])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("handles empty headers", func(t *testing.T) {
|
||||||
|
headers := map[string]string{}
|
||||||
|
redactHeaders(headers)
|
||||||
|
assert.Empty(t, headers)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMetricsMonitor_WrapHandler_Capture(t *testing.T) {
|
||||||
|
t.Run("captures request and response when enabled", func(t *testing.T) {
|
||||||
|
mm := newMetricsMonitor(testLogger, 10, 5)
|
||||||
|
|
||||||
|
requestBody := `{"model": "test", "prompt": "hello"}`
|
||||||
|
responseBody := `{"usage": {"prompt_tokens": 100, "completion_tokens": 50}}`
|
||||||
|
|
||||||
|
nextHandler := func(modelID string, w http.ResponseWriter, r *http.Request) error {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Header().Set("X-Custom", "header-value")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte(responseBody))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
req := httptest.NewRequest("POST", "/test", bytes.NewBufferString(requestBody))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Authorization", "Bearer secret")
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
ginCtx, _ := gin.CreateTestContext(rec)
|
||||||
|
|
||||||
|
err := mm.wrapHandler("test-model", ginCtx.Writer, req, nextHandler)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Check metric was recorded
|
||||||
|
metrics := mm.getMetrics()
|
||||||
|
assert.Equal(t, 1, len(metrics))
|
||||||
|
metricID := metrics[0].ID
|
||||||
|
|
||||||
|
// Check capture was stored with same ID
|
||||||
|
capture := mm.getCaptureByID(metricID)
|
||||||
|
assert.NotNil(t, capture)
|
||||||
|
assert.Equal(t, metricID, capture.ID)
|
||||||
|
assert.Equal(t, []byte(requestBody), capture.ReqBody)
|
||||||
|
assert.Equal(t, []byte(responseBody), capture.RespBody)
|
||||||
|
assert.Equal(t, "/test", capture.ReqPath)
|
||||||
|
assert.Equal(t, "application/json", capture.ReqHeaders["Content-Type"])
|
||||||
|
assert.Equal(t, "[REDACTED]", capture.ReqHeaders["Authorization"])
|
||||||
|
assert.Equal(t, "application/json", capture.RespHeaders["Content-Type"])
|
||||||
|
assert.Equal(t, "header-value", capture.RespHeaders["X-Custom"])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("does not capture when disabled", func(t *testing.T) {
|
||||||
|
mm := newMetricsMonitor(testLogger, 10, 0)
|
||||||
|
|
||||||
|
requestBody := `{"model": "test"}`
|
||||||
|
responseBody := `{"usage": {"prompt_tokens": 100, "completion_tokens": 50}}`
|
||||||
|
|
||||||
|
nextHandler := func(modelID string, w http.ResponseWriter, r *http.Request) error {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte(responseBody))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
req := httptest.NewRequest("POST", "/test", bytes.NewBufferString(requestBody))
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
ginCtx, _ := gin.CreateTestContext(rec)
|
||||||
|
|
||||||
|
err := mm.wrapHandler("test-model", ginCtx.Writer, req, nextHandler)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Metrics should still be recorded
|
||||||
|
metrics := mm.getMetrics()
|
||||||
|
assert.Equal(t, 1, len(metrics))
|
||||||
|
|
||||||
|
// But no capture
|
||||||
|
capture := mm.getCaptureByID(metrics[0].ID)
|
||||||
|
assert.Nil(t, capture)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -151,7 +151,7 @@ func New(proxyConfig config.Config) *ProxyManager {
|
|||||||
muxLogger: muxLogger,
|
muxLogger: muxLogger,
|
||||||
upstreamLogger: upstreamLogger,
|
upstreamLogger: upstreamLogger,
|
||||||
|
|
||||||
metricsMonitor: newMetricsMonitor(proxyLogger, maxMetrics),
|
metricsMonitor: newMetricsMonitor(proxyLogger, maxMetrics, proxyConfig.CaptureBuffer),
|
||||||
|
|
||||||
processGroups: make(map[string]*ProcessGroup),
|
processGroups: make(map[string]*ProcessGroup),
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -31,6 +32,7 @@ func addApiHandlers(pm *ProxyManager) {
|
|||||||
apiGroup.GET("/events", pm.apiSendEvents)
|
apiGroup.GET("/events", pm.apiSendEvents)
|
||||||
apiGroup.GET("/metrics", pm.apiGetMetrics)
|
apiGroup.GET("/metrics", pm.apiGetMetrics)
|
||||||
apiGroup.GET("/version", pm.apiGetVersion)
|
apiGroup.GET("/version", pm.apiGetVersion)
|
||||||
|
apiGroup.GET("/captures/:id", pm.apiGetCapture)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,3 +252,20 @@ func (pm *ProxyManager) apiGetVersion(c *gin.Context) {
|
|||||||
"build_date": pm.buildDate,
|
"build_date": pm.buildDate,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pm *ProxyManager) apiGetCapture(c *gin.Context) {
|
||||||
|
idStr := c.Param("id")
|
||||||
|
id, err := strconv.Atoi(idStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid capture ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
capture := pm.metricsMonitor.getCaptureByID(id)
|
||||||
|
if capture == nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "capture not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, capture)
|
||||||
|
}
|
||||||
|
|||||||
Generated
+7
@@ -925,6 +925,7 @@
|
|||||||
"integrity": "sha512-Y1Cs7hhTc+a5E9Va/xwKlAJoariQyHY+5zBgCZg4PFWNYQ1nMN9sjK1zhw1gK69DuqVP++sht/1GZg1aRwmAXQ==",
|
"integrity": "sha512-Y1Cs7hhTc+a5E9Va/xwKlAJoariQyHY+5zBgCZg4PFWNYQ1nMN9sjK1zhw1gK69DuqVP++sht/1GZg1aRwmAXQ==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"peer": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sveltejs/vite-plugin-svelte-inspector": "^4.0.1",
|
"@sveltejs/vite-plugin-svelte-inspector": "^4.0.1",
|
||||||
"debug": "^4.4.1",
|
"debug": "^4.4.1",
|
||||||
@@ -1307,6 +1308,7 @@
|
|||||||
"integrity": "sha512-t7frlewr6+cbx+9Ohpl0NOTKXZNV9xHRmNOvql47BFJKcEG1CxtxlPEEe+gR9uhVWM4DwhnvTF110mIL4yP9RA==",
|
"integrity": "sha512-t7frlewr6+cbx+9Ohpl0NOTKXZNV9xHRmNOvql47BFJKcEG1CxtxlPEEe+gR9uhVWM4DwhnvTF110mIL4yP9RA==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"peer": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"undici-types": "~7.16.0"
|
"undici-types": "~7.16.0"
|
||||||
}
|
}
|
||||||
@@ -1439,6 +1441,7 @@
|
|||||||
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz",
|
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz",
|
||||||
"integrity": "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==",
|
"integrity": "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"peer": true,
|
||||||
"bin": {
|
"bin": {
|
||||||
"acorn": "bin/acorn"
|
"acorn": "bin/acorn"
|
||||||
},
|
},
|
||||||
@@ -3449,6 +3452,7 @@
|
|||||||
"integrity": "sha512-e5lPJi/aui4TO1LpAXIRLySmwXSE8k3b9zoGfd42p67wzxog4WHjiZF3M2uheQih4DGyc25QEV4yRBbpueNiUA==",
|
"integrity": "sha512-e5lPJi/aui4TO1LpAXIRLySmwXSE8k3b9zoGfd42p67wzxog4WHjiZF3M2uheQih4DGyc25QEV4yRBbpueNiUA==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"peer": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/estree": "1.0.8"
|
"@types/estree": "1.0.8"
|
||||||
},
|
},
|
||||||
@@ -3561,6 +3565,7 @@
|
|||||||
"resolved": "https://registry.npmjs.org/svelte/-/svelte-5.48.5.tgz",
|
"resolved": "https://registry.npmjs.org/svelte/-/svelte-5.48.5.tgz",
|
||||||
"integrity": "sha512-NB3o70OxfmnE5UPyLr8uH3IV02Q43qJVAuWigYmsSOYsS0s/rHxP0TF81blG0onF/xkhNvZw4G8NfzIX+By5ZQ==",
|
"integrity": "sha512-NB3o70OxfmnE5UPyLr8uH3IV02Q43qJVAuWigYmsSOYsS0s/rHxP0TF81blG0onF/xkhNvZw4G8NfzIX+By5ZQ==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"peer": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@jridgewell/remapping": "^2.3.4",
|
"@jridgewell/remapping": "^2.3.4",
|
||||||
"@jridgewell/sourcemap-codec": "^1.5.0",
|
"@jridgewell/sourcemap-codec": "^1.5.0",
|
||||||
@@ -3716,6 +3721,7 @@
|
|||||||
"integrity": "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ==",
|
"integrity": "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
|
"peer": true,
|
||||||
"bin": {
|
"bin": {
|
||||||
"tsc": "bin/tsc",
|
"tsc": "bin/tsc",
|
||||||
"tsserver": "bin/tsserver"
|
"tsserver": "bin/tsserver"
|
||||||
@@ -3894,6 +3900,7 @@
|
|||||||
"integrity": "sha512-+Oxm7q9hDoLMyJOYfUYBuHQo+dkAloi33apOPP56pzj+vsdJDzr+j1NISE5pyaAuKL4A3UD34qd0lx5+kfKp2g==",
|
"integrity": "sha512-+Oxm7q9hDoLMyJOYfUYBuHQo+dkAloi33apOPP56pzj+vsdJDzr+j1NISE5pyaAuKL4A3UD34qd0lx5+kfKp2g==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"peer": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"esbuild": "^0.25.0",
|
"esbuild": "^0.25.0",
|
||||||
"fdir": "^6.4.4",
|
"fdir": "^6.4.4",
|
||||||
|
|||||||
@@ -0,0 +1,452 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import type { ReqRespCapture } from "../lib/types";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
capture: ReqRespCapture | null;
|
||||||
|
open: boolean;
|
||||||
|
onclose: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
let { capture, open, onclose }: Props = $props();
|
||||||
|
|
||||||
|
let dialogEl: HTMLDialogElement | undefined = $state();
|
||||||
|
|
||||||
|
type BodyTab = "raw" | "pretty" | "chat";
|
||||||
|
let reqBodyTab: BodyTab = $state("pretty");
|
||||||
|
let respBodyTab: BodyTab = $state("pretty");
|
||||||
|
let copiedReq = $state(false);
|
||||||
|
let copiedResp = $state(false);
|
||||||
|
|
||||||
|
$effect(() => {
|
||||||
|
if (open && dialogEl) {
|
||||||
|
dialogEl.showModal();
|
||||||
|
} else if (!open && dialogEl) {
|
||||||
|
dialogEl.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Reset tabs when capture changes
|
||||||
|
$effect(() => {
|
||||||
|
if (capture) {
|
||||||
|
const reqCt = getContentType(capture.req_headers);
|
||||||
|
const respCt = getContentType(capture.resp_headers);
|
||||||
|
reqBodyTab = reqCt.includes("json") ? "pretty" : "raw";
|
||||||
|
respBodyTab = respCt.includes("text/event-stream")
|
||||||
|
? "chat"
|
||||||
|
: respCt.includes("json")
|
||||||
|
? "pretty"
|
||||||
|
: "raw";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function handleDialogClose() {
|
||||||
|
onclose();
|
||||||
|
}
|
||||||
|
|
||||||
|
function decodeBody(body: string | null | undefined): string {
|
||||||
|
if (!body) return "";
|
||||||
|
try {
|
||||||
|
const binary = atob(body);
|
||||||
|
const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0));
|
||||||
|
return new TextDecoder().decode(bytes);
|
||||||
|
} catch {
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatJson(str: string): string {
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(str);
|
||||||
|
return JSON.stringify(parsed, null, 2);
|
||||||
|
} catch {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getContentType(
|
||||||
|
headers: Record<string, string> | null | undefined,
|
||||||
|
): string {
|
||||||
|
if (!headers) return "";
|
||||||
|
const ct = headers["Content-Type"] || headers["content-type"] || "";
|
||||||
|
return ct.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
function isImageContentType(contentType: string): boolean {
|
||||||
|
return contentType.startsWith("image/");
|
||||||
|
}
|
||||||
|
|
||||||
|
function isTextContentType(contentType: string): boolean {
|
||||||
|
return (
|
||||||
|
contentType.startsWith("text/") ||
|
||||||
|
contentType.includes("application/json") ||
|
||||||
|
contentType.includes("application/xml") ||
|
||||||
|
contentType.includes("application/javascript")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getImageDataUrl(body: string, contentType: string): string {
|
||||||
|
const mimeType = contentType.split(";")[0].trim();
|
||||||
|
return `data:${mimeType};base64,${body}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SSEChat {
|
||||||
|
reasoning: string;
|
||||||
|
content: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseSSEChat(text: string): SSEChat {
|
||||||
|
const result: SSEChat = { reasoning: "", content: "" };
|
||||||
|
for (const line of text.split("\n")) {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
if (!trimmed || !trimmed.startsWith("data: ")) continue;
|
||||||
|
const data = trimmed.slice(6);
|
||||||
|
if (data === "[DONE]") continue;
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(data);
|
||||||
|
const delta = parsed.choices?.[0]?.delta;
|
||||||
|
if (delta?.content) result.content += delta.content;
|
||||||
|
if (delta?.reasoning_content) result.reasoning += delta.reasoning_content;
|
||||||
|
} catch {
|
||||||
|
// skip unparseable lines
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function copyToClipboard(text: string, type: "req" | "resp") {
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(text);
|
||||||
|
if (type === "req") {
|
||||||
|
copiedReq = true;
|
||||||
|
setTimeout(() => (copiedReq = false), 1500);
|
||||||
|
} else {
|
||||||
|
copiedResp = true;
|
||||||
|
setTimeout(() => (copiedResp = false), 1500);
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCopyText(): string {
|
||||||
|
if (respBodyTab === "chat") {
|
||||||
|
let text = "";
|
||||||
|
if (sseChat.reasoning) text += sseChat.reasoning + "\n\n";
|
||||||
|
text += sseChat.content;
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
return displayedResponseBody;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Request body derivations
|
||||||
|
let requestContentType = $derived(
|
||||||
|
capture ? getContentType(capture.req_headers) : "",
|
||||||
|
);
|
||||||
|
let isRequestJson = $derived(requestContentType.includes("json"));
|
||||||
|
|
||||||
|
let requestBodyRaw = $derived.by(() => {
|
||||||
|
if (!capture) return "";
|
||||||
|
return decodeBody(capture.req_body);
|
||||||
|
});
|
||||||
|
|
||||||
|
let requestBodyPretty = $derived.by(() => {
|
||||||
|
if (!isRequestJson) return requestBodyRaw;
|
||||||
|
return formatJson(requestBodyRaw);
|
||||||
|
});
|
||||||
|
|
||||||
|
let displayedRequestBody = $derived(
|
||||||
|
reqBodyTab === "pretty" ? requestBodyPretty : requestBodyRaw,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Response body derivations
|
||||||
|
let responseContentType = $derived(
|
||||||
|
capture ? getContentType(capture.resp_headers) : "",
|
||||||
|
);
|
||||||
|
let isResponseImage = $derived(isImageContentType(responseContentType));
|
||||||
|
let isResponseText = $derived(isTextContentType(responseContentType));
|
||||||
|
let isResponseJson = $derived(responseContentType.includes("json"));
|
||||||
|
let isSSE = $derived(responseContentType.includes("text/event-stream"));
|
||||||
|
|
||||||
|
let responseBodyRaw = $derived.by(() => {
|
||||||
|
if (!capture) return "";
|
||||||
|
return decodeBody(capture.resp_body);
|
||||||
|
});
|
||||||
|
|
||||||
|
let responseBodyPretty = $derived.by(() => {
|
||||||
|
if (!isResponseJson) return responseBodyRaw;
|
||||||
|
return formatJson(responseBodyRaw);
|
||||||
|
});
|
||||||
|
|
||||||
|
let sseChat = $derived.by(() => {
|
||||||
|
if (!isSSE || !responseBodyRaw)
|
||||||
|
return { reasoning: "", content: "" } as SSEChat;
|
||||||
|
return parseSSEChat(responseBodyRaw);
|
||||||
|
});
|
||||||
|
|
||||||
|
let displayedResponseBody = $derived.by(() => {
|
||||||
|
if (respBodyTab === "pretty") return responseBodyPretty;
|
||||||
|
return responseBodyRaw;
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<dialog
|
||||||
|
bind:this={dialogEl}
|
||||||
|
onclose={handleDialogClose}
|
||||||
|
class="bg-surface text-txtmain rounded-lg shadow-xl max-w-4xl w-full max-h-[90vh] p-0 backdrop:bg-black/50 m-auto"
|
||||||
|
>
|
||||||
|
{#if capture}
|
||||||
|
<div class="flex flex-col max-h-[90vh]">
|
||||||
|
<div
|
||||||
|
class="flex justify-between items-center p-4 border-b border-card-border"
|
||||||
|
>
|
||||||
|
<h2 class="text-xl font-bold pb-0">Capture #{capture.id + 1}{#if capture.req_path} <span class="text-base font-mono font-normal text-txtsecondary">{capture.req_path}</span>{/if}</h2>
|
||||||
|
<button
|
||||||
|
onclick={() => dialogEl?.close()}
|
||||||
|
class="text-txtsecondary hover:text-txtmain text-2xl leading-none"
|
||||||
|
>
|
||||||
|
×
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="overflow-y-auto flex-1 p-4 space-y-4">
|
||||||
|
<!-- Request Headers -->
|
||||||
|
<details class="group" open>
|
||||||
|
<summary
|
||||||
|
class="cursor-pointer font-semibold text-sm uppercase tracking-wider text-txtsecondary hover:text-txtmain"
|
||||||
|
>
|
||||||
|
Request Headers
|
||||||
|
</summary>
|
||||||
|
<div
|
||||||
|
class="mt-2 bg-background rounded border border-card-border overflow-auto max-h-48"
|
||||||
|
>
|
||||||
|
<table class="w-full text-sm">
|
||||||
|
<tbody>
|
||||||
|
{#each Object.entries(capture.req_headers || {}) as [key, value]}
|
||||||
|
<tr class="border-b border-card-border-inner last:border-0">
|
||||||
|
<td class="px-3 py-1 font-mono text-primary whitespace-nowrap"
|
||||||
|
>{key}</td
|
||||||
|
>
|
||||||
|
<td class="px-3 py-1 font-mono break-all">{value}</td>
|
||||||
|
</tr>
|
||||||
|
{/each}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<!-- Request Body -->
|
||||||
|
<details class="group" open>
|
||||||
|
<summary
|
||||||
|
class="cursor-pointer font-semibold text-sm uppercase tracking-wider text-txtsecondary hover:text-txtmain"
|
||||||
|
>
|
||||||
|
Request Body
|
||||||
|
</summary>
|
||||||
|
{#if requestBodyRaw}
|
||||||
|
<div class="mt-2 flex items-center justify-between">
|
||||||
|
<div class="flex gap-1">
|
||||||
|
{#if isRequestJson}
|
||||||
|
<button
|
||||||
|
class="tab-btn"
|
||||||
|
class:tab-btn-active={reqBodyTab === "pretty"}
|
||||||
|
onclick={() => (reqBodyTab = "pretty")}>Pretty</button
|
||||||
|
>
|
||||||
|
<button
|
||||||
|
class="tab-btn"
|
||||||
|
class:tab-btn-active={reqBodyTab === "raw"}
|
||||||
|
onclick={() => (reqBodyTab = "raw")}>Raw</button
|
||||||
|
>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
class="tab-btn"
|
||||||
|
onclick={() =>
|
||||||
|
copyToClipboard(displayedRequestBody, "req")}
|
||||||
|
>
|
||||||
|
{#if copiedReq}
|
||||||
|
Copied!
|
||||||
|
{:else}
|
||||||
|
Copy
|
||||||
|
{/if}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
class="mt-1 bg-background rounded border border-card-border overflow-auto max-h-96"
|
||||||
|
>
|
||||||
|
<pre
|
||||||
|
class="p-3 text-sm font-mono whitespace-pre-wrap break-all">{displayedRequestBody}</pre>
|
||||||
|
</div>
|
||||||
|
{:else}
|
||||||
|
<div
|
||||||
|
class="mt-2 bg-background rounded border border-card-border overflow-auto max-h-96"
|
||||||
|
>
|
||||||
|
<pre class="p-3 text-sm font-mono whitespace-pre-wrap break-all"
|
||||||
|
>(empty)</pre
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<!-- Response Headers -->
|
||||||
|
<details class="group" open>
|
||||||
|
<summary
|
||||||
|
class="cursor-pointer font-semibold text-sm uppercase tracking-wider text-txtsecondary hover:text-txtmain"
|
||||||
|
>
|
||||||
|
Response Headers
|
||||||
|
</summary>
|
||||||
|
<div
|
||||||
|
class="mt-2 bg-background rounded border border-card-border overflow-auto max-h-48"
|
||||||
|
>
|
||||||
|
<table class="w-full text-sm">
|
||||||
|
<tbody>
|
||||||
|
{#each Object.entries(capture.resp_headers || {}) as [key, value]}
|
||||||
|
<tr class="border-b border-card-border-inner last:border-0">
|
||||||
|
<td class="px-3 py-1 font-mono text-primary whitespace-nowrap"
|
||||||
|
>{key}</td
|
||||||
|
>
|
||||||
|
<td class="px-3 py-1 font-mono break-all">{value}</td>
|
||||||
|
</tr>
|
||||||
|
{/each}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<!-- Response Body -->
|
||||||
|
<details class="group" open>
|
||||||
|
<summary
|
||||||
|
class="cursor-pointer font-semibold text-sm uppercase tracking-wider text-txtsecondary hover:text-txtmain"
|
||||||
|
>
|
||||||
|
Response Body
|
||||||
|
</summary>
|
||||||
|
{#if isResponseImage && capture.resp_body}
|
||||||
|
<div
|
||||||
|
class="mt-2 bg-background rounded border border-card-border overflow-auto max-h-96"
|
||||||
|
>
|
||||||
|
<div class="p-3 flex justify-center">
|
||||||
|
<img
|
||||||
|
src={getImageDataUrl(capture.resp_body, responseContentType)}
|
||||||
|
alt="Response"
|
||||||
|
class="max-w-full h-auto"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{:else if isSSE || isResponseText}
|
||||||
|
<div class="mt-2 flex items-center justify-between">
|
||||||
|
<div class="flex gap-1">
|
||||||
|
{#if isSSE}
|
||||||
|
<button
|
||||||
|
class="tab-btn"
|
||||||
|
class:tab-btn-active={respBodyTab === "chat"}
|
||||||
|
onclick={() => (respBodyTab = "chat")}>Chat</button
|
||||||
|
>
|
||||||
|
{/if}
|
||||||
|
{#if isResponseJson}
|
||||||
|
<button
|
||||||
|
class="tab-btn"
|
||||||
|
class:tab-btn-active={respBodyTab === "pretty"}
|
||||||
|
onclick={() => (respBodyTab = "pretty")}>Pretty</button
|
||||||
|
>
|
||||||
|
{/if}
|
||||||
|
{#if isSSE || isResponseJson}
|
||||||
|
<button
|
||||||
|
class="tab-btn"
|
||||||
|
class:tab-btn-active={respBodyTab === "raw"}
|
||||||
|
onclick={() => (respBodyTab = "raw")}>Raw</button
|
||||||
|
>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
class="tab-btn"
|
||||||
|
onclick={() => copyToClipboard(getCopyText(), "resp")}
|
||||||
|
>
|
||||||
|
{#if copiedResp}
|
||||||
|
Copied!
|
||||||
|
{:else}
|
||||||
|
Copy
|
||||||
|
{/if}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
class="mt-1 bg-background rounded border border-card-border overflow-auto max-h-96"
|
||||||
|
>
|
||||||
|
{#if respBodyTab === "chat"}
|
||||||
|
<div class="p-3 text-sm space-y-3">
|
||||||
|
{#if sseChat.reasoning}
|
||||||
|
<div>
|
||||||
|
<div
|
||||||
|
class="text-xs font-semibold uppercase tracking-wider text-txtsecondary mb-1"
|
||||||
|
>
|
||||||
|
Reasoning
|
||||||
|
</div>
|
||||||
|
<pre
|
||||||
|
class="font-mono whitespace-pre-wrap break-all text-txtsecondary">{sseChat.reasoning}</pre>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
{#if sseChat.content}
|
||||||
|
<div>
|
||||||
|
{#if sseChat.reasoning}
|
||||||
|
<div
|
||||||
|
class="text-xs font-semibold uppercase tracking-wider text-txtsecondary mb-1"
|
||||||
|
>
|
||||||
|
Response
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
<pre
|
||||||
|
class="font-mono whitespace-pre-wrap break-all">{sseChat.content}</pre>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
{#if !sseChat.reasoning && !sseChat.content}
|
||||||
|
<pre class="font-mono">(empty)</pre>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
{:else}
|
||||||
|
<pre
|
||||||
|
class="p-3 text-sm font-mono whitespace-pre-wrap break-all">{displayedResponseBody || "(empty)"}</pre>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
{:else if responseBodyRaw}
|
||||||
|
<div
|
||||||
|
class="mt-2 bg-background rounded border border-card-border overflow-auto max-h-96"
|
||||||
|
>
|
||||||
|
<div class="p-3 text-sm text-txtsecondary italic">
|
||||||
|
(binary data - {responseContentType || "unknown content type"})
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{:else}
|
||||||
|
<div
|
||||||
|
class="mt-2 bg-background rounded border border-card-border overflow-auto max-h-96"
|
||||||
|
>
|
||||||
|
<pre class="p-3 text-sm font-mono">(empty)</pre>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</details>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="p-4 border-t border-card-border flex justify-end">
|
||||||
|
<button onclick={() => dialogEl?.close()} class="btn"> Close </button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</dialog>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.tab-btn {
|
||||||
|
padding: 2px 10px;
|
||||||
|
font-size: 0.75rem;
|
||||||
|
border-radius: 4px;
|
||||||
|
color: var(--color-txtsecondary);
|
||||||
|
cursor: pointer;
|
||||||
|
border: 1px solid transparent;
|
||||||
|
background: transparent;
|
||||||
|
transition: all 0.15s;
|
||||||
|
}
|
||||||
|
.tab-btn:hover {
|
||||||
|
color: var(--color-txtmain);
|
||||||
|
background: var(--color-secondary);
|
||||||
|
}
|
||||||
|
.tab-btn-active {
|
||||||
|
color: var(--color-primary);
|
||||||
|
background: color-mix(in srgb, var(--color-primary) 12%, transparent);
|
||||||
|
border-color: color-mix(in srgb, var(--color-primary) 25%, transparent);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -21,6 +21,16 @@ export interface Metrics {
|
|||||||
prompt_per_second: number;
|
prompt_per_second: number;
|
||||||
tokens_per_second: number;
|
tokens_per_second: number;
|
||||||
duration_ms: number;
|
duration_ms: number;
|
||||||
|
has_capture: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ReqRespCapture {
|
||||||
|
id: number;
|
||||||
|
req_path: string;
|
||||||
|
req_headers: Record<string, string>;
|
||||||
|
req_body: string; // base64 encoded bytes
|
||||||
|
resp_headers: Record<string, string>;
|
||||||
|
resp_body: string; // base64 encoded bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LogData {
|
export interface LogData {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { metrics } from "../stores/api";
|
import { metrics, getCapture } from "../stores/api";
|
||||||
import Tooltip from "../components/Tooltip.svelte";
|
import Tooltip from "../components/Tooltip.svelte";
|
||||||
|
import CaptureDialog from "../components/CaptureDialog.svelte";
|
||||||
|
import type { ReqRespCapture } from "../lib/types";
|
||||||
|
|
||||||
function formatSpeed(speed: number): string {
|
function formatSpeed(speed: number): string {
|
||||||
return speed < 0 ? "unknown" : speed.toFixed(2) + " t/s";
|
return speed < 0 ? "unknown" : speed.toFixed(2) + " t/s";
|
||||||
@@ -38,6 +40,25 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
let sortedMetrics = $derived([...$metrics].sort((a, b) => b.id - a.id));
|
let sortedMetrics = $derived([...$metrics].sort((a, b) => b.id - a.id));
|
||||||
|
|
||||||
|
let selectedCapture = $state<ReqRespCapture | null>(null);
|
||||||
|
let dialogOpen = $state(false);
|
||||||
|
let loadingCaptureId = $state<number | null>(null);
|
||||||
|
|
||||||
|
async function viewCapture(id: number) {
|
||||||
|
loadingCaptureId = id;
|
||||||
|
const capture = await getCapture(id);
|
||||||
|
loadingCaptureId = null;
|
||||||
|
if (capture) {
|
||||||
|
selectedCapture = capture;
|
||||||
|
dialogOpen = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeDialog() {
|
||||||
|
dialogOpen = false;
|
||||||
|
selectedCapture = null;
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="p-2">
|
<div class="p-2">
|
||||||
@@ -65,6 +86,7 @@
|
|||||||
<th class="px-6 py-3">Prompt Processing</th>
|
<th class="px-6 py-3">Prompt Processing</th>
|
||||||
<th class="px-6 py-3">Generation Speed</th>
|
<th class="px-6 py-3">Generation Speed</th>
|
||||||
<th class="px-6 py-3">Duration</th>
|
<th class="px-6 py-3">Duration</th>
|
||||||
|
<th class="px-6 py-3">Capture</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody class="divide-y">
|
<tbody class="divide-y">
|
||||||
@@ -79,6 +101,19 @@
|
|||||||
<td class="px-6 py-4">{formatSpeed(metric.prompt_per_second)}</td>
|
<td class="px-6 py-4">{formatSpeed(metric.prompt_per_second)}</td>
|
||||||
<td class="px-6 py-4">{formatSpeed(metric.tokens_per_second)}</td>
|
<td class="px-6 py-4">{formatSpeed(metric.tokens_per_second)}</td>
|
||||||
<td class="px-6 py-4">{formatDuration(metric.duration_ms)}</td>
|
<td class="px-6 py-4">{formatDuration(metric.duration_ms)}</td>
|
||||||
|
<td class="px-6 py-4">
|
||||||
|
{#if metric.has_capture}
|
||||||
|
<button
|
||||||
|
onclick={() => viewCapture(metric.id)}
|
||||||
|
disabled={loadingCaptureId === metric.id}
|
||||||
|
class="btn btn--sm"
|
||||||
|
>
|
||||||
|
{loadingCaptureId === metric.id ? "..." : "View"}
|
||||||
|
</button>
|
||||||
|
{:else}
|
||||||
|
<span class="text-txtsecondary">-</span>
|
||||||
|
{/if}
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{/each}
|
{/each}
|
||||||
</tbody>
|
</tbody>
|
||||||
@@ -86,3 +121,5 @@
|
|||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<CaptureDialog capture={selectedCapture} open={dialogOpen} onclose={closeDialog} />
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { writable } from "svelte/store";
|
import { writable } from "svelte/store";
|
||||||
import type { Model, Metrics, VersionInfo, LogData, APIEventEnvelope } from "../lib/types";
|
import type { Model, Metrics, VersionInfo, LogData, APIEventEnvelope, ReqRespCapture } from "../lib/types";
|
||||||
import { connectionState } from "./theme";
|
import { connectionState } from "./theme";
|
||||||
|
|
||||||
const LOG_LENGTH_LIMIT = 1024 * 100; /* 100KB of log data */
|
const LOG_LENGTH_LIMIT = 1024 * 100; /* 100KB of log data */
|
||||||
@@ -172,3 +172,19 @@ export async function loadModel(model: string): Promise<void> {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getCapture(id: number): Promise<ReqRespCapture | null> {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/captures/${id}`);
|
||||||
|
if (response.status === 404) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to fetch capture: ${response.status}`);
|
||||||
|
}
|
||||||
|
return await response.json();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to fetch capture:", error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user