-
Notifications
You must be signed in to change notification settings - Fork 1
/
stat.go
96 lines (83 loc) · 2.42 KB
/
stat.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package requests
import (
"bytes"
"encoding/json"
"io"
)
const RequestId = "Request-Id"
// Stat stats
type Stat struct {
RequestId string
StartAt string `json:"StartAt"`
Cost int64 `json:"Cost"`
Request struct {
Method string `json:"Method"`
Header map[string]string `json:"Header"`
URL string `json:"URL"`
Body any `json:"Body"`
} `json:"Request"`
Response struct {
Header map[string]string `json:"Header"`
Body any `json:"Body"`
StatusCode int `json:"StatusCode"`
ContentLength int64 `json:"ContentLength"`
} `json:"Response"`
Err string `json:"Err"`
}
// String implement fmt.Stringer interface.
func (stat *Stat) String() string {
b, _ := json.Marshal(stat)
return string(b)
}
const dateTime = "2006-01-02 15:04:05.000"
// StatLoad stat.
func StatLoad(resp *Response) *Stat {
stat := &Stat{
StartAt: resp.StartAt.Format(dateTime),
Cost: resp.Cost.Milliseconds(),
}
if resp.Response != nil {
var err error
if resp.Content == nil || resp.Response.ContentLength != 0 {
resp.Content = &bytes.Buffer{}
if resp.Response.ContentLength, err = resp.Content.ReadFrom(resp.Response.Body); err == nil {
defer resp.Response.Body.Close()
resp.Response.Body = io.NopCloser(bytes.NewReader(resp.Content.Bytes()))
}
}
stat.Response.Body = make(map[string]any)
if err := json.Unmarshal(resp.Content.Bytes(), &stat.Response.Body); err != nil {
stat.Response.Body = resp.Content.String()
}
stat.Response.Header = make(map[string]string)
for k, v := range resp.Response.Header {
stat.Response.Header[k] = v[0]
}
stat.Response.ContentLength = resp.Response.ContentLength
stat.Response.StatusCode = resp.StatusCode
}
if resp.Request != nil {
stat.RequestId = resp.Request.Header.Get(RequestId)
stat.Request.Method = resp.Request.Method
stat.Request.URL = resp.Request.URL.String()
if resp.Request.GetBody != nil {
body, _ := resp.Request.GetBody()
var buf bytes.Buffer
_, _ = buf.ReadFrom(body)
m := make(map[string]any)
if err := json.Unmarshal(buf.Bytes(), &m); err != nil {
stat.Request.Body = buf.String()
} else {
stat.Request.Body = m
}
}
stat.Request.Header = make(map[string]string)
for k, v := range resp.Request.Header {
stat.Request.Header[k] = v[0]
}
}
if resp.Err != nil {
stat.Err = resp.Err.Error()
}
return stat
}