func LogMessage(msg *events.Envelope) Event { logMessage := msg.GetLogMessage() fields := logrus.Fields{ "origin": msg.GetOrigin(), "cf_app_id": logMessage.GetAppId(), "timestamp": logMessage.GetTimestamp(), "source_type": logMessage.GetSourceType(), "message_type": logMessage.GetMessageType().String(), "source_instance": logMessage.GetSourceInstance(), } return Event{ Fields: fields, Msg: string(logMessage.GetMessage()), Type: msg.GetEventType().String(), } }
// 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 }