mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
Rudder key and dataplane in Config (#15026)
* To allow for ease of testing telemetry changes, we should make it so that the rudder key and dataplane URL can be customized through the config or environment. * Instead of using a real config element that would be exposed to end users, we'll just use 'secret' environment variables to inject Rudder config data. Co-authored-by: Alex Dovenmuehle <alex.dovenmuehle@mattermost.com> Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
@@ -69,13 +70,33 @@ const (
|
||||
// declaring this as var to allow overriding in tests
|
||||
var SENTRY_DSN = "placeholder_sentry_dsn"
|
||||
|
||||
type RudderConfig struct {
|
||||
RudderKey string
|
||||
DataplaneUrl string
|
||||
}
|
||||
|
||||
func (s *Server) SendDailyDiagnostics() {
|
||||
s.sendDailyDiagnostics(false)
|
||||
}
|
||||
|
||||
func (s *Server) getRudderConfig() RudderConfig {
|
||||
if !strings.Contains(RUDDER_KEY, "placeholder") && !strings.Contains(RUDDER_DATAPLANE_URL, "placeholder") {
|
||||
return RudderConfig{RUDDER_KEY, RUDDER_DATAPLANE_URL}
|
||||
} else if os.Getenv("RUDDER_KEY") != "" && os.Getenv("RUDDER_DATAPLANE_URL") != "" {
|
||||
return RudderConfig{os.Getenv("RUDDER_KEY"), os.Getenv("RUDDER_DATAPLANE_URL")}
|
||||
} else {
|
||||
return RudderConfig{}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) diagnosticsEnabled() bool {
|
||||
return *s.Config().LogSettings.EnableDiagnostics && s.IsLeader()
|
||||
}
|
||||
|
||||
func (s *Server) sendDailyDiagnostics(override bool) {
|
||||
if *s.Config().LogSettings.EnableDiagnostics && s.IsLeader() && ((!strings.Contains(RUDDER_KEY, "placeholder") && !strings.Contains(RUDDER_DATAPLANE_URL, "placeholder")) || override) {
|
||||
s.initDiagnostics(RUDDER_DATAPLANE_URL)
|
||||
config := s.getRudderConfig()
|
||||
if s.diagnosticsEnabled() && ((config.DataplaneUrl != "" && config.RudderKey != "") || override) {
|
||||
s.initDiagnostics(config.DataplaneUrl, config.RudderKey)
|
||||
s.trackActivity()
|
||||
s.trackConfig()
|
||||
s.trackLicense()
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -123,7 +124,7 @@ func TestRudderDiagnostics(t *testing.T) {
|
||||
|
||||
diagnosticID := "test-diagnostic-id-12345"
|
||||
th.App.SetDiagnosticId(diagnosticID)
|
||||
th.Server.initDiagnostics(server.URL)
|
||||
th.Server.initDiagnostics(server.URL, RUDDER_KEY)
|
||||
|
||||
assertPayload := func(t *testing.T, actual payload, event string, properties map[string]interface{}) {
|
||||
t.Helper()
|
||||
@@ -334,4 +335,16 @@ func TestRudderDiagnostics(t *testing.T) {
|
||||
// Did not receive diagnostics
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("RudderConfigUsesConfigForValues", func(t *testing.T) {
|
||||
os.Setenv("RUDDER_KEY", "abc123")
|
||||
os.Setenv("RUDDER_DATAPLANE_URL", "arudderstackplace")
|
||||
defer os.Unsetenv("RUDDER_KEY")
|
||||
defer os.Unsetenv("RUDDER_DATAPLANE_URL")
|
||||
|
||||
config := th.App.Srv().getRudderConfig()
|
||||
|
||||
assert.Equal(t, "arudderstackplace", config.DataplaneUrl)
|
||||
assert.Equal(t, "abc123", config.RudderKey)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1299,7 +1299,7 @@ func (s *Server) stopSearchEngine() {
|
||||
}
|
||||
|
||||
// initDiagnostics initialises the Rudder client for the diagnostics system.
|
||||
func (s *Server) initDiagnostics(endpoint string) {
|
||||
func (s *Server) initDiagnostics(endpoint string, rudderKey string) {
|
||||
if s.rudderClient == nil {
|
||||
config := rudder.Config{}
|
||||
config.Logger = rudder.StdLogger(s.Log.StdLog(mlog.String("source", "rudder")))
|
||||
@@ -1309,7 +1309,7 @@ func (s *Server) initDiagnostics(endpoint string) {
|
||||
config.Verbose = true
|
||||
config.BatchSize = 1
|
||||
}
|
||||
client, err := rudder.NewWithConfig(RUDDER_KEY, endpoint, config)
|
||||
client, err := rudder.NewWithConfig(rudderKey, endpoint, config)
|
||||
if err != nil {
|
||||
mlog.Error("Failed to create Rudder instance", mlog.Err(err))
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user