-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: rename * chore: rename
- Loading branch information
Showing
7 changed files
with
73 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters