Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions server/egin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Config struct {
EnableMetricInterceptor bool // 是否开启监控,默认开启
EnableTraceInterceptor bool // 是否开启链路追踪,默认开启
EnableLocalMainIP bool // 自动获取ip地址
EnableResHeaderApp bool // header展示APP Name
SlowLogThreshold time.Duration // 服务慢日志,默认500ms
EnableAccessInterceptor bool // 是否开启,记录请求数据
EnableAccessInterceptorReq bool // 是否开启记录请求参数,默认不开启
Expand Down Expand Up @@ -75,6 +76,7 @@ func DefaultConfig() *Config {
EnableAccessInterceptor: true,
EnableTraceInterceptor: true,
EnableMetricInterceptor: true,
EnableResHeaderApp: true,
AccessInterceptorReqMaxLength: 4096,
AccessInterceptorResMaxLength: 4096,
EnableSentinel: true,
Expand Down
2 changes: 1 addition & 1 deletion server/egin/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (c *Container) Build(options ...Option) *Component {
server := newComponent(c.name, c.config, c.logger)
server.Use(healthcheck.Default())
server.Use(c.defaultServerInterceptor())
server.Use(NewXResCostTimer)
server.Use(NewXResCostTimer(c.config.EnableResHeaderApp))
if c.config.ContextTimeout > 0 {
server.Use(timeoutMiddleware(c.config.ContextTimeout))
}
Expand Down
39 changes: 29 additions & 10 deletions server/egin/interceptor_cost.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ import (
// XResCostTimer wrap gin reponse writer add start time
type XResCostTimer struct {
gin.ResponseWriter
start time.Time
ginCtx *gin.Context
start time.Time
ginCtx *gin.Context
enableHeaderApp bool
}

// 如果写入header,需要这么处理
// ctx.Request = ctx.Request.WithContext(sdkCtx.Context)
func (w *XResCostTimer) WriteHeader(statusCode int) {
// header必须在c.json响应。
cost := float64(time.Since(w.start).Microseconds()) / 1000
w.Header().Set(eapp.EgoHeaderExpose()+"time", strconv.FormatFloat(cost, 'f', -1, 64))
if w.enableHeaderApp {
w.Header().Set(eapp.EgoHeaderExpose()+"time", strconv.FormatFloat(cost, 'f', -1, 64)+"|"+strconv.Itoa(statusCode))
} else {
w.Header().Set(eapp.EgoHeaderExpose()+"time", strconv.FormatFloat(cost, 'f', -1, 64))
}
for _, key := range transport.CustomHeaderKeys() {
if value := cast.ToString(w.ginCtx.Request.Context().Value(key)); value != "" {
// x-expose 需要在这里获取
Expand All @@ -41,12 +46,26 @@ func (w *XResCostTimer) Write(b []byte) (int, error) {
}

// NewXResCostTimer middleware to add X-Res-Cost-Time
func NewXResCostTimer(c *gin.Context) {
blw := &XResCostTimer{
ResponseWriter: c.Writer,
start: time.Now(),
ginCtx: c,
//func NewXResCostTimer(c *gin.Context) {
// blw := &XResCostTimer{
// ResponseWriter: c.Writer,
// start: time.Now(),
// ginCtx: c,
// }
// c.Writer = blw
// c.Next()
//}

// NewXResCostTimer middleware to add X-Res-Cost-Time
func NewXResCostTimer(enableHeaderApp bool) gin.HandlerFunc {
return func(c *gin.Context) {
blw := &XResCostTimer{
ResponseWriter: c.Writer,
start: time.Now(),
ginCtx: c,
enableHeaderApp: enableHeaderApp,
}
c.Writer = blw
c.Next()
}
c.Writer = blw
c.Next()
}
Loading