satellite/analytics: Add flag for disabling analytics (default disabled)

Just in case we need to have an easy flag for turning analytics on or
off.

Change-Id: I219071200e0a4c156ba92cbbaa4de4ab75b2cb68
This commit is contained in:
Moby von Briesen 2021-03-27 23:43:22 +01:00 committed by Maximillian von Briesen
parent a65aecfd98
commit 23c8331d1f
2 changed files with 26 additions and 11 deletions

View File

@ -17,6 +17,7 @@ const (
// Config is a configuration struct for analytics Service.
type Config struct {
SegmentWriteKey string `help:"segment write key" default:""`
Enabled bool `help:"enable analytics reporting" default:"false"`
}
// Service for sending analytics.
@ -32,17 +33,23 @@ type Service struct {
// NewService creates new service for creating sending analytics.
func NewService(log *zap.Logger, config Config, satelliteName string) *Service {
return &Service{
service := &Service{
log: log,
config: config,
satelliteName: satelliteName,
segment: segment.New(config.SegmentWriteKey),
}
if config.Enabled {
service.segment = segment.New(config.SegmentWriteKey)
}
return service
}
// Close closes the Segment client.
func (service *Service) Close() error {
if !service.config.Enabled {
return nil
}
return service.segment.Close()
}
@ -67,19 +74,27 @@ type TrackCreateUserFields struct {
JobTitle string
}
func (service *Service) enqueueMessage(message segment.Message) {
if !service.config.Enabled {
return
}
err := service.segment.Enqueue(message)
if err != nil {
service.log.Error("Error enqueueing message", zap.Error(err))
}
}
// TrackCreateUser sends an "Account Created" event to Segment.
func (service *Service) TrackCreateUser(fields TrackCreateUserFields) {
traits := segment.NewTraits()
traits.SetName(fields.FullName)
traits.SetEmail(fields.Email)
err := service.segment.Enqueue(segment.Identify{
service.enqueueMessage(segment.Identify{
UserId: fields.ID.String(),
Traits: traits,
})
if err != nil {
service.log.Error("Error with identify event", zap.Error(err))
}
props := segment.NewProperties()
props.Set("email", fields.Email)
@ -93,12 +108,9 @@ func (service *Service) TrackCreateUser(fields TrackCreateUserFields) {
props.Set("job_title", fields.JobTitle)
}
err = service.segment.Enqueue(segment.Track{
service.enqueueMessage(segment.Track{
UserId: fields.ID.String(),
Event: eventAccountCreated,
Properties: props,
})
if err != nil {
service.log.Error("Error with track event", zap.Error(err))
}
}

View File

@ -1,6 +1,9 @@
# admin peer http listening address
# admin.address: ""
# enable analytics reporting
# analytics.enabled: false
# segment write key
# analytics.segment-write-key: ""