Skip to content

Commit 4111539

Browse files
committed
Change logAsError to logLevel in carbonapi
This way, we can support WARN on carbonapi access logs. It is used to log 499 responses as they may indicate issues.
1 parent 870c9b8 commit 4111539

File tree

3 files changed

+62
-54
lines changed

3 files changed

+62
-54
lines changed

app/carbonapi/app.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"expvar"
66
"fmt"
7+
"go.uber.org/zap/zapcore"
78
"net"
89
"net/http"
910
"os"
@@ -383,7 +384,7 @@ func setUpConfig(app *App, logger *zap.Logger) {
383384

384385
}
385386

386-
func (app *App) deferredAccessLogging(accessLogger *zap.Logger, r *http.Request, accessLogDetails *carbonapipb.AccessLogDetails, t time.Time, logAsError bool) {
387+
func (app *App) deferredAccessLogging(accessLogger *zap.Logger, r *http.Request, accessLogDetails *carbonapipb.AccessLogDetails, t time.Time, level zapcore.Level) {
387388
accessLogDetails.Runtime = time.Since(t).Seconds()
388389
accessLogDetails.RequestMethod = r.Method
389390

@@ -392,20 +393,18 @@ func (app *App) deferredAccessLogging(accessLogger *zap.Logger, r *http.Request,
392393
accessLogger.Error("could not marshal access log details", zap.Error(err))
393394
}
394395
var logMsg string
395-
if accessLogDetails.HttpCode/100 == 2 {
396+
if accessLogDetails.HttpCode/100 < 4 {
396397
logMsg = "request served"
398+
apiMetrics.Responses.Add(1)
399+
} else if accessLogDetails.HttpCode/100 == 4 {
400+
logMsg = "request failed with client error"
401+
apiMetrics.Responses.Add(1)
397402
} else {
398-
logMsg = "request failed"
399-
}
400-
// TODO (grzkv) This logic is not obvious for the user
401-
if logAsError {
402-
accessLogger.Error(logMsg, fields...)
403+
logMsg = "request failed with server error"
403404
apiMetrics.Errors.Add(1)
404-
} else {
405-
// TODO (grzkv) The code can differ from the real one. Clean up
406-
// accessLogDetails.HttpCode = http.StatusOK
407-
accessLogger.Info(logMsg, fields...)
408-
apiMetrics.Responses.Add(1)
405+
}
406+
if ce := accessLogger.Check(level, logMsg); ce != nil {
407+
ce.Write(fields...)
409408
}
410409

411410
if app != nil {

0 commit comments

Comments
 (0)