Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
2fd65a46
B
beego
项目概览
二次元的罪恶王冠
/
beego
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beego
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2fd65a46
编写于
8月 07, 2020
作者:
M
Ming Deng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support prometheus
上级
08cec917
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
144 addition
and
0 deletion
+144
-0
pkg/orm/filter/prometheus/filter.go
pkg/orm/filter/prometheus/filter.go
+84
-0
pkg/orm/filter/prometheus/filter_test.go
pkg/orm/filter/prometheus/filter_test.go
+60
-0
未找到文件。
pkg/orm/filter/prometheus/filter.go
0 → 100644
浏览文件 @
2fd65a46
// 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
(
"context"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
beego
"github.com/astaxie/beego/pkg"
"github.com/astaxie/beego/pkg/orm"
)
// FilterChainBuilder is an extension point,
// when we want to support some configuration,
// please use this structure
type
FilterChainBuilder
struct
{
summaryVec
prometheus
.
ObserverVec
}
func
NewFilterChainBuilder
()
*
FilterChainBuilder
{
summaryVec
:=
prometheus
.
NewSummaryVec
(
prometheus
.
SummaryOpts
{
Name
:
"beego"
,
Subsystem
:
"orm_operation"
,
ConstLabels
:
map
[
string
]
string
{
"server"
:
beego
.
BConfig
.
ServerName
,
"env"
:
beego
.
BConfig
.
RunMode
,
"appname"
:
beego
.
BConfig
.
AppName
,
},
Help
:
"The statics info for orm operation"
,
},
[]
string
{
"method"
,
"name"
,
"duration"
,
"insideTx"
,
"txName"
})
prometheus
.
MustRegister
(
summaryVec
)
return
&
FilterChainBuilder
{
summaryVec
:
summaryVec
,
}
}
func
(
builder
*
FilterChainBuilder
)
FilterChain
(
next
orm
.
Filter
)
orm
.
Filter
{
return
func
(
ctx
context
.
Context
,
inv
*
orm
.
Invocation
)
{
startTime
:=
time
.
Now
()
next
(
ctx
,
inv
)
endTime
:=
time
.
Now
()
dur
:=
(
endTime
.
Sub
(
startTime
))
/
time
.
Millisecond
// if the TPS is too large, here may be some problem
// thinking about using goroutine pool
go
builder
.
report
(
ctx
,
inv
,
dur
)
}
}
func
(
builder
*
FilterChainBuilder
)
report
(
ctx
context
.
Context
,
inv
*
orm
.
Invocation
,
dur
time
.
Duration
)
{
// start a transaction, we don't record it
if
strings
.
HasPrefix
(
inv
.
Method
,
"Begin"
)
{
return
}
if
inv
.
Method
==
"Commit"
||
inv
.
Method
==
"Rollback"
{
builder
.
reportTxn
(
ctx
,
inv
)
return
}
builder
.
summaryVec
.
WithLabelValues
(
inv
.
Method
,
inv
.
GetTableName
(),
strconv
.
Itoa
(
int
(
dur
)),
strconv
.
FormatBool
(
inv
.
InsideTx
),
inv
.
TxName
)
}
func
(
builder
*
FilterChainBuilder
)
reportTxn
(
ctx
context
.
Context
,
inv
*
orm
.
Invocation
)
{
dur
:=
time
.
Now
()
.
Sub
(
inv
.
TxStartTime
)
/
time
.
Millisecond
builder
.
summaryVec
.
WithLabelValues
(
inv
.
Method
,
inv
.
TxName
,
strconv
.
Itoa
(
int
(
dur
)),
strconv
.
FormatBool
(
inv
.
InsideTx
),
inv
.
TxName
)
}
pkg/orm/filter/prometheus/filter_test.go
0 → 100644
浏览文件 @
2fd65a46
// 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
(
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/astaxie/beego/pkg/orm"
)
func
TestFilterChainBuilder_FilterChain
(
t
*
testing
.
T
)
{
builder
:=
NewFilterChainBuilder
()
assert
.
NotNil
(
t
,
builder
.
summaryVec
)
filter
:=
builder
.
FilterChain
(
func
(
ctx
context
.
Context
,
inv
*
orm
.
Invocation
)
{
inv
.
Method
=
"coming"
})
assert
.
NotNil
(
t
,
filter
)
inv
:=
&
orm
.
Invocation
{}
filter
(
context
.
Background
(),
inv
)
assert
.
Equal
(
t
,
"coming"
,
inv
.
Method
)
inv
=
&
orm
.
Invocation
{
Method
:
"Hello"
,
TxStartTime
:
time
.
Now
(),
}
builder
.
reportTxn
(
context
.
Background
(),
inv
)
inv
=
&
orm
.
Invocation
{
Method
:
"Begin"
,
}
ctx
:=
context
.
Background
()
// it will be ignored
builder
.
report
(
ctx
,
inv
,
time
.
Second
)
inv
.
Method
=
"Commit"
builder
.
report
(
ctx
,
inv
,
time
.
Second
)
inv
.
Method
=
"Update"
builder
.
report
(
ctx
,
inv
,
time
.
Second
)
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录