Skip to content

Commit

Permalink
Merge branch 'master' into feature/h2c
Browse files Browse the repository at this point in the history
  • Loading branch information
askuy authored Sep 27, 2023
2 parents ef99771 + fde12bd commit 95e89aa
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 134 deletions.
20 changes: 11 additions & 9 deletions client/ehttp/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package ehttp
import (
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"

"github.com/go-resty/resty/v2"
"github.com/gotomicro/ego/client/ehttp/resolver"
"github.com/gotomicro/ego/core/eregistry"
"golang.org/x/net/publicsuffix"

"github.com/gotomicro/ego/core/eapp"
"github.com/gotomicro/ego/core/elog"
"github.com/gotomicro/ego/core/eregistry"
)

// PackageName 设置包名
Expand All @@ -36,23 +33,28 @@ func newComponent(name string, config *Config, logger *elog.Component) *Componen
if err != nil {
elog.Panic("parse addr error", elog.FieldErr(err), elog.FieldKey(config.Addr))
}
addr := strings.ReplaceAll(config.Addr, egoTarget.Scheme+"://", "http://")
// 这里的目的是为了,将k8s:// 替换为 http://,所以需要判断下是否为非HTTP,HTTPS。
// 因为resty默认只要http和https的协议
addr := config.Addr
if egoTarget.Scheme != "http" && egoTarget.Scheme != "https" {
// 因为内部协议,都是内网,所以直接替换为HTTP
addr = strings.ReplaceAll(config.Addr, egoTarget.Scheme+"://", "http://")
}
builder := resolver.Get(egoTarget.Scheme)
resolver, err := builder.Build(addr)
resolverBuild, err := builder.Build(addr)
if err != nil {
elog.Panic("build resolver error", elog.FieldErr(err), elog.FieldKey(config.Addr))
}

// resty的默认方法,无法设置长连接个数,和是否开启长连接,这里重新构造http client。
cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
interceptors := []interceptor{fixedInterceptor, logInterceptor, metricInterceptor, traceInterceptor}
cli := resty.NewWithClient(&http.Client{Transport: createTransport(config), Jar: cookieJar}).
cli := resty.NewWithClient(&http.Client{Transport: createTransport(config), Jar: config.cookieJar}).
SetDebug(config.RawDebug).
SetTimeout(config.ReadTimeout).
SetHeader("app", eapp.Name()).
SetBaseURL(addr)
for _, interceptor := range interceptors {
onBefore, onAfter, onErr := interceptor(name, config, logger, resolver)
onBefore, onAfter, onErr := interceptor(name, config, logger, resolverBuild)
if onBefore != nil {
cli.OnBeforeRequest(onBefore)
}
Expand Down
30 changes: 16 additions & 14 deletions client/ehttp/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ehttp

import (
"net/http"
"regexp"
"runtime"
"time"
Expand All @@ -10,20 +11,21 @@ import (

// Config HTTP配置选项
type Config struct {
Addr string // 连接地址
Debug bool // 是否开启调试,默认不开启,开启后并加上export EGO_DEBUG=true,可以看到每次请求,配置名、地址、耗时、请求数据、响应数据
RawDebug bool // 是否开启原生调试,默认不开启
ReadTimeout time.Duration // 读超时,默认2s
SlowLogThreshold time.Duration // 慢日志记录的阈值,默认500ms
IdleConnTimeout time.Duration // 设置空闲连接时间,默认90 * time.Second
MaxIdleConns int // 设置最大空闲连接数
MaxIdleConnsPerHost int // 设置长连接个数
EnableTraceInterceptor bool // 是否开启链路追踪,默认开启
EnableKeepAlives bool // 是否开启长连接,默认打开
EnableAccessInterceptor bool // 是否开启记录请求数据,默认不开启
EnableAccessInterceptorRes bool // 是否开启记录响应参数,默认不开启
PathRelabel []Relabel // path 重命名 (metric 用)
EnableMetricsInterceptor bool // 是否开启监控,默认开启
Addr string // 连接地址
Debug bool // 是否开启调试,默认不开启,开启后并加上export EGO_DEBUG=true,可以看到每次请求,配置名、地址、耗时、请求数据、响应数据
RawDebug bool // 是否开启原生调试,默认不开启
ReadTimeout time.Duration // 读超时,默认2s
SlowLogThreshold time.Duration // 慢日志记录的阈值,默认500ms
IdleConnTimeout time.Duration // 设置空闲连接时间,默认90 * time.Second
MaxIdleConns int // 设置最大空闲连接数
MaxIdleConnsPerHost int // 设置长连接个数
EnableTraceInterceptor bool // 是否开启链路追踪,默认开启
EnableKeepAlives bool // 是否开启长连接,默认打开
EnableAccessInterceptor bool // 是否开启记录请求数据,默认不开启
EnableAccessInterceptorRes bool // 是否开启记录响应参数,默认不开启
PathRelabel []Relabel // path 重命名 (metric 用)
cookieJar http.CookieJar // 用于缓存cookie
EnableMetricsInterceptor bool // 是否开启监控,默认开启
}

// Relabel ...
Expand Down
13 changes: 12 additions & 1 deletion client/ehttp/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ehttp

import "time"
import (
"net/http"
"time"
)

// WithAddr 设置HTTP地址
func WithAddr(addr string) Option {
Expand Down Expand Up @@ -99,3 +102,11 @@ func WithPathRelabel(match string, replacement string) Option {
c.config.PathRelabel = append(c.config.PathRelabel, Relabel{Match: match, Replacement: replacement})
}
}

// WithJar 设置Cookie,设置后,请求第一次接口后获取Cookie,第二次请求会带上Cookie,适合一些登录场景
// 例子:cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
func WithJar(jar http.CookieJar) Option {
return func(c *Container) {
c.config.cookieJar = jar
}
}
28 changes: 28 additions & 0 deletions core/elog/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ writer = "stderr"
return Load("stderr").Build()
}

func newStdoutLogger() *Component {
conf := `
[stdout]
level = "info"
writer = "stdout"
`
var err error
if err = econf.LoadFromReader(strings.NewReader(conf), toml.Unmarshal); err != nil {
log.Println("load conf fail", err)
return nil
}
log.Println("start to send logs to stdout")
return Load("stdout").Build()
}

func newAliLogger() *Component {
conf := `
[ali]
Expand Down Expand Up @@ -154,6 +169,19 @@ func BenchmarkStderrWriter(b *testing.B) {
})
}

func BenchmarkStdoutWriter(b *testing.B) {
b.Logf("Logging at a disabled level with some accumulated context.")
logger := newStdoutLogger()
b.Run("stdout\n", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Info(getMessage(0))
}
})
})
}

func BenchmarkZapWriter(b *testing.B) {
b.Logf("Logging at a disabled level with some accumulated context.")
logger := newZapLogger()
Expand Down
1 change: 1 addition & 0 deletions core/elog/elog_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func init() {
registry = make(map[string]WriterBuilder)
Register(&stderrWriterBuilder{})
Register(&rotateWriterBuilder{})
Register(&stdoutWriterBuilder{})
DefaultLogger = DefaultContainer().Build(WithFileName(DefaultLoggerName), WithCallSkip(2)) // DefaultLogger 默认为2层
EgoLogger = DefaultContainer().Build(WithFileName(EgoLoggerName))
}
Expand Down
33 changes: 33 additions & 0 deletions core/elog/stdout_writer_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package elog

import (
"go.uber.org/zap/zapcore"
"io"
"os"
)

const (
writerStdout = "stdout"
)

var _ WriterBuilder = &stdoutWriterBuilder{}

// fileDataSource file Provider.
type stdoutWriterBuilder struct{}

type stdoutWriter struct {
zapcore.Core
io.Closer
}

func (s *stdoutWriterBuilder) Build(key string, c *Config) Writer {
// Debug output to console and file by default
w := &stdoutWriter{}
w.Core = zapcore.NewCore(zapcore.NewJSONEncoder(*c.EncoderConfig()), os.Stdout, c.AtomicLevel())
w.Closer = CloseFunc(noopCloseFunc)
return w
}

func (*stdoutWriterBuilder) Scheme() string {
return writerStdout
}
36 changes: 17 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,24 @@ require (
github.com/modern-go/reflect2 v1.0.2
github.com/prometheus/client_golang v1.12.1
github.com/robfig/cron/v3 v3.0.1
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/spf13/cast v1.4.1
github.com/stretchr/testify v1.8.3
github.com/stretchr/testify v1.8.4
github.com/wk8/go-ordered-map v1.0.0
go.opencensus.io v0.23.0
go.opentelemetry.io/otel v1.7.0
go.opentelemetry.io/otel/exporters/jaeger v1.7.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.7.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.7.0
go.opentelemetry.io/otel/sdk v1.7.0
go.opentelemetry.io/otel/trace v1.7.0
go.opentelemetry.io/otel v1.18.0
go.opentelemetry.io/otel/exporters/jaeger v1.17.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.18.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.18.0
go.opentelemetry.io/otel/sdk v1.18.0
go.opentelemetry.io/otel/trace v1.18.0
go.uber.org/automaxprocs v1.5.1
go.uber.org/zap v1.21.0
golang.org/x/net v0.10.0
golang.org/x/sync v0.2.0
golang.org/x/net v0.12.0
golang.org/x/sync v0.3.0
golang.org/x/tools v0.9.1
google.golang.org/genproto v0.0.0-20220505152158-f39f71e6c8f3
google.golang.org/grpc v1.46.0
google.golang.org/protobuf v1.30.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -55,18 +53,18 @@ require (
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/klauspost/compress v1.16.3 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
Expand All @@ -88,15 +86,15 @@ require (
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.45.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.7.0 // indirect
go.opentelemetry.io/proto/otlp v0.16.0 // indirect
go.opentelemetry.io/otel/metric v1.18.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.11.0 // indirect
google.golang.org/genproto v0.0.0-20230711160842-782d3b101e98 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 95e89aa

Please sign in to comment.