Documentation
¶
Index ¶
- func CompressRequestBody(body []byte) ([]byte, string, error)
- func InitDebugLogger(dir, fileName string)
- func InitDetailedLogger(dir string)
- func NewCompressionWriter(w http.ResponseWriter, r *http.Request) *compressionWriter
- type DetailedLogger
- func (dl *DetailedLogger) Close() error
- func (dl *DetailedLogger) Log(msg string)
- func (dl *DetailedLogger) LogAuthHeaderConstruction(scheme string, headerName string, keySet bool)
- func (dl *DetailedLogger) LogEnvVarLoad(envKey string, found bool, value string)
- func (dl *DetailedLogger) LogError(context string, err error)
- func (dl *DetailedLogger) LogPolicyDetails(baseKeyEnv, providerName, upstreamURL string)
- func (dl *DetailedLogger) LogPolicyLookup(tokenHash string, found bool)
- func (dl *DetailedLogger) LogProxyShutdown()
- func (dl *DetailedLogger) LogProxyStartup(httpPort, httpsPort int, tlsEnabled bool)
- func (dl *DetailedLogger) LogRequestComplete(method, path, model, provider string, ...)
- func (dl *DetailedLogger) LogTokenExtraction(tokenSource string, tokenHash string)
- func (dl *DetailedLogger) LogUpstreamCall(method, url, authScheme string)
- func (dl *DetailedLogger) LogUpstreamResponse(statusCode int, statusText string, duration time.Duration)
- func (dl *DetailedLogger) Logf(format string, args ...interface{})
- type Handler
- func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (h *Handler) SetDebugLogDir(dir string)
- func (h *Handler) SetDefaultProvider(provider string)
- func (h *Handler) SetLedger(l *ledger.Ledger)
- func (h *Handler) SetLogging(cfg config.LoggingConfig)
- func (h *Handler) SetRedisMemoryWriter(w session.MemoryWriter)
- func (h *Handler) Stats() *UsageStats
- type OpenClawMetadata
- type RateLimiter
- func (rl *RateLimiter) Acquire(tokenHash string, cfg *policy.RateLimitConfig)
- func (rl *RateLimiter) Allow(tokenHash string, cfg *policy.RateLimitConfig) error
- func (rl *RateLimiter) RecordTokens(tokenHash string, cfg *policy.RateLimitConfig, tokens int)
- func (rl *RateLimiter) Release(tokenHash string, cfg *policy.RateLimitConfig)
- type RequestLog
- type RuleMatchLog
- type SessionEntry
- type UsageEntry
- type UsageStats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompressRequestBody ¶ added in v0.1.2
CompressRequestBody compresses a request body using brotli (preferred) or gzip. Returns the compressed body, encoding used ("br" or "gzip"), and any error. If compression is not beneficial or fails, returns the original body with empty encoding. Minimum size threshold: 1 KB (don't compress very small bodies).
func InitDebugLogger ¶
func InitDebugLogger(dir, fileName string)
InitDebugLogger initializes the debug logger with the configured directory
func InitDetailedLogger ¶
func InitDetailedLogger(dir string)
InitDetailedLogger reinitializes the global detailed logger with a configured directory
func NewCompressionWriter ¶ added in v0.1.2
func NewCompressionWriter(w http.ResponseWriter, r *http.Request) *compressionWriter
NewCompressionWriter creates a new compression wrapper based on Accept-Encoding header. It supports brotli (preferred) and gzip encoding.
Types ¶
type DetailedLogger ¶
type DetailedLogger struct {
}
DetailedLogger writes detailed logs via the shared proxy logger.
func GetDetailedLogger ¶
func GetDetailedLogger() *DetailedLogger
GetDetailedLogger returns the global detailed logger
func NewDetailedLogger ¶
func NewDetailedLogger() *DetailedLogger
NewDetailedLogger creates a detailed logger using shared logging.
func NewDetailedLoggerWithDir ¶
func NewDetailedLoggerWithDir(dir string) *DetailedLogger
NewDetailedLoggerWithDir creates a detailed logger. Directory is accepted for compatibility with existing call sites.
func (*DetailedLogger) LogAuthHeaderConstruction ¶
func (dl *DetailedLogger) LogAuthHeaderConstruction(scheme string, headerName string, keySet bool)
LogAuthHeaderConstruction logs auth header construction
func (*DetailedLogger) LogEnvVarLoad ¶
func (dl *DetailedLogger) LogEnvVarLoad(envKey string, found bool, value string)
LogEnvVarLoad logs environment variable loading
func (*DetailedLogger) LogError ¶
func (dl *DetailedLogger) LogError(context string, err error)
LogError logs an error
func (*DetailedLogger) LogPolicyDetails ¶
func (dl *DetailedLogger) LogPolicyDetails(baseKeyEnv, providerName, upstreamURL string)
LogPolicyDetails logs policy details
func (*DetailedLogger) LogPolicyLookup ¶
func (dl *DetailedLogger) LogPolicyLookup(tokenHash string, found bool)
LogPolicyLookup logs policy lookup
func (*DetailedLogger) LogProxyShutdown ¶
func (dl *DetailedLogger) LogProxyShutdown()
LogProxyShutdown logs proxy shutdown
func (*DetailedLogger) LogProxyStartup ¶
func (dl *DetailedLogger) LogProxyStartup(httpPort, httpsPort int, tlsEnabled bool)
LogProxyStartup logs proxy startup
func (*DetailedLogger) LogRequestComplete ¶
func (dl *DetailedLogger) LogRequestComplete(method, path, model, provider string, inputTokens, outputTokens, statusCode int)
LogRequestComplete logs a completed request
func (*DetailedLogger) LogTokenExtraction ¶
func (dl *DetailedLogger) LogTokenExtraction(tokenSource string, tokenHash string)
LogTokenExtraction logs token extraction
func (*DetailedLogger) LogUpstreamCall ¶
func (dl *DetailedLogger) LogUpstreamCall(method, url, authScheme string)
LogUpstreamCall logs upstream API call
func (*DetailedLogger) LogUpstreamResponse ¶
func (dl *DetailedLogger) LogUpstreamResponse(statusCode int, statusText string, duration time.Duration)
LogUpstreamResponse logs upstream response
func (*DetailedLogger) Logf ¶
func (dl *DetailedLogger) Logf(format string, args ...interface{})
Logf writes a formatted message
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
func NewHandler ¶
func (*Handler) SetDebugLogDir ¶
SetDebugLogDir sets the directory for debug logs from the configured path.
func (*Handler) SetDefaultProvider ¶
SetDefaultProvider sets the default provider to use when policies don't specify one.
func (*Handler) SetLogging ¶
func (h *Handler) SetLogging(cfg config.LoggingConfig)
SetLogging configures logging behavior from the config.
func (*Handler) SetRedisMemoryWriter ¶
func (h *Handler) SetRedisMemoryWriter(w session.MemoryWriter)
SetRedisMemoryWriter sets the Redis-backed memory writer for session logging.
func (*Handler) Stats ¶
func (h *Handler) Stats() *UsageStats
Stats returns the UsageStats for registering the stats endpoint.
type OpenClawMetadata ¶ added in v0.1.1
type OpenClawMetadata struct {
AgentID string
AgentType string
Team string
Channel string
Skill string
Environment string
}
OpenClawMetadata holds optional metadata from OpenClaw agents
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter enforces per-token rate limits using sliding or fixed windows.
func NewRateLimiter ¶
func NewRateLimiter() *RateLimiter
func (*RateLimiter) Acquire ¶
func (rl *RateLimiter) Acquire(tokenHash string, cfg *policy.RateLimitConfig)
Acquire increments the parallel request counter. Call Release when done.
func (*RateLimiter) Allow ¶
func (rl *RateLimiter) Allow(tokenHash string, cfg *policy.RateLimitConfig) error
Allow checks if a request is allowed under the rate limit. Returns nil if allowed, or an error describing which limit was hit.
func (*RateLimiter) RecordTokens ¶
func (rl *RateLimiter) RecordTokens(tokenHash string, cfg *policy.RateLimitConfig, tokens int)
RecordTokens records token usage for token-based rate limiting.
func (*RateLimiter) Release ¶
func (rl *RateLimiter) Release(tokenHash string, cfg *policy.RateLimitConfig)
Release decrements the parallel request counter.
type RequestLog ¶
type RequestLog struct {
Timestamp string `json:"timestamp"`
Method string `json:"method"`
Path string `json:"path"`
TokenHash string `json:"token_hash"`
Model string `json:"model,omitempty"`
BaseKeyEnv string `json:"base_key_env"`
UpstreamURL string `json:"upstream_url"`
StatusCode int `json:"status_code"`
DurationMs int64 `json:"duration_ms"`
InputTokens int `json:"input_tokens,omitempty"`
OutputTokens int `json:"output_tokens,omitempty"`
Stream bool `json:"stream,omitempty"`
Error string `json:"error,omitempty"`
RemoteAddr string `json:"remote_addr"`
UserAgent string `json:"user_agent,omitempty"`
RetryCount int `json:"retry_count,omitempty"`
FallbackModel string `json:"fallback_model,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
// Rule match tracking
RuleMatches []RuleMatchLog `json:"rule_matches,omitempty"` // Non-blocking rule matches (warn, log, mask)
// Upstream provider tracking IDs
UpstreamRequestID string `json:"upstream_request_id,omitempty"` // Provider's request ID from response header
UpstreamID string `json:"upstream_id,omitempty"` // Provider's response body ID (chatcmpl-*, msg_*, etc.)
ClientRequestID string `json:"client_request_id,omitempty"` // Our outbound tracking ID sent to provider
}
RequestLog is a structured JSON log entry for each proxied request.
type RuleMatchLog ¶
type RuleMatchLog struct {
Name string `json:"name,omitempty"`
Action string `json:"action"`
Message string `json:"message"`
}
RuleMatchLog records a non-blocking rule match in the request log.
type SessionEntry ¶
type SessionEntry struct {
TokenHash string `json:"token_hash"`
RequestCount int64 `json:"request_count"`
InputTokens int64 `json:"input_tokens"`
OutputTokens int64 `json:"output_tokens"`
TotalTokens int64 `json:"total_tokens"`
ErrorCount int64 `json:"error_count"`
LastModel string `json:"last_model"`
BaseKeyEnv string `json:"base_key_env"`
FirstSeen time.Time `json:"first_seen"`
LastSeen time.Time `json:"last_seen"`
}
SessionEntry holds usage for a specific wrapper token (session).
type UsageEntry ¶
type UsageEntry struct {
Model string `json:"model"`
BaseKeyEnv string `json:"base_key_env"`
RequestCount int64 `json:"request_count"`
InputTokens int64 `json:"input_tokens"`
OutputTokens int64 `json:"output_tokens"`
TotalTokens int64 `json:"total_tokens"`
ErrorCount int64 `json:"error_count"`
}
UsageEntry holds aggregated usage for a model/key combination.
type UsageStats ¶
type UsageStats struct {
// contains filtered or unexported fields
}
UsageStats tracks token usage by model/key and by token hash.
func NewUsageStats ¶
func NewUsageStats() *UsageStats
func (*UsageStats) Record ¶
func (u *UsageStats) Record(tokenHash, model, baseKeyEnv string, inputTokens, outputTokens int, isError bool)
Record adds a request's usage to both aggregated stats and per-token session stats.
func (*UsageStats) SessionSnapshot ¶
func (u *UsageStats) SessionSnapshot() []SessionEntry
SessionSnapshot returns a copy of all per-token session entries.
func (*UsageStats) Snapshot ¶
func (u *UsageStats) Snapshot() []UsageEntry
Snapshot returns a copy of all current usage entries.
func (*UsageStats) StatsHandler ¶
func (u *UsageStats) StatsHandler(w http.ResponseWriter, r *http.Request)
StatsHandler serves the /stats endpoint.