diff --git a/cmd/protoc-gen-go-http/http.go b/cmd/protoc-gen-go-http/http.go index 3eb91c061..cebbb6b15 100644 --- a/cmd/protoc-gen-go-http/http.go +++ b/cmd/protoc-gen-go-http/http.go @@ -213,9 +213,9 @@ func buildPathVars(path string) (res map[string]*string) { if strings.HasSuffix(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]*)=?([^{}]*)}`) matches := pattern.FindAllStringSubmatch(path, -1) + res = make(map[string]*string, len(matches)) for _, m := range matches { name := strings.TrimSpace(m[1]) if len(name) > 1 && len(m[2]) > 0 { diff --git a/config/value.go b/config/value.go index 805f155e5..46417e773 100644 --- a/config/value.go +++ b/config/value.go @@ -93,9 +93,9 @@ func (v *atomicValue) Slice() ([]Value, error) { func (v *atomicValue) Map() (map[string]Value, error) { 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 { - a := &atomicValue{} + a := new(atomicValue) a.Store(val) m[key] = a } diff --git a/contrib/registry/consul/client.go b/contrib/registry/consul/client.go index 80ea383a6..e97ec7e42 100644 --- a/contrib/registry/consul/client.go +++ b/contrib/registry/consul/client.go @@ -95,7 +95,7 @@ func (c *Client) Service(ctx context.Context, service string, index uint64, pass // Register register service instance to consul 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)) for _, endpoint := range svc.Endpoints { raw, err := url.Parse(endpoint) diff --git a/contrib/registry/discovery/discovery.go b/contrib/registry/discovery/discovery.go index f6df56dc0..dfff0a972 100644 --- a/contrib/registry/discovery/discovery.go +++ b/contrib/registry/discovery/discovery.go @@ -439,7 +439,7 @@ func (r *Resolve) fetch(ctx context.Context) (ins *disInstancesInfo, ok bool) { ins = new(disInstancesInfo) ins.LastTs = appIns.LastTs 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 { ins.Instances[zone] = in } diff --git a/middleware/tracing/tracer.go b/middleware/tracing/tracer.go index 48d4eaa14..cf9115bbf 100644 --- a/middleware/tracing/tracer.go +++ b/middleware/tracing/tracer.go @@ -24,6 +24,7 @@ type Tracer struct { func NewTracer(kind trace.SpanKind, opts ...Option) *Tracer { op := options{ propagator: propagation.NewCompositeTextMapPropagator(Metadata{}, propagation.Baggage{}, propagation.TraceContext{}), + tracerName: "kratos", } for _, o := range opts { o(&op) @@ -34,9 +35,9 @@ func NewTracer(kind trace.SpanKind, opts ...Option) *Tracer { switch kind { 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: - return &Tracer{tracer: otel.Tracer("kratos"), kind: kind, opt: &op} + return &Tracer{tracer: otel.Tracer(op.tracerName), kind: kind, opt: &op} default: panic(fmt.Sprintf("unsupported span kind: %v", kind)) } diff --git a/middleware/tracing/tracing.go b/middleware/tracing/tracing.go index c04b13d0b..167725f40 100644 --- a/middleware/tracing/tracing.go +++ b/middleware/tracing/tracing.go @@ -15,6 +15,7 @@ import ( type Option func(*options) type options struct { + tracerName string tracerProvider trace.TracerProvider 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. func Server(opts ...Option) middleware.Middleware { tracer := NewTracer(trace.SpanKindServer, opts...) diff --git a/transport/grpc/balancer.go b/transport/grpc/balancer.go index bd50f0983..2047eb0db 100644 --- a/transport/grpc/balancer.go +++ b/transport/grpc/balancer.go @@ -40,7 +40,7 @@ func (b *balancerBuilder) Build(info base.PickerBuildInfo) balancer.Picker { // Block the RPC until a new picker is available via UpdateState(). return base.NewErrPicker(balancer.ErrNoSubConnAvailable) } - nodes := make([]selector.Node, 0) + nodes := make([]selector.Node, 0, len(info.ReadySCs)) for conn, info := range info.ReadySCs { ins, _ := info.Address.Attributes.Value("rawServiceInstance").(*registry.ServiceInstance) nodes = append(nodes, &grpcNode{