From 5f88e5da5d03adc5092c73e838af9e5025325513 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 8 Nov 2020 09:56:11 +0900 Subject: [PATCH] Improve observability on FCM errors (#542) A caller can now observe errors returned from FCM server such as an unauthorized error (401). --- gorush/notification_fcm.go | 28 ++++++++++++++++++++++++++++ gorush/notification_fcm_test.go | 5 +++++ 2 files changed, 33 insertions(+) diff --git a/gorush/notification_fcm.go b/gorush/notification_fcm.go index ca28e05..8a3d155 100644 --- a/gorush/notification_fcm.go +++ b/gorush/notification_fcm.go @@ -141,6 +141,34 @@ Retry: if err != nil { // Send Message error LogError.Error("FCM server send message error: " + err.Error()) + + if req.IsTopic() { + if PushConf.Core.Sync { + req.AddLog(getLogPushEntry(FailedPush, req.To, req, err)) + } else if PushConf.Core.FeedbackURL != "" { + go func(logger *logrus.Logger, log LogPushEntry, url string, timeout int64) { + err := DispatchFeedback(log, url, timeout) + if err != nil { + logger.Error(err) + } + }(LogError, getLogPushEntry(FailedPush, req.To, req, err), PushConf.Core.FeedbackURL, PushConf.Core.FeedbackTimeout) + } + StatStorage.AddAndroidError(1) + } else { + for _, token := range req.Tokens { + if PushConf.Core.Sync { + req.AddLog(getLogPushEntry(FailedPush, token, req, err)) + } else if PushConf.Core.FeedbackURL != "" { + go func(logger *logrus.Logger, log LogPushEntry, url string, timeout int64) { + err := DispatchFeedback(log, url, timeout) + if err != nil { + logger.Error(err) + } + }(LogError, getLogPushEntry(FailedPush, token, req, err), PushConf.Core.FeedbackURL, PushConf.Core.FeedbackTimeout) + } + } + StatStorage.AddAndroidError(int64(len(req.Tokens))) + } return } diff --git a/gorush/notification_fcm_test.go b/gorush/notification_fcm_test.go index 7a35610..530ef26 100644 --- a/gorush/notification_fcm_test.go +++ b/gorush/notification_fcm_test.go @@ -102,6 +102,7 @@ func TestPushToAndroidRightTokenForStringLog(t *testing.T) { func TestOverwriteAndroidAPIKey(t *testing.T) { PushConf, _ = config.LoadConf("") + PushConf.Core.Sync = true PushConf.Android.Enabled = true PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") @@ -113,10 +114,14 @@ func TestOverwriteAndroidAPIKey(t *testing.T) { Message: "Welcome", // overwrite android api key APIKey: "1234", + + log: &[]LogPushEntry{}, } // FCM server error: 401 error: 401 Unauthorized (Wrong API Key) PushToAndroid(req) + + assert.Len(t, *req.log, 2) } func TestFCMMessage(t *testing.T) {