mirror of
https://github.com/openfaas/faas.git
synced 2025-06-10 17:26:47 +00:00
Add gateway_service_count metric for external provider
Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
parent
5254f47c26
commit
c163b7d2a8
72
gateway/metrics/externalwatcher.go
Normal file
72
gateway/metrics/externalwatcher.go
Normal file
@ -0,0 +1,72 @@
|
||||
// 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 (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"log"
|
||||
|
||||
"github.com/alexellis/faas/gateway/requests"
|
||||
)
|
||||
|
||||
func AttachExternalWatcher(endpointURL url.URL, metricsOptions MetricOptions, label string, interval time.Duration) {
|
||||
ticker := time.NewTicker(interval)
|
||||
quit := make(chan struct{})
|
||||
proxyClient := http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 3 * time.Second,
|
||||
KeepAlive: 0,
|
||||
}).DialContext,
|
||||
MaxIdleConns: 1,
|
||||
DisableKeepAlives: true,
|
||||
IdleConnTimeout: 120 * time.Millisecond,
|
||||
ExpectContinueTimeout: 1500 * time.Millisecond,
|
||||
},
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
|
||||
get, _ := http.NewRequest(http.MethodGet, endpointURL.String()+"system/functions", nil)
|
||||
|
||||
services := []requests.Function{}
|
||||
res, err := proxyClient.Do(get)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
bytesOut, readErr := ioutil.ReadAll(res.Body)
|
||||
if readErr != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
unmarshalErr := json.Unmarshal(bytesOut, &services)
|
||||
if unmarshalErr != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, service := range services {
|
||||
metricsOptions.ServiceReplicasCounter.
|
||||
WithLabelValues(service.Name).
|
||||
Set(float64(service.Replicas))
|
||||
}
|
||||
|
||||
break
|
||||
case <-quit:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
@ -77,6 +77,8 @@ func main() {
|
||||
faasHandlers.DeployFunction = makeHandler(reverseProxy, &metricsOptions)
|
||||
faasHandlers.DeleteFunction = makeHandler(reverseProxy, &metricsOptions)
|
||||
|
||||
metrics.AttachExternalWatcher(*config.FunctionsProviderURL, metricsOptions, "func", time.Second*5)
|
||||
|
||||
} else {
|
||||
maxRestarts := uint64(5)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user