[proxy] emit cost-audit logs unconditionally at warn severity

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.
This commit is contained in:
Maycon Santos
2026-07-24 12:25:52 +00:00
parent cf0c9ca857
commit f9f7995ebe
3 changed files with 24 additions and 26 deletions

View File

@@ -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
}
}

View File

@@ -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

View File

@@ -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