Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding telemetry #201

Merged
merged 9 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ DLV_DEBUG_PORT := 2346
DEFAULT_GOOS := $(shell go env GOOS)
DEFAULT_GOARCH := $(shell go env GOARCH)
INCLUDE_FFMPEG ?=
BUILD_HASH = $(shell git rev-parse HEAD)
LDFLAGS += -X "main.buildHash=$(BUILD_HASH)"
LDFLAGS += -X "main.isDebug=$(MM_DEBUG)"
LDFLAGS += -X "main.rudderWriteKey=$(MM_RUDDER_COPILOT_PROD)"
LDFLAGS += -X "main.rudderDataplaneURL=$(MM_RUDDER_DATAPLANE_URL)"
GO_BUILD_FLAGS += -ldflags '$(LDFLAGS)'

export GO111MODULE=on

Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/mattermost/mattermost/server/public v0.1.4
github.com/nicksnyder/go-i18n/v2 v2.4.0
github.com/r3labs/sse/v2 v2.10.0
github.com/rudderlabs/analytics-go v3.3.3+incompatible
github.com/sashabaranov/go-openai v1.24.0
github.com/stretchr/testify v1.8.4
golang.org/x/text v0.14.0
Expand Down Expand Up @@ -65,15 +66,20 @@ require (
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/segmentio/backo-go v1.0.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tinylib/msgp v1.1.9 // indirect
github.com/trivago/tgo v1.0.7 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/wiggin77/merror v1.0.5 // indirect
github.com/wiggin77/srslog v1.0.1 // indirect
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
Expand Down
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ github.com/asticode/go-astits v1.8.0/go.mod h1:DkOWmBNQpnr9mv24KfZjq4JawCFX1FCqj
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g=
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
Expand Down Expand Up @@ -211,9 +213,13 @@ github.com/r3labs/sse/v2 v2.10.0 h1:hFEkLLFY4LDifoHdiCN/LlGBAdVJYsANaLqNYa1l/v0=
github.com/r3labs/sse/v2 v2.10.0/go.mod h1:Igau6Whc+F17QUgML1fYe1VPZzTV6EMCnYktEmkNJ7I=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rudderlabs/analytics-go v3.3.3+incompatible h1:OG0XlKoXfr539e2t1dXtTB+Gr89uFW+OUNQBVhHIIBY=
github.com/rudderlabs/analytics-go v3.3.3+incompatible/go.mod h1:LF8/ty9kUX4PTY3l5c97K3nZZaX5Hwsvt+NBaRL/f30=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/sashabaranov/go-openai v1.24.0 h1:4H4Pg8Bl2RH/YSnU8DYumZbuHnnkfioor/dtNlB20D4=
github.com/sashabaranov/go-openai v1.24.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/segmentio/backo-go v1.0.1 h1:68RQccglxZeyURy93ASB/2kc9QudzgIDexJ927N++y4=
github.com/segmentio/backo-go v1.0.1/go.mod h1:9/Rh6yILuLysoQnZ2oNooD2g7aBnvM7r/fNVxRNWfBc=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY=
github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM=
Expand Down Expand Up @@ -259,6 +265,13 @@ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tinylib/msgp v1.1.9 h1:SHf3yoO2sGA0veCJeCBYLHuttAVFHGm2RHgNodW7wQU=
github.com/tinylib/msgp v1.1.9/go.mod h1:BCXGB54lDD8qUEPmiG0cQQUANC4IUQyB2ItS2UDlO/k=
github.com/trivago/tgo v1.0.7 h1:uaWH/XIy9aWYWpjm2CU3RpcqZXmX2ysQ9/Go+d9gyrM=
Expand All @@ -276,6 +289,8 @@ github.com/wiggin77/merror v1.0.5 h1:P+lzicsn4vPMycAf2mFf7Zk6G9eco5N+jB1qJ2XW3ME
github.com/wiggin77/merror v1.0.5/go.mod h1:H2ETSu7/bPE0Ymf4bEwdUoo73OOEkdClnoRisfw0Nm0=
github.com/wiggin77/srslog v1.0.1 h1:gA2XjSMy3DrRdX9UqLuDtuVAAshb8bE1NhX1YK0Qe+8=
github.com/wiggin77/srslog v1.0.1/go.mod h1:fehkyYDq1QfuYn60TDPu9YdY2bB85VUW2mvN1WynEls=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c h1:3lbZUMbMiGUW/LMkfsEABsc5zNT9+b1CvsJx47JzJ8g=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c/go.mod h1:UrdRz5enIKZ63MEE3IF9l2/ebyx59GyGgPi+tICQdmM=
go.opencensus.io v0.18.0/go.mod h1:vKdFvxhtzZ9onBp9VKHK8z/sRpBMnKAsufL7wlDrCOA=
go4.org v0.0.0-20180809161055-417644f6feb5/go.mod h1:MkTOUMDaeVYJUOUsaDXIhWPZYa1yOyC1qaOBpL57BhE=
golang.org/x/build v0.0.0-20190111050920-041ab4dc3f9d/go.mod h1:OWs+y06UdEOHN4y+MfF/py+xQ/tYqIWW03b70/CG9Rw=
Expand Down
3 changes: 3 additions & 0 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const (
ContextPostKey = "post"
ContextChannelKey = "channel"
ContextBotKey = "bot"

requestBodyMaxSizeBytes = 1024 * 1024 // 1MB
)

func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
Expand All @@ -25,6 +27,7 @@ func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Req

router.GET("/ai_threads", p.handleGetAIThreads)
router.GET("/ai_bots", p.handleGetAIBots)
router.POST("/telemetry/track", p.handleTrackEvent)

botRequriedRouter := router.Group("")
botRequriedRouter.Use(p.aiBotRequired)
Expand Down
11 changes: 11 additions & 0 deletions server/api_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ func (p *Plugin) handleSince(c *gin.Context) {
return
}

p.track(evSummarizeUnreadMessages, map[string]interface{}{
"channel_id": channel.Id,
"user_id": user.Id,
jespino marked this conversation as resolved.
Show resolved Hide resolved
"since": data.Since,
"type": promptPreset,
"feature": map[string]string{
jespino marked this conversation as resolved.
Show resolved Hide resolved
"name": "AI",
"skus": "enterprise",
},
})

prompt, err := p.prompts.ChatCompletion(promptPreset, context)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
Expand Down
20 changes: 20 additions & 0 deletions server/api_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ func (p *Plugin) handleSummarize(c *gin.Context) {
return
}

p.track(evSummarizeThread, map[string]interface{}{
"channel_id": channel.Id,
"post_id": post.Id,
"user_id": user.Id,
"feature": map[string]string{
"name": "AI",
"skus": "enterprise",
},
})

createdPost, err := p.startNewSummaryThread(bot, post.Id, p.MakeConversationContext(bot, user, channel, nil))
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("unable to produce summary: %w", err))
Expand Down Expand Up @@ -198,6 +208,16 @@ func (p *Plugin) handleSummarizeTranscription(c *gin.Context) {
return
}

p.track(evSummarizeTranscription, map[string]interface{}{
"channel_id": channel.Id,
"post_id": post.Id,
"user_id": user.Id,
"feature": map[string]string{
"name": "AI",
"skus": "enterprise",
},
})

createdPost, err := p.newCallTranscriptionSummaryThread(bot, user, post, channel)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("unable to summarize transcription: %w", err))
Expand Down
9 changes: 9 additions & 0 deletions server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ func (p *Plugin) setConfiguration(configuration *configuration) {

// OnConfigurationChange is invoked when configuration changes may have been made.
func (p *Plugin) OnConfigurationChange() error {
serverConfig := p.API.GetConfig()
jespino marked this conversation as resolved.
Show resolved Hide resolved
if serverConfig != nil {
if err := p.initTelemetry(serverConfig.LogSettings.EnableDiagnostics); err != nil {
p.API.LogError(err.Error())
}
} else {
p.API.LogError("OnConfigurationChange: failed to get server config")
}

var configuration = new(configuration)

// Load the public configuration fields from the Mattermost server configuration.
Expand Down
4 changes: 4 additions & 0 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"github.com/mattermost/mattermost/server/public/plugin"
)

var buildHash string
var rudderWriteKey string
var rudderDataplaneURL string

func main() {
plugin.ClientMain(&Plugin{})
}
40 changes: 40 additions & 0 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/mattermost/mattermost-plugin-ai/server/ai/asksage"
"github.com/mattermost/mattermost-plugin-ai/server/ai/openai"
"github.com/mattermost/mattermost-plugin-ai/server/enterprise"
"github.com/mattermost/mattermost-plugin-ai/server/telemetry"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/pluginapi"
Expand Down Expand Up @@ -46,6 +47,9 @@ type Plugin struct {

pluginAPI *pluginapi.Client

telemetry *telemetry.Client
telemetryMut sync.RWMutex

ffmpegPath string

db *sqlx.DB
Expand Down Expand Up @@ -115,6 +119,13 @@ func (p *Plugin) OnActivate() error {
return nil
}

func (p *Plugin) OnDeactivate() error {
if err := p.uninitTelemetry(); err != nil {
p.API.LogError(err.Error())
}
return nil
}

func (p *Plugin) getLLM(llmServiceConfig ai.ServiceConfig) ai.LanguageModel {
var llm ai.LanguageModel
switch llmServiceConfig.Type {
Expand Down Expand Up @@ -233,6 +244,15 @@ func (p *Plugin) handleMentions(bot *Bot, post *model.Post, postingUser *model.U
return err
}

p.track(evAIBotMention, map[string]interface{}{
"user_id": postingUser.Id,
"bot_id": bot.mmBot.UserId,
"bot_service_type": bot.cfg.Service.Type,
"feature": map[string]string{
"name": "AI",
},
})

if err := p.processUserRequestToBot(bot, p.MakeConversationContext(bot, postingUser, channel, post)); err != nil {
return fmt.Errorf("unable to process bot mention: %w", err)
}
Expand All @@ -245,6 +265,26 @@ func (p *Plugin) handleDMs(bot *Bot, channel *model.Channel, postingUser *model.
return err
}

if post.RootId == "" {
p.track(evUserStartedConversation, map[string]interface{}{
"user_id": postingUser.Id,
"bot_id": bot.mmBot.UserId,
"bot_service_type": bot.cfg.Service.Type,
"feature": map[string]string{
"name": "AI",
},
})
} else {
p.track(evContextualInterrogation, map[string]interface{}{
"user_id": postingUser.Id,
"bot_id": bot.mmBot.UserId,
"bot_service_type": bot.cfg.Service.Type,
"feature": map[string]string{
"name": "AI",
},
})
}

if err := p.processUserRequestToBot(bot, p.MakeConversationContext(bot, postingUser, channel, post)); err != nil {
return fmt.Errorf("unable to process bot DM: %w", err)
}
Expand Down
136 changes: 136 additions & 0 deletions server/telemetry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package main

import (
"encoding/json"
"net/http"

"github.com/gin-gonic/gin"
"github.com/mattermost/mattermost-plugin-ai/server/telemetry"
)

const (
// server-side events
evUserStartedConversation = "user_started_conversation"
evContextualInterrogation = "contextual_interrogation"
evAIBotMention = "ai_bot_mention"
evSummarizeUnreadMessages = "summarize_unread_messages"
evSummarizeThread = "summarize_thread"
evSummarizeTranscription = "summarize_transcription"
)

var (
telemetryClientTypes = []string{"web", "mobile", "desktop"}
telemetryClientEvents = []string{
"copilot_apps_bar_clicked",
}
telemetryClientTypesMap map[string]struct{}
telemetryClientEventsMap map[string]struct{}
)

func init() {
telemetryClientEventsMap = make(map[string]struct{}, len(telemetryClientEvents))
for _, eventType := range telemetryClientEvents {
telemetryClientEventsMap[eventType] = struct{}{}
}
telemetryClientTypesMap = make(map[string]struct{}, len(telemetryClientTypes))
for _, clientType := range telemetryClientTypes {
telemetryClientTypesMap[clientType] = struct{}{}
}
}

type trackEventRequest struct {
Event string `json:"event"`
ClientType string `json:"clientType"`
Source string `json:"source"`
Props map[string]interface{} `json:"props"`
}

func (p *Plugin) track(ev string, props map[string]interface{}) {
p.telemetryMut.RLock()
defer p.telemetryMut.RUnlock()
if p.telemetry == nil {
return
}
if err := p.telemetry.Track(ev, props); err != nil {
p.API.LogError(err.Error())
}
}

func (p *Plugin) uninitTelemetry() error {
p.telemetryMut.Lock()
defer p.telemetryMut.Unlock()
if p.telemetry == nil {
return nil
}
return p.telemetry.Close()
}

func (p *Plugin) initTelemetry(enableDiagnostics *bool) error {
p.telemetryMut.Lock()
defer p.telemetryMut.Unlock()
if p.telemetry == nil && enableDiagnostics != nil && *enableDiagnostics {
p.API.LogDebug("Initializing telemetry")
// setup telemetry
client, err := telemetry.NewClient(telemetry.ClientConfig{
WriteKey: rudderWriteKey,
DataplaneURL: rudderDataplaneURL,
DiagnosticID: p.API.GetDiagnosticId(),
DefaultProps: map[string]interface{}{
"ServerVersion": p.API.GetServerVersion(),
"PluginVersion": manifest.Version,
"PluginBuild": buildHash,
},
})
if err != nil {
return err
}
p.telemetry = client
} else if p.telemetry != nil && (enableDiagnostics == nil || !*enableDiagnostics) {
p.API.LogDebug("Deinitializing telemetry")
// destroy telemetry
if err := p.telemetry.Close(); err != nil {
return err
}
p.telemetry = nil
}
return nil
}

func (p *Plugin) handleTrackEvent(c *gin.Context) {
p.telemetryMut.RLock()
telemetryEnabled := p.telemetry != nil
p.telemetryMut.RUnlock()

if !telemetryEnabled {
return
}

var data trackEventRequest
if err := json.NewDecoder(http.MaxBytesReader(c.Writer, c.Request.Body, requestBodyMaxSizeBytes)).Decode(&data); err != nil {
return
}

if _, ok := telemetryClientEventsMap[data.Event]; !ok {
return
}

if _, ok := telemetryClientTypesMap[data.ClientType]; !ok {
return
}

if data.Props == nil {
data.Props = map[string]interface{}{}
}

if data.Source != "" {
data.Props["Source"] = data.Source
}

data.Props["ActualUserID"] = c.GetHeader("Mattermost-User-Id")
data.Props["ClientType"] = data.ClientType

p.track(data.Event, data.Props)
}
Loading