Skip to content

Commit 5c78d22

Browse files
author
shugenniu
committed
bugfix: fix prometheus server post process callback
1 parent 8324356 commit 5c78d22

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

apiserver/prometheussd/server.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (h *PrometheusServer) Initialize(_ context.Context, option map[string]inter
6464
api map[string]apiserver.APIConfig) error {
6565
h.option = option
6666
h.openAPI = api
67-
h.listenIP = option["listenIP"].(string)
67+
h.listenIP, _ = option["listenIP"].(string)
6868
h.listenPort = uint32(option["listenPort"].(int))
6969
// 连接数限制的配置
7070
if raw, _ := option["connLimit"].(map[interface{}]interface{}); raw != nil {
@@ -234,14 +234,14 @@ func (h *PrometheusServer) process(req *restful.Request, rsp *restful.Response,
234234
}
235235

236236
// preprocess 请求预处理
237-
func (h *PrometheusServer) preprocess(req *restful.Request, rsp *restful.Response) error {
237+
func (h *PrometheusServer) preprocess(req *restful.Request, _ *restful.Response) error {
238238
// 设置开始时间
239239
req.SetAttribute("start-time", time.Now())
240240
return nil
241241
}
242242

243243
// postProcess 请求后处理:统计
244-
func (h *PrometheusServer) postProcess(req *restful.Request, rsp *restful.Response) {
244+
func (h *PrometheusServer) postProcess(req *restful.Request, _ *restful.Response) {
245245
now := time.Now()
246246

247247
// 接口调用统计
@@ -250,17 +250,15 @@ func (h *PrometheusServer) postProcess(req *restful.Request, rsp *restful.Respon
250250
// 去掉最后一个"/"
251251
path = strings.TrimSuffix(path, "/")
252252
}
253-
startTime := req.Attribute("start-time").(time.Time)
254-
255-
diff := now.Sub(startTime)
253+
startTime, _ := req.Attribute("start-time").(time.Time)
256254
// 打印耗时超过1s的请求
257-
if diff > time.Second {
255+
if diff := now.Sub(startTime); diff > time.Second {
258256
log.Info("[API-Server][Prometheus] handling time > 1s",
259257
zap.String("client-address", req.Request.RemoteAddr),
260258
zap.String("user-agent", req.HeaderParameter("User-Agent")),
261259
zap.String("request-id", req.HeaderParameter("Request-Id")),
262260
zap.String("method", req.Request.Method),
263-
zap.String("url", req.Request.URL.String()),
261+
zap.String("url", path),
264262
zap.Duration("handling-time", diff),
265263
)
266264
}

0 commit comments

Comments
 (0)