From e2c0609e9d7463502984373018ba355c7977f261 Mon Sep 17 00:00:00 2001 From: chen quan Date: Sun, 5 Mar 2023 00:00:06 +0800 Subject: [PATCH] chore: rename (#17) * chore: rename * chore: rename --- internal/tag/tag.go | 57 +++++++++++++++++++++++ rest/internal/handler/tag_handler.go | 12 ++--- rest/rest.go | 4 +- tag/tag.go | 57 ++--------------------- zrpc/internal/p2c/p2c.go | 2 +- zrpc/internal/resolver/discovbuilder.go | 2 +- zrpc/internal/selector/defaultselector.go | 6 +-- 7 files changed, 73 insertions(+), 67 deletions(-) create mode 100644 internal/tag/tag.go diff --git a/internal/tag/tag.go b/internal/tag/tag.go new file mode 100644 index 0000000..fbfebc0 --- /dev/null +++ b/internal/tag/tag.go @@ -0,0 +1,57 @@ +package tag + +import ( + "context" + + "github.com/zeromicro/go-zero/core/logx" + "go.opentelemetry.io/otel/baggage" + "google.golang.org/grpc/attributes" +) + +const Key = "x-zero-flow-tag" + +func ContextWithTag(ctx context.Context, tag string) context.Context { + bg := baggage.FromContext(ctx) + member, err := baggage.NewMember(Key, tag) + if err != nil { + logx.WithContext(ctx).Error(err) + return ctx + } + + bg, err = bg.SetMember(member) + if err != nil { + logx.WithContext(ctx).Error(err) + return ctx + } + + ctx = baggage.ContextWithBaggage(ctx, bg) + + return ctx +} + +func FromContext(ctx context.Context) string { + bg := baggage.FromContext(ctx) + member := bg.Member(Key) + + return member.Value() +} + +type tagAttributesKey struct{} + +func NewAttributes(tag string) *attributes.Attributes { + return attributes.New(tagAttributesKey{}, tag) +} + +func FromGrpcAttributes(attributes *attributes.Attributes) (string, bool) { + value := attributes.Value(tagAttributesKey{}) + if value == nil { + return "", false + } + + m, ok := value.(string) + if !ok { + return "", false + } + + return m, true +} diff --git a/rest/internal/handler/tag_handler.go b/rest/internal/handler/tag_handler.go index cb0ad36..781c69a 100644 --- a/rest/internal/handler/tag_handler.go +++ b/rest/internal/handler/tag_handler.go @@ -4,7 +4,7 @@ import ( "context" "net/http" - "github.com/chenquan/zero-flow/tag" + "github.com/chenquan/zero-flow/internal/tag" "github.com/zeromicro/go-zero/core/logx" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" @@ -12,23 +12,23 @@ import ( var httpColorAttributeKey = attribute.Key("http.header.color") -func TagHandler(headerTag string) func(next http.Handler) http.Handler { +func TagHandler(tagHeader string) func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { ctx := request.Context() - ctx = newBaggage(ctx, request, headerTag) + ctx = newBaggage(ctx, request, tagHeader) next.ServeHTTP(writer, request.WithContext(ctx)) }) } } -func newBaggage(ctx context.Context, request *http.Request, headerTag string) context.Context { +func newBaggage(ctx context.Context, request *http.Request, tagHeader string) context.Context { span := trace.SpanFromContext(ctx) - tagString := request.Header.Get(headerTag) + tagString := request.Header.Get(tagHeader) if len(tagString) == 0 { return ctx } - logx.WithContext(ctx).Debugw("flow staining...", logx.Field("tag", tagString)) + logx.WithContext(ctx).Debugw("flow staining...", logx.Field(tag.Key, tagString)) ctx = tag.ContextWithTag(ctx, tagString) span.SetAttributes(httpColorAttributeKey.String(tagString)) diff --git a/rest/rest.go b/rest/rest.go index dd14b5d..f1716ac 100644 --- a/rest/rest.go +++ b/rest/rest.go @@ -10,12 +10,12 @@ type ( Server = rest.Server RestConf struct { rest.RestConf - HeaderTag string `json:",optional"` + TagHeader string `json:",optional"` } ) func MustNewServer(c RestConf, opts ...RunOption) *Server { server := rest.MustNewServer(c.RestConf, opts...) - server.Use(rest.ToMiddleware(handler.TagHandler(c.HeaderTag))) + server.Use(rest.ToMiddleware(handler.TagHandler(c.TagHeader))) return server } diff --git a/tag/tag.go b/tag/tag.go index 7ede816..6512a44 100644 --- a/tag/tag.go +++ b/tag/tag.go @@ -1,57 +1,8 @@ package tag -import ( - "context" +import "github.com/chenquan/zero-flow/internal/tag" - "github.com/zeromicro/go-zero/core/logx" - "go.opentelemetry.io/otel/baggage" - "google.golang.org/grpc/attributes" +var ( + ContextWithTag = tag.ContextWithTag + FromContext = tag.FromContext ) - -const tagKey = "x-zero-flow-tag" - -func ContextWithTag(ctx context.Context, tag string) context.Context { - bg := baggage.FromContext(ctx) - member, err := baggage.NewMember(tagKey, tag) - if err != nil { - logx.WithContext(ctx).Error(err) - return ctx - } - - bg, err = bg.SetMember(member) - if err != nil { - logx.WithContext(ctx).Error(err) - return ctx - } - - ctx = baggage.ContextWithBaggage(ctx, bg) - - return ctx -} - -func FromContext(ctx context.Context) string { - bg := baggage.FromContext(ctx) - member := bg.Member(tagKey) - - return member.Value() -} - -type tagAttributesKey struct{} - -func NewAttributes(tag string) *attributes.Attributes { - return attributes.New(tagAttributesKey{}, tag) -} - -func FromGrpcAttributes(attributes *attributes.Attributes) (string, bool) { - value := attributes.Value(tagAttributesKey{}) - if value == nil { - return "", false - } - - m, ok := value.(string) - if !ok { - return "", false - } - - return m, true -} diff --git a/zrpc/internal/p2c/p2c.go b/zrpc/internal/p2c/p2c.go index 7c06a51..9af2d1b 100644 --- a/zrpc/internal/p2c/p2c.go +++ b/zrpc/internal/p2c/p2c.go @@ -9,7 +9,7 @@ import ( "sync/atomic" "time" - "github.com/chenquan/zero-flow/tag" + "github.com/chenquan/zero-flow/internal/tag" "github.com/chenquan/zero-flow/zrpc/internal/selector" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/syncx" diff --git a/zrpc/internal/resolver/discovbuilder.go b/zrpc/internal/resolver/discovbuilder.go index 86d88d9..f495fb4 100644 --- a/zrpc/internal/resolver/discovbuilder.go +++ b/zrpc/internal/resolver/discovbuilder.go @@ -4,7 +4,7 @@ import ( "net/url" "strings" - "github.com/chenquan/zero-flow/tag" + "github.com/chenquan/zero-flow/internal/tag" "github.com/chenquan/zero-flow/zrpc/internal/targets" "github.com/zeromicro/go-zero/core/discov" "github.com/zeromicro/go-zero/core/logx" diff --git a/zrpc/internal/selector/defaultselector.go b/zrpc/internal/selector/defaultselector.go index 877b3ac..b37ec99 100644 --- a/zrpc/internal/selector/defaultselector.go +++ b/zrpc/internal/selector/defaultselector.go @@ -1,14 +1,12 @@ package selector import ( - "github.com/chenquan/zero-flow/tag" + "github.com/chenquan/zero-flow/internal/tag" "github.com/zeromicro/go-zero/core/logx" "go.opentelemetry.io/otel/trace" "google.golang.org/grpc/balancer" ) -const tagKey = "tag" - var ( DefaultSelector = defaultSelector{} _ Selector = (*defaultSelector)(nil) @@ -30,7 +28,7 @@ func (d defaultSelector) Select(conns []Conn, info balancer.PickInfo) []Conn { } if len(newConns) != 0 { - logx.WithContext(info.Ctx).Debugw("flow staining...", logx.Field(tagKey, tagString)) + logx.WithContext(info.Ctx).Debugw("flow staining...", logx.Field(tag.Key, tagString)) spanCtx := trace.SpanFromContext(info.Ctx) spanCtx.SetAttributes(tagAttributeKey.String(tagString))