Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
971a8238
提交
971a8238
authored
6月 30, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
request cost
上级
f17fbc21
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
61 行增加
和
31 行删除
+61
-31
ctx.go
gin/ctxUtils/ctx.go
+28
-0
logging.go
gin/middleware/logging.go
+17
-17
requstid.go
grpc/interceptor/requstid.go
+1
-1
httpcli.go
httpcli/httpcli.go
+7
-13
ctxUtil.go
utils/ctxUtils/ctxUtil.go
+8
-0
没有找到文件。
gin/ctxUtils/ctx.go
浏览文件 @
971a8238
...
...
@@ -3,6 +3,9 @@ package ctxUtils
import
(
"context"
"errors"
"fmt"
"strings"
"time"
xsf
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
...
...
@@ -235,3 +238,28 @@ func SetVal(ctx context.Context, key string, val interface{}) context.Context {
}
return
context
.
WithValue
(
ctx
,
key
,
val
)
}
func
AddApiCost
(
c
*
gin
.
Context
,
appName
string
,
start
time
.
Time
)
{
cost
:=
c
.
Writer
.
Header
()
.
Get
(
KeyCost
)
currentCost
:=
fmt
.
Sprintf
(
"%s:%d"
,
appName
,
time
.
Since
(
start
)
.
Milliseconds
())
if
cost
==
""
{
cost
=
currentCost
}
else
{
cost
=
fmt
.
Sprintf
(
"%s,%s"
,
cost
,
currentCost
)
}
c
.
Writer
.
Header
()
.
Set
(
KeyCost
,
cost
)
}
func
SetApiCost
(
ctx
context
.
Context
,
header
map
[
string
][]
string
)
{
if
ginCtx
,
_
:=
GetGinCtx
(
ctx
);
ginCtx
!=
nil
&&
len
(
header
)
>
0
{
cost
:=
""
for
k
,
v
:=
range
header
{
if
k
==
KeyCost
{
cost
=
strings
.
Join
(
v
,
","
)
}
}
if
cost
!=
""
{
ginCtx
.
Writer
.
Header
()
.
Set
(
KeyCost
,
cost
)
}
}
}
gin/middleware/logging.go
浏览文件 @
971a8238
...
...
@@ -2,13 +2,12 @@ package middleware
import
(
"bytes"
"fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/entity"
"io"
"net/http"
"time"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
ctxUtil
s
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/ips"
"github.com/gin-gonic/gin"
...
...
@@ -196,37 +195,37 @@ func Logging(opts ...Option) gin.HandlerFunc {
copyHttpReq
.
URL
=
c
.
Request
.
URL
.
String
()
copyHttpReq
.
Headers
=
c
.
Request
.
Header
copyHttpReq
.
Body
=
buf
.
String
()
c
.
Set
(
ctxUtil
.
KeyReqBody
,
copyHttpReq
)
c
.
Set
(
ctxUtil
s
.
KeyReqBody
,
copyHttpReq
)
reqID
:=
""
if
o
.
traceIDFrom
==
1
{
if
v
,
isExist
:=
c
.
Get
(
ctxUtil
.
ContextTraceIDKey
);
isExist
{
if
v
,
isExist
:=
c
.
Get
(
ctxUtil
s
.
ContextTraceIDKey
);
isExist
{
if
requestID
,
ok
:=
v
.
(
string
);
ok
{
reqID
=
requestID
fields
=
append
(
fields
,
zap
.
String
(
ctxUtil
.
ContextTraceIDKey
,
reqID
))
fields
=
append
(
fields
,
zap
.
String
(
ctxUtil
s
.
ContextTraceIDKey
,
reqID
))
}
}
}
else
if
o
.
traceIDFrom
==
2
{
reqID
=
c
.
Request
.
Header
.
Get
(
ctxUtil
.
HeaderXRequestIDKey
)
fields
=
append
(
fields
,
zap
.
String
(
ctxUtil
.
ContextTraceIDKey
,
reqID
))
reqID
=
c
.
Request
.
Header
.
Get
(
ctxUtil
s
.
HeaderXRequestIDKey
)
fields
=
append
(
fields
,
zap
.
String
(
ctxUtil
s
.
ContextTraceIDKey
,
reqID
))
}
o
.
log
.
Info
(
"<<<<<<<<<req"
,
fields
...
)
c
.
Request
.
Body
=
io
.
NopCloser
(
&
buf
)
//
replace writer
//replace writer
newWriter
:=
&
bodyLogWriter
{
body
:
&
bytes
.
Buffer
{},
ResponseWriter
:
c
.
Writer
}
c
.
Writer
=
newWriter
ip
:=
ips
.
GetClientIP
(
c
)
c
.
Set
(
ctxUtil
.
KeyClientIP
,
ip
)
c
.
Set
(
ctxUtil
s
.
KeyClientIP
,
ip
)
// processing requests
c
.
Next
()
c
.
Writer
.
Header
()
.
Add
(
ctxUtil
.
KeyCost
,
fmt
.
Sprintf
(
"%s-%d"
,
o
.
appName
,
time
.
Since
(
start
)
.
Milliseconds
())
)
c
txUtils
.
AddApiCost
(
c
.
Writer
,
o
.
appName
,
start
)
//
print return message after processing
//print return message after processing
fields
=
[]
zap
.
Field
{
zap
.
Int
(
"code"
,
c
.
Writer
.
Status
()),
zap
.
String
(
"method"
,
c
.
Request
.
Method
),
...
...
@@ -238,15 +237,16 @@ func Logging(opts ...Option) gin.HandlerFunc {
zap
.
ByteString
(
"body"
,
getResponseBody
(
newWriter
.
body
,
o
.
maxLength
)),
}
if
reqID
!=
""
{
fields
=
append
(
fields
,
zap
.
String
(
ctxUtil
.
ContextTraceIDKey
,
reqID
))
fields
=
append
(
fields
,
zap
.
String
(
ctxUtil
s
.
ContextTraceIDKey
,
reqID
))
}
o
.
log
.
Info
(
">>>>>>>>>rsp"
,
fields
...
)
}
}
func
GetCopyReq
(
c
*
gin
.
Context
)
*
entity
.
CopyHttpReq
{
if
val
,
isExist
:=
c
.
Get
(
ctxUtil
.
KeyReqBody
);
isExist
{
if
val
,
isExist
:=
c
.
Get
(
ctxUtil
s
.
KeyReqBody
);
isExist
{
if
req
,
ok
:=
val
.
(
*
entity
.
CopyHttpReq
);
ok
{
return
req
}
...
...
@@ -255,7 +255,7 @@ func GetCopyReq(c *gin.Context) *entity.CopyHttpReq {
}
func
GetCopyRsp
(
c
*
gin
.
Context
)
*
entity
.
CopyHttpRsp
{
if
val
,
isExist
:=
c
.
Get
(
ctxUtil
.
KeyRspBody
);
isExist
{
if
val
,
isExist
:=
c
.
Get
(
ctxUtil
s
.
KeyRspBody
);
isExist
{
if
rsp
,
ok
:=
val
.
(
*
entity
.
CopyHttpRsp
);
ok
{
return
rsp
}
...
...
@@ -279,13 +279,13 @@ func SimpleLog(opts ...Option) gin.HandlerFunc {
reqID
:=
""
if
o
.
traceIDFrom
==
1
{
if
v
,
isExist
:=
c
.
Get
(
ctxUtil
.
ContextTraceIDKey
);
isExist
{
if
v
,
isExist
:=
c
.
Get
(
ctxUtil
s
.
ContextTraceIDKey
);
isExist
{
if
requestID
,
ok
:=
v
.
(
string
);
ok
{
reqID
=
requestID
}
}
}
else
if
o
.
traceIDFrom
==
2
{
reqID
=
c
.
Request
.
Header
.
Get
(
ctxUtil
.
HeaderXRequestIDKey
)
reqID
=
c
.
Request
.
Header
.
Get
(
ctxUtil
s
.
HeaderXRequestIDKey
)
}
// processing requests
...
...
@@ -300,7 +300,7 @@ func SimpleLog(opts ...Option) gin.HandlerFunc {
zap
.
Int
(
"size"
,
c
.
Writer
.
Size
()),
}
if
reqID
!=
""
{
fields
=
append
(
fields
,
zap
.
String
(
ctxUtil
.
ContextTraceIDKey
,
reqID
))
fields
=
append
(
fields
,
zap
.
String
(
ctxUtil
s
.
ContextTraceIDKey
,
reqID
))
}
o
.
log
.
Info
(
"Gin msg"
,
fields
...
)
}
...
...
grpc/interceptor/requstid.go
浏览文件 @
971a8238
...
...
@@ -123,7 +123,7 @@ func StreamServerRequestID() grpc.StreamServerInterceptor {
//ctx := stream.Context()
//requestID := ServerCtxRequestID(ctx)
//if requestID == "" {
// requestID = ctxUtil.GenerateTid()
// requestID = ctxUtil
s
.GenerateTid()
// ctx = grpc_metadata.ExtractIncoming(ctx).Add(ContextRequestIDKey, requestID).ToIncoming(ctx)
//}
return
handler
(
srv
,
stream
)
...
...
httpcli/httpcli.go
浏览文件 @
971a8238
...
...
@@ -313,8 +313,9 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
req
.
timeout
=
defaultTimeout
}
client
:=
http
.
Client
{
Timeout
:
req
.
timeout
}
resp
:=
new
(
Response
)
resp
.
Header
=
make
(
http
.
Header
)
resp
:=
&
Response
{
Header
:
make
(
http
.
Header
),
}
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
time
.
Second
*
20
)
defer
cancel
()
var
err
error
...
...
@@ -329,7 +330,7 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
resp
.
Status
=
response
.
Status
resp
.
StatusCode
=
response
.
StatusCode
for
k
,
v
:=
range
response
.
Header
{
resp
.
Header
[
k
]
=
v
resp
.
Header
[
k
]
=
append
(
resp
.
Header
[
k
],
v
...
)
}
defer
response
.
Body
.
Close
()
body
,
err
:=
io
.
ReadAll
(
response
.
Body
)
...
...
@@ -357,7 +358,7 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
resp
.
Status
=
response
.
Status
resp
.
StatusCode
=
response
.
StatusCode
for
k
,
v
:=
range
response
.
Header
{
resp
.
Header
[
k
]
=
v
resp
.
Header
[
k
]
=
append
(
resp
.
Header
[
k
],
v
...
)
}
resp
.
err
=
err
}
...
...
@@ -367,7 +368,7 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
resp
.
Status
=
response
.
Status
resp
.
StatusCode
=
response
.
StatusCode
for
k
,
v
:=
range
response
.
Header
{
resp
.
Header
[
k
]
=
v
resp
.
Header
[
k
]
=
append
(
resp
.
Header
[
k
],
v
...
)
}
defer
response
.
Body
.
Close
()
body
,
e
:=
io
.
ReadAll
(
response
.
Body
)
...
...
@@ -387,14 +388,7 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
logger
.
Error
(
"httpCli fail"
,
logger
.
Any
(
"err"
,
err
),
ctxUtils
.
CtxTraceIDField
(
ctx
))
}
if
resp
!=
nil
{
if
resp
.
Header
!=
nil
{
ginCtx
,
_
:=
ctxUtils
.
GetGinCtx
(
ctx
)
if
ginCtx
!=
nil
{
ginCtx
.
Writer
.
Header
()
.
Add
(
ctxUtils
.
KeyCost
,
resp
.
Header
.
Get
(
ctxUtils
.
KeyCost
))
}
}
}
ctxUtils
.
SetApiCost
(
ctx
,
resp
.
Header
)
req
.
response
=
resp
req
.
err
=
err
...
...
utils/ctxUtils/ctxUtil.go
0 → 100644
浏览文件 @
971a8238
package
ctxUtils
//
//func AddApiCost(c *gin.Context, appName string, start time.Time) {
// cost := c.Writer.Header().Get(ctxUtil.KeyCost)
// cost += fmt.Sprintf("%s:%d", appName, time.Since(start).Milliseconds())
// c.Writer.Header().Set(ctxUtil.KeyCost, cost)
//}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论