2020-10-19 14:55:20 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
|
|
package telemetry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-06-04 16:38:49 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/mlog"
|
2021-01-27 00:13:46 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/scheduler"
|
2020-10-19 14:55:20 +02:00
|
|
|
rudder "github.com/rudderlabs/analytics-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-10-22 13:34:42 +02:00
|
|
|
rudderKey = "placeholder_rudder_key"
|
|
|
|
rudderDataplaneURL = "placeholder_rudder_dataplane_url"
|
|
|
|
timeBetweenTelemetryChecks = 10 * time.Minute
|
2020-10-19 14:55:20 +02:00
|
|
|
)
|
|
|
|
|
2021-06-04 16:38:49 +02:00
|
|
|
type TrackerFunc func() (Tracker, error)
|
|
|
|
|
|
|
|
type Tracker map[string]interface{}
|
2020-10-19 14:55:20 +02:00
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
type Service struct {
|
2021-06-04 16:38:49 +02:00
|
|
|
trackers map[string]TrackerFunc
|
|
|
|
logger *mlog.Logger
|
2020-10-19 14:55:20 +02:00
|
|
|
rudderClient rudder.Client
|
|
|
|
telemetryID string
|
|
|
|
timestampLastTelemetrySent time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type RudderConfig struct {
|
|
|
|
RudderKey string
|
2020-10-22 13:34:42 +02:00
|
|
|
DataplaneURL string
|
2020-10-19 14:55:20 +02:00
|
|
|
}
|
|
|
|
|
2021-06-04 16:38:49 +02:00
|
|
|
func New(telemetryID string, logger *mlog.Logger) *Service {
|
2020-10-22 13:34:42 +02:00
|
|
|
service := &Service{
|
2021-06-04 16:38:49 +02:00
|
|
|
logger: logger,
|
2020-10-19 14:55:20 +02:00
|
|
|
telemetryID: telemetryID,
|
2021-06-04 16:38:49 +02:00
|
|
|
trackers: map[string]TrackerFunc{},
|
2020-10-19 14:55:20 +02:00
|
|
|
}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-19 14:55:20 +02:00
|
|
|
return service
|
|
|
|
}
|
|
|
|
|
2021-06-04 16:38:49 +02:00
|
|
|
func (ts *Service) RegisterTracker(name string, f TrackerFunc) {
|
|
|
|
ts.trackers[name] = f
|
2020-10-19 14:55:20 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
func (ts *Service) getRudderConfig() RudderConfig {
|
|
|
|
if !strings.Contains(rudderKey, "placeholder") && !strings.Contains(rudderDataplaneURL, "placeholder") {
|
|
|
|
return RudderConfig{rudderKey, rudderDataplaneURL}
|
2021-07-09 03:09:02 +02:00
|
|
|
}
|
|
|
|
if os.Getenv("RUDDER_KEY") != "" && os.Getenv("RUDDER_DATAPLANE_URL") != "" {
|
2020-10-19 14:55:20 +02:00
|
|
|
return RudderConfig{os.Getenv("RUDDER_KEY"), os.Getenv("RUDDER_DATAPLANE_URL")}
|
|
|
|
}
|
2021-07-09 03:09:02 +02:00
|
|
|
return RudderConfig{}
|
2020-10-19 14:55:20 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
func (ts *Service) sendDailyTelemetry(override bool) {
|
2020-10-19 14:55:20 +02:00
|
|
|
config := ts.getRudderConfig()
|
2020-10-22 13:34:42 +02:00
|
|
|
if (config.DataplaneURL != "" && config.RudderKey != "") || override {
|
|
|
|
ts.initRudder(config.DataplaneURL, config.RudderKey)
|
|
|
|
|
2020-10-19 14:55:20 +02:00
|
|
|
for name, tracker := range ts.trackers {
|
2021-06-04 16:38:49 +02:00
|
|
|
m, err := tracker()
|
|
|
|
if err != nil {
|
|
|
|
ts.logger.Error("Error fetching telemetry data", mlog.String("name", name), mlog.Err(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ts.sendTelemetry(name, m)
|
2020-10-19 14:55:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
func (ts *Service) sendTelemetry(event string, properties map[string]interface{}) {
|
2020-10-19 14:55:20 +02:00
|
|
|
if ts.rudderClient != nil {
|
|
|
|
var context *rudder.Context
|
2021-06-21 11:21:42 +02:00
|
|
|
_ = ts.rudderClient.Enqueue(rudder.Track{
|
2020-10-19 14:55:20 +02:00
|
|
|
Event: event,
|
|
|
|
UserId: ts.telemetryID,
|
|
|
|
Properties: properties,
|
|
|
|
Context: context,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 10:28:26 +02:00
|
|
|
func (ts *Service) initRudder(endpoint, rudderKey string) {
|
2020-10-19 14:55:20 +02:00
|
|
|
if ts.rudderClient == nil {
|
|
|
|
config := rudder.Config{}
|
2021-06-04 16:38:49 +02:00
|
|
|
config.Logger = rudder.StdLogger(ts.logger.StdLogger(mlog.Telemetry))
|
2020-10-19 14:55:20 +02:00
|
|
|
config.Endpoint = endpoint
|
|
|
|
// For testing
|
2020-10-22 13:34:42 +02:00
|
|
|
if endpoint != rudderDataplaneURL {
|
2020-10-19 14:55:20 +02:00
|
|
|
config.Verbose = true
|
|
|
|
config.BatchSize = 1
|
|
|
|
}
|
|
|
|
client, err := rudder.NewWithConfig(rudderKey, endpoint, config)
|
|
|
|
if err != nil {
|
2021-06-04 16:38:49 +02:00
|
|
|
ts.logger.Fatal("Failed to create Rudder instance")
|
2020-10-19 14:55:20 +02:00
|
|
|
return
|
|
|
|
}
|
2021-06-21 11:21:42 +02:00
|
|
|
_ = client.Enqueue(rudder.Identify{
|
2020-10-19 14:55:20 +02:00
|
|
|
UserId: ts.telemetryID,
|
|
|
|
})
|
|
|
|
|
|
|
|
ts.rudderClient = client
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
func (ts *Service) doTelemetryIfNeeded(firstRun time.Time) {
|
2020-10-19 14:55:20 +02:00
|
|
|
hoursSinceFirstServerRun := time.Since(firstRun).Hours()
|
2021-07-09 03:09:02 +02:00
|
|
|
|
2020-10-19 14:55:20 +02:00
|
|
|
// Send once every 10 minutes for the first hour
|
|
|
|
if hoursSinceFirstServerRun < 1 {
|
|
|
|
ts.doTelemetry()
|
2021-07-09 03:09:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send once every hour thereafter for the first 12 hours
|
|
|
|
if hoursSinceFirstServerRun <= 12 && time.Since(ts.timestampLastTelemetrySent) >= time.Hour {
|
2020-10-19 14:55:20 +02:00
|
|
|
ts.doTelemetry()
|
2021-07-09 03:09:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send at the 24 hour mark and every 24 hours after
|
|
|
|
if hoursSinceFirstServerRun > 12 && time.Since(ts.timestampLastTelemetrySent) >= 24*time.Hour {
|
2020-10-19 14:55:20 +02:00
|
|
|
ts.doTelemetry()
|
2021-07-09 03:09:02 +02:00
|
|
|
return
|
2020-10-19 14:55:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
func (ts *Service) RunTelemetryJob(firstRun int64) {
|
2020-10-19 14:55:20 +02:00
|
|
|
// Send on boot
|
|
|
|
ts.doTelemetry()
|
|
|
|
scheduler.CreateRecurringTask("Telemetry", func() {
|
|
|
|
ts.doTelemetryIfNeeded(time.Unix(0, firstRun*int64(time.Millisecond)))
|
2020-10-22 13:34:42 +02:00
|
|
|
}, timeBetweenTelemetryChecks)
|
2020-10-19 14:55:20 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
func (ts *Service) doTelemetry() {
|
2020-10-19 14:55:20 +02:00
|
|
|
ts.timestampLastTelemetrySent = time.Now()
|
|
|
|
ts.sendDailyTelemetry(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown closes the telemetry client.
|
2020-10-22 13:34:42 +02:00
|
|
|
func (ts *Service) Shutdown() error {
|
2020-10-19 14:55:20 +02:00
|
|
|
if ts.rudderClient != nil {
|
|
|
|
return ts.rudderClient.Close()
|
|
|
|
}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-19 14:55:20 +02:00
|
|
|
return nil
|
|
|
|
}
|