Skip to content

Commit

Permalink
Support relabel path of ehttp client to solve the high cardinality issue
Browse files Browse the repository at this point in the history
  • Loading branch information
clannadxr committed Mar 29, 2023
1 parent ca292bd commit 6511233
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
8 changes: 8 additions & 0 deletions client/ehttp/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ehttp

import (
"regexp"
"runtime"
"time"

Expand All @@ -21,6 +22,13 @@ type Config struct {
EnableKeepAlives bool // 是否开启长连接,默认打开
EnableAccessInterceptor bool // 是否开启记录请求数据,默认不开启
EnableAccessInterceptorRes bool // 是否开启记录响应参数,默认不开启
PathRelabel []Relabel // path 重命名 (metric 用)
}

type Relabel struct {
Match string
matchReg *regexp.Regexp
Replacement string
}

// DefaultConfig ...
Expand Down
9 changes: 9 additions & 0 deletions client/ehttp/container.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ehttp

import (
"regexp"

"github.com/gotomicro/ego/core/econf"
"github.com/gotomicro/ego/core/elog"
)
Expand Down Expand Up @@ -31,6 +33,13 @@ func Load(key string) *Container {
c.logger.Panic("parse config error", elog.FieldErr(err), elog.FieldKey(key))
return c
}
for idx, relabel := range c.config.PathRelabel {
if reg, err := regexp.Compile(relabel.Match); err == nil {
c.config.PathRelabel[idx].matchReg = reg
} else {
c.logger.Panic("parse path relabel error", elog.FieldErr(err), elog.FieldKey(key))
}
}
c.name = key
return c
}
Expand Down
16 changes: 15 additions & 1 deletion client/ehttp/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,27 @@ func beg(ctx context.Context) time.Time {
func fixedInterceptor(name string, config *Config, logger *elog.Component) (resty.RequestMiddleware, resty.ResponseMiddleware, resty.ErrorHook) {
return func(cli *resty.Client, req *resty.Request) error {
// 这个URL可能不准,每次请求都需要重复url.Parse(),会增加一定的性能损耗
concatURL := strings.TrimRight(config.Addr, "/") + "/" + strings.TrimLeft(req.URL, "/")
var concatURL string
if config.Addr == "" {
// 没有配置addr,host可能在url里面 (request.Get("http://xxx.com/xxx"))
concatURL = req.URL
} else {
concatURL = strings.TrimRight(config.Addr, "/") + "/" + strings.TrimLeft(req.URL, "/")
}
u, err := url.Parse(concatURL)
if err != nil {
logger.Warn("invalid url", elog.String("concatURL", concatURL), elog.FieldErr(err))
req.SetContext(context.WithValue(context.WithValue(req.Context(), begKey{}, time.Now()), urlKey{}, &url.URL{}))
return err
}
if len(config.PathRelabel) > 0 {
for _, relabel := range config.PathRelabel {
if relabel.matchReg.MatchString(u.Path) {
u.Path = relabel.Replacement
break
}
}
}
req.SetContext(context.WithValue(context.WithValue(req.Context(), begKey{}, time.Now()), urlKey{}, u))
return nil
}, nil, nil
Expand Down
7 changes: 7 additions & 0 deletions client/ehttp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,10 @@ func WithEnableAccessInterceptorRes(enableAccessInterceptorRes bool) Option {
c.config.EnableAccessInterceptorRes = enableAccessInterceptorRes
}
}

// WithPathRelabel 设置路径重命名
func WithPathRelabel(match string, replacement string) Option {
return func(c *Container) {
c.config.PathRelabel = append(c.config.PathRelabel, Relabel{Match: match, Replacement: replacement})
}
}

0 comments on commit 6511233

Please sign in to comment.