71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
/*
|
|
* Copyright 2024 Jonas Kaninda
|
|
*
|
|
* 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 (
|
|
"github.com/gorilla/mux"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type PrometheusRoute struct {
|
|
Name string
|
|
Path string
|
|
}
|
|
|
|
var TotalRequests = prometheus.NewCounterVec(
|
|
prometheus.CounterOpts{
|
|
Name: "http_requests_total",
|
|
Help: "Number of get requests.",
|
|
},
|
|
[]string{"name", "path"},
|
|
)
|
|
|
|
var ResponseStatus = prometheus.NewCounterVec(
|
|
prometheus.CounterOpts{
|
|
Name: "response_status",
|
|
Help: "Status of HTTP response",
|
|
},
|
|
[]string{"status"},
|
|
)
|
|
|
|
var HttpDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
|
Name: "http_response_time_seconds",
|
|
Help: "Duration of HTTP requests.",
|
|
}, []string{"name", "path"})
|
|
|
|
func (pr PrometheusRoute) PrometheusMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
path := pr.Path
|
|
if len(path) == 0 {
|
|
route := mux.CurrentRoute(r)
|
|
path, _ = route.GetPathTemplate()
|
|
}
|
|
timer := prometheus.NewTimer(HttpDuration.WithLabelValues(pr.Name, path))
|
|
|
|
ResponseStatus.WithLabelValues(strconv.Itoa(http.StatusOK)).Inc()
|
|
TotalRequests.WithLabelValues(pr.Name, path).Inc()
|
|
|
|
timer.ObserveDuration()
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
|
|
}
|