Function-based metrics for CE

OpenFaaS CE exposes metrics about function invocations and
about the gateway itself. OpenFaaS Pro has a richer set of
metrics including HTTP RED.

See also: https://docs.openfaas.com/architecture/metrics/

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis (OpenFaaS Ltd) 2023-01-28 09:10:48 +00:00
parent c26ec5221e
commit a128df471f
4 changed files with 2 additions and 52 deletions

View File

@ -17,20 +17,6 @@ type HTTPNotifier interface {
Notify(method string, URL string, originalURL string, statusCode int, event string, duration time.Duration)
}
// PrometheusServiceNotifier notifier for core service endpoints
type PrometheusServiceNotifier struct {
ServiceMetrics *metrics.ServiceMetricOptions
}
// Notify about service metrics
func (psn PrometheusServiceNotifier) Notify(method string, URL string, originalURL string, statusCode int, event string, duration time.Duration) {
code := fmt.Sprintf("%d", statusCode)
path := urlToLabel(URL)
psn.ServiceMetrics.Counter.WithLabelValues(method, path, code).Inc()
psn.ServiceMetrics.Histogram.WithLabelValues(method, path, code).Observe(duration.Seconds())
}
func urlToLabel(path string) string {
if len(path) > 0 {
path = strings.TrimRight(path, "/")

View File

@ -82,13 +82,9 @@ func main() {
FunctionNamespace: config.Namespace,
}
prometheusServiceNotifier := handlers.PrometheusServiceNotifier{
ServiceMetrics: metricsOptions.ServiceMetrics,
}
functionNotifiers := []handlers.HTTPNotifier{loggingNotifier, prometheusNotifier}
forwardingNotifiers := []handlers.HTTPNotifier{loggingNotifier, prometheusServiceNotifier}
quietNotifier := []handlers.HTTPNotifier{prometheusServiceNotifier}
forwardingNotifiers := []handlers.HTTPNotifier{loggingNotifier}
quietNotifier := []handlers.HTTPNotifier{}
urlResolver := middleware.SingleHostBaseURLResolver{BaseURL: config.FunctionsProviderURL.String()}
var functionURLResolver middleware.BaseURLResolver

View File

@ -46,9 +46,6 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.metricOptions.GatewayFunctionsHistogram.Describe(ch)
e.metricOptions.ServiceReplicasGauge.Describe(ch)
e.metricOptions.GatewayFunctionInvocationStarted.Describe(ch)
e.metricOptions.ServiceMetrics.Counter.Describe(ch)
e.metricOptions.ServiceMetrics.Histogram.Describe(ch)
}
// Collect collects data to be consumed by prometheus
@ -75,9 +72,6 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
}
e.metricOptions.ServiceReplicasGauge.Collect(ch)
e.metricOptions.ServiceMetrics.Counter.Collect(ch)
e.metricOptions.ServiceMetrics.Histogram.Collect(ch)
}
// StartServiceWatcher starts a ticker and collects service replica counts to expose to prometheus

View File

@ -18,8 +18,6 @@ type MetricOptions struct {
GatewayFunctionInvocationStarted *prometheus.CounterVec
ServiceReplicasGauge *prometheus.GaugeVec
ServiceMetrics *ServiceMetricOptions
}
// ServiceMetricOptions provides RED metrics
@ -69,24 +67,6 @@ func BuildMetricsOptions() MetricOptions {
[]string{"function_name"},
)
// For automatic monitoring and alerting (RED method)
histogram := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Subsystem: "http",
Name: "request_duration_seconds",
Help: "Seconds spent serving HTTP requests.",
Buckets: prometheus.DefBuckets,
}, []string{"method", "path", "status"})
// Can be used Kubernetes HPA v2
counter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: "http",
Name: "requests_total",
Help: "The total number of HTTP requests.",
},
[]string{"method", "path", "status"},
)
gatewayFunctionInvocationStarted := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gateway",
@ -97,16 +77,10 @@ func BuildMetricsOptions() MetricOptions {
[]string{"function_name"},
)
serviceMetricOptions := &ServiceMetricOptions{
Counter: counter,
Histogram: histogram,
}
metricsOptions := MetricOptions{
GatewayFunctionsHistogram: gatewayFunctionsHistogram,
GatewayFunctionInvocation: gatewayFunctionInvocation,
ServiceReplicasGauge: serviceReplicas,
ServiceMetrics: serviceMetricOptions,
GatewayFunctionInvocationStarted: gatewayFunctionInvocationStarted,
}