mirror of
https://github.com/openfaas/faas.git
synced 2025-06-26 16:53:26 +00:00
Add HTTP status code to histogram
The histogram for gateway_functions_seconds excluded the status code that gives important information for setting up SLOs. Fixes: #1725 Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alex@openfaas.com>
This commit is contained in:
@ -1,33 +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 handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/openfaas/faas/gateway/metrics"
|
||||
"github.com/openfaas/faas/gateway/requests"
|
||||
)
|
||||
|
||||
// MakeAsyncReport makes a handler for asynchronous invocations to report back into.
|
||||
func MakeAsyncReport(metrics metrics.MetricOptions) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
report := requests.AsyncReport{}
|
||||
bytesOut, _ := ioutil.ReadAll(r.Body)
|
||||
json.Unmarshal(bytesOut, &report)
|
||||
|
||||
trackInvocation(report.FunctionName, metrics, report.StatusCode)
|
||||
|
||||
var taken time.Duration
|
||||
taken = time.Duration(report.TimeTaken)
|
||||
trackTimeExact(taken, metrics, report.FunctionName)
|
||||
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
}
|
@ -1,31 +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 handlers
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/openfaas/faas/gateway/metrics"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
func trackInvocation(service string, metrics metrics.MetricOptions, code int) {
|
||||
metrics.GatewayFunctionInvocation.With(
|
||||
prometheus.Labels{"function_name": service,
|
||||
"code": strconv.Itoa(code)}).Inc()
|
||||
}
|
||||
|
||||
func trackTime(then time.Time, metrics metrics.MetricOptions, name string) {
|
||||
since := time.Since(then)
|
||||
metrics.GatewayFunctionsHistogram.
|
||||
WithLabelValues(name).
|
||||
Observe(since.Seconds())
|
||||
}
|
||||
|
||||
func trackTimeExact(duration time.Duration, metrics metrics.MetricOptions, name string) {
|
||||
metrics.GatewayFunctionsHistogram.
|
||||
WithLabelValues(name).
|
||||
Observe(float64(duration))
|
||||
}
|
@ -56,16 +56,17 @@ func (p PrometheusFunctionNotifier) Notify(method string, URL string, originalUR
|
||||
}
|
||||
}
|
||||
|
||||
code := strconv.Itoa(statusCode)
|
||||
labels := prometheus.Labels{"function_name": serviceName, "code": code}
|
||||
|
||||
if event == "completed" {
|
||||
seconds := duration.Seconds()
|
||||
p.Metrics.GatewayFunctionsHistogram.
|
||||
WithLabelValues(serviceName).
|
||||
With(labels).
|
||||
Observe(seconds)
|
||||
|
||||
code := strconv.Itoa(statusCode)
|
||||
|
||||
p.Metrics.GatewayFunctionInvocation.
|
||||
With(prometheus.Labels{"function_name": serviceName, "code": code}).
|
||||
With(labels).
|
||||
Inc()
|
||||
} else if event == "started" {
|
||||
p.Metrics.GatewayFunctionInvocationStarted.WithLabelValues(serviceName).Inc()
|
||||
|
Reference in New Issue
Block a user