// publishMetricsOnce is invoked by the ticker to periodically publish metrics to backend. func (cs *clientServer) publishMetricsOnce() { metadata, taskMetrics, err := cs.statsEngine.GetInstanceMetrics() if err != nil { log.Warn("Error getting instance metrics", "err", err) return } if *metadata.Idle { // Idle instance, send message and return. cs.MakeRequest(ecstcs.NewPublishMetricsRequest(metadata, taskMetrics)) return } var messageTaskMetrics []*ecstcs.TaskMetric for i := range taskMetrics { messageTaskMetrics = append(messageTaskMetrics, taskMetrics[i]) if (i+1)%tasksInMessage == 0 { // Construct payload with tasksInMessage number of task metrics and send to backend. cs.MakeRequest(ecstcs.NewPublishMetricsRequest(metadata, messageTaskMetrics)) messageTaskMetrics = messageTaskMetrics[:0] } } if len(messageTaskMetrics) > 0 { // Send remaining task metrics to backend. cs.MakeRequest(ecstcs.NewPublishMetricsRequest(metadata, messageTaskMetrics)) } }
// metricsToPublishMetricRequests gets task metrics and converts them to a list of PublishMetricRequest // objects. func (cs *clientServer) metricsToPublishMetricRequests() ([]*ecstcs.PublishMetricsRequest, error) { metadata, taskMetrics, err := cs.statsEngine.GetInstanceMetrics() if err != nil { return nil, err } var requests []*ecstcs.PublishMetricsRequest if *metadata.Idle { metadata.Fin = aws.Boolean(true) // Idle instance, we have only one request to send to backend. requests = append(requests, ecstcs.NewPublishMetricsRequest(metadata, taskMetrics)) return requests, nil } var messageTaskMetrics []*ecstcs.TaskMetric numTasks := len(taskMetrics) for i, taskMetric := range taskMetrics { messageTaskMetrics = append(messageTaskMetrics, taskMetric) var requestMetadata *ecstcs.MetricsMetadata if (i + 1) == numTasks { // If this is the last task to send, set fin to true requestMetadata = copyMetricsMetadata(metadata, true) } else { requestMetadata = copyMetricsMetadata(metadata, false) } if (i+1)%tasksInMessage == 0 { // Construct payload with tasksInMessage number of task metrics and send to backend. requests = append(requests, ecstcs.NewPublishMetricsRequest(requestMetadata, copyTaskMetrics(messageTaskMetrics))) messageTaskMetrics = messageTaskMetrics[:0] } } if len(messageTaskMetrics) > 0 { // Create the new metadata object and set fin to true as this is the last message in the payload. requestMetadata := copyMetricsMetadata(metadata, true) // Create a request with remaining task metrics. requests = append(requests, ecstcs.NewPublishMetricsRequest(requestMetadata, messageTaskMetrics)) } return requests, nil }