mirror of
https://github.com/openfaas/faas.git
synced 2025-06-25 08:13:25 +00:00
Merge master into breakout_swarm
Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
@ -40,17 +40,24 @@ func (q PrometheusQuery) Fetch(query string) (*VectorQueryResponse, error) {
|
||||
return nil, getErr
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
if res.Body != nil {
|
||||
defer res.Body.Close()
|
||||
}
|
||||
|
||||
bytesOut, readErr := ioutil.ReadAll(res.Body)
|
||||
if readErr != nil {
|
||||
return nil, readErr
|
||||
}
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("Unexpected status code from Prometheus want: %d, got: %d, body: %s", http.StatusOK, res.StatusCode, string(bytesOut))
|
||||
}
|
||||
|
||||
var values VectorQueryResponse
|
||||
|
||||
unmarshalErr := json.Unmarshal(bytesOut, &values)
|
||||
if unmarshalErr != nil {
|
||||
return nil, unmarshalErr
|
||||
return nil, fmt.Errorf("Error unmarshaling result: %s, '%s'", unmarshalErr, string(bytesOut))
|
||||
}
|
||||
|
||||
return &values, nil
|
||||
|
@ -1,51 +0,0 @@
|
||||
// Copyright (c) Alex Ellis 2017. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/client"
|
||||
)
|
||||
|
||||
// AttachSwarmWatcher adds a go-route to monitor the amount of service replicas in the swarm
|
||||
// matching a 'function' label.
|
||||
func AttachSwarmWatcher(dockerClient *client.Client, metricsOptions MetricOptions, label string, interval time.Duration) {
|
||||
ticker := time.NewTicker(interval)
|
||||
|
||||
quit := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
serviceFilter := filters.NewArgs()
|
||||
|
||||
options := types.ServiceListOptions{
|
||||
Filters: serviceFilter,
|
||||
}
|
||||
|
||||
services, err := dockerClient.ServiceList(context.Background(), options)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
for _, service := range services {
|
||||
if len(service.Spec.TaskTemplate.ContainerSpec.Labels[label]) > 0 {
|
||||
metricsOptions.ServiceReplicasCounter.
|
||||
WithLabelValues(service.Spec.Name).
|
||||
Set(float64(*service.Spec.Mode.Replicated.Replicas))
|
||||
}
|
||||
}
|
||||
break
|
||||
case <-quit:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
Reference in New Issue
Block a user