From f7f343fe55e393b11de206a3b37d722f3e10e960 Mon Sep 17 00:00:00 2001 From: binwiederhier Date: Sat, 25 Feb 2023 15:31:12 -0500 Subject: [PATCH] Logging fixes --- docs/releases.md | 2 ++ server/log.go | 4 ++++ server/server.go | 12 +++++++----- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/docs/releases.md b/docs/releases.md index 3034980..b6f267e 100644 --- a/docs/releases.md +++ b/docs/releases.md @@ -6,6 +6,8 @@ and the [ntfy Android app](https://github.com/binwiederhier/ntfy-android/release This release changes the way UnifiedPush (UP) topics are rate limited from publisher-based rate limiting to subscriber-based rate limiting. This allows UP application servers to send higher volumes, since the subscribers carry the rate limits. +However, it also means that UP clients have to subscribe to a topic first before they are allowed to publish. If they do +no, clients will receive an HTTP 507 response from the server. We also fixed another issue with UnifiedPush: Some Mastodon servers were sending unsupported `Authorization` headers, which ntfy rejected with an HTTP 401. We now ignore unsupported header values. diff --git a/server/log.go b/server/log.go index d385821..0425768 100644 --- a/server/log.go +++ b/server/log.go @@ -30,6 +30,10 @@ const ( tagMatrix = "matrix" ) +var ( + normalErrorCodes = []int{http.StatusNotFound, http.StatusBadRequest, http.StatusTooManyRequests, http.StatusUnauthorized, http.StatusInsufficientStorage} +) + // logr creates a new log event with HTTP request fields func logr(r *http.Request) *log.Event { return log.Tag(tagHTTP).Fields(httpContext(r)) // Tag may be overwritten diff --git a/server/server.go b/server/server.go index 4be299f..50a4723 100644 --- a/server/server.go +++ b/server/server.go @@ -319,19 +319,21 @@ func (s *Server) handleError(w http.ResponseWriter, r *http.Request, v *visitor, if !ok { httpErr = errHTTPInternalError } - isNormalError := strings.Contains(err.Error(), "i/o timeout") || util.Contains([]int{http.StatusNotFound, http.StatusBadRequest, http.StatusTooManyRequests, http.StatusUnauthorized}, httpErr.HTTPCode) + isNormalError := strings.Contains(err.Error(), "i/o timeout") || util.Contains(normalErrorCodes, httpErr.HTTPCode) + ev := logvr(v, r).Err(err) if websocket.IsWebSocketUpgrade(r) { + ev.Tag(tagWebsocket).Fields(websocketErrorContext(err)) if isNormalError { - logvr(v, r).Tag(tagWebsocket).Err(err).Fields(websocketErrorContext(err)).Debug("WebSocket error (this error is okay, it happens a lot): %s", err.Error()) + ev.Debug("WebSocket error (this error is okay, it happens a lot): %s", err.Error()) } else { - logvr(v, r).Tag(tagWebsocket).Err(err).Fields(websocketErrorContext(err)).Info("WebSocket error: %s", err.Error()) + ev.Info("WebSocket error: %s", err.Error()) } return // Do not attempt to write to upgraded connection } if isNormalError { - logvr(v, r).Err(err).Debug("Connection closed with HTTP %d (ntfy error %d)", httpErr.HTTPCode, httpErr.Code) + ev.Debug("Connection closed with HTTP %d (ntfy error %d)", httpErr.HTTPCode, httpErr.Code) } else { - logvr(v, r).Err(err).Info("Connection closed with HTTP %d (ntfy error %d)", httpErr.HTTPCode, httpErr.Code) + ev.Info("Connection closed with HTTP %d (ntfy error %d)", httpErr.HTTPCode, httpErr.Code) } w.Header().Set("Content-Type", "application/json") w.Header().Set("Access-Control-Allow-Origin", s.config.AccessControlAllowOrigin) // CORS, allow cross-origin requests