mirror of
https://github.com/openfaas/faas.git
synced 2025-06-08 16:26:47 +00:00
Add User-Agent to Prometheus Queries
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alex@openfaas.com>
This commit is contained in:
parent
637b0b045f
commit
546bfee9dc
@ -167,7 +167,7 @@ func main() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
prometheusQuery := metrics.NewPrometheusQuery(config.PrometheusHost, config.PrometheusPort, &http.Client{})
|
prometheusQuery := metrics.NewPrometheusQuery(config.PrometheusHost, config.PrometheusPort, &http.Client{}, version.BuildVersion())
|
||||||
faasHandlers.ListFunctions = metrics.AddMetricsHandler(faasHandlers.ListFunctions, prometheusQuery)
|
faasHandlers.ListFunctions = metrics.AddMetricsHandler(faasHandlers.ListFunctions, prometheusQuery)
|
||||||
faasHandlers.ScaleFunction = scaling.MakeHorizontalScalingHandler(handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers, urlResolver, nilURLTransformer, serviceAuthInjector))
|
faasHandlers.ScaleFunction = scaling.MakeHorizontalScalingHandler(handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers, urlResolver, nilURLTransformer, serviceAuthInjector))
|
||||||
|
|
||||||
|
@ -9,9 +9,10 @@ import (
|
|||||||
|
|
||||||
// PrometheusQuery represents parameters for querying Prometheus
|
// PrometheusQuery represents parameters for querying Prometheus
|
||||||
type PrometheusQuery struct {
|
type PrometheusQuery struct {
|
||||||
Port int
|
host string
|
||||||
Host string
|
port int
|
||||||
Client *http.Client
|
client *http.Client
|
||||||
|
userAgentVersion string
|
||||||
}
|
}
|
||||||
|
|
||||||
type PrometheusQueryFetcher interface {
|
type PrometheusQueryFetcher interface {
|
||||||
@ -19,45 +20,47 @@ type PrometheusQueryFetcher interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewPrometheusQuery create a NewPrometheusQuery
|
// NewPrometheusQuery create a NewPrometheusQuery
|
||||||
func NewPrometheusQuery(host string, port int, client *http.Client) PrometheusQuery {
|
func NewPrometheusQuery(host string, port int, client *http.Client, userAgentVersion string) PrometheusQuery {
|
||||||
return PrometheusQuery{
|
return PrometheusQuery{
|
||||||
Client: client,
|
client: client,
|
||||||
Host: host,
|
host: host,
|
||||||
Port: port,
|
port: port,
|
||||||
|
userAgentVersion: userAgentVersion,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch queries aggregated stats
|
// Fetch queries aggregated stats
|
||||||
func (q PrometheusQuery) Fetch(query string) (*VectorQueryResponse, error) {
|
func (q PrometheusQuery) Fetch(query string) (*VectorQueryResponse, error) {
|
||||||
|
|
||||||
req, reqErr := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s:%d/api/v1/query?query=%s", q.Host, q.Port, query), nil)
|
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s:%d/api/v1/query?query=%s", q.host, q.port, query), nil)
|
||||||
if reqErr != nil {
|
if err != nil {
|
||||||
return nil, reqErr
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res, getErr := q.Client.Do(req)
|
req.Header.Set("User-Agent", fmt.Sprintf("openfaas-gateway/%s (Prometheus query)", q.userAgentVersion))
|
||||||
if getErr != nil {
|
|
||||||
return nil, getErr
|
res, err := q.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.Body != nil {
|
if res.Body != nil {
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
bytesOut, readErr := io.ReadAll(res.Body)
|
bytesOut, err := io.ReadAll(res.Body)
|
||||||
if readErr != nil {
|
if err != nil {
|
||||||
return nil, readErr
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.StatusCode != http.StatusOK {
|
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))
|
return nil, fmt.Errorf("unexpected status code from Prometheus want: %d, got: %d, body: %s", http.StatusOK, res.StatusCode, string(bytesOut))
|
||||||
}
|
}
|
||||||
|
|
||||||
var values VectorQueryResponse
|
var values VectorQueryResponse
|
||||||
|
|
||||||
unmarshalErr := json.Unmarshal(bytesOut, &values)
|
if err := json.Unmarshal(bytesOut, &values); err != nil {
|
||||||
if unmarshalErr != nil {
|
return nil, fmt.Errorf("error unmarshaling result: %s, '%s'", err, string(bytesOut))
|
||||||
return nil, fmt.Errorf("Error unmarshaling result: %s, '%s'", unmarshalErr, string(bytesOut))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &values, nil
|
return &values, nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user