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

remove need for nil checks around statsd metrics #932

Merged
merged 1 commit into from
Aug 1, 2024
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
11 changes: 5 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type configuration struct {
// with all signers and permissions configured
type autographer struct {
db *database.Handler
stats *statsd.Client
stats statsd.ClientInterface
nonces *lru.Cache
debug bool
heartbeatConf *heartbeatConfig
Expand Down Expand Up @@ -166,11 +166,9 @@ func run(conf configuration, listen string, debug bool) {
ag.initHSM(conf)
}

if conf.Statsd.Addr != "" {
err = ag.addStats(conf)
if err != nil {
log.Fatal(err)
}
err = ag.addStats(conf)
if err != nil {
log.Fatal(err)
}

err = ag.addSigners(conf.Signers)
Expand Down Expand Up @@ -292,6 +290,7 @@ func newAutographer(cachesize int) (a *autographer) {
a.authBackend = newInMemoryAuthBackend()
a.nonces, err = lru.New(cachesize)
a.exit = make(chan interface{})
a.stats = &statsd.NoOpClient{}
if err != nil {
log.Fatal(err)
}
Expand Down
8 changes: 3 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ func TestMain(m *testing.M) {
if err != nil {
log.Fatal(err)
}
if conf.Statsd.Addr != "" {
err = ag.addStats(conf)
if err != nil {
log.Fatal(err)
}
err = ag.addStats(conf)
if err != nil {
log.Fatal(err)
}
if conf.HawkTimestampValidity != "" {
ag.hawkMaxTimestampSkew, err = time.ParseDuration(conf.HawkTimestampValidity)
Expand Down
7 changes: 2 additions & 5 deletions signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,14 +523,11 @@ type StatsClient struct {
signerTags []string

// stats is the statsd client for reporting metrics
stats *statsd.Client
stats statsd.ClientInterface
}

// NewStatsClient makes a new stats client
func NewStatsClient(signerConfig Configuration, stats *statsd.Client) (*StatsClient, error) {
if stats == nil {
return nil, fmt.Errorf("xpi: statsd client is nil. Could not create StatsClient for signer %s", signerConfig.ID)
}
func NewStatsClient(signerConfig Configuration, stats statsd.ClientInterface) (*StatsClient, error) {
return &StatsClient{
stats: stats,
signerTags: []string{
Expand Down
7 changes: 1 addition & 6 deletions signer/xpi/xpi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ func TestSignFile(t *testing.T) {
StatsSampleRate: 10 * time.Second,
}

statsdClient, err := statsd.NewBuffered("localhost:8135", 1)
if err != nil {
t.Fatalf("passing testcase %d: Error constructing statsdClient: %v", i, err)
}
statsdClient.Namespace = "test_autograph_stats_ns"
signerStatsClient, err := signer.NewStatsClient(testcase, statsdClient)
signerStatsClient, err := signer.NewStatsClient(testcase, &statsd.NoOpClient{})
if err != nil {
t.Fatalf("passing testcase %d: Error constructing signer.StatsdClient: %v", i, err)
}
Expand Down
17 changes: 14 additions & 3 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,19 @@ func loadStatsd(conf configuration) (*statsd.Client, error) {
return statsdClient, nil
}

func (a *autographer) addStats(conf configuration) (err error) {
a.stats, err = loadStatsd(conf)
func (a *autographer) addStats(conf configuration) error {
if conf.Statsd.Addr == "" {
// a.stats is set to a safe value in newAutographer, so we leave it
// alone and return.
log.Infof("Statsd left disabled as no `statsd.addr` was provided in config")
return nil
}

stats, err := loadStatsd(conf)
if err != nil {
return err
}
a.stats = stats
log.Infof("Statsd enabled at %s with namespace %s", conf.Statsd.Addr, conf.Statsd.Namespace)
return err
return nil
}
Loading