-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.go
50 lines (45 loc) · 1.21 KB
/
http.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
// Copyright 2022 YBCZ, Inc. All rights reserved.
//
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file in the root of the source
// tree.
package mqms
import (
"github.com/google/uuid"
"net/http"
"time"
)
// HttpTrace http 性能信息
type HttpTrace struct {
EvtID uuid.UUID `json:"evt_id"`
Method string `json:"method"`
Url string `json:"url"`
BeginAt time.Time `json:"begin_at"`
EndAt time.Time `json:"end_at"`
ReqContentLength int64 `json:"req_content_length"`
ResStatus string `json:"res_status"`
}
// 检测HTTP性能
type httpTransport struct {
t http.Transport
engine *Engine
evtID uuid.UUID
}
func (m *httpTransport) RoundTrip(req *http.Request) (res *http.Response, err error) {
ht := HttpTrace{
EvtID: m.evtID,
Method: req.Method,
Url: req.URL.String(),
BeginAt: time.Now(),
ReqContentLength: req.ContentLength,
}
res, err = m.t.RoundTrip(req)
ht.EndAt = time.Now()
ht.ResStatus = res.Status
// 忽略跳转跟踪
if res.StatusCode >= 300 && res.StatusCode < 400 {
return
}
m.engine.handler.HttpTrace(ht)
return
}