diff --git a/server/api.go b/server/api.go index 05f0821e1..7d4a1a8f2 100644 --- a/server/api.go +++ b/server/api.go @@ -114,7 +114,9 @@ func (a *API) processActivity(w http.ResponseWriter, req *http.Request) { continue } - a.p.metricsService.ObserveChangeEventTotal(activity.ChangeType) + if a.p.metricsService != nil { + a.p.metricsService.ObserveChangeEventTotal(activity.ChangeType) + } if err := a.p.activityHandler.Handle(activity); err != nil { a.p.API.LogError("Unable to process created activity", "activity", activity, "error", err.Error()) errors += err.Error() + "\n" @@ -153,7 +155,9 @@ func (a *API) processLifecycle(w http.ResponseWriter, req *http.Request) { a.p.API.LogError("Invalid webhook secret received in lifecycle event") continue } - a.p.metricsService.ObserveLifecycleEventTotal(event.LifecycleEvent) + if a.p.metricsService != nil { + a.p.metricsService.ObserveLifecycleEventTotal(event.LifecycleEvent) + } a.p.activityHandler.HandleLifecycleEvent(event) } diff --git a/server/command.go b/server/command.go index 3ce91bc47..01d8dd5f4 100644 --- a/server/command.go +++ b/server/command.go @@ -203,7 +203,9 @@ func (p *Plugin) executeLinkCommand(args *model.CommandArgs, parameters []string return p.cmdError(args.UserId, args.ChannelId, "Unable to subscribe to the channel") } - p.metricsService.ObserveSubscriptionsCount(metrics.SubscriptionConnected) + if p.metricsService != nil { + p.metricsService.ObserveSubscriptionsCount(metrics.SubscriptionConnected) + } if err = p.store.StoreChannelLink(&channelLink); err != nil { p.API.LogDebug("Unable to create the new link", "error", err.Error()) return p.cmdError(args.UserId, args.ChannelId, "Unable to create new link.") diff --git a/server/handlers/attachments.go b/server/handlers/attachments.go index 96b7dab16..cb382175c 100644 --- a/server/handlers/attachments.go +++ b/server/handlers/attachments.go @@ -11,7 +11,7 @@ import ( "strings" "time" - "github.com/mattermost/mattermost-plugin-msteams-sync/server/metrics" + m "github.com/mattermost/mattermost-plugin-msteams-sync/server/metrics" "github.com/mattermost/mattermost-plugin-msteams-sync/server/msteams" "github.com/mattermost/mattermost-server/v6/app/imaging" "github.com/mattermost/mattermost-server/v6/model" @@ -121,6 +121,7 @@ func (ah *ActivityHandler) handleAttachments(channelID, userID, text string, msg isDirectMessage = true } + metrics := ah.plugin.GetMetrics() for _, a := range msg.Attachments { // remove the attachment tags from the text newText = attachRE.ReplaceAllString(newText, "") @@ -152,14 +153,18 @@ func (ah *ActivityHandler) handleAttachments(channelID, userID, text string, msg attachmentData, err = ah.handleDownloadFile(a.ContentURL, client) if err != nil { ah.plugin.GetAPI().LogError("failed to download the file", "filename", a.Name, "error", err.Error()) - ah.plugin.GetMetrics().ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMSTeams, discardedReasonUnableToGetTeamsData, isDirectMessage) + if metrics != nil { + metrics.ObserveFileCount(m.ActionCreated, m.ActionSourceMSTeams, discardedReasonUnableToGetTeamsData, isDirectMessage) + } continue } } else { fileSize, downloadURL, err = client.GetFileSizeAndDownloadURL(a.ContentURL) if err != nil { ah.plugin.GetAPI().LogError("failed to get file size and download URL", "error", err.Error()) - ah.plugin.GetMetrics().ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMSTeams, discardedReasonUnableToGetTeamsData, isDirectMessage) + if metrics != nil { + metrics.ObserveFileCount(m.ActionCreated, m.ActionSourceMSTeams, discardedReasonUnableToGetTeamsData, isDirectMessage) + } continue } @@ -167,7 +172,9 @@ func (ah *ActivityHandler) handleAttachments(channelID, userID, text string, msg if fileSize > fileSizeAllowed { ah.plugin.GetAPI().LogError("skipping file download from MS Teams because the file size is greater than the allowed size") errorFound = true - ah.plugin.GetMetrics().ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMSTeams, discardedReasonMaxFileSizeExceeded, isDirectMessage) + if metrics != nil { + metrics.ObserveFileCount(m.ActionCreated, m.ActionSourceMSTeams, discardedReasonMaxFileSizeExceeded, isDirectMessage) + } continue } @@ -176,7 +183,9 @@ func (ah *ActivityHandler) handleAttachments(channelID, userID, text string, msg attachmentData, err = client.GetFileContent(downloadURL) if err != nil { ah.plugin.GetAPI().LogError("failed to get file content", "error", err.Error()) - ah.plugin.GetMetrics().ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMSTeams, discardedReasonUnableToGetTeamsData, isDirectMessage) + if metrics != nil { + metrics.ObserveFileCount(m.ActionCreated, m.ActionSourceMSTeams, discardedReasonUnableToGetTeamsData, isDirectMessage) + } continue } } @@ -197,15 +206,23 @@ func (ah *ActivityHandler) handleAttachments(channelID, userID, text string, msg } if fileInfoID == "" { - ah.plugin.GetMetrics().ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMSTeams, discardedReasonEmptyFileID, isDirectMessage) + if metrics != nil { + metrics.ObserveFileCount(m.ActionCreated, m.ActionSourceMSTeams, discardedReasonEmptyFileID, isDirectMessage) + } continue } attachments = append(attachments, fileInfoID) - ah.plugin.GetMetrics().ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMSTeams, "", isDirectMessage) + if metrics != nil { + metrics.ObserveFileCount(m.ActionCreated, m.ActionSourceMSTeams, "", isDirectMessage) + } countFileAttachments++ if countFileAttachments == maxFileAttachmentsSupported { ah.plugin.GetAPI().LogDebug("discarding the rest of the attachments as Mattermost supports only 10 attachments per post") - ah.plugin.GetMetrics().ObserveFilesCount(metrics.ActionCreated, metrics.ActionSourceMSTeams, discardedReasonFileLimitReached, isDirectMessage, int64(len(msg.Attachments)-countNonFileAttachments-countFileAttachments)) + if metrics != nil { + // Calculate the count of file attachments discarded by subtracting handled file attachments and other attachments from total message attachments. + fileAttachmentsDiscarded := len(msg.Attachments) - countNonFileAttachments - countFileAttachments + metrics.ObserveFilesCount(m.ActionCreated, m.ActionSourceMSTeams, discardedReasonFileLimitReached, isDirectMessage, int64(fileAttachmentsDiscarded)) + } break } } diff --git a/server/handlers/attachments_test.go b/server/handlers/attachments_test.go index f91981171..a66ebf883 100644 --- a/server/handlers/attachments_test.go +++ b/server/handlers/attachments_test.go @@ -355,7 +355,7 @@ func TestHandleAttachments(t *testing.T) { p.On("GetClientForApp").Return(client).Once() p.On("GetAPI").Return(mockAPI) p.On("GetMaxSizeForCompleteDownload").Return(1).Times(10) - p.On("GetMetrics").Return(mockmetrics).Times(11) + p.On("GetMetrics").Return(mockmetrics).Times(1) }, setupAPI: func(mockAPI *plugintest.API) { mockAPI.On("GetConfig").Return(&model.Config{ @@ -384,6 +384,7 @@ func TestHandleAttachments(t *testing.T) { description: "Attachment type code snippet", setupPlugin: func(p *mocksPlugin.PluginIface, mockAPI *plugintest.API, client *mocksClient.Client, store *mocksStore.Store, mockmetrics *mocksMetrics.Metrics) { p.On("GetClientForApp").Return(client) + p.On("GetMetrics").Return(mockmetrics).Times(1) }, setupAPI: func(mockAPI *plugintest.API) { mockAPI.On("GetConfig").Return(&model.Config{ @@ -409,6 +410,7 @@ func TestHandleAttachments(t *testing.T) { { description: "Attachment type message reference", setupPlugin: func(p *mocksPlugin.PluginIface, mockAPI *plugintest.API, client *mocksClient.Client, store *mocksStore.Store, mockmetrics *mocksMetrics.Metrics) { + p.On("GetMetrics").Return(mockmetrics).Times(1) p.On("GetClientForApp").Return(client) p.On("GetStore").Return(store, nil) p.On("GetAPI").Return(mockAPI) diff --git a/server/handlers/converters_test.go b/server/handlers/converters_test.go index 157c78b07..4a924d140 100644 --- a/server/handlers/converters_test.go +++ b/server/handlers/converters_test.go @@ -7,6 +7,7 @@ import ( "time" mocksPlugin "github.com/mattermost/mattermost-plugin-msteams-sync/server/handlers/mocks" + mocksMetrics "github.com/mattermost/mattermost-plugin-msteams-sync/server/metrics/mocks" "github.com/mattermost/mattermost-plugin-msteams-sync/server/msteams" mocksClient "github.com/mattermost/mattermost-plugin-msteams-sync/server/msteams/mocks" mocksStore "github.com/mattermost/mattermost-plugin-msteams-sync/server/store/mocks" @@ -33,7 +34,7 @@ func TestMsgToPost(t *testing.T) { senderID string message *msteams.Message post *model.Post - setupPlugin func(plugin *mocksPlugin.PluginIface, mockAPI *plugintest.API, client *mocksClient.Client) + setupPlugin func(plugin *mocksPlugin.PluginIface, mockAPI *plugintest.API, client *mocksClient.Client, mockmetrics *mocksMetrics.Metrics) setupAPI func(*plugintest.API) }{ { @@ -47,10 +48,11 @@ func TestMsgToPost(t *testing.T) { UserID: testutils.GetUserID(), CreateAt: msteamsCreateAtTime, }, - setupPlugin: func(p *mocksPlugin.PluginIface, mockAPI *plugintest.API, client *mocksClient.Client) { + setupPlugin: func(p *mocksPlugin.PluginIface, mockAPI *plugintest.API, client *mocksClient.Client, mockmetrics *mocksMetrics.Metrics) { p.On("GetBotUserID").Return(testutils.GetSenderID()) p.On("GetURL").Return("https://example.com/") p.On("GetClientForApp").Return(client) + p.On("GetMetrics").Return(mockmetrics).Times(1) }, setupAPI: func(api *plugintest.API) { api.On("LogDebug", "Unable to get user avatar", "Error", mock.Anything).Once() @@ -75,8 +77,9 @@ func TestMsgToPost(t *testing.T) { ah := ActivityHandler{} client := mocksClient.NewClient(t) mockAPI := &plugintest.API{} + mockMetrics := mocksMetrics.NewMetrics(t) testCase.setupAPI(mockAPI) - testCase.setupPlugin(p, mockAPI, client) + testCase.setupPlugin(p, mockAPI, client, mockMetrics) ah.plugin = p diff --git a/server/handlers/handlers.go b/server/handlers/handlers.go index 8692cf9a2..b7f6dbda8 100644 --- a/server/handlers/handlers.go +++ b/server/handlers/handlers.go @@ -11,7 +11,7 @@ import ( "time" "github.com/enescakir/emoji" - "github.com/mattermost/mattermost-plugin-msteams-sync/server/metrics" + m "github.com/mattermost/mattermost-plugin-msteams-sync/server/metrics" "github.com/mattermost/mattermost-plugin-msteams-sync/server/msteams" "github.com/mattermost/mattermost-plugin-msteams-sync/server/store" "github.com/mattermost/mattermost-plugin-msteams-sync/server/store/storemodels" @@ -51,7 +51,7 @@ const ( type PluginIface interface { GetAPI() plugin.API GetStore() store.Store - GetMetrics() metrics.Metrics + GetMetrics() m.Metrics GetSyncDirectMessages() bool GetSyncGuestUsers() bool GetMaxSizeForCompleteDownload() int @@ -93,15 +93,20 @@ func New(plugin PluginIface) *ActivityHandler { } func (ah *ActivityHandler) Start() { - // This is constant for now, but report it as a metric to future proof dashboards. - ah.plugin.GetMetrics().ObserveChangeEventQueueCapacity(activityQueueSize) + metrics := ah.plugin.GetMetrics() + if metrics != nil { + // This is constant for now, but report it as a metric to future proof dashboards. + metrics.ObserveChangeEventQueueCapacity(activityQueueSize) + } for i := 0; i < numberOfWorkers; i++ { go func() { for { select { case activity := <-ah.queue: - ah.plugin.GetMetrics().DecrementChangeEventQueueLength(activity.ChangeType) + if metrics != nil { + metrics.DecrementChangeEventQueueLength(activity.ChangeType) + } ah.handleActivity(activity) case <-ah.quit: // we have received a signal to stop @@ -122,7 +127,10 @@ func (ah *ActivityHandler) Stop() { func (ah *ActivityHandler) Handle(activity msteams.Activity) error { ah.queue <- activity - ah.plugin.GetMetrics().IncrementChangeEventQueueLength(activity.ChangeType) + metrics := ah.plugin.GetMetrics() + if metrics != nil { + metrics.IncrementChangeEventQueueLength(activity.ChangeType) + } return nil } @@ -137,7 +145,10 @@ func (ah *ActivityHandler) HandleLifecycleEvent(event msteams.Activity) { if err != nil { ah.plugin.GetAPI().LogError("Unable to refresh the subscription", "error", err.Error()) } else { - ah.plugin.GetMetrics().ObserveSubscriptionsCount(metrics.SubscriptionRefreshed) + metrics := ah.plugin.GetMetrics() + if metrics != nil { + metrics.ObserveSubscriptionsCount(m.SubscriptionRefreshed) + } if err = ah.plugin.GetStore().UpdateSubscriptionExpiresOn(event.SubscriptionID, *expiresOn); err != nil { ah.plugin.GetAPI().LogError("Unable to store the subscription new expiry date", "subscriptionID", event.SubscriptionID, "error", err.Error()) } @@ -169,7 +180,9 @@ func (ah *ActivityHandler) handleActivity(activity msteams.Activity) { if activityIds.ChatID == "" { if !ah.checkSubscription(activity.SubscriptionID) { - metrics.ObserveProcessedChangeEventTotal(activity.ChangeType, discardedReasonExpiredSubscription) + if metrics != nil { + metrics.ObserveProcessedChangeEventTotal(activity.ChangeType, discardedReasonExpiredSubscription) + } return } } @@ -187,7 +200,9 @@ func (ah *ActivityHandler) handleActivity(activity msteams.Activity) { ah.plugin.GetAPI().LogError("Unsupported change type", "change_type", activity.ChangeType) } - metrics.ObserveProcessedChangeEventTotal(activity.ChangeType, discardedReason) + if metrics != nil { + metrics.ObserveProcessedChangeEventTotal(activity.ChangeType, discardedReason) + } } func (ah *ActivityHandler) handleCreatedActivity(activityIds msteams.ActivityIds) string { @@ -207,12 +222,17 @@ func (ah *ActivityHandler) handleCreatedActivity(activityIds msteams.ActivityIds return discardedReasonNotUserEvent } + isDirectMessage := IsDirectMessage(activityIds.ChatID) + + metrics := ah.plugin.GetMetrics() // Avoid possible duplication postInfo, _ := ah.plugin.GetStore().GetPostInfoByMSTeamsID(msg.ChatID+msg.ChannelID, msg.ID) if postInfo != nil { ah.plugin.GetAPI().LogDebug("duplicate post") ah.updateLastReceivedChangeDate(msg.LastUpdateAt) - ah.plugin.GetMetrics().ObserveMessagesConfirmedCount(metrics.ActionSourceMattermost, IsDirectMessage(activityIds.ChatID)) + if metrics != nil { + metrics.ObserveMessagesConfirmedCount(m.ActionSourceMattermost, isDirectMessage) + } return discardedReasonDuplicatedPost } @@ -284,8 +304,11 @@ func (ah *ActivityHandler) handleCreatedActivity(activityIds msteams.ActivityIds return discardedReasonOther } + if metrics != nil { + metrics.ObserveMessagesCount(m.ActionCreated, m.ActionSourceMSTeams, isDirectMessage) + } + ah.plugin.GetAPI().LogDebug("Post created") - ah.plugin.GetMetrics().ObserveMessagesCount(metrics.ActionCreated, metrics.ActionSourceMSTeams, IsDirectMessage(activityIds.ChatID)) if errorFound { _ = ah.plugin.GetAPI().SendEphemeralPost(senderID, &model.Post{ ChannelId: channelID, @@ -400,7 +423,10 @@ func (ah *ActivityHandler) handleUpdatedActivity(activityIds msteams.ActivityIds } isDirectMessage := IsDirectMessage(activityIds.ChatID) - ah.plugin.GetMetrics().ObserveMessagesCount(metrics.ActionUpdated, metrics.ActionSourceMSTeams, isDirectMessage) + metrics := ah.plugin.GetMetrics() + if metrics != nil { + metrics.ObserveMessagesCount(m.ActionUpdated, m.ActionSourceMSTeams, isDirectMessage) + } ah.updateLastReceivedChangeDate(msg.LastUpdateAt) ah.handleReactions(postInfo.MattermostID, channelID, isDirectMessage, msg.Reactions) return discardedReasonNone @@ -438,13 +464,16 @@ func (ah *ActivityHandler) handleReactions(postID, channelID string, isDirectMes allReactions[reactionUserID+emojiName] = true } + metrics := ah.plugin.GetMetrics() for _, r := range postReactions { if !allReactions[r.UserId+r.EmojiName] { r.ChannelId = "removedfromplugin" if appErr = ah.plugin.GetAPI().RemoveReaction(r); appErr != nil { ah.plugin.GetAPI().LogError("Unable to remove reaction", "error", appErr.Error()) } - ah.plugin.GetMetrics().ObserveReactionsCount(metrics.ReactionUnsetAction, metrics.ActionSourceMSTeams, isDirectMessage) + if metrics != nil { + metrics.ObserveReactionsCount(m.ReactionUnsetAction, m.ActionSourceMSTeams, isDirectMessage) + } } } @@ -474,7 +503,9 @@ func (ah *ActivityHandler) handleReactions(postID, channelID string, isDirectMes continue } ah.plugin.GetAPI().LogDebug("Added reaction", "reaction", r) - ah.plugin.GetMetrics().ObserveReactionsCount(metrics.ReactionSetAction, metrics.ActionSourceMSTeams, isDirectMessage) + if metrics != nil { + metrics.ObserveReactionsCount(m.ReactionSetAction, m.ActionSourceMSTeams, isDirectMessage) + } } } } @@ -495,7 +526,10 @@ func (ah *ActivityHandler) handleDeletedActivity(activityIds msteams.ActivityIds return discardedReasonOther } - ah.plugin.GetMetrics().ObserveMessagesCount(metrics.ActionDeleted, metrics.ActionSourceMSTeams, IsDirectMessage(activityIds.ChatID)) + metrics := ah.plugin.GetMetrics() + if metrics != nil { + metrics.ObserveMessagesCount(m.ActionDeleted, m.ActionSourceMSTeams, IsDirectMessage(activityIds.ChatID)) + } return discardedReasonNone } diff --git a/server/handlers/handlers_test.go b/server/handlers/handlers_test.go index a0ed9dcea..9e3248cb8 100644 --- a/server/handlers/handlers_test.go +++ b/server/handlers/handlers_test.go @@ -162,6 +162,7 @@ func TestHandleCreatedActivity(t *testing.T) { p.On("GetAPI").Return(mockAPI).Times(2) p.On("GetStore").Return(store).Times(2) p.On("GetBotUserID").Return("mock-BotUserID").Times(1) + p.On("GetMetrics").Return(mockmetrics).Times(1) }, setupClient: func(client *mocksClient.Client) { client.On("GetChat", testutils.GetChatID()).Return(&msteams.Chat{ @@ -204,6 +205,7 @@ func TestHandleCreatedActivity(t *testing.T) { p.On("GetStore").Return(store).Times(2) p.On("GetBotUserID").Return("mock-BotUserID").Times(1) p.On("GetSyncDirectMessages").Return(true).Times(1) + p.On("GetMetrics").Return(mockmetrics).Times(1) }, setupClient: func(client *mocksClient.Client) { client.On("GetChat", testutils.GetChatID()).Return(&msteams.Chat{ @@ -248,6 +250,7 @@ func TestHandleCreatedActivity(t *testing.T) { p.On("GetStore").Return(store).Times(5) p.On("GetBotUserID").Return("mock-BotUserID").Times(1) p.On("GetSyncDirectMessages").Return(true).Times(1) + p.On("GetMetrics").Return(mockmetrics).Times(1) }, setupClient: func(client *mocksClient.Client) { client.On("GetChat", testutils.GetChatID()).Return(&msteams.Chat{ @@ -297,6 +300,7 @@ func TestHandleCreatedActivity(t *testing.T) { p.On("GetStore").Return(store).Times(5) p.On("GetBotUserID").Return("mock-BotUserID").Times(3) p.On("GetSyncDirectMessages").Return(true).Times(1) + p.On("GetMetrics").Return(mockmetrics).Times(2) }, setupClient: func(client *mocksClient.Client) { client.On("GetChat", testutils.GetChatID()).Return(&msteams.Chat{ @@ -348,7 +352,7 @@ func TestHandleCreatedActivity(t *testing.T) { p.On("GetStore").Return(store).Times(6) p.On("GetBotUserID").Return("mock-BotUserID").Times(3) p.On("GetSyncDirectMessages").Return(true).Times(1) - p.On("GetMetrics").Return(mockmetrics).Times(1) + p.On("GetMetrics").Return(mockmetrics).Times(2) }, setupClient: func(client *mocksClient.Client) { client.On("GetChat", testutils.GetChatID()).Return(&msteams.Chat{ @@ -409,7 +413,7 @@ func TestHandleCreatedActivity(t *testing.T) { p.On("GetStore").Return(store).Times(6) p.On("GetBotUserID").Return("mock-BotUserID").Times(3) p.On("GetSyncDirectMessages").Return(true).Times(1) - p.On("GetMetrics").Return(mockmetrics).Times(1) + p.On("GetMetrics").Return(mockmetrics).Times(2) }, setupClient: func(client *mocksClient.Client) { client.On("GetChat", testutils.GetChatID()).Return(&msteams.Chat{ @@ -468,7 +472,7 @@ func TestHandleCreatedActivity(t *testing.T) { p.On("GetAPI").Return(mockAPI).Times(5) p.On("GetStore").Return(store).Times(5) p.On("GetBotUserID").Return("mock-BotUserID").Times(3) - p.On("GetMetrics").Return(mockmetrics).Times(1) + p.On("GetMetrics").Return(mockmetrics).Times(2) }, setupClient: func(client *mocksClient.Client) { client.On("GetMessage", "mockTeamID", testutils.GetChannelID(), testutils.GetMessageID()).Return(&msteams.Message{ @@ -802,7 +806,7 @@ func TestHandleUpdatedActivity(t *testing.T) { p.On("GetBotUserID").Return("mock-BotUserID").Times(1) p.On("GetBotUserID").Return(testutils.GetSenderID()).Times(2) p.On("GetSyncDirectMessages").Return(true).Once() - p.On("GetMetrics").Return(mockmetrics).Times(1) + p.On("GetMetrics").Return(mockmetrics).Times(2) }, setupClient: func(client *mocksClient.Client) { client.On("GetChat", testutils.GetChatID()).Return(&msteams.Chat{ @@ -856,7 +860,7 @@ func TestHandleUpdatedActivity(t *testing.T) { p.On("GetStore").Return(store).Times(4) p.On("GetBotUserID").Return("mock-BotUserID").Times(1) p.On("GetBotUserID").Return(testutils.GetSenderID()).Times(2) - p.On("GetMetrics").Return(mockmetrics).Times(1) + p.On("GetMetrics").Return(mockmetrics).Times(2) }, setupClient: func(client *mocksClient.Client) { client.On("GetMessage", "mockTeamID", testutils.GetChannelID(), testutils.GetMessageID()).Return(&msteams.Message{ diff --git a/server/message_hooks.go b/server/message_hooks.go index 495613550..5181637c9 100644 --- a/server/message_hooks.go +++ b/server/message_hooks.go @@ -252,7 +252,9 @@ func (p *Plugin) SetChatReaction(teamsMessageID, srcUser, channelID, emojiName s return txErr } - p.metricsService.ObserveReactionsCount(metrics.ReactionSetAction, metrics.ActionSourceMattermost, true) + if p.metricsService != nil { + p.metricsService.ObserveReactionsCount(metrics.ReactionSetAction, metrics.ActionSourceMattermost, true) + } } else { teamsMessage, txErr = client.GetChatMessage(chatID, teamsMessageID) if txErr != nil { @@ -325,7 +327,9 @@ func (p *Plugin) SetReaction(teamID, channelID, userID string, post *model.Post, return txErr } - p.metricsService.ObserveReactionsCount(metrics.ReactionSetAction, metrics.ActionSourceMattermost, false) + if p.metricsService != nil { + p.metricsService.ObserveReactionsCount(metrics.ReactionSetAction, metrics.ActionSourceMattermost, false) + } } else { teamsMessage, txErr = getUpdatedMessage(teamID, channelID, parentID, postInfo.MSTeamsID, client) if txErr != nil { @@ -390,7 +394,9 @@ func (p *Plugin) UnsetChatReaction(teamsMessageID, srcUser, channelID string, em return txErr } - p.metricsService.ObserveReactionsCount(metrics.ReactionUnsetAction, metrics.ActionSourceMattermost, true) + if p.metricsService != nil { + p.metricsService.ObserveReactionsCount(metrics.ReactionUnsetAction, metrics.ActionSourceMattermost, true) + } if txErr = p.store.SetPostLastUpdateAtByMSTeamsID(tx, teamsMessageID, teamsMessage.LastUpdateAt); txErr != nil { p.API.LogWarn("Error updating the msteams/mattermost post link metadata", "error", txErr.Error()) } @@ -453,7 +459,9 @@ func (p *Plugin) UnsetReaction(teamID, channelID, userID string, post *model.Pos return txErr } - p.metricsService.ObserveReactionsCount(metrics.ReactionUnsetAction, metrics.ActionSourceMattermost, false) + if p.metricsService != nil { + p.metricsService.ObserveReactionsCount(metrics.ReactionUnsetAction, metrics.ActionSourceMattermost, false) + } if txErr = p.store.SetPostLastUpdateAtByMattermostID(tx, postInfo.MattermostID, teamsMessage.LastUpdateAt); txErr != nil { p.API.LogWarn("Error updating the msteams/mattermost post link metadata", "error", txErr.Error()) } @@ -509,13 +517,17 @@ func (p *Plugin) SendChat(srcUser string, usersIDs []string, post *model.Post) ( fileInfo, appErr := p.API.GetFileInfo(fileID) if appErr != nil { p.API.LogWarn("Unable to get file info", "error", appErr) - p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, discardedReasonUnableToGetMMData, true) + if p.metricsService != nil { + p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, discardedReasonUnableToGetMMData, true) + } continue } fileData, appErr := p.API.GetFile(fileInfo.Id) if appErr != nil { p.API.LogWarn("Error in getting file attachment from Mattermost", "error", appErr) - p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, discardedReasonUnableToGetMMData, true) + if p.metricsService != nil { + p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, discardedReasonUnableToGetMMData, true) + } continue } @@ -524,11 +536,15 @@ func (p *Plugin) SendChat(srcUser string, usersIDs []string, post *model.Post) ( attachment, err = client.UploadFile("", "", fileName+"_"+fileInfo.Id+fileExtension, int(fileInfo.Size), fileInfo.MimeType, bytes.NewReader(fileData), chat) if err != nil { p.API.LogWarn("Error in uploading file attachment to MS Teams", "error", err) - p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, discardedReasonUnableToUploadFileOnTeams, true) + if p.metricsService != nil { + p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, discardedReasonUnableToUploadFileOnTeams, true) + } continue } attachments = append(attachments, attachment) - p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, "", true) + if p.metricsService != nil { + p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, "", true) + } } md := markdown.New(markdown.XHTMLOutput(true), markdown.Typographer(false)) @@ -550,7 +566,9 @@ func (p *Plugin) SendChat(srcUser string, usersIDs []string, post *model.Post) ( return "", err } - p.metricsService.ObserveMessagesCount(metrics.ActionCreated, metrics.ActionSourceMattermost, true) + if p.metricsService != nil { + p.metricsService.ObserveMessagesCount(metrics.ActionCreated, metrics.ActionSourceMattermost, true) + } if post.Id != "" { if err := p.store.LinkPosts(nil, storemodels.PostInfo{MattermostID: post.Id, MSTeamsChannel: chat.ID, MSTeamsID: newMessage.ID, MSTeamsLastUpdateAt: newMessage.LastUpdateAt}); err != nil { p.API.LogWarn("Error updating the msteams/mattermost post link metadata", "error", err) @@ -605,13 +623,17 @@ func (p *Plugin) Send(teamID, channelID string, user *model.User, post *model.Po fileInfo, appErr := p.API.GetFileInfo(fileID) if appErr != nil { p.API.LogWarn("Unable to get file info", "error", appErr) - p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, discardedReasonUnableToGetMMData, false) + if p.metricsService != nil { + p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, discardedReasonUnableToGetMMData, false) + } continue } fileData, appErr := p.API.GetFile(fileInfo.Id) if appErr != nil { p.API.LogWarn("Error in getting file attachment from Mattermost", "error", appErr) - p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, discardedReasonUnableToGetMMData, false) + if p.metricsService != nil { + p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, discardedReasonUnableToGetMMData, false) + } continue } @@ -620,11 +642,15 @@ func (p *Plugin) Send(teamID, channelID string, user *model.User, post *model.Po attachment, err = client.UploadFile(teamID, channelID, fileName+"_"+fileInfo.Id+fileExtension, int(fileInfo.Size), fileInfo.MimeType, bytes.NewReader(fileData), nil) if err != nil { p.API.LogWarn("Error in uploading file attachment to MS Teams", "error", err) - p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, discardedReasonUnableToUploadFileOnTeams, false) + if p.metricsService != nil { + p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, discardedReasonUnableToUploadFileOnTeams, false) + } continue } attachments = append(attachments, attachment) - p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, "", false) + if p.metricsService != nil { + p.metricsService.ObserveFileCount(metrics.ActionCreated, metrics.ActionSourceMattermost, "", false) + } } md := markdown.New(markdown.XHTMLOutput(true), markdown.Typographer(false)) @@ -638,7 +664,9 @@ func (p *Plugin) Send(teamID, channelID string, user *model.User, post *model.Po return "", err } - p.metricsService.ObserveMessagesCount(metrics.ActionCreated, metrics.ActionSourceMattermost, false) + if p.metricsService != nil { + p.metricsService.ObserveMessagesCount(metrics.ActionCreated, metrics.ActionSourceMattermost, false) + } if post.Id != "" { if err := p.store.LinkPosts(nil, storemodels.PostInfo{MattermostID: post.Id, MSTeamsChannel: channelID, MSTeamsID: newMessage.ID, MSTeamsLastUpdateAt: newMessage.LastUpdateAt}); err != nil { p.API.LogWarn("Error updating the msteams/mattermost post link metadata", "error", err) @@ -682,7 +710,9 @@ func (p *Plugin) Delete(teamID, channelID string, user *model.User, post *model. return err } - p.metricsService.ObserveMessagesCount(metrics.ActionDeleted, metrics.ActionSourceMattermost, false) + if p.metricsService != nil { + p.metricsService.ObserveMessagesCount(metrics.ActionDeleted, metrics.ActionSourceMattermost, false) + } return nil } @@ -711,7 +741,9 @@ func (p *Plugin) DeleteChat(chatID string, user *model.User, post *model.Post) e return err } - p.metricsService.ObserveMessagesCount(metrics.ActionDeleted, metrics.ActionSourceMattermost, true) + if p.metricsService != nil { + p.metricsService.ObserveMessagesCount(metrics.ActionDeleted, metrics.ActionSourceMattermost, true) + } return nil } @@ -787,7 +819,9 @@ func (p *Plugin) Update(teamID, channelID string, user *model.User, newPost, old } } - p.metricsService.ObserveMessagesCount(metrics.ActionUpdated, metrics.ActionSourceMattermost, false) + if p.metricsService != nil { + p.metricsService.ObserveMessagesCount(metrics.ActionUpdated, metrics.ActionSourceMattermost, false) + } } else { updatedMessage, txErr = getUpdatedMessage(teamID, channelID, parentID, postInfo.MSTeamsID, client) if txErr != nil { @@ -862,7 +896,9 @@ func (p *Plugin) UpdateChat(chatID string, user *model.User, newPost, oldPost *m } } - p.metricsService.ObserveMessagesCount(metrics.ActionUpdated, metrics.ActionSourceMattermost, true) + if p.metricsService != nil { + p.metricsService.ObserveMessagesCount(metrics.ActionUpdated, metrics.ActionSourceMattermost, true) + } } else { updatedMessage, txErr = client.GetChatMessage(chatID, postInfo.MSTeamsID) if txErr != nil { diff --git a/server/monitor/subscriptions.go b/server/monitor/subscriptions.go index aa8adb93e..784a0fac3 100644 --- a/server/monitor/subscriptions.go +++ b/server/monitor/subscriptions.go @@ -135,7 +135,9 @@ func (m *Monitor) CreateAndSaveChatSubscription(mmSubscription *storemodels.Glob return } - m.metrics.ObserveSubscriptionsCount(metrics.SubscriptionConnected) + if m.metrics != nil { + m.metrics.ObserveSubscriptionsCount(metrics.SubscriptionConnected) + } if mmSubscription != nil { if err := m.store.DeleteSubscription(mmSubscription.SubscriptionID); err != nil { m.api.LogError("Unable to delete the old all chats subscription", "error", err.Error()) @@ -174,7 +176,9 @@ func (m *Monitor) recreateChannelSubscription(subscriptionID, teamID, channelID, return } - m.metrics.ObserveSubscriptionsCount(metrics.SubscriptionReconnected) + if m.metrics != nil { + m.metrics.ObserveSubscriptionsCount(metrics.SubscriptionReconnected) + } if subscriptionID != "" { if err = m.store.DeleteSubscription(subscriptionID); err != nil { m.api.LogDebug("Unable to delete old channel subscription from DB", "subscriptionID", subscriptionID, "error", err.Error()) @@ -217,7 +221,9 @@ func (m *Monitor) recreateGlobalSubscription(subscriptionID, secret string) erro return err } - m.metrics.ObserveSubscriptionsCount(metrics.SubscriptionReconnected) + if m.metrics != nil { + m.metrics.ObserveSubscriptionsCount(metrics.SubscriptionReconnected) + } if err = m.store.DeleteSubscription(subscriptionID); err != nil { m.api.LogDebug("Unable to delete old global subscription from DB", "subscriptionID", subscriptionID, "error", err.Error()) } @@ -230,7 +236,9 @@ func (m *Monitor) refreshSubscription(subscriptionID string) error { return err } - m.metrics.ObserveSubscriptionsCount(metrics.SubscriptionRefreshed) + if m.metrics != nil { + m.metrics.ObserveSubscriptionsCount(metrics.SubscriptionRefreshed) + } return m.store.UpdateSubscriptionExpiresOn(subscriptionID, *newSubscriptionTime) } diff --git a/server/plugin.go b/server/plugin.go index 4206477c4..921e5644d 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -416,7 +416,9 @@ func (p *Plugin) syncUsers() { return } - p.metricsService.ObserveUpstreamUsers(int64(len(msUsers))) + if p.metricsService != nil { + p.metricsService.ObserveUpstreamUsers(int64(len(msUsers))) + } mmUsers, appErr := p.API.GetUsers(&model.UserGetOptions{Page: 0, PerPage: math.MaxInt32}) if appErr != nil { p.API.LogError("Unable to get MM users during sync user job", "error", appErr.Error()) @@ -616,9 +618,11 @@ func (p *Plugin) runMetricsUpdaterTask(store store.Store, updateMetricsTaskFrequ if err != nil { p.API.LogError("failed to update computed metrics", "error", err) } - p.metricsService.ObserveConnectedUsers(stats.ConnectedUsers) - p.metricsService.ObserveSyntheticUsers(stats.SyntheticUsers) - p.metricsService.ObserveLinkedChannels(stats.LinkedChannels) + if p.metricsService != nil { + p.metricsService.ObserveConnectedUsers(stats.ConnectedUsers) + p.metricsService.ObserveSyntheticUsers(stats.SyntheticUsers) + p.metricsService.ObserveLinkedChannels(stats.LinkedChannels) + } } metricsUpdater() diff --git a/server/store/layer_generators/timer_layer.go.tmpl b/server/store/layer_generators/timer_layer.go.tmpl index 1f1317cc5..31e3faee0 100644 --- a/server/store/layer_generators/timer_layer.go.tmpl +++ b/server/store/layer_generators/timer_layer.go.tmpl @@ -35,7 +35,9 @@ func (s *{{$.Name}}) {{$index}}({{$element.Params | joinParamsWithType}}) {{$ele if {{$element.Results | errorToBoolean}} { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.{{$index}}", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.{{$index}}", success, elapsed) + } {{ with (genResultsVars $element.Results false ) -}} return {{ . }} {{- else -}} diff --git a/server/store/store.go b/server/store/store.go index 57a99a21d..aa15066d3 100644 --- a/server/store/store.go +++ b/server/store/store.go @@ -1,4 +1,5 @@ //go:generate mockery --name=Store +//go:generate go run layer_generators/main.go package store import ( diff --git a/server/store/timerlayer/timerlayer.go b/server/store/timerlayer/timerlayer.go index a4dcc2fa4..9ba5630cd 100644 --- a/server/store/timerlayer/timerlayer.go +++ b/server/store/timerlayer/timerlayer.go @@ -32,7 +32,9 @@ func (s *TimerLayer) CheckEnabledTeamByTeamID(teamID string) bool { if true { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.CheckEnabledTeamByTeamID", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.CheckEnabledTeamByTeamID", success, elapsed) + } return result } @@ -46,7 +48,9 @@ func (s *TimerLayer) CompareAndSetJobStatus(jobName string, oldStatus bool, newS if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.CompareAndSetJobStatus", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.CompareAndSetJobStatus", success, elapsed) + } return result, err } @@ -60,7 +64,9 @@ func (s *TimerLayer) DeleteDMAndGMChannelPromptTime(userID string) error { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.DeleteDMAndGMChannelPromptTime", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.DeleteDMAndGMChannelPromptTime", success, elapsed) + } return err } @@ -74,7 +80,9 @@ func (s *TimerLayer) DeleteLinkByChannelID(channelID string) error { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.DeleteLinkByChannelID", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.DeleteLinkByChannelID", success, elapsed) + } return err } @@ -88,7 +96,9 @@ func (s *TimerLayer) DeleteSubscription(subscriptionID string) error { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.DeleteSubscription", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.DeleteSubscription", success, elapsed) + } return err } @@ -102,7 +112,9 @@ func (s *TimerLayer) DeleteUserInfo(mmUserID string) error { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.DeleteUserInfo", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.DeleteUserInfo", success, elapsed) + } return err } @@ -116,7 +128,9 @@ func (s *TimerLayer) GetAvatarCache(userID string) ([]byte, error) { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetAvatarCache", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetAvatarCache", success, elapsed) + } return result, err } @@ -130,7 +144,9 @@ func (s *TimerLayer) GetChannelSubscription(subscriptionID string) (*storemodels if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetChannelSubscription", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetChannelSubscription", success, elapsed) + } return result, err } @@ -144,7 +160,9 @@ func (s *TimerLayer) GetChannelSubscriptionByTeamsChannelID(teamsChannelID strin if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetChannelSubscriptionByTeamsChannelID", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetChannelSubscriptionByTeamsChannelID", success, elapsed) + } return result, err } @@ -158,7 +176,9 @@ func (s *TimerLayer) GetChatSubscription(subscriptionID string) (*storemodels.Ch if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetChatSubscription", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetChatSubscription", success, elapsed) + } return result, err } @@ -172,7 +192,9 @@ func (s *TimerLayer) GetConnectedUsers(page int, perPage int) ([]*storemodels.Co if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetConnectedUsers", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetConnectedUsers", success, elapsed) + } return result, err } @@ -186,7 +208,9 @@ func (s *TimerLayer) GetDMAndGMChannelPromptTime(channelID string, userID string if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetDMAndGMChannelPromptTime", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetDMAndGMChannelPromptTime", success, elapsed) + } return result, err } @@ -200,7 +224,9 @@ func (s *TimerLayer) GetGlobalSubscription(subscriptionID string) (*storemodels. if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetGlobalSubscription", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetGlobalSubscription", success, elapsed) + } return result, err } @@ -214,7 +240,9 @@ func (s *TimerLayer) GetLinkByChannelID(channelID string) (*storemodels.ChannelL if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetLinkByChannelID", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetLinkByChannelID", success, elapsed) + } return result, err } @@ -228,7 +256,9 @@ func (s *TimerLayer) GetLinkByMSTeamsChannelID(teamID string, channelID string) if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetLinkByMSTeamsChannelID", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetLinkByMSTeamsChannelID", success, elapsed) + } return result, err } @@ -242,7 +272,9 @@ func (s *TimerLayer) GetPostInfoByMSTeamsID(chatID string, postID string) (*stor if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetPostInfoByMSTeamsID", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetPostInfoByMSTeamsID", success, elapsed) + } return result, err } @@ -256,7 +288,9 @@ func (s *TimerLayer) GetPostInfoByMattermostID(postID string) (*storemodels.Post if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetPostInfoByMattermostID", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetPostInfoByMattermostID", success, elapsed) + } return result, err } @@ -270,7 +304,9 @@ func (s *TimerLayer) GetSizeOfWhitelist() (int, error) { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetSizeOfWhitelist", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetSizeOfWhitelist", success, elapsed) + } return result, err } @@ -284,7 +320,9 @@ func (s *TimerLayer) GetStats() (*storemodels.Stats, error) { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetStats", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetStats", success, elapsed) + } return result, err } @@ -298,7 +336,9 @@ func (s *TimerLayer) GetSubscriptionType(subscriptionID string) (string, error) if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetSubscriptionType", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetSubscriptionType", success, elapsed) + } return result, err } @@ -312,7 +352,9 @@ func (s *TimerLayer) GetTokenForMSTeamsUser(userID string) (*oauth2.Token, error if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetTokenForMSTeamsUser", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetTokenForMSTeamsUser", success, elapsed) + } return result, err } @@ -326,7 +368,9 @@ func (s *TimerLayer) GetTokenForMattermostUser(userID string) (*oauth2.Token, er if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.GetTokenForMattermostUser", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.GetTokenForMattermostUser", success, elapsed) + } return result, err } @@ -340,7 +384,9 @@ func (s *TimerLayer) Init() error { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.Init", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.Init", success, elapsed) + } return err } @@ -354,7 +400,9 @@ func (s *TimerLayer) IsUserPresentInWhitelist(userID string) (bool, error) { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.IsUserPresentInWhitelist", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.IsUserPresentInWhitelist", success, elapsed) + } return result, err } @@ -368,7 +416,9 @@ func (s *TimerLayer) LinkPosts(tx *sql.Tx, postInfo storemodels.PostInfo) error if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.LinkPosts", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.LinkPosts", success, elapsed) + } return err } @@ -382,7 +432,9 @@ func (s *TimerLayer) ListChannelLinks() ([]storemodels.ChannelLink, error) { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.ListChannelLinks", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.ListChannelLinks", success, elapsed) + } return result, err } @@ -396,7 +448,9 @@ func (s *TimerLayer) ListChannelLinksWithNames() ([]*storemodels.ChannelLink, er if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.ListChannelLinksWithNames", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.ListChannelLinksWithNames", success, elapsed) + } return result, err } @@ -410,7 +464,9 @@ func (s *TimerLayer) ListChannelSubscriptions() ([]*storemodels.ChannelSubscript if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.ListChannelSubscriptions", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.ListChannelSubscriptions", success, elapsed) + } return result, err } @@ -424,7 +480,9 @@ func (s *TimerLayer) ListChannelSubscriptionsToRefresh() ([]*storemodels.Channel if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.ListChannelSubscriptionsToRefresh", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.ListChannelSubscriptionsToRefresh", success, elapsed) + } return result, err } @@ -438,7 +496,9 @@ func (s *TimerLayer) ListChatSubscriptionsToCheck() ([]storemodels.ChatSubscript if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.ListChatSubscriptionsToCheck", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.ListChatSubscriptionsToCheck", success, elapsed) + } return result, err } @@ -452,7 +512,9 @@ func (s *TimerLayer) ListGlobalSubscriptions() ([]*storemodels.GlobalSubscriptio if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.ListGlobalSubscriptions", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.ListGlobalSubscriptions", success, elapsed) + } return result, err } @@ -466,7 +528,9 @@ func (s *TimerLayer) ListGlobalSubscriptionsToRefresh() ([]*storemodels.GlobalSu if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.ListGlobalSubscriptionsToRefresh", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.ListGlobalSubscriptionsToRefresh", success, elapsed) + } return result, err } @@ -480,7 +544,9 @@ func (s *TimerLayer) LockPostByMMPostID(tx *sql.Tx, messageID string) error { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.LockPostByMMPostID", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.LockPostByMMPostID", success, elapsed) + } return err } @@ -494,7 +560,9 @@ func (s *TimerLayer) LockPostByMSTeamsPostID(tx *sql.Tx, messageID string) error if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.LockPostByMSTeamsPostID", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.LockPostByMSTeamsPostID", success, elapsed) + } return err } @@ -508,7 +576,9 @@ func (s *TimerLayer) MattermostToTeamsUserID(userID string) (string, error) { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.MattermostToTeamsUserID", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.MattermostToTeamsUserID", success, elapsed) + } return result, err } @@ -522,7 +592,9 @@ func (s *TimerLayer) PrefillWhitelist() error { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.PrefillWhitelist", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.PrefillWhitelist", success, elapsed) + } return err } @@ -536,7 +608,9 @@ func (s *TimerLayer) RecoverPost(postID string) error { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.RecoverPost", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.RecoverPost", success, elapsed) + } return err } @@ -550,7 +624,9 @@ func (s *TimerLayer) SaveChannelSubscription(tx *sql.Tx, subscription storemodel if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.SaveChannelSubscription", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.SaveChannelSubscription", success, elapsed) + } return err } @@ -564,7 +640,9 @@ func (s *TimerLayer) SaveChatSubscription(subscription storemodels.ChatSubscript if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.SaveChatSubscription", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.SaveChatSubscription", success, elapsed) + } return err } @@ -578,7 +656,9 @@ func (s *TimerLayer) SaveGlobalSubscription(subscription storemodels.GlobalSubsc if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.SaveGlobalSubscription", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.SaveGlobalSubscription", success, elapsed) + } return err } @@ -592,7 +672,9 @@ func (s *TimerLayer) SetAvatarCache(userID string, photo []byte) error { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.SetAvatarCache", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.SetAvatarCache", success, elapsed) + } return err } @@ -606,7 +688,9 @@ func (s *TimerLayer) SetJobStatus(jobName string, status bool) error { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.SetJobStatus", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.SetJobStatus", success, elapsed) + } return err } @@ -620,7 +704,9 @@ func (s *TimerLayer) SetPostLastUpdateAtByMSTeamsID(tx *sql.Tx, postID string, l if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.SetPostLastUpdateAtByMSTeamsID", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.SetPostLastUpdateAtByMSTeamsID", success, elapsed) + } return err } @@ -634,7 +720,9 @@ func (s *TimerLayer) SetPostLastUpdateAtByMattermostID(tx *sql.Tx, postID string if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.SetPostLastUpdateAtByMattermostID", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.SetPostLastUpdateAtByMattermostID", success, elapsed) + } return err } @@ -648,7 +736,9 @@ func (s *TimerLayer) SetUserInfo(userID string, msTeamsUserID string, token *oau if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.SetUserInfo", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.SetUserInfo", success, elapsed) + } return err } @@ -662,7 +752,9 @@ func (s *TimerLayer) StoreChannelLink(link *storemodels.ChannelLink) error { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.StoreChannelLink", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.StoreChannelLink", success, elapsed) + } return err } @@ -676,7 +768,9 @@ func (s *TimerLayer) StoreDMAndGMChannelPromptTime(channelID string, userID stri if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.StoreDMAndGMChannelPromptTime", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.StoreDMAndGMChannelPromptTime", success, elapsed) + } return err } @@ -690,7 +784,9 @@ func (s *TimerLayer) StoreOAuth2State(state string) error { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.StoreOAuth2State", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.StoreOAuth2State", success, elapsed) + } return err } @@ -704,7 +800,9 @@ func (s *TimerLayer) StoreUserInWhitelist(userID string) error { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.StoreUserInWhitelist", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.StoreUserInWhitelist", success, elapsed) + } return err } @@ -718,7 +816,9 @@ func (s *TimerLayer) TeamsToMattermostUserID(userID string) (string, error) { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.TeamsToMattermostUserID", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.TeamsToMattermostUserID", success, elapsed) + } return result, err } @@ -732,7 +832,9 @@ func (s *TimerLayer) UpdateSubscriptionExpiresOn(subscriptionID string, expiresO if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.UpdateSubscriptionExpiresOn", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.UpdateSubscriptionExpiresOn", success, elapsed) + } return err } @@ -746,7 +848,9 @@ func (s *TimerLayer) VerifyOAuth2State(state string) error { if err == nil { success = "true" } - s.metrics.ObserveStoreMethodDuration("Store.VerifyOAuth2State", success, elapsed) + if s.metrics != nil { + s.metrics.ObserveStoreMethodDuration("Store.VerifyOAuth2State", success, elapsed) + } return err }