1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-14 06:30:55 +00:00
Beego/metric/prometheus.go

100 lines
3.1 KiB
Go
Raw Normal View History

2020-06-21 03:43:54 +00:00
// Copyright 2020 astaxie
//
// 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 metric
import (
"net/http"
"reflect"
"strconv"
2020-06-24 18:15:56 +00:00
"strings"
2020-06-21 03:43:54 +00:00
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
)
func PrometheusMiddleWare(next http.Handler) http.Handler {
summaryVec := prometheus.NewSummaryVec(prometheus.SummaryOpts{
2020-06-21 09:35:34 +00:00
Name: "beego",
2020-06-21 03:43:54 +00:00
Subsystem: "http_request",
ConstLabels: map[string]string{
2020-06-21 09:35:34 +00:00
"server": beego.BConfig.ServerName,
"env": beego.BConfig.RunMode,
"appname": beego.BConfig.AppName,
2020-06-21 03:43:54 +00:00
},
Help: "The statics info for http request",
2020-06-21 09:35:34 +00:00
}, []string{"pattern", "method", "status", "duration"})
2020-06-21 03:43:54 +00:00
prometheus.MustRegister(summaryVec)
2020-06-24 18:15:56 +00:00
registerBuildInfo()
2020-06-21 03:43:54 +00:00
return http.HandlerFunc(func(writer http.ResponseWriter, q *http.Request) {
start := time.Now()
next.ServeHTTP(writer, q)
end := time.Now()
go report(end.Sub(start), writer, q, summaryVec)
})
}
2020-06-24 18:15:56 +00:00
func registerBuildInfo() {
buildInfo := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "beego",
Subsystem: "build_info",
Help: "The building information",
ConstLabels: map[string]string{
2020-07-22 15:00:06 +00:00
"appname": beego.BConfig.AppName,
2020-06-24 18:15:56 +00:00
"build_version": beego.BuildVersion,
"build_revision": beego.BuildGitRevision,
"build_status": beego.BuildStatus,
"build_tag": beego.BuildTag,
2020-07-22 15:00:06 +00:00
"build_time": strings.Replace(beego.BuildTime, "--", " ", 1),
2020-06-24 18:15:56 +00:00
"go_version": beego.GoVersion,
"git_branch": beego.GitBranch,
2020-07-22 15:00:06 +00:00
"start_time": time.Now().Format("2006-01-02 15:04:05"),
2020-06-24 18:15:56 +00:00
},
}, []string{})
prometheus.MustRegister(buildInfo)
buildInfo.WithLabelValues().Set(1)
}
2020-06-21 03:43:54 +00:00
func report(dur time.Duration, writer http.ResponseWriter, q *http.Request, vec *prometheus.SummaryVec) {
ctrl := beego.BeeApp.Handlers
ctx := ctrl.GetContext()
ctx.Reset(writer, q)
defer ctrl.GiveBackContext(ctx)
// We cannot read the status code from q.Response.StatusCode
// since the http server does not set q.Response. So q.Response is nil
// Thus, we use reflection to read the status from writer whose concrete type is http.response
responseVal := reflect.ValueOf(writer).Elem()
field := responseVal.FieldByName("status")
status := -1
if field.IsValid() && field.Kind() == reflect.Int {
status = int(field.Int())
}
ptn := "UNKNOWN"
if rt, found := ctrl.FindRouter(ctx); found {
ptn = rt.GetPattern()
} else {
logs.Warn("we can not find the router info for this request, so request will be recorded as UNKNOWN: " + q.URL.String())
}
2020-06-21 09:35:34 +00:00
ms := dur / time.Millisecond
vec.WithLabelValues(ptn, q.Method, strconv.Itoa(status), strconv.Itoa(int(ms))).Observe(float64(ms))
2020-06-21 03:43:54 +00:00
}