Skip to content

Commit b2e73af

Browse files
authored
Merge pull request #43 from mozilla-services/add-statsd
Add statsd
2 parents 23cb06d + 68c9344 commit b2e73af

21 files changed

+1841
-20
lines changed

glide.lock

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stubservice/main.go

+31-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"go.mozilla.org/mozlogrus"
1111

1212
"github.com/Sirupsen/logrus"
13+
"github.com/alexcesaro/statsd"
1314
"github.com/aws/aws-sdk-go/aws"
1415
"github.com/aws/aws-sdk-go/aws/ec2metadata"
1516
"github.com/aws/aws-sdk-go/aws/session"
@@ -18,6 +19,7 @@ import (
1819
"github.com/mozilla-services/stubattribution/attributioncode"
1920
"github.com/mozilla-services/stubattribution/stubservice/backends"
2021
"github.com/mozilla-services/stubattribution/stubservice/stubhandlers"
22+
"github.com/oremj/asyncstatsd"
2123
)
2224

2325
const hmacTimeoutDefault = 10 * time.Minute
@@ -33,13 +35,26 @@ var (
3335
s3Bucket = os.Getenv("S3_BUCKET")
3436
s3Prefix = os.Getenv("S3_PREFIX")
3537

38+
statsdPrefix = os.Getenv("STATSD_PREFIX")
39+
statsdAddr = os.Getenv("STATSD_ADDR")
40+
3641
cdnPrefix = os.Getenv("CDN_PREFIX")
3742

3843
addr = os.Getenv("ADDR")
3944

4045
sentryDSN = os.Getenv("SENTRY_DSN")
4146
)
4247

48+
var statsdClient asyncstatsd.Client
49+
50+
func mustStatsd(opts ...statsd.Option) *statsd.Client {
51+
c, err := statsd.New(opts...)
52+
if err != nil {
53+
logrus.WithError(err).Fatal("Could not initiate statsd")
54+
}
55+
return c
56+
}
57+
4358
func init() {
4459
mozlogrus.Enable("StubAttribution")
4560

@@ -85,6 +100,17 @@ func init() {
85100
awsSess = awsSess.Copy(&aws.Config{Region: aws.String(region)})
86101
}
87102
}
103+
104+
if statsdAddr == "" {
105+
statsdAddr = "127.0.0.1:8125"
106+
}
107+
if statsdPrefix == "" {
108+
statsdPrefix = "stubattribution"
109+
}
110+
statsdClient = asyncstatsd.New(mustStatsd(
111+
statsd.Prefix(statsdPrefix),
112+
statsd.Address(statsdAddr),
113+
), 10000)
88114
}
89115

90116
func okHandler(w http.ResponseWriter, req *http.Request) {
@@ -119,10 +145,11 @@ func main() {
119145
stubHandler = stubhandlers.NewDirectHandler()
120146
}
121147

122-
stubService := &stubhandlers.StubService{
123-
Handler: stubHandler,
124-
AttributionCodeValidator: attributioncode.NewValidator(hmacKey, hmacTimeout),
125-
}
148+
stubService := stubhandlers.NewStubService(
149+
stubHandler,
150+
attributioncode.NewValidator(hmacKey, hmacTimeout),
151+
statsdClient,
152+
)
126153

127154
mux := http.NewServeMux()
128155
mux.Handle("/", stubService)

stubservice/stubhandlers/stubhandler_test.go

+9-12
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,9 @@ func TestRedirectFull(t *testing.T) {
113113
BouncerURL = "https://download.mozilla.org/"
114114
}()
115115

116-
svc := &StubService{
117-
AttributionCodeValidator: &attributioncode.Validator{},
118-
Handler: NewRedirectHandler(storage, server.URL+"/cdn/", ""),
119-
}
116+
svc := NewStubService(
117+
NewRedirectHandler(storage, server.URL+"/cdn/", ""),
118+
&attributioncode.Validator{}, nil)
120119

121120
recorder := httptest.NewRecorder()
122121
attributionCode := "campaign=%28not+set%29&content=%28not+set%29&medium=organic&source=www.google.com"
@@ -176,10 +175,9 @@ func TestDirectFull(t *testing.T) {
176175
BouncerURL = "https://download.mozilla.org/"
177176
}()
178177

179-
svc := &StubService{
180-
AttributionCodeValidator: &attributioncode.Validator{},
181-
Handler: NewDirectHandler(),
182-
}
178+
svc := NewStubService(
179+
NewDirectHandler(),
180+
&attributioncode.Validator{}, nil)
183181

184182
recorder := httptest.NewRecorder()
185183
attributionCode := "campaign=%28not+set%29&content=%28not+set%29&medium=organic&source=www.google.com"
@@ -206,10 +204,9 @@ func TestDirectFull(t *testing.T) {
206204
}
207205

208206
func TestStubServiceErrorCases(t *testing.T) {
209-
svc := &StubService{
210-
AttributionCodeValidator: &attributioncode.Validator{},
211-
Handler: NewDirectHandler(),
212-
}
207+
svc := NewStubService(
208+
NewDirectHandler(),
209+
&attributioncode.Validator{}, nil)
213210

214211
fetchURL := func(url string) *httptest.ResponseRecorder {
215212
recorder := httptest.NewRecorder()

stubservice/stubhandlers/stubservice.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,37 @@ import (
55

66
"github.com/Sirupsen/logrus"
77
"github.com/mozilla-services/stubattribution/attributioncode"
8+
"github.com/oremj/asyncstatsd"
89
)
910

10-
// StubService serves redirects or modified stubs
11-
type StubService struct {
11+
type stubService struct {
1212
Handler StubHandler
1313

1414
AttributionCodeValidator *attributioncode.Validator
15+
Statsd asyncstatsd.Client
1516
}
1617

17-
func (s *StubService) ServeHTTP(w http.ResponseWriter, req *http.Request) {
18+
func NewStubService(stubHandler StubHandler, validator *attributioncode.Validator, statsd asyncstatsd.Client) http.Handler {
19+
if statsd == nil {
20+
statsd = asyncstatsd.NewNoop()
21+
}
22+
return &stubService{
23+
Handler: stubHandler,
24+
AttributionCodeValidator: validator,
25+
Statsd: statsd,
26+
}
27+
}
28+
29+
func (s *stubService) ServeHTTP(w http.ResponseWriter, req *http.Request) {
30+
defer s.Statsd.NewTiming().Send("request.time")
31+
defer s.Statsd.Increment("request.count")
32+
1833
query := req.URL.Query()
1934

2035
redirectBouncer := func() {
2136
backupURL := bouncerURL(query.Get("product"), query.Get("lang"), query.Get("os"))
2237
http.Redirect(w, req, backupURL, http.StatusFound)
38+
defer s.Statsd.Increment("request.error")
2339
}
2440

2541
attributionCode := query.Get("attribution_code")

vendor/github.com/alexcesaro/statsd/.travis.yml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/alexcesaro/statsd/CHANGELOG.md

+64
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/alexcesaro/statsd/LICENSE

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/alexcesaro/statsd/README.md

+50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)