func (s *mgoStatsSuite) assertGauge(c *gc.C, m prometheus.Metric, value float64) { var metric dto.Metric err := m.Write(&metric) c.Assert(err, jc.ErrorIsNil) c.Assert(metric.Gauge, gc.Not(gc.IsNil)) c.Assert(*metric.Gauge.Value, gc.Equals, value) }
func readMetric(m prometheus.Metric) MetricResult { pb := &dto.Metric{} m.Write(pb) labels := make(labelMap, len(pb.Label)) for _, v := range pb.Label { labels[v.GetName()] = v.GetValue() } if pb.Gauge != nil { return MetricResult{labels: labels, value: pb.GetGauge().GetValue(), metricType: dto.MetricType_GAUGE} } if pb.Counter != nil { return MetricResult{labels: labels, value: pb.GetCounter().GetValue(), metricType: dto.MetricType_COUNTER} } if pb.Untyped != nil { return MetricResult{labels: labels, value: pb.GetUntyped().GetValue(), metricType: dto.MetricType_UNTYPED} } panic("Unsupported metric type") }
func (t *UptimeSuite) TestUptimeReporting(c *gc.C) { now := time.Now() u, err := monitoring.NewUptimeCollector("test", "test", "test", time.Now) c.Assert(err, jc.ErrorIsNil) ch := make(chan prometheus.Metric, 1000) u.Collect(ch) var m prometheus.Metric select { case m = <-ch: default: c.Error("metric not provided by collector") } var raw prometheusinternal.Metric err = m.Write(&raw) c.Assert(err, jc.ErrorIsNil) cnt := raw.GetCounter() val := cnt.GetValue() c.Assert(val, gc.Equals, float64(now.Unix())) }
func (s *collectionSizeSuite) TestCollectionSizeReporting(c *gc.C) { collection := s.Session.DB("test").C("test_collection") u := monitoring.NewCollectionSizeCollector("test", "test", "test", collection) defer u.Close() err := collection.Insert(bson.M{"test": true}) c.Assert(err, jc.ErrorIsNil) ch := make(chan prometheus.Metric, 2) u.Collect(ch) var m prometheus.Metric // read the size select { case m = <-ch: default: c.Error("metric not provided by collector") } var raw prometheusinternal.Metric err = m.Write(&raw) c.Assert(err, jc.ErrorIsNil) cnt := raw.GetGauge() valueOne := cnt.GetValue() // read the count select { case m = <-ch: default: c.Error("metric not provided by collector") } err = m.Write(&raw) c.Assert(err, jc.ErrorIsNil) cnt = raw.GetGauge() val := cnt.GetValue() c.Assert(val, gc.Equals, float64(1.0)) err = collection.Insert(bson.M{"test": true}) c.Assert(err, jc.ErrorIsNil) u.Collect(ch) // read the size select { case m = <-ch: default: c.Error("metric not provided by collector") } err = m.Write(&raw) c.Assert(err, jc.ErrorIsNil) cnt = raw.GetGauge() valueTwo := cnt.GetValue() c.Assert(2*valueOne, gc.Equals, valueTwo) // read the count select { case m = <-ch: default: c.Error("metric not provided by collector") } err = m.Write(&raw) c.Assert(err, jc.ErrorIsNil) cnt = raw.GetGauge() val = cnt.GetValue() c.Assert(val, gc.Equals, float64(2.0)) }