From f9f7995ebea3e131f7e95fec4702d58a9f8c9cb6 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Fri, 24 Jul 2026 12:25:52 +0000 Subject: [PATCH] [proxy] emit cost-audit logs unconditionally at warn severity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Drop the IsLevelEnabled pre-checks around the cost-audit trail: the lines keep WARN severity, but emission is no longer constrained by a level gate on our side — filtering is left entirely to the logger's own configuration. When the middleware context carries no logger, fall back to the process-wide standard logger instead of skipping, so the audit trail never silently disappears. --- proxy/internal/llm/pricing/pricing.go | 18 ++++++------------ .../builtin/cost_meter/middleware.go | 16 +++++++++------- .../builtin/llm_response_parser/middleware.go | 16 +++++++++------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/proxy/internal/llm/pricing/pricing.go b/proxy/internal/llm/pricing/pricing.go index e40d51922..332830582 100644 --- a/proxy/internal/llm/pricing/pricing.go +++ b/proxy/internal/llm/pricing/pricing.go @@ -169,10 +169,8 @@ func (t *Table) Cost(provider, model string, inTokens, outTokens, cachedInput, c nonCached := float64(inTokens-clamped) / 1000.0 * entry.InputPer1K cached := float64(clamped) / 1000.0 * cachedRate total := nonCached + cached + output - if log.IsLevelEnabled(log.WarnLevel) { - log.Warnf("pricing %s/%s: non_cached_input %d/1000×$%v=$%.6f + cached_input %d/1000×$%v=$%.6f + output %d/1000×$%v=$%.6f => $%.6f", - provider, model, inTokens-clamped, entry.InputPer1K, nonCached, clamped, cachedRate, cached, outTokens, entry.OutputPer1K, output, total) - } + log.Warnf("pricing %s/%s: non_cached_input %d/1000×$%v=$%.6f + cached_input %d/1000×$%v=$%.6f + output %d/1000×$%v=$%.6f => $%.6f", + provider, model, inTokens-clamped, entry.InputPer1K, nonCached, clamped, cachedRate, cached, outTokens, entry.OutputPer1K, output, total) return total, true case "anthropic", "bedrock": // Bedrock-Anthropic returns the same additive cache buckets as @@ -190,18 +188,14 @@ func (t *Table) Cost(provider, model string, inTokens, outTokens, cachedInput, c read := float64(cachedInput) / 1000.0 * readRate create := float64(cacheCreation) / 1000.0 * createRate total := input + read + create + output - if log.IsLevelEnabled(log.WarnLevel) { - log.Warnf("pricing %s/%s: input %d/1000×$%v=$%.6f + cache_read %d/1000×$%v=$%.6f + cache_creation %d/1000×$%v=$%.6f + output %d/1000×$%v=$%.6f => $%.6f", - provider, model, inTokens, entry.InputPer1K, input, cachedInput, readRate, read, cacheCreation, createRate, create, outTokens, entry.OutputPer1K, output, total) - } + log.Warnf("pricing %s/%s: input %d/1000×$%v=$%.6f + cache_read %d/1000×$%v=$%.6f + cache_creation %d/1000×$%v=$%.6f + output %d/1000×$%v=$%.6f => $%.6f", + provider, model, inTokens, entry.InputPer1K, input, cachedInput, readRate, read, cacheCreation, createRate, create, outTokens, entry.OutputPer1K, output, total) return total, true default: input := float64(inTokens) / 1000.0 * entry.InputPer1K total := input + output - if log.IsLevelEnabled(log.WarnLevel) { - log.Warnf("pricing %s/%s: input %d/1000×$%v=$%.6f + output %d/1000×$%v=$%.6f => $%.6f", - provider, model, inTokens, entry.InputPer1K, input, outTokens, entry.OutputPer1K, output, total) - } + log.Warnf("pricing %s/%s: input %d/1000×$%v=$%.6f + output %d/1000×$%v=$%.6f => $%.6f", + provider, model, inTokens, entry.InputPer1K, input, outTokens, entry.OutputPer1K, output, total) return total, true } } diff --git a/proxy/internal/middleware/builtin/cost_meter/middleware.go b/proxy/internal/middleware/builtin/cost_meter/middleware.go index 480dc4f0a..033e5c195 100644 --- a/proxy/internal/middleware/builtin/cost_meter/middleware.go +++ b/proxy/internal/middleware/builtin/cost_meter/middleware.go @@ -166,15 +166,17 @@ func (m *Middleware) Invoke(_ context.Context, in *middleware.Input) (*middlewar return out, nil } -// auditLogger returns the proxy logger when warn-level logging is enabled, -// nil otherwise. Cost-audit logging is emitted at WARN so it is visible on -// default production log levels while the cost pipeline is being verified. +// auditLogger returns the logger the cost-audit lines are emitted on. The +// lines carry WARN severity so they surface on default production log +// levels, but emission is not gated on any level check here — filtering is +// left entirely to the logger's own configuration. Falls back to the +// process-wide standard logger when the middleware context carries none, so +// the audit trail never silently disappears. func auditLogger() *log.Logger { - logger := builtin.Context().Logger - if logger == nil || !logger.IsLevelEnabled(log.WarnLevel) { - return nil + if logger := builtin.Context().Logger; logger != nil { + return logger } - return logger + return log.StandardLogger() } // skip returns a single-entry metadata slice carrying the given skip diff --git a/proxy/internal/middleware/builtin/llm_response_parser/middleware.go b/proxy/internal/middleware/builtin/llm_response_parser/middleware.go index 1cb46e606..1cbcfbd5a 100644 --- a/proxy/internal/middleware/builtin/llm_response_parser/middleware.go +++ b/proxy/internal/middleware/builtin/llm_response_parser/middleware.go @@ -155,15 +155,17 @@ func (m *Middleware) Invoke(_ context.Context, in *middleware.Input) (*middlewar // envelope without flooding the log with multi-megabyte completions. const debugLogRawBytes = 4096 -// auditLogger returns the proxy logger when warn-level logging is enabled, -// nil otherwise. Cost-audit logging is emitted at WARN so it is visible on -// default production log levels while the cost pipeline is being verified. +// auditLogger returns the logger the cost-audit lines are emitted on. The +// lines carry WARN severity so they surface on default production log +// levels, but emission is not gated on any level check here — filtering is +// left entirely to the logger's own configuration. Falls back to the +// process-wide standard logger when the middleware context carries none, so +// the audit trail never silently disappears. func auditLogger() *log.Logger { - logger := builtin.Context().Logger - if logger == nil || !logger.IsLevelEnabled(log.WarnLevel) { - return nil + if logger := builtin.Context().Logger; logger != nil { + return logger } - return logger + return log.StandardLogger() } // logRawResponse debug-logs the (decompressed) upstream response body so an