// ContainerMetrics is deprecated in favor of ContainerEnvelopes, since // returning the ContainerMetric type directly hides important // information, like the timestamp. // // The returned values will be the same as ContainerEnvelopes, just with // the Envelope stripped out. func (c *Consumer) ContainerMetrics(appGuid string, authToken string) ([]*events.ContainerMetric, error) { envelopes, err := c.ContainerEnvelopes(appGuid, authToken) if err != nil { return nil, err } messages := make([]*events.ContainerMetric, 0, len(envelopes)) for _, env := range envelopes { messages = append(messages, env.GetContainerMetric()) } noaa.SortContainerMetrics(messages) return messages, nil }
// ContainerMetrics connects to traffic controller via its 'containermetrics' // http(s) endpoint and returns the most recent messages for an app. The // returned metrics will be sorted by InstanceIndex. func (c *Consumer) ContainerMetrics(appGuid string, authToken string) ([]*events.ContainerMetric, error) { messages := make([]*events.ContainerMetric, 0, 200) callback := func(envelope *events.Envelope) error { if envelope.GetEventType() == events.Envelope_LogMessage { return errors.New(fmt.Sprintf("Upstream error: %s", envelope.GetLogMessage().GetMessage())) } messages = append(messages, envelope.GetContainerMetric()) return nil } err := c.readTC(appGuid, authToken, "containermetrics", callback) if err != nil { return nil, err } noaa.SortContainerMetrics(messages) return messages, err }
"github.com/cloudfoundry/noaa/events" "github.com/gogo/protobuf/proto" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("SortContainerMetrics", func() { var messages []*events.ContainerMetric BeforeEach(func() { messages = []*events.ContainerMetric{ &events.ContainerMetric{ ApplicationId: proto.String("appId"), InstanceIndex: proto.Int32(2), }, &events.ContainerMetric{ ApplicationId: proto.String("appId"), InstanceIndex: proto.Int32(1), }, } }) It("sorts container metrics by instance index", func() { sortedMessages := noaa.SortContainerMetrics(messages) Expect(sortedMessages[0].GetInstanceIndex()).To(Equal(int32(1))) Expect(sortedMessages[1].GetInstanceIndex()).To(Equal(int32(2))) }) })