[MM-16620] Properly handle analytics client life cycle

Properly handle analytics client life cycle

This change improves the life cycle management of the analytics
client. It does so in two primary ways:
 - Move the client from being a shared pointer to belonging to the
   Server struct.
 - Expose the client.Close() method for proper shutdown of the
   client.
Этот коммит содержится в:
Gabe Jackson
2019-07-03 12:28:51 -04:00
коммит произвёл GitHub
родитель df869e3f86
Коммит f56a8f5a99
3 изменённых файлов: 59 добавлений и 58 удалений

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

@@ -20,6 +20,7 @@ import (
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/rs/cors"
analytics "github.com/segmentio/analytics-go"
"github.com/throttled/throttled"
"golang.org/x/crypto/acme/autocert"
@@ -100,7 +101,9 @@ type Server struct {
clientConfig map[string]string
clientConfigHash string
limitedClientConfig map[string]string
diagnosticId string
diagnosticId string
diagnosticClient *analytics.Client
phase2PermissionsMigrationComplete bool
@@ -321,6 +324,11 @@ func (s *Server) Shutdown() error {
s.RunOldAppShutdown()
err := s.shutdownDiagnostics()
if err != nil {
mlog.Error(fmt.Sprintf("Unable to cleanly shutdown diagnostic client: %s", err))
}
s.StopHTTPServer()
s.WaitForGoroutines()
@@ -734,3 +742,30 @@ func (s *Server) StartElasticsearch() {
}
})
}
func (s *Server) initDiagnostics(endpoint string) {
if s.diagnosticClient == nil {
client := analytics.New(SEGMENT_KEY)
client.Logger = s.Log.StdLog(mlog.String("source", "segment"))
// For testing
if endpoint != "" {
client.Endpoint = endpoint
client.Verbose = true
client.Size = 1
}
client.Identify(&analytics.Identify{
UserId: s.diagnosticId,
})
s.diagnosticClient = client
}
}
// ShutdownDiagnostics closes the diagnostic client.
func (s *Server) shutdownDiagnostics() error {
if s.diagnosticClient != nil {
return s.diagnosticClient.Close()
}
return nil
}