Merge remote-tracking branch 'origin/main' into #2228

pull/2324/head
wangtingshun 2 years ago
commit 811ae8067e
  1. 2
      cmd/protoc-gen-go-http/http.go
  2. 4
      config/value.go
  3. 2
      contrib/registry/consul/client.go
  4. 2
      contrib/registry/discovery/discovery.go
  5. 5
      middleware/tracing/tracer.go
  6. 8
      middleware/tracing/tracing.go
  7. 2
      transport/grpc/balancer.go

@ -213,9 +213,9 @@ func buildPathVars(path string) (res map[string]*string) {
if strings.HasSuffix(path, "/") { if strings.HasSuffix(path, "/") {
fmt.Fprintf(os.Stderr, "\u001B[31mWARN\u001B[m: Path %s should not end with \"/\" \n", path) fmt.Fprintf(os.Stderr, "\u001B[31mWARN\u001B[m: Path %s should not end with \"/\" \n", path)
} }
res = make(map[string]*string)
pattern := regexp.MustCompile(`(?i){([a-z\.0-9_\s]*)=?([^{}]*)}`) pattern := regexp.MustCompile(`(?i){([a-z\.0-9_\s]*)=?([^{}]*)}`)
matches := pattern.FindAllStringSubmatch(path, -1) matches := pattern.FindAllStringSubmatch(path, -1)
res = make(map[string]*string, len(matches))
for _, m := range matches { for _, m := range matches {
name := strings.TrimSpace(m[1]) name := strings.TrimSpace(m[1])
if len(name) > 1 && len(m[2]) > 0 { if len(name) > 1 && len(m[2]) > 0 {

@ -93,9 +93,9 @@ func (v *atomicValue) Slice() ([]Value, error) {
func (v *atomicValue) Map() (map[string]Value, error) { func (v *atomicValue) Map() (map[string]Value, error) {
if vals, ok := v.Load().(map[string]interface{}); ok { if vals, ok := v.Load().(map[string]interface{}); ok {
m := make(map[string]Value) m := make(map[string]Value, len(vals))
for key, val := range vals { for key, val := range vals {
a := &atomicValue{} a := new(atomicValue)
a.Store(val) a.Store(val)
m[key] = a m[key] = a
} }

@ -95,7 +95,7 @@ func (c *Client) Service(ctx context.Context, service string, index uint64, pass
// Register register service instance to consul // Register register service instance to consul
func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enableHealthCheck bool) error { func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enableHealthCheck bool) error {
addresses := make(map[string]api.ServiceAddress) addresses := make(map[string]api.ServiceAddress, len(svc.Endpoints))
checkAddresses := make([]string, 0, len(svc.Endpoints)) checkAddresses := make([]string, 0, len(svc.Endpoints))
for _, endpoint := range svc.Endpoints { for _, endpoint := range svc.Endpoints {
raw, err := url.Parse(endpoint) raw, err := url.Parse(endpoint)

@ -439,7 +439,7 @@ func (r *Resolve) fetch(ctx context.Context) (ins *disInstancesInfo, ok bool) {
ins = new(disInstancesInfo) ins = new(disInstancesInfo)
ins.LastTs = appIns.LastTs ins.LastTs = appIns.LastTs
ins.Scheduler = appIns.Scheduler ins.Scheduler = appIns.Scheduler
ins.Instances = make(map[string][]*discoveryInstance) ins.Instances = make(map[string][]*discoveryInstance, len(appIns.Instances))
for zone, in := range appIns.Instances { for zone, in := range appIns.Instances {
ins.Instances[zone] = in ins.Instances[zone] = in
} }

@ -24,6 +24,7 @@ type Tracer struct {
func NewTracer(kind trace.SpanKind, opts ...Option) *Tracer { func NewTracer(kind trace.SpanKind, opts ...Option) *Tracer {
op := options{ op := options{
propagator: propagation.NewCompositeTextMapPropagator(Metadata{}, propagation.Baggage{}, propagation.TraceContext{}), propagator: propagation.NewCompositeTextMapPropagator(Metadata{}, propagation.Baggage{}, propagation.TraceContext{}),
tracerName: "kratos",
} }
for _, o := range opts { for _, o := range opts {
o(&op) o(&op)
@ -34,9 +35,9 @@ func NewTracer(kind trace.SpanKind, opts ...Option) *Tracer {
switch kind { switch kind {
case trace.SpanKindClient: case trace.SpanKindClient:
return &Tracer{tracer: otel.Tracer("kratos"), kind: kind, opt: &op} return &Tracer{tracer: otel.Tracer(op.tracerName), kind: kind, opt: &op}
case trace.SpanKindServer: case trace.SpanKindServer:
return &Tracer{tracer: otel.Tracer("kratos"), kind: kind, opt: &op} return &Tracer{tracer: otel.Tracer(op.tracerName), kind: kind, opt: &op}
default: default:
panic(fmt.Sprintf("unsupported span kind: %v", kind)) panic(fmt.Sprintf("unsupported span kind: %v", kind))
} }

@ -15,6 +15,7 @@ import (
type Option func(*options) type Option func(*options)
type options struct { type options struct {
tracerName string
tracerProvider trace.TracerProvider tracerProvider trace.TracerProvider
propagator propagation.TextMapPropagator propagator propagation.TextMapPropagator
} }
@ -34,6 +35,13 @@ func WithTracerProvider(provider trace.TracerProvider) Option {
} }
} }
// WithTracerName with tracer name
func WithTracerName(tracerName string) Option {
return func(opts *options) {
opts.tracerName = tracerName
}
}
// Server returns a new server middleware for OpenTelemetry. // Server returns a new server middleware for OpenTelemetry.
func Server(opts ...Option) middleware.Middleware { func Server(opts ...Option) middleware.Middleware {
tracer := NewTracer(trace.SpanKindServer, opts...) tracer := NewTracer(trace.SpanKindServer, opts...)

@ -40,7 +40,7 @@ func (b *balancerBuilder) Build(info base.PickerBuildInfo) balancer.Picker {
// Block the RPC until a new picker is available via UpdateState(). // Block the RPC until a new picker is available via UpdateState().
return base.NewErrPicker(balancer.ErrNoSubConnAvailable) return base.NewErrPicker(balancer.ErrNoSubConnAvailable)
} }
nodes := make([]selector.Node, 0) nodes := make([]selector.Node, 0, len(info.ReadySCs))
for conn, info := range info.ReadySCs { for conn, info := range info.ReadySCs {
ins, _ := info.Address.Attributes.Value("rawServiceInstance").(*registry.ServiceInstance) ins, _ := info.Address.Attributes.Value("rawServiceInstance").(*registry.ServiceInstance)
nodes = append(nodes, &grpcNode{ nodes = append(nodes, &grpcNode{

Loading…
Cancel
Save