From cb6176dbbbe98b6a4c7bf913cda0689bb2dc6d01 Mon Sep 17 00:00:00 2001 From: Germiniku Date: Wed, 24 Aug 2022 12:39:20 +0800 Subject: [PATCH 1/2] feat(tracing) custom tracer name (#2310) * feat(log): add tencent cls fix update * refactor(log) use the same interface name * fix(log) tencent toString * feat(tracing) custom tracer name --- middleware/tracing/tracer.go | 5 +++-- middleware/tracing/tracing.go | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) 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...) From 73a8323ee7166fe6373f39774390cd89a8e64639 Mon Sep 17 00:00:00 2001 From: "jesse.tang" <1430482733@qq.com> Date: Wed, 24 Aug 2022 18:19:07 +0800 Subject: [PATCH 2/2] fix: slice/map make init cap (#2300) --- cmd/protoc-gen-go-http/http.go | 2 +- config/value.go | 4 ++-- contrib/registry/consul/client.go | 2 +- contrib/registry/discovery/discovery.go | 2 +- transport/grpc/balancer.go | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) 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/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{