func (p *ContainerMetricProcessor) Process(e *events.Envelope) []metrics.WMetric {
	processedMetrics := make([]metrics.WMetric, 3)
	containerMetricEvent := e.GetContainerMetric()

	processedMetrics[0] = *p.ProcessContainerMetricCPU(containerMetricEvent)
	processedMetrics[1] = *p.ProcessContainerMetricMemory(containerMetricEvent)
	processedMetrics[2] = *p.ProcessContainerMetricDisk(containerMetricEvent)

	return processedMetrics
}
Example #2
0
func ContainerMetric(msg *events.Envelope) Event {
	containerMetric := msg.GetContainerMetric()

	fields := logrus.Fields{
		"origin":         msg.GetOrigin(),
		"cf_app_id":      containerMetric.GetApplicationId(),
		"cpu_percentage": containerMetric.GetCpuPercentage(),
		"disk_bytes":     containerMetric.GetDiskBytes(),
		"instance_index": containerMetric.GetInstanceIndex(),
		"memory_bytes":   containerMetric.GetMemoryBytes(),
	}

	return Event{
		Fields: fields,
		Msg:    "",
		Type:   msg.GetEventType().String(),
	}
}
Example #3
0
// 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 (cnsmr *Consumer) ContainerMetrics(appGuid string, authToken string) ([]*events.ContainerMetric, error) {
	resp, err := cnsmr.makeHttpRequestToTrafficController(appGuid, authToken, "containermetrics")

	if err != nil {
		return nil, errors.New(fmt.Sprintf("Error dialing traffic controller server: %s.\nPlease ask your Cloud Foundry Operator to check the platform configuration (traffic controller endpoint is %s).", err.Error(), cnsmr.trafficControllerUrl))
	}

	defer resp.Body.Close()

	err = checkForErrors(resp)
	if err != nil {
		return nil, err
	}

	reader, err := checkContentsAndFindBoundaries(resp)
	if err != nil {
		return nil, err
	}

	var buffer bytes.Buffer
	messages := make([]*events.ContainerMetric, 0, 200)

	for part, loopErr := reader.NextPart(); loopErr == nil; part, loopErr = reader.NextPart() {
		buffer.Reset()

		msg := new(events.Envelope)
		_, err := buffer.ReadFrom(part)
		if err != nil {
			break
		}
		proto.Unmarshal(buffer.Bytes(), msg)

		if msg.GetEventType() == events.Envelope_LogMessage {
			return []*events.ContainerMetric{}, errors.New(fmt.Sprintf("Upstream error: %s", msg.GetLogMessage().GetMessage()))
		}

		messages = append(messages, msg.GetContainerMetric())
	}

	SortContainerMetrics(messages)

	return messages, err
}