Skip to content

Commit a7dedd0

Browse files
committed
#### Version 1.1
* 主要新增集成基础统计数据 * 【新增】dotweb/state接口,提供基础统计数据,主要数据说明: * 1、ServerStartTime 服务启动时间 * 2、TotalRequestCount 服务启动以来累计请求数(排除了"/dotweb/"下系统自有页面的访问数) * 3、TotalErrorCount 服务启动以来累计错误数 * 4、IntervalRequestData 按1分钟为间隔,默认保存最近60分钟,每分钟的请求数(排除了"/dotweb/"下系统自有页面的访问数) * 5、DetailRequestPageData 服务启动以来,每个页面的累计请求数(排除了"/dotweb/"下系统自有页面的访问数) * 6、IntervalErrorData 按1分钟为间隔,默认保存最近60分钟,每分钟的错误数 * 7、DetailErrorPageData 服务启动以来,每个页面的累计错误数 * 8、DetailErrorData 服务启动以来,每个异常的累计数 * 9、DetailHttpCodeData 服务启动以来,每个Http状态码的累计数 * 10、可通过 {host}/dotweb/state 获取数据 * 【新增】dotweb/state/interval接口,提供按分钟级的基础数据查询 * 主要数据说明: * 1、Time 表示查询时间的字符串,最小单位为分钟,例如:201709251200 * 2、RequestCount 单位时间内累计请求数(排除了"/dotweb/"下系统自有页面的访问数) * 3、ErrorCount 单位时间内累计错误数 * 2017-09-25 13:00
1 parent fbbbf3c commit a7dedd0

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

core/state.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,20 @@ type pool struct {
6161
httpCodeInfo sync.Pool
6262
}
6363

64+
//http request count info
6465
type RequestInfo struct {
6566
Url string
6667
Num uint64
6768
}
69+
70+
//error count info
6871
type ErrorInfo struct {
6972
Url string
7073
ErrMsg string
7174
Num uint64
7275
}
7376

77+
//httpcode count info
7478
type HttpCodeInfo struct {
7579
Url string
7680
Code int
@@ -96,7 +100,7 @@ type ServerStateInfo struct {
96100
//明细异常数据 - 以不带参数的访问url为key
97101
DetailErrorData *ItemContext
98102
//明细Http状态码数据 - 以HttpCode为key,例如200、500等
99-
DetailHttpCodeData *ItemContext
103+
DetailHTTPCodeData *ItemContext
100104

101105
dataChan_Request chan *RequestInfo
102106
dataChan_Error chan *ErrorInfo
@@ -134,9 +138,9 @@ func (state *ServerStateInfo) ShowHtmlData() string {
134138
data += "DetailErrorData : " + jsonutil.GetJsonString(state.DetailErrorData.GetCurrentMap())
135139
state.DetailErrorData.RUnlock()
136140
data += "<br>"
137-
state.DetailHttpCodeData.RLock()
138-
data += "DetailHttpCodeData : " + jsonutil.GetJsonString(state.DetailHttpCodeData.GetCurrentMap())
139-
state.DetailHttpCodeData.RUnlock()
141+
state.DetailHTTPCodeData.RLock()
142+
data += "DetailHttpCodeData : " + jsonutil.GetJsonString(state.DetailHTTPCodeData.GetCurrentMap())
143+
state.DetailHTTPCodeData.RUnlock()
140144
data += "</div></body></html>"
141145
return data
142146
}
@@ -151,7 +155,7 @@ func (state *ServerStateInfo) QueryIntervalErrorData(queryKey string) uint64 {
151155
return state.IntervalErrorData.GetUInt64(queryKey)
152156
}
153157

154-
//增加请求数
158+
//AddRequestCount 增加请求数
155159
func (state *ServerStateInfo) AddRequestCount(page string, num uint64) uint64 {
156160
if strings.Index(page, "/dotweb/") != 0 {
157161
atomic.AddUint64(&state.TotalRequestCount, num)
@@ -160,15 +164,15 @@ func (state *ServerStateInfo) AddRequestCount(page string, num uint64) uint64 {
160164
return state.TotalRequestCount
161165
}
162166

163-
//增加Http状态码数据
164-
func (state *ServerStateInfo) AddHttpCodeCount(page string, code int, num uint64) uint64 {
167+
//AddHttpCodeCount 增加Http状态码数据
168+
func (state *ServerStateInfo) AddTTPCodeCount(page string, code int, num uint64) uint64 {
165169
if strings.Index(page, "/dotweb/") != 0 {
166-
state.addHttpCodeData(page, code, num)
170+
state.addHTTPCodeData(page, code, num)
167171
}
168172
return state.TotalErrorCount
169173
}
170174

171-
//增加错误数
175+
//AddErrorCount 增加错误数
172176
func (state *ServerStateInfo) AddErrorCount(page string, err error, num uint64) uint64 {
173177
atomic.AddUint64(&state.TotalErrorCount, num)
174178
state.addErrorData(page, err, num)
@@ -192,7 +196,7 @@ func (state *ServerStateInfo) addErrorData(page string, err error, num uint64) {
192196
state.dataChan_Error <- info
193197
}
194198

195-
func (state *ServerStateInfo) addHttpCodeData(page string, code int, num uint64) {
199+
func (state *ServerStateInfo) addHTTPCodeData(page string, code int, num uint64) {
196200
//get from pool
197201
info := state.infoPool.httpCodeInfo.Get().(*HttpCodeInfo)
198202
info.Url = page
@@ -244,8 +248,8 @@ func (state *ServerStateInfo) handleInfo() {
244248
{
245249
//set detail error page data
246250
key := strconv.Itoa(info.Code)
247-
val := state.DetailHttpCodeData.GetUInt64(key)
248-
state.DetailHttpCodeData.Set(key, val+info.Num)
251+
val := state.DetailHTTPCodeData.GetUInt64(key)
252+
state.DetailHTTPCodeData.Set(key, val+info.Num)
249253

250254
//put info obj
251255
state.infoPool.httpCodeInfo.Put(info)
@@ -261,7 +265,7 @@ func (state *ServerStateInfo) checkAndRemoveIntervalData() {
261265
//check IntervalRequestData
262266
state.IntervalRequestData.RLock()
263267
if state.IntervalRequestData.Len() > 10 {
264-
for k, _ := range state.IntervalRequestData.GetCurrentMap() {
268+
for k := range state.IntervalRequestData.GetCurrentMap() {
265269
if t, err := time.Parse(minuteTimeLayout, k); err != nil {
266270
needRemoveKey = append(needRemoveKey, k)
267271
} else {
@@ -281,7 +285,7 @@ func (state *ServerStateInfo) checkAndRemoveIntervalData() {
281285
needRemoveKey = []string{}
282286
state.IntervalErrorData.RLock()
283287
if state.IntervalErrorData.Len() > 10 {
284-
for k, _ := range state.IntervalErrorData.GetCurrentMap() {
288+
for k := range state.IntervalErrorData.GetCurrentMap() {
285289
if t, err := time.Parse(minuteTimeLayout, k); err != nil {
286290
needRemoveKey = append(needRemoveKey, k)
287291
} else {

router.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ func (r *router) MatchPath(ctx Context, routePath string) bool {
202202

203203
// ServeHTTP makes the router implement the http.Handler interface.
204204
func (r *router) ServeHTTP(ctx *HttpContext) {
205+
//增加状态计数
206+
core.GlobalState.AddRequestCount(ctx.Request().Path(), 1)
207+
205208
req := ctx.Request().Request
206209
w := ctx.Response().Writer()
207210
path := req.URL.Path

server.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,9 @@ func (server *HttpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
111111
httpCtx := server.pool.context.Get().(*HttpContext)
112112
httpCtx.reset(response, request, server, nil, nil, nil)
113113

114-
//增加状态计数
115-
core.GlobalState.AddRequestCount(httpCtx.Request().Path(), 1)
116-
117114
server.Router().ServeHTTP(httpCtx)
118115

119-
core.GlobalState.AddHttpCodeCount(httpCtx.Request().Path(), httpCtx.Response().HttpCode(), 1)
116+
core.GlobalState.AddTTPCodeCount(httpCtx.Request().Path(), httpCtx.Response().HttpCode(), 1)
120117

121118
//release response
122119
response.release()

0 commit comments

Comments
 (0)