Skip to content

Commit 0aa2e93

Browse files
authored
Log with logrus (#42)
* vendor: add mozlogrus, logrus and logrus_sentry * stubservice: log with logrus
1 parent a0cbf43 commit 0aa2e93

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+5756
-36
lines changed

glide.lock

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

stubservice/main.go

+20-16
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ package main
33
import (
44
"fmt"
55
"io/ioutil"
6-
"log"
76
"net/http"
87
"os"
98
"time"
109

11-
"go.mozilla.org/mozlog"
10+
"go.mozilla.org/mozlogrus"
1211

12+
"github.com/Sirupsen/logrus"
1313
"github.com/aws/aws-sdk-go/aws"
1414
"github.com/aws/aws-sdk-go/aws/ec2metadata"
1515
"github.com/aws/aws-sdk-go/aws/session"
1616
"github.com/aws/aws-sdk-go/service/s3"
17-
raven "github.com/getsentry/raven-go"
17+
"github.com/evalphobia/logrus_sentry"
1818
"github.com/mozilla-services/stubattribution/attributioncode"
1919
"github.com/mozilla-services/stubattribution/stubservice/backends"
2020
"github.com/mozilla-services/stubattribution/stubservice/stubhandlers"
@@ -37,12 +37,11 @@ var (
3737

3838
addr = os.Getenv("ADDR")
3939

40-
sentryDSN = os.Getenv("SENTRY_DSN")
41-
ravenClient *raven.Client
40+
sentryDSN = os.Getenv("SENTRY_DSN")
4241
)
4342

4443
func init() {
45-
mozlog.Logger.LoggerName = "StubAttribution"
44+
mozlogrus.Enable("StubAttribution")
4645

4746
switch returnMode {
4847
case "redirect":
@@ -59,18 +58,21 @@ func init() {
5958
addr = "127.0.0.1:8000"
6059
}
6160
if sentryDSN != "" {
62-
var err error
63-
ravenClient, err = raven.New(sentryDSN)
61+
hook, err := logrus_sentry.NewSentryHook(sentryDSN, []logrus.Level{
62+
logrus.PanicLevel,
63+
logrus.FatalLevel,
64+
logrus.ErrorLevel,
65+
})
6466
if err != nil {
65-
log.Printf("SetDSN: %v", err)
66-
ravenClient = nil
67+
logrus.WithError(err).Fatal("Could not create raven client")
6768
}
69+
logrus.AddHook(hook)
6870
}
6971

7072
if hmacTimeoutEnv != "" {
7173
d, err := time.ParseDuration(hmacTimeoutEnv)
7274
if err != nil {
73-
log.Fatalf("Could not parse HMAC_TIMEOUT: %v", err)
75+
logrus.WithError(err).Fatal("Could not parse HMAC_TIMEOUT")
7476
}
7577
hmacTimeout = d
7678
}
@@ -92,7 +94,7 @@ var versionFilePath = "/app/version.json"
9294
func versionHandler(w http.ResponseWriter, req *http.Request) {
9395
versionFile, err := ioutil.ReadFile(versionFilePath)
9496
if err != nil {
95-
log.Printf("Error reading %s err: %v", versionFilePath, err)
97+
logrus.WithError(err).Errorf("Could not read %s", versionFilePath)
9698
http.Error(w, "Could not read version file.", http.StatusNotFound)
9799
return
98100
}
@@ -104,18 +106,20 @@ func versionHandler(w http.ResponseWriter, req *http.Request) {
104106
func main() {
105107
var stubHandler stubhandlers.StubHandler
106108
if returnMode == "redirect" {
107-
log.Printf("Starting in redirect mode. bucket: %s%s cdn: %s", s3Bucket, s3Prefix, cdnPrefix)
109+
logrus.WithFields(logrus.Fields{
110+
"bucket": s3Bucket + s3Prefix,
111+
"cdn": cdnPrefix,
112+
}).Info("Starting in redirect mode")
108113
storage := backends.NewS3(s3.New(awsSess), s3Bucket)
109114
stubHandler = stubhandlers.NewRedirectHandler(storage, cdnPrefix, s3Prefix)
110115
} else {
111-
log.Println("Starting in direct mode.")
116+
logrus.Info("Starting in direct mode")
112117
stubHandler = stubhandlers.NewDirectHandler()
113118
}
114119

115120
stubService := &stubhandlers.StubService{
116121
Handler: stubHandler,
117122
AttributionCodeValidator: attributioncode.NewValidator(hmacKey, hmacTimeout),
118-
RavenClient: ravenClient,
119123
}
120124

121125
mux := http.NewServeMux()
@@ -124,5 +128,5 @@ func main() {
124128
mux.HandleFunc("/__heartbeat__", okHandler)
125129
mux.HandleFunc("/__version__", versionHandler)
126130

127-
log.Fatal(http.ListenAndServe(addr, mux))
131+
logrus.Fatal(http.ListenAndServe(addr, mux))
128132
}

stubservice/stubhandlers/stubservice.go

+7-19
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
package stubhandlers
22

33
import (
4-
"fmt"
5-
"log"
64
"net/http"
75

8-
raven "github.com/getsentry/raven-go"
6+
"github.com/Sirupsen/logrus"
97
"github.com/mozilla-services/stubattribution/attributioncode"
10-
"github.com/pkg/errors"
118
)
129

1310
// StubService serves redirects or modified stubs
1411
type StubService struct {
1512
Handler StubHandler
1613

1714
AttributionCodeValidator *attributioncode.Validator
18-
19-
RavenClient *raven.Client
2015
}
2116

2217
func (s *StubService) ServeHTTP(w http.ResponseWriter, req *http.Request) {
@@ -27,25 +22,18 @@ func (s *StubService) ServeHTTP(w http.ResponseWriter, req *http.Request) {
2722
http.Redirect(w, req, backupURL, http.StatusFound)
2823
}
2924

30-
handleError := func(err error) {
31-
log.Println(err)
32-
if s.RavenClient != nil {
33-
raven.CaptureMessage(fmt.Sprintf("%v", err), map[string]string{
34-
"url": req.URL.String(),
35-
})
36-
}
37-
redirectBouncer()
38-
}
39-
40-
code, err := s.AttributionCodeValidator.Validate(query.Get("attribution_code"), query.Get("attribution_sig"))
25+
attributionCode := query.Get("attribution_code")
26+
code, err := s.AttributionCodeValidator.Validate(attributionCode, query.Get("attribution_sig"))
4127
if err != nil {
42-
handleError(errors.Wrapf(err, "could not validate attribution_code: %s", trimToLen(query.Get("attribution_code"), 200)))
28+
logrus.WithError(err).WithField("attribution_code", trimToLen(attributionCode, 200)).Error("Could not validate attribution_code")
29+
redirectBouncer()
4330
return
4431
}
4532

4633
err = s.Handler.ServeStub(w, req, code)
4734
if err != nil {
48-
handleError(errors.Wrapf(err, "ServeStub url: %s", req.URL.String()))
35+
logrus.WithError(err).WithField("url", req.URL.String()).Error("Error serving stub")
36+
redirectBouncer()
4937
return
5038
}
5139
}

vendor/github.com/Sirupsen/logrus/.gitignore

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

vendor/github.com/Sirupsen/logrus/.travis.yml

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

vendor/github.com/Sirupsen/logrus/CHANGELOG.md

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

vendor/github.com/Sirupsen/logrus/LICENSE

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

0 commit comments

Comments
 (0)