MM-23568: Add rudder to server diagnostics. (#14151)

* MM-23568: Add rudder to server diagnostics.

* Add unit test.

* Go mod tidy.

* CSP Header fix.

* Fix review comments.

* Update web/handlers.go

Co-Authored-By: Jesse Hallam <jesse.hallam@gmail.com>

* Partially address review comments.

* fix tests.

* Finish implementing review suggestions and then fixing tests.

* Fix CSP Header tests.

Co-authored-by: Jesse Hallam <jesse.hallam@gmail.com>
Этот коммит содержится в:
George Goldberg
2020-04-21 09:23:00 +01:00
коммит произвёл GitHub
родитель 26310720be
Коммит 6cabc40e62
33 изменённых файлов: 2177 добавлений и 15 удалений

Просмотреть файл

@@ -8,6 +8,7 @@ import (
"runtime"
"strings"
rudder "github.com/rudderlabs/analytics-go"
"github.com/segmentio/analytics-go"
"github.com/mattermost/mattermost-server/v5/mlog"
@@ -16,7 +17,9 @@ import (
)
const (
SEGMENT_KEY = "placeholder_segment_key"
SEGMENT_KEY = "placeholder_segment_key"
RUDDER_KEY = "placeholder_rudder_key"
RUDDER_DATAPLANE_URL = "placeholder_rudder_dataplane_url"
TRACK_CONFIG_SERVICE = "config_service"
TRACK_CONFIG_TEAM = "config_team"
@@ -80,14 +83,37 @@ func (a *App) sendDailyDiagnostics(override bool) {
a.trackGroups()
a.trackChannelModeration()
}
if *a.Config().LogSettings.EnableDiagnostics && a.IsLeader() && ((!strings.Contains(RUDDER_KEY, "placeholder") && !strings.Contains(RUDDER_DATAPLANE_URL, "placeholder")) || override) {
a.Srv().initRudder(RUDDER_DATAPLANE_URL)
a.trackActivity()
a.trackConfig()
a.trackLicense()
a.trackPlugins()
a.trackServer()
a.trackPermissions()
a.trackElasticsearch()
a.trackGroups()
a.trackChannelModeration()
}
}
func (a *App) SendDiagnostic(event string, properties map[string]interface{}) {
a.Srv().diagnosticClient.Enqueue(analytics.Track{
Event: event,
UserId: a.DiagnosticId(),
Properties: properties,
})
if a.Srv().diagnosticClient != nil {
a.Srv().diagnosticClient.Enqueue(analytics.Track{
Event: event,
UserId: a.DiagnosticId(),
Properties: properties,
})
}
if a.Srv().rudderClient != nil {
a.Srv().rudderClient.Enqueue(rudder.Track{
Event: event,
UserId: a.DiagnosticId(),
Properties: properties,
})
}
}
func isDefault(setting interface{}, defaultValue interface{}) bool {

Просмотреть файл

@@ -63,7 +63,7 @@ func TestPluginVersion(t *testing.T) {
assert.Empty(t, pluginVersion(plugins, "unknown.plugin"))
}
func TestDiagnostics(t *testing.T) {
func TestSegmentDiagnostics(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
@@ -275,3 +275,210 @@ func TestDiagnostics(t *testing.T) {
}
})
}
func TestRudderDiagnostics(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
th := SetupWithCustomConfig(t, func(config *model.Config) {
*config.PluginSettings.Enable = false
})
defer th.TearDown()
type payload struct {
MessageId string
SentAt time.Time
Batch []struct {
MessageId string
UserId string
Event string
Timestamp time.Time
Properties map[string]interface{}
}
Context struct {
Library struct {
Name string
Version string
}
}
}
data := make(chan payload, 100)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
var p payload
err = json.Unmarshal(body, &p)
require.NoError(t, err)
data <- p
}))
defer server.Close()
diagnosticID := "test-diagnostic-id-12345"
th.App.SetDiagnosticId(diagnosticID)
th.Server.initRudder(server.URL)
assertPayload := func(t *testing.T, actual payload, event string, properties map[string]interface{}) {
t.Helper()
assert.NotEmpty(t, actual.MessageId)
assert.False(t, actual.SentAt.IsZero())
if assert.Len(t, actual.Batch, 1) {
assert.NotEmpty(t, actual.Batch[0].MessageId, "message id should not be empty")
assert.Equal(t, diagnosticID, actual.Batch[0].UserId)
if event != "" {
assert.Equal(t, event, actual.Batch[0].Event)
}
assert.False(t, actual.Batch[0].Timestamp.IsZero(), "batch timestamp should not be the zero value")
if properties != nil {
assert.Equal(t, properties, actual.Batch[0].Properties)
}
}
assert.Equal(t, "analytics-go", actual.Context.Library.Name)
assert.Equal(t, "3.0.0", actual.Context.Library.Version)
}
collectInfo := func(info *[]string) {
t.Helper()
for {
select {
case result := <-data:
assertPayload(t, result, "", nil)
*info = append(*info, result.Batch[0].Event)
case <-time.After(time.Second * 1):
return
}
}
}
// Should send a client identify message
select {
case identifyMessage := <-data:
assertPayload(t, identifyMessage, "", nil)
case <-time.After(time.Second * 1):
require.Fail(t, "Did not receive ID message")
}
t.Run("Send", func(t *testing.T) {
testValue := "test-send-value-6789"
th.App.SendDiagnostic("Testing Diagnostic", map[string]interface{}{
"hey": testValue,
})
select {
case result := <-data:
assertPayload(t, result, "Testing Diagnostic", map[string]interface{}{
"hey": testValue,
})
case <-time.After(time.Second * 1):
require.Fail(t, "Did not receive diagnostic")
}
})
// Plugins remain disabled at this point
t.Run("SendDailyDiagnosticsPluginsDisabled", func(t *testing.T) {
th.App.sendDailyDiagnostics(true)
var info []string
// Collect the info sent.
collectInfo(&info)
for _, item := range []string{
TRACK_CONFIG_SERVICE,
TRACK_CONFIG_TEAM,
TRACK_CONFIG_SQL,
TRACK_CONFIG_LOG,
TRACK_CONFIG_NOTIFICATION_LOG,
TRACK_CONFIG_FILE,
TRACK_CONFIG_RATE,
TRACK_CONFIG_EMAIL,
TRACK_CONFIG_PRIVACY,
TRACK_CONFIG_OAUTH,
TRACK_CONFIG_LDAP,
TRACK_CONFIG_COMPLIANCE,
TRACK_CONFIG_LOCALIZATION,
TRACK_CONFIG_SAML,
TRACK_CONFIG_PASSWORD,
TRACK_CONFIG_CLUSTER,
TRACK_CONFIG_METRICS,
TRACK_CONFIG_SUPPORT,
TRACK_CONFIG_NATIVEAPP,
TRACK_CONFIG_EXPERIMENTAL,
TRACK_CONFIG_ANALYTICS,
TRACK_CONFIG_PLUGIN,
TRACK_ACTIVITY,
TRACK_SERVER,
TRACK_CONFIG_MESSAGE_EXPORT,
// TRACK_PLUGINS,
} {
require.Contains(t, info, item)
}
})
// Enable plugins for the remainder of the tests.
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = true })
t.Run("SendDailyDiagnostics", func(t *testing.T) {
th.App.sendDailyDiagnostics(true)
var info []string
// Collect the info sent.
collectInfo(&info)
for _, item := range []string{
TRACK_CONFIG_SERVICE,
TRACK_CONFIG_TEAM,
TRACK_CONFIG_SQL,
TRACK_CONFIG_LOG,
TRACK_CONFIG_NOTIFICATION_LOG,
TRACK_CONFIG_FILE,
TRACK_CONFIG_RATE,
TRACK_CONFIG_EMAIL,
TRACK_CONFIG_PRIVACY,
TRACK_CONFIG_OAUTH,
TRACK_CONFIG_LDAP,
TRACK_CONFIG_COMPLIANCE,
TRACK_CONFIG_LOCALIZATION,
TRACK_CONFIG_SAML,
TRACK_CONFIG_PASSWORD,
TRACK_CONFIG_CLUSTER,
TRACK_CONFIG_METRICS,
TRACK_CONFIG_SUPPORT,
TRACK_CONFIG_NATIVEAPP,
TRACK_CONFIG_EXPERIMENTAL,
TRACK_CONFIG_ANALYTICS,
TRACK_CONFIG_PLUGIN,
TRACK_ACTIVITY,
TRACK_SERVER,
TRACK_CONFIG_MESSAGE_EXPORT,
TRACK_PLUGINS,
} {
require.Contains(t, info, item)
}
})
t.Run("SendDailyDiagnosticsNoRudderKey", func(t *testing.T) {
th.App.SendDailyDiagnostics()
select {
case <-data:
require.Fail(t, "Should not send diagnostics when the rudder key is not set")
case <-time.After(time.Second * 1):
// Did not receive diagnostics
}
})
t.Run("SendDailyDiagnosticsDisabled", func(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.LogSettings.EnableDiagnostics = false })
th.App.sendDailyDiagnostics(true)
select {
case <-data:
require.Fail(t, "Should not send diagnostics when they are disabled")
case <-time.After(time.Second * 1):
// Did not receive diagnostics
}
})
}

Просмотреть файл

@@ -20,6 +20,7 @@ import (
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/rs/cors"
rudder "github.com/rudderlabs/analytics-go"
analytics "github.com/segmentio/analytics-go"
"github.com/throttled/throttled"
"golang.org/x/crypto/acme/autocert"
@@ -115,6 +116,7 @@ type Server struct {
diagnosticId string
diagnosticClient analytics.Client
rudderClient rudder.Client
phase2PermissionsMigrationComplete bool
@@ -874,13 +876,47 @@ func (s *Server) initDiagnostics(endpoint string) {
}
}
func (s *Server) initRudder(endpoint string) {
if s.rudderClient == nil {
config := rudder.Config{}
config.Logger = rudder.StdLogger(s.Log.StdLog(mlog.String("source", "rudder")))
config.Endpoint = endpoint
// For testing
if endpoint != RUDDER_DATAPLANE_URL {
config.Verbose = true
config.BatchSize = 1
}
client, err := rudder.NewWithConfig(RUDDER_KEY, config)
if err != nil {
mlog.Error("Failed to create Rudder instance", mlog.Err(err))
return
}
client.Enqueue(rudder.Identify{
UserId: s.diagnosticId,
})
s.rudderClient = client
}
}
// shutdownDiagnostics closes the diagnostic client.
func (s *Server) shutdownDiagnostics() error {
var segmentErr, rudderErr error
if s.diagnosticClient != nil {
return s.diagnosticClient.Close()
segmentErr = s.diagnosticClient.Close()
}
return nil
if s.rudderClient != nil {
rudderErr = s.rudderClient.Close()
}
if segmentErr != nil && rudderErr != nil {
return errors.New(fmt.Sprintf("%s, %s", segmentErr.Error(), rudderErr.Error()))
} else if segmentErr != nil {
return segmentErr
}
return rudderErr
}
// GetHubs returns the list of hubs. This method is safe