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

Add statsd request success function on handleGetAuthKeyIDs #929

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,5 @@ func (a *autographer) handleGetAuthKeyIDs(w http.ResponseWriter, r *http.Request
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(signerIDsJSON)
a.statsWriteSuccess(r, "getauthkeyid.success")
}
22 changes: 22 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"testing"
"time"

"net/http"

log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -458,3 +460,23 @@ func TestPortOverride(t *testing.T) {
t.Errorf("expected listen %s got %s", expected, listen)
}
}

func TestStatsWriteSuccess(t *testing.T) {
t.Parallel()
req, err := http.NewRequest("GET", "https://foo.bar", nil)
if err != nil {
t.Errorf("Error setting up request.")
}
ag.statsWriteSuccess(req, "test")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, the stats object on ag would be some sort of mock or other thing that we can inspect afterwards. Or at the very least, we need to ensure that it was called appropriately.

}

func TestStatsWriteSuccessNoStats(t *testing.T) {
t.Parallel()
// No stats set on autographer
tmpag := newAutographer(1)
req, err := http.NewRequest("GET", "https://foo.bar", nil)
if err != nil {
t.Errorf("Error setting up request.")
}
tmpag.statsWriteSuccess(req, "test")
}
15 changes: 14 additions & 1 deletion stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"fmt"
"time"

"github.com/DataDog/datadog-go/statsd"
"net/http"

"github.com/DataDog/datadog-go/statsd"
log "github.com/sirupsen/logrus"
)

Expand All @@ -23,3 +25,14 @@ func (a *autographer) addStats(conf configuration) (err error) {
log.Infof("Statsd enabled at %s with namespace %s", conf.Statsd.Addr, conf.Statsd.Namespace)
return err
}

// Send statsd success request
func (a *autographer) statsWriteSuccess(r *http.Request, key string) {
if a.stats != nil {
starttime := getRequestStartTime(r)
sendStatsErr := a.stats.Timing(key, time.Since(starttime), nil, 1.0)
if sendStatsErr != nil {
log.Warnf("Error sending statsd on success %s: %s", key, sendStatsErr)
}
}
}