/
http.go
219 lines (207 loc) · 5.8 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2020 tree xie
//
// 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 request
import (
"net/http"
"net/url"
"strconv"
"github.com/tidwall/gjson"
"github.com/vicanso/forest/cs"
"github.com/vicanso/forest/helper"
"github.com/vicanso/forest/interceptor"
"github.com/vicanso/forest/log"
"github.com/vicanso/forest/util"
"github.com/vicanso/go-axios"
"github.com/vicanso/hes"
)
func newOnDone(serviceName string) axios.OnDone {
return func(conf *axios.Config, resp *axios.Response, err error) {
stats := axios.GetStats(conf, err)
ht := conf.HTTPTrace
use := ""
tags := map[string]string{
cs.TagService: serviceName,
cs.TagRoute: stats.Route,
cs.TagMethod: stats.Method,
cs.TagResult: strconv.Itoa(stats.Result),
}
fields := map[string]any{
cs.FieldURI: stats.URI,
cs.FieldStatus: stats.Status,
}
if ht != nil {
use = ht.Stats().String()
fields[cs.FieldReused] = stats.Reused
fields[cs.FieldLatency] = stats.Use
if stats.DNSUse != 0 {
fields[cs.FieldDNSUse] = stats.DNSUse
}
if stats.TCPUse != 0 {
fields[cs.FieldTCPUse] = stats.TCPUse
}
if stats.TLSUse != 0 {
fields[cs.FieldTLSUse] = stats.TLSUse
}
if stats.ServerProcessingUse != 0 {
fields[cs.FieldProcessingUse] = stats.ServerProcessingUse
}
if stats.ContentTransferUse != 0 {
fields[cs.FieldTransferUse] = stats.ContentTransferUse
}
fields[cs.FieldAddr] = stats.Addr
}
message := ""
if err != nil {
he := hes.Wrap(err)
message = he.Error()
fields[cs.FieldError] = message
errCategory := he.Category
if errCategory != "" {
fields[cs.FieldErrCategory] = errCategory
}
if he.Exception {
fields[cs.FieldException] = true
}
}
// 输出响应数据,如果响应数据为隐私数据可不输出
var data any
size := stats.Size
if resp != nil {
data = resp.UnmarshalData
}
// 由于http请求是较频繁的操作,因此判断是否启用debug再输出
if log.DebugEnabled() {
respData := ""
if resp != nil {
respData = string(resp.Data)
}
log.Debug(conf.Context).
Str("curl", conf.CURL()).
Str("data", respData).
Msg("request log")
}
requestURL := stats.URI
urlInfo, _ := url.Parse(requestURL)
if urlInfo != nil {
requestURL = urlInfo.RequestURI()
}
event := log.Info(conf.Context).
Str("category", "requestStats").
Str("service", serviceName).
Str("method", stats.Method).
Str("route", stats.Route).
Str("url", requestURL)
if len(conf.Params) != 0 {
event = event.Dict("params", log.Struct(conf.Params))
}
if len(conf.Query) != 0 {
event = event.Dict("query", log.URLValues(conf.Query))
}
if data != nil {
event = event.Dict("data", log.Struct(data))
} else if resp != nil {
event = event.Str("data", string(resp.Data))
}
event.Int("size", size).
Int("status", stats.Status).
Str("addr", stats.Addr).
Bool("reused", stats.Reused).
Str("use", use)
if message != "" {
event = event.Str("error", message)
}
event.Msg("")
helper.GetInfluxDB().Write(cs.MeasurementHTTPRequest, tags, fields)
}
}
func newOnBeforeRequestInterceptor(service string) axios.OnBeforeNewRequest {
return func(config *axios.Config) (err error) {
inter, err := interceptor.NewHTTPRequest(service, config)
if err != nil {
return err
}
if inter == nil {
return nil
}
_, err = inter.Before()
return err
}
}
func newResponseInterceptor(service string) axios.ResponseInterceptor {
return func(resp *axios.Response) (err error) {
inter, err := interceptor.NewHTTPRequest(service, resp.Config)
if err != nil {
return err
}
if inter == nil {
return nil
}
_, err = inter.After()
return err
}
}
// newConvertResponseToError 将http响应码为>=400的转换为出错
func newConvertResponseToError(service string) axios.ResponseInterceptor {
return func(resp *axios.Response) error {
if resp.Status >= 400 {
message := gjson.GetBytes(resp.Data, "message").String()
exception := false
if message == "" {
message = util.CutRune(string(resp.Data), 30)
// 如果出错响应不符合,则认为是异常响应
exception = true
}
he := hes.NewWithStatusCode(message, resp.Status)
he.Exception = exception
return he
}
return nil
}
}
// newOnError 新建error的处理函数
func newOnError(serviceName string) axios.OnError {
return func(err error, conf *axios.Config) error {
code := -1
if conf.Response != nil {
code = conf.Response.Status
}
he := hes.Wrap(err)
if code >= http.StatusBadRequest {
he.StatusCode = code
}
// 如果状态码>500,认为是异常(一般nginx之类才会使用502 503之类的状态码)
if code > http.StatusInternalServerError {
he.Exception = true
}
// 如果未设置http响应码(<400),则设置为500
if he.StatusCode < http.StatusBadRequest {
he.StatusCode = http.StatusInternalServerError
}
// 如果为空,则通过error获取
if he.Category == "" {
he.Category = axios.GetInternalErrorCategory(err)
// 如果返回错误类型,则认为异常
// 因为返回出错均是网络连接上的异常
if he.Category != "" {
he.Exception = true
}
}
if !util.IsProduction() {
he.AddExtra("requestRoute", conf.Route)
he.AddExtra("requestService", serviceName)
he.AddExtra("requestCURL", conf.CURL())
}
return he
}
}