Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): allow analytics cfg override #3186

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type noopTracker struct{}
func (t noopTracker) Track(event string, props map[string]string) error { return nil }
func (t noopTracker) Ready() bool { return true }

func Init(enabled bool, serverID, appVersion, env string) error {
func Init(enabled bool, serverID, appVersion, env, secretKey, frontendKey string) error {
// ga not enabled, use dumb settings
if !enabled {
defaultClient = noopTracker{}
Expand All @@ -29,7 +29,7 @@ func Init(enabled bool, serverID, appVersion, env string) error {
return fmt.Errorf("could not get hostname: %w", err)
}

defaultClient = newSegmentTracker(hostname, serverID, appVersion, env)
defaultClient = newSegmentTracker(hostname, serverID, appVersion, env, secretKey, frontendKey)

return nil
}
Expand Down
10 changes: 5 additions & 5 deletions server/analytics/analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (
)

func TestReadyness(t *testing.T) {
analytics.Init(false, "serverID", "1.0", "env")
analytics.Init(false, "serverID", "1.0", "env", "123", "123")
assert.True(t, analytics.Ready())

analytics.Init(true, "serverID", "1.0", "env")
analytics.Init(true, "serverID", "1.0", "env", "123", "123")
assert.True(t, analytics.Ready())

analytics.Init(true, "serverID", "", "env")
analytics.Init(true, "serverID", "", "env", "", "")
assert.False(t, analytics.Ready())

analytics.Init(true, "", "1.0", "env")
analytics.Init(true, "", "1.0", "env", "", "")
assert.False(t, analytics.Ready())

analytics.Init(true, "serverID", "1.0", "")
analytics.Init(true, "serverID", "1.0", "", "", "")
assert.False(t, analytics.Ready())

}
10 changes: 9 additions & 1 deletion server/analytics/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ var (
FrontendKey = ""
)

func newSegmentTracker(hostname, serverID, appVersion, env string) Tracker {
func newSegmentTracker(hostname, serverID, appVersion, env, secretKey, frontendKey string) Tracker {
if secretKey != "" {
SecretKey = secretKey
}

if frontendKey != "" {
FrontendKey = frontendKey
}

client, _ := segment.NewWithConfig(SecretKey, segment.Config{
BatchSize: 1,
})
Expand Down
2 changes: 1 addition & 1 deletion server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (app *App) subscribeToConfigChanges(sm *subscription.Manager) {
}

func (app *App) initAnalytics(configFromDB config.Config) error {
return analytics.Init(configFromDB.IsAnalyticsEnabled(), app.serverID, Version, Env)
return analytics.Init(configFromDB.IsAnalyticsEnabled(), app.serverID, Version, Env, app.cfg.AnalyticsServerKey(), app.cfg.AnalyticsFrontendKey())
}

var instanceID = id.GenerateID().String()
Expand Down
24 changes: 24 additions & 0 deletions server/config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ var serverOptions = options{
defaultValue: "true",
description: "enable otlp server",
},
{
key: "analytics.serverKey",
defaultValue: "",
description: "analytics server key",
},
{
key: "analytics.frontendKey",
defaultValue: "",
description: "analytics frontend key",
},
}

func init() {
Expand Down Expand Up @@ -181,3 +191,17 @@ func (c *AppConfig) OtlpServerEnabled() bool {

return c.vp.GetString("otlpServer.enabled") == "true"
}

func (c *AppConfig) AnalyticsServerKey() string {
c.mu.Lock()
defer c.mu.Unlock()

return c.vp.GetString("analytics.serverKey")
}

func (c *AppConfig) AnalyticsFrontendKey() string {
c.mu.Lock()
defer c.mu.Unlock()

return c.vp.GetString("analytics.frontendKey")
}