Example #1
0
// ToWire converts the state.MetricBatch into a type
// that can be sent over the wire to the collector.
func ToWire(mb *state.MetricBatch) *wireformat.MetricBatch {
	metrics := make([]wireformat.Metric, len(mb.Metrics()))
	for i, m := range mb.Metrics() {
		metrics[i] = wireformat.Metric{
			Key:   m.Key,
			Value: m.Value,
			Time:  m.Time.UTC(),
		}
	}
	return &wireformat.MetricBatch{
		UUID:        mb.UUID(),
		ModelUUID:   mb.ModelUUID(),
		UnitName:    mb.Unit(),
		CharmUrl:    mb.CharmURL(),
		Created:     mb.Created().UTC(),
		Metrics:     metrics,
		Credentials: mb.Credentials(),
	}
}
Example #2
0
func assertSameMetric(c *gc.C, a params.MetricResult, b *state.MetricBatch) {
	c.Assert(a.Key, gc.Equals, b.Metrics()[0].Key)
	c.Assert(a.Value, gc.Equals, b.Metrics()[0].Value)
	c.Assert(a.Time, jc.TimeBetween(b.Metrics()[0].Time, b.Metrics()[0].Time))
}
Example #3
0
func (s *MetricLocalCharmSuite) TestModelMetricBatches(c *gc.C) {
	now := s.State.NowToTheSecond()
	// Add 2 metric batches to a single unit.
	m := state.Metric{"pings", "5", now}
	m2 := state.Metric{"pings", "10", now}
	_, err := s.State.AddMetrics(
		state.BatchParam{
			UUID:     utils.MustNewUUID().String(),
			Created:  now,
			CharmURL: s.meteredCharm.URL().String(),
			Metrics:  []state.Metric{m},
			Unit:     s.unit.UnitTag(),
		},
	)
	c.Assert(err, jc.ErrorIsNil)
	newUnit, err := s.application.AddUnit()
	c.Assert(err, jc.ErrorIsNil)
	_, err = s.State.AddMetrics(
		state.BatchParam{
			UUID:     utils.MustNewUUID().String(),
			Created:  now.Add(time.Second),
			CharmURL: s.meteredCharm.URL().String(),
			Metrics:  []state.Metric{m2},
			Unit:     newUnit.UnitTag(),
		},
	)
	c.Assert(err, jc.ErrorIsNil)

	// Create a new model and add a metric batch.
	st := s.Factory.MakeModel(c, nil)
	defer st.Close()
	f := factory.NewFactory(st)
	meteredCharm := f.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "cs:quantal/metered"})
	service := f.MakeApplication(c, &factory.ApplicationParams{Charm: meteredCharm})
	unit := f.MakeUnit(c, &factory.UnitParams{Application: service, SetCharmURL: true})
	_, err = st.AddMetrics(
		state.BatchParam{
			UUID:     utils.MustNewUUID().String(),
			Created:  now,
			CharmURL: meteredCharm.URL().String(),
			Metrics:  []state.Metric{m},
			Unit:     unit.UnitTag(),
		},
	)
	c.Assert(err, jc.ErrorIsNil)

	// We expect 2 metric batches in our first model.
	metricBatches, err := s.State.MetricBatchesForModel()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(metricBatches, gc.HasLen, 2)

	var first, second state.MetricBatch
	for _, m := range metricBatches {
		if m.Unit() == "metered/0" {
			first = m
		}
		if m.Unit() == "metered/1" {
			second = m
		}
	}
	c.Assert(first, gc.NotNil)
	c.Assert(second, gc.NotNil)

	c.Check(first.Unit(), gc.Equals, "metered/0")
	c.Check(first.CharmURL(), gc.Equals, "local:quantal/metered")
	c.Check(first.ModelUUID(), gc.Equals, s.State.ModelUUID())
	c.Check(first.Sent(), jc.IsFalse)
	c.Assert(first.Metrics(), gc.HasLen, 1)
	c.Check(first.Metrics()[0].Value, gc.Equals, "5")

	c.Check(second.Unit(), gc.Equals, "metered/1")
	c.Check(second.CharmURL(), gc.Equals, "local:quantal/metered")
	c.Check(second.ModelUUID(), gc.Equals, s.State.ModelUUID())
	c.Check(second.Sent(), jc.IsFalse)
	c.Assert(second.Metrics(), gc.HasLen, 1)
	c.Check(second.Metrics()[0].Value, gc.Equals, "10")

	// And a single metric batch in the second model.
	metricBatches, err = st.MetricBatchesForModel()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(metricBatches, gc.HasLen, 1)
}