Skip to content

Commit a2af521

Browse files
committed
Add Prometheus instrumentation to the client.
1 parent 029c3cd commit a2af521

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

go/client/doorman/client.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131

3232
log "github.com/golang/glog"
3333
"github.com/golang/protobuf/proto"
34+
"github.com/prometheus/client_golang/prometheus"
3435
"github.com/youtube/doorman/go/connection"
3536
"github.com/youtube/doorman/go/timeutil"
3637
"golang.org/x/net/context"
@@ -65,6 +66,37 @@ var (
6566
ErrInvalidWants = errors.New("wants must be > 0.0")
6667
)
6768

69+
var (
70+
requestLabels = []string{"server", "method"}
71+
72+
requests = prometheus.NewCounterVec(prometheus.CounterOpts{
73+
Namespace: "doorman",
74+
Subsystem: "client",
75+
Name: "requests",
76+
Help: "Requests sent to a Doorman service.",
77+
}, requestLabels)
78+
79+
requestErrors = prometheus.NewCounterVec(prometheus.CounterOpts{
80+
Namespace: "doorman",
81+
Subsystem: "client",
82+
Name: "request_errors",
83+
Help: "Requests sent to a Doorman service that returned an error.",
84+
}, requestLabels)
85+
86+
requestDurations = prometheus.NewHistogramVec(prometheus.HistogramOpts{
87+
Namespace: "doorman",
88+
Subsystem: "client",
89+
Name: "request_durations",
90+
Help: "Duration of different requests in seconds.",
91+
}, requestLabels)
92+
)
93+
94+
func init() {
95+
prometheus.MustRegister(requests)
96+
prometheus.MustRegister(requestErrors)
97+
prometheus.MustRegister(requestDurations)
98+
}
99+
68100
// NOTE: We're wrapping connection package's functions and types here in the client,
69101
// because we do not want our users to be aware of the internal connection package.
70102

@@ -405,12 +437,17 @@ func (client *Client) Close() {
405437
func (client *Client) releaseCapacity(in *pb.ReleaseCapacityRequest) (*pb.ReleaseCapacityResponse, error) {
406438
// context.TODO(ryszard): Plumb a context through.
407439
out, err := client.conn.ExecuteRPC(func() (connection.HasMastership, error) {
440+
start := time.Now()
441+
requests.WithLabelValues(client.conn.String(), "ReleaseCapacity").Inc()
442+
defer requestDurations.WithLabelValues(client.conn.String(), "ReleaseCapacity").Observe(time.Since(start).Seconds())
443+
408444
return client.conn.Stub.ReleaseCapacity(context.TODO(), in)
409445

410446
})
411447

412448
// Returns an error if we could not execute the RPC.
413449
if err != nil {
450+
requestErrors.WithLabelValues(client.conn.String(), "ReleaseCapacity").Inc()
414451
return nil, err
415452
}
416453

@@ -423,12 +460,19 @@ func (client *Client) releaseCapacity(in *pb.ReleaseCapacityRequest) (*pb.Releas
423460
func (client *Client) getCapacity(in *pb.GetCapacityRequest) (*pb.GetCapacityResponse, error) {
424461
// context.TODO(ryszard): Plumb a context through.
425462
out, err := client.conn.ExecuteRPC(func() (connection.HasMastership, error) {
463+
start := time.Now()
464+
requests.WithLabelValues(client.conn.String(), "GetCapacity").Inc()
465+
defer func() {
466+
log.Infof("%v %v", time.Since(start).Seconds(), time.Since(start))
467+
requestDurations.WithLabelValues(client.conn.String(), "GetCapacity").Observe(time.Since(start).Seconds())
468+
}()
426469
return client.conn.Stub.GetCapacity(context.TODO(), in)
427470

428471
})
429472

430473
// Returns an error if we could not execute the RPC.
431474
if err != nil {
475+
requestErrors.WithLabelValues(client.conn.String(), "GetCapacity").Inc()
432476
return nil, err
433477
}
434478

go/connection/connection.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ type Connection struct {
4646
Opts *Options
4747
}
4848

49+
func (connection *Connection) String() string {
50+
return connection.currentMaster
51+
}
52+
4953
// New creates a new Connection with the given server address.
5054
func New(addr string, options ...Option) (*Connection, error) {
5155
connection := &Connection{

0 commit comments

Comments
 (0)