Skip to content

Commit e2c0609

Browse files
authored
chore: rename (#17)
* chore: rename * chore: rename
1 parent 69d94ee commit e2c0609

File tree

7 files changed

+73
-67
lines changed

7 files changed

+73
-67
lines changed

internal/tag/tag.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package tag
2+
3+
import (
4+
"context"
5+
6+
"github.com/zeromicro/go-zero/core/logx"
7+
"go.opentelemetry.io/otel/baggage"
8+
"google.golang.org/grpc/attributes"
9+
)
10+
11+
const Key = "x-zero-flow-tag"
12+
13+
func ContextWithTag(ctx context.Context, tag string) context.Context {
14+
bg := baggage.FromContext(ctx)
15+
member, err := baggage.NewMember(Key, tag)
16+
if err != nil {
17+
logx.WithContext(ctx).Error(err)
18+
return ctx
19+
}
20+
21+
bg, err = bg.SetMember(member)
22+
if err != nil {
23+
logx.WithContext(ctx).Error(err)
24+
return ctx
25+
}
26+
27+
ctx = baggage.ContextWithBaggage(ctx, bg)
28+
29+
return ctx
30+
}
31+
32+
func FromContext(ctx context.Context) string {
33+
bg := baggage.FromContext(ctx)
34+
member := bg.Member(Key)
35+
36+
return member.Value()
37+
}
38+
39+
type tagAttributesKey struct{}
40+
41+
func NewAttributes(tag string) *attributes.Attributes {
42+
return attributes.New(tagAttributesKey{}, tag)
43+
}
44+
45+
func FromGrpcAttributes(attributes *attributes.Attributes) (string, bool) {
46+
value := attributes.Value(tagAttributesKey{})
47+
if value == nil {
48+
return "", false
49+
}
50+
51+
m, ok := value.(string)
52+
if !ok {
53+
return "", false
54+
}
55+
56+
return m, true
57+
}

rest/internal/handler/tag_handler.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@ import (
44
"context"
55
"net/http"
66

7-
"github.com/chenquan/zero-flow/tag"
7+
"github.com/chenquan/zero-flow/internal/tag"
88
"github.com/zeromicro/go-zero/core/logx"
99
"go.opentelemetry.io/otel/attribute"
1010
"go.opentelemetry.io/otel/trace"
1111
)
1212

1313
var httpColorAttributeKey = attribute.Key("http.header.color")
1414

15-
func TagHandler(headerTag string) func(next http.Handler) http.Handler {
15+
func TagHandler(tagHeader string) func(next http.Handler) http.Handler {
1616
return func(next http.Handler) http.Handler {
1717
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
1818
ctx := request.Context()
19-
ctx = newBaggage(ctx, request, headerTag)
19+
ctx = newBaggage(ctx, request, tagHeader)
2020
next.ServeHTTP(writer, request.WithContext(ctx))
2121
})
2222
}
2323
}
2424

25-
func newBaggage(ctx context.Context, request *http.Request, headerTag string) context.Context {
25+
func newBaggage(ctx context.Context, request *http.Request, tagHeader string) context.Context {
2626
span := trace.SpanFromContext(ctx)
27-
tagString := request.Header.Get(headerTag)
27+
tagString := request.Header.Get(tagHeader)
2828
if len(tagString) == 0 {
2929
return ctx
3030
}
31-
logx.WithContext(ctx).Debugw("flow staining...", logx.Field("tag", tagString))
31+
logx.WithContext(ctx).Debugw("flow staining...", logx.Field(tag.Key, tagString))
3232

3333
ctx = tag.ContextWithTag(ctx, tagString)
3434
span.SetAttributes(httpColorAttributeKey.String(tagString))

rest/rest.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ type (
1010
Server = rest.Server
1111
RestConf struct {
1212
rest.RestConf
13-
HeaderTag string `json:",optional"`
13+
TagHeader string `json:",optional"`
1414
}
1515
)
1616

1717
func MustNewServer(c RestConf, opts ...RunOption) *Server {
1818
server := rest.MustNewServer(c.RestConf, opts...)
19-
server.Use(rest.ToMiddleware(handler.TagHandler(c.HeaderTag)))
19+
server.Use(rest.ToMiddleware(handler.TagHandler(c.TagHeader)))
2020
return server
2121
}

tag/tag.go

+4-53
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,8 @@
11
package tag
22

3-
import (
4-
"context"
3+
import "github.com/chenquan/zero-flow/internal/tag"
54

6-
"github.com/zeromicro/go-zero/core/logx"
7-
"go.opentelemetry.io/otel/baggage"
8-
"google.golang.org/grpc/attributes"
5+
var (
6+
ContextWithTag = tag.ContextWithTag
7+
FromContext = tag.FromContext
98
)
10-
11-
const tagKey = "x-zero-flow-tag"
12-
13-
func ContextWithTag(ctx context.Context, tag string) context.Context {
14-
bg := baggage.FromContext(ctx)
15-
member, err := baggage.NewMember(tagKey, tag)
16-
if err != nil {
17-
logx.WithContext(ctx).Error(err)
18-
return ctx
19-
}
20-
21-
bg, err = bg.SetMember(member)
22-
if err != nil {
23-
logx.WithContext(ctx).Error(err)
24-
return ctx
25-
}
26-
27-
ctx = baggage.ContextWithBaggage(ctx, bg)
28-
29-
return ctx
30-
}
31-
32-
func FromContext(ctx context.Context) string {
33-
bg := baggage.FromContext(ctx)
34-
member := bg.Member(tagKey)
35-
36-
return member.Value()
37-
}
38-
39-
type tagAttributesKey struct{}
40-
41-
func NewAttributes(tag string) *attributes.Attributes {
42-
return attributes.New(tagAttributesKey{}, tag)
43-
}
44-
45-
func FromGrpcAttributes(attributes *attributes.Attributes) (string, bool) {
46-
value := attributes.Value(tagAttributesKey{})
47-
if value == nil {
48-
return "", false
49-
}
50-
51-
m, ok := value.(string)
52-
if !ok {
53-
return "", false
54-
}
55-
56-
return m, true
57-
}

zrpc/internal/p2c/p2c.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"sync/atomic"
1010
"time"
1111

12-
"github.com/chenquan/zero-flow/tag"
12+
"github.com/chenquan/zero-flow/internal/tag"
1313
"github.com/chenquan/zero-flow/zrpc/internal/selector"
1414
"github.com/zeromicro/go-zero/core/logx"
1515
"github.com/zeromicro/go-zero/core/syncx"

zrpc/internal/resolver/discovbuilder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"net/url"
55
"strings"
66

7-
"github.com/chenquan/zero-flow/tag"
7+
"github.com/chenquan/zero-flow/internal/tag"
88
"github.com/chenquan/zero-flow/zrpc/internal/targets"
99
"github.com/zeromicro/go-zero/core/discov"
1010
"github.com/zeromicro/go-zero/core/logx"

zrpc/internal/selector/defaultselector.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package selector
22

33
import (
4-
"github.com/chenquan/zero-flow/tag"
4+
"github.com/chenquan/zero-flow/internal/tag"
55
"github.com/zeromicro/go-zero/core/logx"
66
"go.opentelemetry.io/otel/trace"
77
"google.golang.org/grpc/balancer"
88
)
99

10-
const tagKey = "tag"
11-
1210
var (
1311
DefaultSelector = defaultSelector{}
1412
_ Selector = (*defaultSelector)(nil)
@@ -30,7 +28,7 @@ func (d defaultSelector) Select(conns []Conn, info balancer.PickInfo) []Conn {
3028
}
3129

3230
if len(newConns) != 0 {
33-
logx.WithContext(info.Ctx).Debugw("flow staining...", logx.Field(tagKey, tagString))
31+
logx.WithContext(info.Ctx).Debugw("flow staining...", logx.Field(tag.Key, tagString))
3432

3533
spanCtx := trace.SpanFromContext(info.Ctx)
3634
spanCtx.SetAttributes(tagAttributeKey.String(tagString))

0 commit comments

Comments
 (0)