/
filter.go
107 lines (94 loc) · 3.19 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2020 beego
//
// 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 prometheus
import (
"strconv"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/W3-Partha/Beego"
"github.com/W3-Partha/Beego/core/logs"
"github.com/W3-Partha/Beego/server/web"
"github.com/W3-Partha/Beego/server/web/context"
)
const unknownRouterPattern = "UnknownRouterPattern"
// FilterChainBuilder is an extension point,
// when we want to support some configuration,
// please use this structure
type FilterChainBuilder struct{}
var (
summaryVec prometheus.ObserverVec
initSummaryVec sync.Once
)
// FilterChain returns a FilterFunc. The filter will records some metrics
func (builder *FilterChainBuilder) FilterChain(next web.FilterFunc) web.FilterFunc {
initSummaryVec.Do(func() {
summaryVec = builder.buildVec()
err := prometheus.Register(summaryVec)
if _, ok := err.(*prometheus.AlreadyRegisteredError); err != nil && !ok {
logs.Error("web module register prometheus vector failed, %+v", err)
}
registerBuildInfo()
})
return func(ctx *context.Context) {
startTime := time.Now()
next(ctx)
endTime := time.Now()
go report(endTime.Sub(startTime), ctx, summaryVec)
}
}
func (builder *FilterChainBuilder) buildVec() *prometheus.SummaryVec {
summaryVec := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: "beego",
Subsystem: "http_request",
ConstLabels: map[string]string{
"server": web.BConfig.ServerName,
"env": web.BConfig.RunMode,
"appname": web.BConfig.AppName,
},
Help: "The statics info for http request",
}, []string{"pattern", "method", "status"})
return summaryVec
}
func registerBuildInfo() {
buildInfo := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "beego",
Subsystem: "build_info",
Help: "The building information",
ConstLabels: map[string]string{
"appname": web.BConfig.AppName,
"build_version": beego.BuildVersion,
"build_revision": beego.BuildGitRevision,
"build_status": beego.BuildStatus,
"build_tag": beego.BuildTag,
"build_time": strings.Replace(beego.BuildTime, "--", " ", 1),
"go_version": beego.GoVersion,
"git_branch": beego.GitBranch,
"start_time": time.Now().Format("2006-01-02 15:04:05"),
},
}, []string{})
_ = prometheus.Register(buildInfo)
buildInfo.WithLabelValues().Set(1)
}
func report(dur time.Duration, ctx *context.Context, vec prometheus.ObserverVec) {
status := ctx.Output.Status
ptnItf := ctx.Input.GetData("RouterPattern")
ptn := unknownRouterPattern
if ptnItf != nil {
ptn = ptnItf.(string)
}
ms := dur / time.Millisecond
vec.WithLabelValues(ptn, ctx.Input.Method(), strconv.Itoa(status)).Observe(float64(ms))
}