Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
8fc4f884
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,发现更多精彩内容 >>
提交
8fc4f884
编写于
9月 02, 2020
作者:
M
Ming Deng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
adapt grace and metric
上级
3bf5cde3
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
285 addition
and
1 deletion
+285
-1
pkg/adapter/context/renderer.go
pkg/adapter/context/renderer.go
+0
-1
pkg/adapter/grace/grace.go
pkg/adapter/grace/grace.go
+96
-0
pkg/adapter/grace/server.go
pkg/adapter/grace/server.go
+48
-0
pkg/adapter/metric/prometheus.go
pkg/adapter/metric/prometheus.go
+99
-0
pkg/adapter/metric/prometheus_test.go
pkg/adapter/metric/prometheus_test.go
+42
-0
未找到文件。
pkg/adapter/context/renderer.go
浏览文件 @
8fc4f884
...
...
@@ -6,4 +6,3 @@ import (
// Renderer defines an http response renderer
type
Renderer
context
.
Renderer
pkg/adapter/grace/grace.go
0 → 100644
浏览文件 @
8fc4f884
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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 grace use to hot reload
// Description: http://grisha.org/blog/2014/06/03/graceful-restart-in-golang/
//
// Usage:
//
// import(
// "log"
// "net/http"
// "os"
//
// "github.com/astaxie/beego/grace"
// )
//
// func handler(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("WORLD!"))
// }
//
// func main() {
// mux := http.NewServeMux()
// mux.HandleFunc("/hello", handler)
//
// err := grace.ListenAndServe("localhost:8080", mux)
// if err != nil {
// log.Println(err)
// }
// log.Println("Server on 8080 stopped")
// os.Exit(0)
// }
package
grace
import
(
"net/http"
"time"
"github.com/astaxie/beego/pkg/server/web/grace"
)
const
(
// PreSignal is the position to add filter before signal
PreSignal
=
iota
// PostSignal is the position to add filter after signal
PostSignal
// StateInit represent the application inited
StateInit
// StateRunning represent the application is running
StateRunning
// StateShuttingDown represent the application is shutting down
StateShuttingDown
// StateTerminate represent the application is killed
StateTerminate
)
var
(
// DefaultReadTimeOut is the HTTP read timeout
DefaultReadTimeOut
time
.
Duration
// DefaultWriteTimeOut is the HTTP Write timeout
DefaultWriteTimeOut
time
.
Duration
// DefaultMaxHeaderBytes is the Max HTTP Header size, default is 0, no limit
DefaultMaxHeaderBytes
int
// DefaultTimeout is the shutdown server's timeout. default is 60s
DefaultTimeout
=
grace
.
DefaultTimeout
)
// NewServer returns a new graceServer.
func
NewServer
(
addr
string
,
handler
http
.
Handler
)
(
srv
*
Server
)
{
return
(
*
Server
)(
grace
.
NewServer
(
addr
,
handler
))
}
// ListenAndServe refer http.ListenAndServe
func
ListenAndServe
(
addr
string
,
handler
http
.
Handler
)
error
{
server
:=
NewServer
(
addr
,
handler
)
return
server
.
ListenAndServe
()
}
// ListenAndServeTLS refer http.ListenAndServeTLS
func
ListenAndServeTLS
(
addr
string
,
certFile
string
,
keyFile
string
,
handler
http
.
Handler
)
error
{
server
:=
NewServer
(
addr
,
handler
)
return
server
.
ListenAndServeTLS
(
certFile
,
keyFile
)
}
pkg/adapter/grace/server.go
0 → 100644
浏览文件 @
8fc4f884
package
grace
import
(
"os"
"github.com/astaxie/beego/pkg/server/web/grace"
)
// Server embedded http.Server
type
Server
grace
.
Server
// Serve accepts incoming connections on the Listener l,
// creating a new service goroutine for each.
// The service goroutines read requests and then call srv.Handler to reply to them.
func
(
srv
*
Server
)
Serve
()
(
err
error
)
{
return
(
*
grace
.
Server
)(
srv
)
.
Serve
()
}
// ListenAndServe listens on the TCP network address srv.Addr and then calls Serve
// to handle requests on incoming connections. If srv.Addr is blank, ":http" is
// used.
func
(
srv
*
Server
)
ListenAndServe
()
(
err
error
)
{
return
(
*
grace
.
Server
)(
srv
)
.
ListenAndServe
()
}
// ListenAndServeTLS listens on the TCP network address srv.Addr and then calls
// Serve to handle requests on incoming TLS connections.
//
// Filenames containing a certificate and matching private key for the server must
// be provided. If the certificate is signed by a certificate authority, the
// certFile should be the concatenation of the server's certificate followed by the
// CA's certificate.
//
// If srv.Addr is blank, ":https" is used.
func
(
srv
*
Server
)
ListenAndServeTLS
(
certFile
,
keyFile
string
)
error
{
return
(
*
grace
.
Server
)(
srv
)
.
ListenAndServeTLS
(
certFile
,
keyFile
)
}
// ListenAndServeMutualTLS listens on the TCP network address srv.Addr and then calls
// Serve to handle requests on incoming mutual TLS connections.
func
(
srv
*
Server
)
ListenAndServeMutualTLS
(
certFile
,
keyFile
,
trustFile
string
)
error
{
return
(
*
grace
.
Server
)(
srv
)
.
ListenAndServeMutualTLS
(
certFile
,
keyFile
,
trustFile
)
}
// RegisterSignalHook registers a function to be run PreSignal or PostSignal for a given signal.
func
(
srv
*
Server
)
RegisterSignalHook
(
ppFlag
int
,
sig
os
.
Signal
,
f
func
())
error
{
return
(
*
grace
.
Server
)(
srv
)
.
RegisterSignalHook
(
ppFlag
,
sig
,
f
)
}
pkg/adapter/metric/prometheus.go
0 → 100644
浏览文件 @
8fc4f884
// 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"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/astaxie/beego/pkg/infrastructure/logs"
"github.com/astaxie/beego/pkg/server/web"
)
func
PrometheusMiddleWare
(
next
http
.
Handler
)
http
.
Handler
{
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"
,
"duration"
})
prometheus
.
MustRegister
(
summaryVec
)
registerBuildInfo
()
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
)
})
}
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"
:
web
.
BuildVersion
,
"build_revision"
:
web
.
BuildGitRevision
,
"build_status"
:
web
.
BuildStatus
,
"build_tag"
:
web
.
BuildTag
,
"build_time"
:
strings
.
Replace
(
web
.
BuildTime
,
"--"
,
" "
,
1
),
"go_version"
:
web
.
GoVersion
,
"git_branch"
:
web
.
GitBranch
,
"start_time"
:
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
),
},
},
[]
string
{})
prometheus
.
MustRegister
(
buildInfo
)
buildInfo
.
WithLabelValues
()
.
Set
(
1
)
}
func
report
(
dur
time
.
Duration
,
writer
http
.
ResponseWriter
,
q
*
http
.
Request
,
vec
*
prometheus
.
SummaryVec
)
{
ctrl
:=
web
.
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
())
}
ms
:=
dur
/
time
.
Millisecond
vec
.
WithLabelValues
(
ptn
,
q
.
Method
,
strconv
.
Itoa
(
status
),
strconv
.
Itoa
(
int
(
ms
)))
.
Observe
(
float64
(
ms
))
}
pkg/adapter/metric/prometheus_test.go
0 → 100644
浏览文件 @
8fc4f884
// 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"
"net/url"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/astaxie/beego/context"
)
func
TestPrometheusMiddleWare
(
t
*
testing
.
T
)
{
middleware
:=
PrometheusMiddleWare
(
http
.
HandlerFunc
(
func
(
http
.
ResponseWriter
,
*
http
.
Request
)
{}))
writer
:=
&
context
.
Response
{}
request
:=
&
http
.
Request
{
URL
:
&
url
.
URL
{
Host
:
"localhost"
,
RawPath
:
"/a/b/c"
,
},
Method
:
"POST"
,
}
vec
:=
prometheus
.
NewSummaryVec
(
prometheus
.
SummaryOpts
{},
[]
string
{
"pattern"
,
"method"
,
"status"
,
"duration"
})
report
(
time
.
Second
,
writer
,
request
,
vec
)
middleware
.
ServeHTTP
(
writer
,
request
)
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录