Skip to content

Commit dbae167

Browse files
committed
Add Prometheus instrumentation to the Doorman server.
1 parent 2b0802c commit dbae167

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

go/cmd/doorman/doorman_server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
log "github.com/golang/glog"
3232
"github.com/golang/protobuf/proto"
33+
"github.com/prometheus/client_golang/prometheus"
3334
rpc "google.golang.org/grpc"
3435
"google.golang.org/grpc/credentials"
3536

@@ -224,6 +225,8 @@ func main() {
224225
http.Handle("/", http.RedirectHandler("/debug/status", http.StatusMovedPermanently))
225226
AddServer(dm)
226227

228+
http.Handle("/metrics", prometheus.Handler())
229+
227230
go http.ListenAndServe(fmt.Sprintf(":%v", *debugPort), nil)
228231

229232
// Waits for the server to get its initial configuration. This guarantees that

go/server/doorman/server.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
log "github.com/golang/glog"
3030
"github.com/golang/protobuf/proto"
31+
"github.com/prometheus/client_golang/prometheus"
3132
"github.com/youtube/doorman/go/connection"
3233
"github.com/youtube/doorman/go/server/election"
3334
"github.com/youtube/doorman/go/timeutil"
@@ -89,6 +90,37 @@ const (
8990
maxBackoff = 1 * time.Minute
9091
)
9192

93+
var (
94+
requestLabels = []string{"method"}
95+
96+
requests = prometheus.NewCounterVec(prometheus.CounterOpts{
97+
Namespace: "doorman",
98+
Subsystem: "server",
99+
Name: "requests",
100+
Help: "Requests sent to a Doorman service.",
101+
}, requestLabels)
102+
103+
requestErrors = prometheus.NewCounterVec(prometheus.CounterOpts{
104+
Namespace: "doorman",
105+
Subsystem: "server",
106+
Name: "request_errors",
107+
Help: "Requests sent to a Doorman service that returned an error.",
108+
}, requestLabels)
109+
110+
requestDurations = prometheus.NewHistogramVec(prometheus.HistogramOpts{
111+
Namespace: "doorman",
112+
Subsystem: "server",
113+
Name: "request_durations",
114+
Help: "Duration of different requests in seconds.",
115+
}, requestLabels)
116+
)
117+
118+
func init() {
119+
prometheus.MustRegister(requests)
120+
prometheus.MustRegister(requestErrors)
121+
prometheus.MustRegister(requestDurations)
122+
}
123+
92124
// Server represents the state of a doorman server.
93125
type Server struct {
94126
Election election.Election
@@ -580,7 +612,15 @@ func (server *Server) ReleaseCapacity(ctx context.Context, in *pb.ReleaseCapacit
580612
out = new(pb.ReleaseCapacityResponse)
581613

582614
log.V(2).Infof("ReleaseCapacity req: %v", in)
583-
defer log.V(2).Infof("ReleaseCapacity res: %v", out)
615+
start := time.Now()
616+
requests.WithLabelValues("ReleaseCapacity").Inc()
617+
defer func() {
618+
defer log.V(2).Infof("ReleaseCapacity res: %v", out)
619+
if err != nil {
620+
requestErrors.WithLabelValues("ReleaseCapacity").Inc()
621+
requestDurations.WithLabelValues("ReleaseCapacity").Observe(time.Since(start).Seconds())
622+
}
623+
}()
584624

585625
// If we are not the master we tell the client who we think the master
586626
// is and we return. There are some subtleties around this: The presence
@@ -634,7 +674,16 @@ func (server *Server) GetCapacity(ctx context.Context, in *pb.GetCapacityRequest
634674
out = new(pb.GetCapacityResponse)
635675

636676
log.V(2).Infof("GetCapacity req: %v", in)
637-
defer log.V(2).Infof("GetCapacity res: %v", out)
677+
678+
start := time.Now()
679+
requests.WithLabelValues("GetCapacity").Inc()
680+
defer func() {
681+
defer log.V(2).Infof("GetCapacity res: %v", out)
682+
if err != nil {
683+
requestErrors.WithLabelValues("GetCapacity").Inc()
684+
requestDurations.WithLabelValues("GetCapacity").Observe(time.Since(start).Seconds())
685+
}
686+
}()
638687

639688
// If we are not the master, we redirect the client.
640689
if !server.IsMaster() {

0 commit comments

Comments
 (0)