* MM-23574: Eliminate Segment.

* Update dependencies.

* Fix review suggestions.
Этот коммит содержится в:
George Goldberg
2020-06-22 08:35:03 +01:00
коммит произвёл GitHub
родитель 6158e91308
Коммит e5addef19b
35 изменённых файлов: 22 добавлений и 2184 удалений

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

@@ -8,16 +8,13 @@ import (
"runtime"
"strings"
rudder "github.com/rudderlabs/analytics-go"
"github.com/segmentio/analytics-go"
"github.com/mattermost/mattermost-server/v5/mlog"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/store"
rudder "github.com/rudderlabs/analytics-go"
)
const (
SEGMENT_KEY = "placeholder_segment_key"
RUDDER_KEY = "placeholder_rudder_key"
RUDDER_DATAPLANE_URL = "placeholder_rudder_dataplane_url"
@@ -75,21 +72,8 @@ func (s *Server) SendDailyDiagnostics() {
}
func (s *Server) sendDailyDiagnostics(override bool) {
if *s.Config().LogSettings.EnableDiagnostics && s.IsLeader() && (!strings.Contains(SEGMENT_KEY, "placeholder") || override) {
s.initDiagnostics("")
s.trackActivity()
s.trackConfig()
s.trackLicense()
s.trackPlugins()
s.trackServer()
s.trackPermissions()
s.trackElasticsearch()
s.trackGroups()
s.trackChannelModeration()
}
if *s.Config().LogSettings.EnableDiagnostics && s.IsLeader() && ((!strings.Contains(RUDDER_KEY, "placeholder") && !strings.Contains(RUDDER_DATAPLANE_URL, "placeholder")) || override) {
s.initRudder(RUDDER_DATAPLANE_URL)
s.initDiagnostics(RUDDER_DATAPLANE_URL)
s.trackActivity()
s.trackConfig()
s.trackLicense()
@@ -103,14 +87,6 @@ func (s *Server) sendDailyDiagnostics(override bool) {
}
func (s *Server) SendDiagnostic(event string, properties map[string]interface{}) {
if s.diagnosticClient != nil {
s.diagnosticClient.Enqueue(analytics.Track{
Event: event,
UserId: s.diagnosticId,
Properties: properties,
})
}
if s.rudderClient != nil {
s.rudderClient.Enqueue(rudder.Track{
Event: event,

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

@@ -63,219 +63,6 @@ func TestPluginVersion(t *testing.T) {
assert.Empty(t, pluginVersion(plugins, "unknown.plugin"))
}
func TestSegmentDiagnostics(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.initDiagnostics(server.URL)
assertPayload := func(t *testing.T, actual payload, event string, properties map[string]interface{}) {
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)
}
// 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.Srv().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.Srv().sendDailyDiagnostics(true)
var info []string
// Collect the info sent.
Loop:
for {
select {
case result := <-data:
assertPayload(t, result, "", nil)
info = append(info, result.Batch[0].Event)
case <-time.After(time.Second * 1):
break Loop
}
}
for _, item := range []string{
TRACK_CONFIG_SERVICE,
TRACK_CONFIG_TEAM,
TRACK_CONFIG_SQL,
TRACK_CONFIG_LOG,
TRACK_CONFIG_AUDIT,
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.Srv().sendDailyDiagnostics(true)
var info []string
// Collect the info sent.
Loop:
for {
select {
case result := <-data:
assertPayload(t, result, "", nil)
info = append(info, result.Batch[0].Event)
case <-time.After(time.Second * 1):
break Loop
}
}
for _, item := range []string{
TRACK_CONFIG_SERVICE,
TRACK_CONFIG_TEAM,
TRACK_CONFIG_SQL,
TRACK_CONFIG_LOG,
TRACK_CONFIG_AUDIT,
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("SendDailyDiagnosticsNoSegmentKey", func(t *testing.T) {
th.App.Srv().SendDailyDiagnostics()
select {
case <-data:
require.Fail(t, "Should not send diagnostics when the segment 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.Srv().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
}
})
}
func TestRudderDiagnostics(t *testing.T) {
if testing.Short() {
t.SkipNow()
@@ -319,7 +106,7 @@ func TestRudderDiagnostics(t *testing.T) {
diagnosticID := "test-diagnostic-id-12345"
th.App.SetDiagnosticId(diagnosticID)
th.Server.initRudder(server.URL)
th.Server.initDiagnostics(server.URL)
assertPayload := func(t *testing.T, actual payload, event string, properties map[string]interface{}) {
t.Helper()

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

@@ -24,7 +24,6 @@ import (
"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"
@@ -132,9 +131,8 @@ type Server struct {
clientConfigHash atomic.Value
limitedClientConfig atomic.Value
diagnosticId string
diagnosticClient analytics.Client
rudderClient rudder.Client
diagnosticId string
rudderClient rudder.Client
phase2PermissionsMigrationComplete bool
@@ -1158,26 +1156,8 @@ func (s *Server) stopSearchEngine() {
}
}
// initDiagnostics initialises the Rudder client for the diagnostics system.
func (s *Server) initDiagnostics(endpoint string) {
if s.diagnosticClient == nil {
config := analytics.Config{}
config.Logger = analytics.StdLogger(s.Log.StdLog(mlog.String("source", "segment")))
// For testing
if endpoint != "" {
config.Endpoint = endpoint
config.Verbose = true
config.BatchSize = 1
}
client, _ := analytics.NewWithConfig(SEGMENT_KEY, config)
client.Enqueue(analytics.Identify{
UserId: s.diagnosticId,
})
s.diagnosticClient = client
}
}
func (s *Server) initRudder(endpoint string) {
if s.rudderClient == nil {
config := rudder.Config{}
config.Logger = rudder.StdLogger(s.Log.StdLog(mlog.String("source", "rudder")))
@@ -1200,24 +1180,13 @@ func (s *Server) initRudder(endpoint string) {
}
}
// shutdownDiagnostics closes the diagnostic client.
// shutdownDiagnostics closes the diagnostics system Rudder client.
func (s *Server) shutdownDiagnostics() error {
var segmentErr, rudderErr error
if s.diagnosticClient != nil {
segmentErr = s.diagnosticClient.Close()
}
if s.rudderClient != nil {
rudderErr = s.rudderClient.Close()
return 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
return nil
}
// GetHubs returns the list of hubs. This method is safe