Skip to content

Commit

Permalink
Merge pull request #39 from gaplyk/master
Browse files Browse the repository at this point in the history
Custom metrics registry for gizmo
  • Loading branch information
jprobinson committed Mar 24, 2016
2 parents 6eb2798 + 9af49e1 commit 11c9c46
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
8 changes: 7 additions & 1 deletion config/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package config

import "net/http"
import (
"net/http"
"github.com/rcrowley/go-metrics"
)

// Server holds info required to configure a gizmo server.Server.
type Server struct {
Expand Down Expand Up @@ -50,6 +53,9 @@ type Server struct {
TLSKeyFile *string `envconfig:"TLS_KEY"`
// NotFoundHandler will override the default server NotfoundHandler if set.
NotFoundHandler http.Handler
// MetricsRegistry will override the default server metrics registry if set.
MetricsRegistry metrics.Registry

}

// LoadServerFromEnv will attempt to load a Server object
Expand Down
6 changes: 5 additions & 1 deletion server/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ func NewRPCServer(cfg *config.Server) *RPCServer {
if cfg.NotFoundHandler != nil {
mx.NotFoundHandler = cfg.NotFoundHandler
}
registry := cfg.MetricsRegistry
if registry == nil {
registry = metrics.NewRegistry()
}
return &RPCServer{
cfg: cfg,
srvr: grpc.NewServer(),
mux: mx,
exit: make(chan chan error),
monitor: NewActivityMonitor(),
registry: metrics.NewRegistry(),
registry: registry,
}
}

Expand Down
6 changes: 5 additions & 1 deletion server/simple_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ func NewSimpleServer(cfg *config.Server) *SimpleServer {
if cfg.NotFoundHandler != nil {
mx.NotFoundHandler = cfg.NotFoundHandler
}
registry := cfg.MetricsRegistry
if registry == nil {
registry = metrics.NewRegistry()
}
return &SimpleServer{
mux: mx,
cfg: cfg,
exit: make(chan chan error),
monitor: NewActivityMonitor(),
ctx: netContext.Background(),
registry: metrics.NewRegistry(),
registry: registry,
}
}

Expand Down
13 changes: 13 additions & 0 deletions server/simple_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/NYTimes/gizmo/config"
"github.com/gorilla/mux"
"golang.org/x/net/context"
"github.com/rcrowley/go-metrics"
)

func BenchmarkSimpleServer_NoParam(b *testing.B) {
Expand Down Expand Up @@ -311,3 +312,15 @@ func TestBasicRegistration(t *testing.T) {
t.Error("Invalid services should produce an error in service registration")
}
}

func TestCustomMetricsRegistry(t *testing.T) {

cfg := &config.Server{MetricsRegistry: metrics.NewRegistry()}
_ = metrics.NewRegisteredTimer("test-timer", cfg.MetricsRegistry)

s := NewSimpleServer(cfg)

if s.registry.Get("test-timer") == nil {
t.Error("Custom metrics registry is failed to register within simple server")
}
}

0 comments on commit 11c9c46

Please sign in to comment.