Skip to content

Commit

Permalink
Add Telemetry to MSCalendar (#152)
Browse files Browse the repository at this point in the history
* Add Telemetry to MSCalendar

* Update go.mod, move rudder client initialization, add rudder client closing, improve simplicity on NewRudderClient and add error logging to tracker

* Add missing linebreak

* Add dev credentials, missing telemetries, and make plugin implementation of the tracker more ubiquitous

* Improve set status tracking

* Add private and development key through environment variables, set "telemetryShortName" for the plugin and use "_" as separator instead of "-"

* Move all makefile settings to custom.mk, and set production variable directly on CircleCI

* Fix test

* Change userID by UserActualID

* Move tracking to the right place
  • Loading branch information
larkox authored Aug 14, 2020
1 parent 3e459f7 commit 5c6f67e
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 103 deletions.
5 changes: 5 additions & 0 deletions build/custom.mk
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# Include custom targets and environment variables here
ifndef MM_RUDDER_WRITE_KEY
MM_RUDDER_WRITE_KEY = 1d5bMvdrfWClLxgK1FvV3s4U1tg
endif

LDFLAGS += -X "github.com/mattermost/mattermost-plugin-mscalendar/server/utils/telemetry.rudderWriteKey=$(MM_RUDDER_WRITE_KEY)"
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/mattermost/mattermost-plugin-api v0.0.9
github.com/mattermost/mattermost-server/v5 v5.3.2-0.20200529093641-c9cdeba1a7e2
github.com/pkg/errors v0.9.1
github.com/rudderlabs/analytics-go v3.2.1+incompatible
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.5.1
github.com/yaegashi/msgraph.go v0.0.0-20191104022859-3f9096c750b2
Expand Down
97 changes: 2 additions & 95 deletions go.sum

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions server/config/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ const (
BotDisplayName = "Microsoft Calendar"
BotDescription = "Created by the Microsoft Calendar Plugin."

ApplicationName = "Microsoft Calendar"
Repository = "mattermost-plugin-mscalendar"
CommandTrigger = "mscalendar"
ApplicationName = "Microsoft Calendar"
Repository = "mattermost-plugin-mscalendar"
CommandTrigger = "mscalendar"
TelemetryShortName = "mscalendar"

PathOAuth2 = "/oauth2"
PathComplete = "/complete"
Expand Down
1 change: 1 addition & 0 deletions server/mscalendar/daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func (m *mscalendar) ProcessAllDailySummary(now time.Time) error {
}

m.Poster.DM(user.MattermostUserID, postStr)
m.Dependencies.Tracker.TrackDailySummarySent(user.MattermostUserID)
dsum.LastPostTime = time.Now().Format(time.RFC3339)
err = m.Store.StoreUser(user)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions server/mscalendar/daily_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (

"github.com/golang/mock/gomock"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar/mock_plugin_api"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendarTracker"
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote"
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote/mock_remote"
"github.com/mattermost/mattermost-plugin-mscalendar/server/store"
"github.com/mattermost/mattermost-plugin-mscalendar/server/store/mock_store"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils/bot/mock_bot"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils/telemetry"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -180,6 +182,7 @@ Wednesday February 12
Poster: poster,
Remote: mockRemote,
PluginAPI: mockPluginAPI,
Tracker: mscalendarTracker.New(telemetry.NewTracker(nil, "", "", "", "", "", true, logger)),
},
}

Expand Down
2 changes: 2 additions & 0 deletions server/mscalendar/mscalendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/mattermost/mattermost-server/v5/model"

"github.com/mattermost/mattermost-plugin-mscalendar/server/config"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendarTracker"
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote"
"github.com/mattermost/mattermost-plugin-mscalendar/server/store"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils/bot"
Expand Down Expand Up @@ -34,6 +35,7 @@ type Dependencies struct {
SettingsPanel settingspanel.Panel
IsAuthorizedAdmin func(string) (bool, error)
Welcomer Welcomer
Tracker mscalendarTracker.Tracker
}

type PluginAPI interface {
Expand Down
3 changes: 3 additions & 0 deletions server/mscalendar/welcomer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (bot *mscBot) Welcome(userID string) error {
}

func (bot *mscBot) AfterSuccessfullyConnect(userID, userLogin string) error {
bot.Tracker.TrackUserAuthenticated(userID)
postID, err := bot.Store.DeleteUserWelcomePost(userID)
if err != nil {
bot.Errorf("error deleting user's welcome post id, err=%v", err)
Expand All @@ -88,6 +89,7 @@ func (bot *mscBot) AfterSuccessfullyConnect(userID, userLogin string) error {
}

func (bot *mscBot) AfterDisconnect(userID string) error {
bot.Tracker.TrackUserDeauthenticated(userID)
errCancel := bot.Cancel(userID)
errClean := bot.cleanWelcomePost(userID)
if errCancel != nil {
Expand All @@ -101,6 +103,7 @@ func (bot *mscBot) AfterDisconnect(userID string) error {
}

func (bot *mscBot) WelcomeFlowEnd(userID string) {
bot.Tracker.TrackWelcomeFlowCompletion(userID)
bot.notifySettings(userID)
}

Expand Down
53 changes: 53 additions & 0 deletions server/mscalendarTracker/tracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package mscalendarTracker

import "github.com/mattermost/mattermost-plugin-mscalendar/server/utils/telemetry"

const (
welcomeFlowCompletionEvent = "welcomeFlowCompletion"
userAuthenticatedEvent = "userAuthenticated"
userDeauthenticatedEvent = "userDeauthenticated"
automaticStatusUpdateEvent = "automaticStatusUpdate"
dailySummarySentEvent = "dailySummarySent"
)

type Tracker interface {
TrackWelcomeFlowCompletion(userID string)
TrackUserAuthenticated(userID string)
TrackUserDeauthenticated(userID string)
TrackDailySummarySent(userID string)
TrackAutomaticStatusUpdate(userID string, value bool, location string)
}

func New(t telemetry.Tracker) Tracker {
return &tracker{
tracker: t,
}
}

type tracker struct {
tracker telemetry.Tracker
}

func (t *tracker) TrackWelcomeFlowCompletion(userID string) {
t.tracker.TrackUserEvent(welcomeFlowCompletionEvent, userID, map[string]interface{}{})
}

func (t *tracker) TrackUserAuthenticated(userID string) {
t.tracker.TrackUserEvent(userAuthenticatedEvent, userID, map[string]interface{}{})
}

func (t *tracker) TrackUserDeauthenticated(userID string) {
t.tracker.TrackUserEvent(userDeauthenticatedEvent, userID, map[string]interface{}{})
}

func (t *tracker) TrackDailySummarySent(userID string) {
t.tracker.TrackUserEvent(dailySummarySentEvent, userID, map[string]interface{}{})
}

func (t *tracker) TrackAutomaticStatusUpdate(userID string, value bool, location string) {
properties := map[string]interface{}{
"value": value,
"location": location,
}
t.tracker.TrackUserEvent(automaticStatusUpdateEvent, userID, properties)
}
27 changes: 23 additions & 4 deletions server/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/mattermost/mattermost-plugin-mscalendar/server/config"
"github.com/mattermost/mattermost-plugin-mscalendar/server/jobs"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendarTracker"
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote"
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote/msgraph"
"github.com/mattermost/mattermost-plugin-mscalendar/server/store"
Expand All @@ -32,6 +33,7 @@ import (
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils/oauth2connect"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils/pluginapi"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils/settingspanel"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils/telemetry"
)

type Env struct {
Expand All @@ -46,9 +48,10 @@ type Env struct {
type Plugin struct {
plugin.MattermostPlugin

envLock *sync.RWMutex
env Env
Templates map[string]*template.Template
envLock *sync.RWMutex
env Env
Templates map[string]*template.Template
telemetryClient telemetry.Client
}

func NewWithEnv(env mscalendar.Env) *Plugin {
Expand Down Expand Up @@ -77,10 +80,23 @@ func (p *Plugin) OnActivate() error {
}

command.Register(p.API.RegisterCommand)

p.telemetryClient, err = telemetry.NewRudderClient()
if err != nil {
p.env.bot.Errorf("Cannot create telemetry client. err=%v", err)
}

return nil
}

func (p *Plugin) OnDeactivate() error {
if p.telemetryClient != nil {
err := p.telemetryClient.Close()
if err != nil {
p.env.Logger.Warnf("OnDeactivate: Failed to close telemetryClient. err=%v", err)
}
}

e := p.getEnv()
if e.jobManager != nil {
if err := e.jobManager.Close(); err != nil {
Expand Down Expand Up @@ -138,9 +154,12 @@ func (p *Plugin) OnConfigurationChange() (err error) {
mscalendarBot := mscalendar.NewMSCalendarBot(e.bot, e.Env, pluginURL)

e.Dependencies.Logger = e.bot

e.Dependencies.Tracker = mscalendarTracker.New(telemetry.NewTracker(p.telemetryClient, p.API.GetDiagnosticId(), p.API.GetServerVersion(), e.PluginID, e.PluginVersion, config.TelemetryShortName, *p.API.GetConfig().LogSettings.EnableDiagnostics, e.Logger))

e.Dependencies.Poster = e.bot
e.Dependencies.Welcomer = mscalendarBot
e.Dependencies.Store = store.NewPluginStore(p.API, e.bot)
e.Dependencies.Store = store.NewPluginStore(p.API, e.bot, e.Dependencies.Tracker)
e.Dependencies.SettingsPanel = mscalendar.NewSettingsPanel(
e.bot,
e.Dependencies.Store,
Expand Down
1 change: 1 addition & 0 deletions server/store/flow_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (s *pluginStore) SetProperty(userID, propertyName string, value bool) error
switch propertyName {
case UpdateStatusPropertyName:
user.Settings.UpdateStatus = value
s.Tracker.TrackAutomaticStatusUpdate(userID, value, "flow")
case GetConfirmationPropertyName:
user.Settings.GetConfirmation = value
case ReceiveUpcomingEventReminderName:
Expand Down
1 change: 1 addition & 0 deletions server/store/setting_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (s *pluginStore) SetSetting(userID, settingID string, value interface{}) er
return fmt.Errorf("cannot read value %v for setting %s (expecting bool)", value, settingID)
}
user.Settings.UpdateStatus = storableValue
s.Tracker.TrackAutomaticStatusUpdate(userID, storableValue, "settings")
case GetConfirmationSettingID:
storableValue, ok := value.(bool)
if !ok {
Expand Down
5 changes: 4 additions & 1 deletion server/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/mattermost/mattermost-server/v5/plugin"

"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendarTracker"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils/bot"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils/flow"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils/kvstore"
Expand Down Expand Up @@ -51,9 +52,10 @@ type pluginStore struct {
welcomeIndexKV kvstore.KVStore
settingsPanelKV kvstore.KVStore
Logger bot.Logger
Tracker mscalendarTracker.Tracker
}

func NewPluginStore(api plugin.API, logger bot.Logger) Store {
func NewPluginStore(api plugin.API, logger bot.Logger, tracker mscalendarTracker.Tracker) Store {
basicKV := kvstore.NewPluginStore(api)
return &pluginStore{
basicKV: basicKV,
Expand All @@ -66,5 +68,6 @@ func NewPluginStore(api plugin.API, logger bot.Logger) Store {
welcomeIndexKV: kvstore.NewHashedKeyStore(basicKV, WelcomeKeyPrefix),
settingsPanelKV: kvstore.NewHashedKeyStore(basicKV, SettingsPanelPrefix),
Logger: logger,
Tracker: tracker,
}
}
46 changes: 46 additions & 0 deletions server/utils/telemetry/rudder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package telemetry

import rudder "github.com/rudderlabs/analytics-go"

const rudderDataPlaneURL = "https://pdat.matterlytics.com"

var rudderWriteKey string

func NewRudderClient() (Client, error) {
return NewRudderClientWithCredentials(rudderWriteKey, rudderDataPlaneURL)
}

func NewRudderClientWithCredentials(writeKey, dataPlaneURL string) (Client, error) {
client, err := rudder.NewWithConfig(writeKey, dataPlaneURL, rudder.Config{})
if err != nil {
return nil, err
}

return &rudderWrapper{client: client}, nil
}

type rudderWrapper struct {
client rudder.Client
}

func (r *rudderWrapper) Enqueue(t Track) error {
err := r.client.Enqueue(rudder.Track{
UserId: t.UserID,
Event: t.Event,
Properties: t.Properties,
})

if err != nil {
return err
}

return nil
}

func (r *rudderWrapper) Close() error {
err := r.client.Close()
if err != nil {
return err
}
return nil
}
69 changes: 69 additions & 0 deletions server/utils/telemetry/tracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package telemetry

import "github.com/mattermost/mattermost-plugin-mscalendar/server/utils/bot"

type Tracker interface {
Track(event string, properties map[string]interface{})
TrackUserEvent(event string, userID string, properties map[string]interface{})
}

type Client interface {
Enqueue(t Track) error
Close() error
}

type Track struct {
UserID string
Event string
Properties map[string]interface{}
}

type tracker struct {
client Client
diagnosticID string
serverVersion string
pluginID string
pluginVersion string
telemetryShortName string
enabled bool
logger bot.Logger
}

func NewTracker(c Client, diagnosticID, serverVersion, pluginID, pluginVersion, telemetryShortName string, enableDiagnostics bool, logger bot.Logger) Tracker {
return &tracker{
telemetryShortName: telemetryShortName,
client: c,
diagnosticID: diagnosticID,
serverVersion: serverVersion,
pluginID: pluginID,
pluginVersion: pluginVersion,
enabled: enableDiagnostics,
logger: logger,
}
}

func (t *tracker) Track(event string, properties map[string]interface{}) {
if !t.enabled || t.client == nil {
return
}

event = t.telemetryShortName + "_" + event
properties["PluginID"] = t.pluginID
properties["PluginVersion"] = t.pluginVersion
properties["ServerVersion"] = t.serverVersion

err := t.client.Enqueue(Track{
UserID: t.diagnosticID,
Event: event,
Properties: properties,
})

if err != nil {
t.logger.Warnf("cannot enqueue telemetry event, err=%s", err.Error())
}
}

func (t *tracker) TrackUserEvent(event string, userID string, properties map[string]interface{}) {
properties["UserActualID"] = userID
t.Track(event, properties)
}

0 comments on commit 5c6f67e

Please sign in to comment.