Permalink
Browse files

adding newrelic

  • Loading branch information...
1 parent 9867b92 commit 37589bd68f0d043a61619d9c9d6f07f5c6e38021 @omercnet omercnet committed Jan 6, 2017
Showing with 23 additions and 3 deletions.
  1. +19 −3 apiserver/api.go
  2. +4 −0 apiserver/config.go
View
@@ -28,6 +28,7 @@ import (
"github.com/go-web/httprl/memcacherl"
"github.com/go-web/httprl/redisrl"
"github.com/prometheus/client_golang/prometheus"
+ "github.com/newrelic/go-agent"
"github.com/rs/cors"
"github.com/fiorix/freegeoip"
@@ -37,6 +38,7 @@ type apiHandler struct {
db *freegeoip.DB
conf *Config
cors *cors.Cors
+ nrapp newrelic.Application
}
// NewHandler creates an http handler for the freegeoip server that
@@ -83,6 +85,14 @@ func (f *apiHandler) config(mc *httpmux.Config) error {
}
mc.Use(rl.Handle)
}
+ if f.conf.NewrelicName != "" && f.conf.NewrelicKey != "" {
+ config := newrelic.NewConfig(f.conf.NewrelicName, f.conf.NewrelicKey)
+ app, err := newrelic.NewApplication(config)
+ if err != nil {
+ return fmt.Errorf("failed to create newrelic application: {name: %v, key: %v}", f.conf.NewrelicName, f.conf.NewrelicKey)
+ }
+ f.nrapp = app
+ }
return nil
}
@@ -126,7 +136,13 @@ func (f *apiHandler) metrics(next http.HandlerFunc) http.HandlerFunc {
type writerFunc func(w http.ResponseWriter, r *http.Request, d *responseRecord)
func (f *apiHandler) register(name string, writer writerFunc) http.HandlerFunc {
- h := prometheus.InstrumentHandler(name, f.iplookup(writer))
+ var h http.Handler
+ if f.nrapp == nil {
+ h = prometheus.InstrumentHandler(name, f.iplookup(writer))
+ } else {
+ h = prometheus.InstrumentHandler(newrelic.WrapHandle(f.nrapp, name, f.iplookup(writer)))
+ }
+
return f.cors.Handler(h).ServeHTTP
}
@@ -272,9 +288,9 @@ func openDB(c *Config) (*freegeoip.DB, error) {
c.DB, err = freegeoip.GeoIPUpdateURL(c.UpdatesHost, c.UserID, c.LicenseKey, c.ProductID)
if err != nil {
return nil, err
- } else {
- log.Println("Using updates URL:", c.DB)
}
+ // else
+ log.Println("Using updates URL:", c.DB)
}
u, err := url.Parse(c.DB)
View
@@ -44,6 +44,8 @@ type Config struct {
LicenseKey string
UserID string
ProductID string
+ NewrelicName string
+ NewrelicKey string
errorLog *log.Logger
accessLog *log.Logger
@@ -104,6 +106,8 @@ func (c *Config) AddFlags(fs *flag.FlagSet) {
fs.StringVar(&c.LicenseKey, "license-key", c.LicenseKey, "MaxMind License key")
fs.StringVar(&c.UserID, "user-id", c.UserID, "MaxMind User ID")
fs.StringVar(&c.ProductID, "product-id", c.ProductID, "MaxMinf product id (e.g GeoIP2-City)")
+ fs.StringVar(&c.NewrelicName, "newrelic-name", c.NewrelicName, "Newrepic APM application name")
+ fs.StringVar(&c.NewrelicKey, "newrelic-key", c.NewrelicKey, "Nerelic API key")
}
func (c *Config) logWriter() io.Writer {

0 comments on commit 37589bd

Please sign in to comment.