@@ -27,7 +27,7 @@ import (
27
27
"github.com/codahale/hdrhistogram"
28
28
"github.com/gogo/protobuf/proto"
29
29
prometheusgo "github.com/prometheus/client_model/go"
30
- "github.com/rcrowley/go-metrics"
30
+ metrics "github.com/rcrowley/go-metrics"
31
31
)
32
32
33
33
const (
@@ -125,16 +125,19 @@ var _ Iterable = &Gauge{}
125
125
var _ Iterable = & GaugeFloat64 {}
126
126
var _ Iterable = & Counter {}
127
127
var _ Iterable = & Histogram {}
128
+ var _ Iterable = & Rate {}
128
129
129
130
var _ json.Marshaler = & Gauge {}
130
131
var _ json.Marshaler = & GaugeFloat64 {}
131
132
var _ json.Marshaler = & Counter {}
132
133
var _ json.Marshaler = & Registry {}
134
+ var _ json.Marshaler = & Rate {}
133
135
134
136
var _ PrometheusExportable = & Gauge {}
135
137
var _ PrometheusExportable = & GaugeFloat64 {}
136
138
var _ PrometheusExportable = & Counter {}
137
139
var _ PrometheusExportable = & Histogram {}
140
+ var _ PrometheusExportable = & Rate {}
138
141
139
142
type periodic interface {
140
143
nextTick () time.Time
@@ -453,7 +456,7 @@ func (g *GaugeFloat64) GetType() *prometheusgo.MetricType {
453
456
return prometheusgo .MetricType_GAUGE .Enum ()
454
457
}
455
458
456
- // Inspect calls the given closure with the empty string and itself.
459
+ // Inspect calls the given closure with itself.
457
460
func (g * GaugeFloat64 ) Inspect (f func (interface {})) { f (g ) }
458
461
459
462
// MarshalJSON marshals to JSON.
@@ -478,6 +481,7 @@ func (g *GaugeFloat64) GetMetadata() Metadata {
478
481
479
482
// A Rate is a exponential weighted moving average.
480
483
type Rate struct {
484
+ Metadata
481
485
mu syncutil.Mutex // protects fields below
482
486
curSum float64
483
487
wrapped ewma.MovingAverage
@@ -487,20 +491,48 @@ type Rate struct {
487
491
488
492
// NewRate creates an EWMA rate on the given timescale. Timescales at
489
493
// or below 2s are illegal and will cause a panic.
490
- func NewRate (timescale time.Duration ) * Rate {
494
+ func NewRate (metadata Metadata , timescale time.Duration ) * Rate {
491
495
const tickInterval = time .Second
492
496
if timescale <= 2 * time .Second {
493
497
panic (fmt .Sprintf ("EWMA with per-second ticks makes no sense on timescale %s" , timescale ))
494
498
}
495
499
avgAge := float64 (timescale ) / float64 (2 * tickInterval )
496
-
497
500
return & Rate {
501
+ Metadata : metadata ,
498
502
interval : tickInterval ,
499
503
nextT : now (),
500
504
wrapped : ewma .NewMovingAverage (avgAge ),
501
505
}
502
506
}
503
507
508
+ // GetType returns the prometheus type enum for this metric.
509
+ func (e * Rate ) GetType () * prometheusgo.MetricType {
510
+ return prometheusgo .MetricType_GAUGE .Enum ()
511
+ }
512
+
513
+ // Inspect calls the given closure with itself.
514
+ func (e * Rate ) Inspect (f func (interface {})) { f (e ) }
515
+
516
+ // MarshalJSON marshals to JSON.
517
+ func (e * Rate ) MarshalJSON () ([]byte , error ) {
518
+ return json .Marshal (e .Value ())
519
+ }
520
+
521
+ // ToPrometheusMetric returns a filled-in prometheus metric of the right type.
522
+ func (e * Rate ) ToPrometheusMetric () * prometheusgo.Metric {
523
+ return & prometheusgo.Metric {
524
+ Gauge : & prometheusgo.Gauge {Value : proto .Float64 (e .Value ())},
525
+ }
526
+ }
527
+
528
+ // GetMetadata returns the metric's metadata including the Prometheus
529
+ // MetricType.
530
+ func (e * Rate ) GetMetadata () Metadata {
531
+ baseMetadata := e .Metadata
532
+ baseMetadata .MetricType = prometheusgo .MetricType_GAUGE
533
+ return baseMetadata
534
+ }
535
+
504
536
// Value returns the current value of the Rate.
505
537
func (e * Rate ) Value () float64 {
506
538
e .mu .Lock ()
0 commit comments