@@ -11,27 +11,37 @@ import (
1111)
1212
1313// Counter is a metric that can only be incremented (monotonically increasing).
14- // The labels parameter contains key-value pairs for metric dimensions
15- // (e.g. rpcService, rpcMethod). It may be nil when no
16- // dimensional context is available.
17- // The concrete type *must* provide a thread-safe implementation for these
18- // methods.
14+ // The concrete type must provide a thread-safe implementation for the method.
1915type Counter interface {
20- Inc (labels map [ string ] string , v int64 )
16+ Inc (v int64 )
2117}
2218
2319// NoOpCounter is a Counter implementation that does nothing.
2420type NoOpCounter struct {}
2521
2622// Inc implements Counter.
27- func (NoOpCounter ) Inc (labels map [string ]string , v int64 ) {}
23+ func (NoOpCounter ) Inc (v int64 ) {}
24+
25+ // LabeledCounter is a Counter that accepts dimensional labels on each
26+ // increment. The labels parameter contains key-value pairs for metric
27+ // dimensions. It may be nil when no dimensional context is available.
28+ // The concrete type must provide a thread-safe implementation.
29+ type LabeledCounter interface {
30+ Inc (labels map [string ]string , v int64 )
31+ }
2832
29- // Gauge is a metric that can increase and decrease (e.g. pool size,
30- // active count). Update sets the gauge to the given absolute value.
33+ // NoOpLabeledCounter is a LabeledCounter implementation that does nothing.
34+ type NoOpLabeledCounter struct {}
35+
36+ // Inc implements LabeledCounter.
37+ func (NoOpLabeledCounter ) Inc (labels map [string ]string , v int64 ) {}
38+
39+ // Gauge is a metric that can increase and decrease (e.g. pool size).
40+ // Update sets the gauge to the given absolute value.
3141//
3242// Note: Gauge values may go up or down; Counter values must only increase.
33- // The concrete type * must* provide a thread-safe implementation for these
34- // methods .
43+ // The concrete type must provide a thread-safe implementation for the
44+ // method .
3545type Gauge interface {
3646 Update (labels map [string ]string , v int64 )
3747}
@@ -42,30 +52,43 @@ type NoOpGauge struct{}
4252// Update implements Gauge.
4353func (NoOpGauge ) Update (labels map [string ]string , v int64 ) {}
4454
45- // TODO (sujatha): Plug-in no-op implementation for nil metrics
46-
47- // MeteredTransport wraps a Transport and increments byte counters on each
55+ // meteredTransport wraps a Transport and increments byte counters on each
4856// Read and Write call.
49- type MeteredTransport struct {
57+ type meteredTransport struct {
5058 drpc.Transport
51- BytesSent Counter
52- BytesRecv Counter
59+ bytesSent Counter
60+ bytesRecv Counter
61+ }
62+
63+ // ToMeteredTransport returns a transport that increments bytesSent and
64+ // bytesRecv on each Write and Read call respectively. Nil counters are
65+ // replaced with no-op implementations.
66+ func ToMeteredTransport (tr drpc.Transport , bytesSent ,
67+ bytesRecv Counter ) drpc.Transport {
68+ if bytesSent == nil {
69+ bytesSent = NoOpCounter {}
70+ }
71+ if bytesRecv == nil {
72+ bytesRecv = NoOpCounter {}
73+ }
74+ return & meteredTransport {Transport : tr , bytesSent : bytesSent ,
75+ bytesRecv : bytesRecv }
5376}
5477
55- // Read reads from the underlying transport and increments BytesRecv .
56- func (t * MeteredTransport ) Read (p []byte ) (n int , err error ) {
78+ // Read reads from the underlying transport and increments bytesRecv .
79+ func (t * meteredTransport ) Read (p []byte ) (n int , err error ) {
5780 n , err = t .Transport .Read (p )
58- if n > 0 && t . BytesRecv != nil {
59- t .BytesRecv .Inc (nil , int64 (n ))
81+ if n > 0 {
82+ t .bytesRecv .Inc (int64 (n ))
6083 }
6184 return n , err
6285}
6386
64- // Write writes to the underlying transport and increments BytesSent .
65- func (t * MeteredTransport ) Write (p []byte ) (n int , err error ) {
87+ // Write writes to the underlying transport and increments bytesSent .
88+ func (t * meteredTransport ) Write (p []byte ) (n int , err error ) {
6689 n , err = t .Transport .Write (p )
67- if n > 0 && t . BytesSent != nil {
68- t .BytesSent .Inc (nil , int64 (n ))
90+ if n > 0 {
91+ t .bytesSent .Inc (int64 (n ))
6992 }
7093 return n , err
7194}
0 commit comments