Prometheus metrics improvements (#2398)

- Enabled metrics in storagesvc, buildermgr and controller.
- Added a middleware in storagesvc, router, executor and controller to monitor total number of http requests, each request's duration and number of requests that are currently being served. These requests can be filtered on their path, method or statuscode.
- Removed functionCallDuration and functionCallResponseSize metrics from router.
- Removed funcAliveSummary, funcIsAlive, funcReapTime and idleTime metrics.
- Replaced function calls for collecting metrics to direct metric calls.

Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>
Co-authored-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
Ankit Chawla
2022-04-13 21:49:46 +05:30
committed by GitHub
co-authored by Sanket Sudake
parent 231de707dc
commit b638a6d047
26 changed files with 359 additions and 303 deletions
+92
View File
@@ -0,0 +1,92 @@
/*
Copyright 2022 The Fission Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package metrics
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/fission/fission/pkg/router/util"
)
type ResponseWriterWrapper struct {
http.ResponseWriter
statusCode int
}
var (
httpRequestsTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Number of requests by path, method and status code.",
},
[]string{"path", "method", "code"},
)
httpRequestDuration = promauto.NewSummaryVec(
prometheus.SummaryOpts{
Name: "http_requests_duration_seconds",
Help: "Time taken to serve the request by path and method.",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
[]string{"path", "method"},
)
httpRequestInFlight = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "http_requests_in_flight",
Help: "Number of requests currently being served by path and method.",
},
[]string{"path", "method"},
)
)
func (rw *ResponseWriterWrapper) WriteHeader(statuscode int) {
rw.statusCode = statuscode
rw.ResponseWriter.WriteHeader(statuscode)
}
func HTTPMetricMiddleware() mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if util.IsWebsocketRequest(r) {
next.ServeHTTP(w, r)
return
}
labels := make(prometheus.Labels, 0)
labels["path"] = r.URL.Path
if route := mux.CurrentRoute(r); route != nil {
if routePath, err := route.GetPathTemplate(); err == nil {
labels["path"] = routePath
}
}
labels["method"] = r.Method
rw := ResponseWriterWrapper{w, http.StatusOK}
httpRequestInFlight.With(labels).Inc()
httpRequestDuration := prometheus.NewTimer(httpRequestDuration.With(labels))
defer func() {
httpRequestDuration.ObserveDuration()
httpRequestInFlight.With(labels).Dec()
labels["code"] = fmt.Sprintf("%d", rw.statusCode)
httpRequestsTotal.With(labels).Inc()
}()
next.ServeHTTP(&rw, r)
})
}
}
+56
View File
@@ -0,0 +1,56 @@
/*
Copyright 2022 The Fission Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package metrics
import (
"context"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
)
func ServeMetrics(ctx context.Context, logger *zap.Logger) {
metricsAddr := os.Getenv("METRICS_ADDR")
if metricsAddr == "" {
metricsAddr = ":8080"
}
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
s := &http.Server{
Addr: metricsAddr,
Handler: mux,
}
logger.Info("Starting metrics server", zap.String("address", metricsAddr))
go func() {
if err := s.ListenAndServe(); err != nil {
if err != http.ErrServerClosed {
logger.Error("Metrics server error", zap.Error(err))
}
}
}()
<-ctx.Done()
logger.Info("Shutting down metrics server")
err := s.Shutdown(ctx)
if err == context.DeadlineExceeded || err == context.Canceled {
return
}
if err != nil {
logger.Error("Failed to shutdown metrics server", zap.Error(err))
}
}