diff --git a/pkg/networkservice/stats/client.go b/pkg/networkservice/stats/client.go index 2034ff15..c9de80a6 100644 --- a/pkg/networkservice/stats/client.go +++ b/pkg/networkservice/stats/client.go @@ -1,4 +1,6 @@ -// Copyright (c) 2021 Doc.ai and/or its affiliates. +// Copyright (c) 2021-2022 Doc.ai and/or its affiliates. +// +// Copyright (c) 2022 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -34,19 +36,26 @@ import ( type statsClient struct { chainCtx context.Context statsConn *core.StatsConnection + statsSock string once sync.Once initErr error } // NewClient provides a NetworkServiceClient chain elements that retrieves vpp interface metrics. -func NewClient(ctx context.Context) networkservice.NetworkServiceClient { +func NewClient(ctx context.Context, options ...Option) networkservice.NetworkServiceClient { + opts := &statsOptions{} + for _, opt := range options { + opt(opts) + } + return &statsClient{ - chainCtx: ctx, + chainCtx: ctx, + statsSock: opts.socket, } } func (s *statsClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) { - initErr := s.init(s.chainCtx) + initErr := s.init() if initErr != nil { log.FromContext(ctx).Errorf("%v", initErr) } @@ -70,9 +79,9 @@ func (s *statsClient) Close(ctx context.Context, conn *networkservice.Connection return &empty.Empty{}, nil } -func (s *statsClient) init(chainCtx context.Context) error { +func (s *statsClient) init() error { s.once.Do(func() { - s.statsConn, s.initErr = initFunc(chainCtx) + s.statsConn, s.initErr = initFunc(s.chainCtx, s.statsSock) }) return s.initErr } diff --git a/pkg/networkservice/stats/common.go b/pkg/networkservice/stats/common.go index 5390a839..e3c45173 100644 --- a/pkg/networkservice/stats/common.go +++ b/pkg/networkservice/stats/common.go @@ -1,4 +1,6 @@ -// Copyright (c) 2021 Doc.ai and/or its affiliates. +// Copyright (c) 2021-2022 Doc.ai and/or its affiliates. +// +// Copyright (c) 2022 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -68,8 +70,11 @@ func retrieveMetrics(ctx context.Context, statsConn *core.StatsConnection, segme } } -func initFunc(chainCtx context.Context) (*core.StatsConnection, error) { - statsConn, err := core.ConnectStats(statsclient.NewStatsClient(adapter.DefaultStatsSocket)) +func initFunc(chainCtx context.Context, statsSocket string) (*core.StatsConnection, error) { + if statsSocket == "" { + statsSocket = adapter.DefaultStatsSocket + } + statsConn, err := core.ConnectStats(statsclient.NewStatsClient(statsSocket)) if err != nil { return nil, errors.WithStack(err) } diff --git a/pkg/networkservice/stats/option.go b/pkg/networkservice/stats/option.go new file mode 100644 index 00000000..3a667a5c --- /dev/null +++ b/pkg/networkservice/stats/option.go @@ -0,0 +1,31 @@ +// Copyright (c) 2022 Cisco and/or its affiliates. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package stats + +type statsOptions struct { + socket string +} + +// Option is an option pattern for stats server/client +type Option func(o *statsOptions) + +// WithSocket sets stats socket name +func WithSocket(socket string) Option { + return func(o *statsOptions) { + o.socket = socket + } +} diff --git a/pkg/networkservice/stats/server.go b/pkg/networkservice/stats/server.go index 7b6c430b..7572c3fd 100644 --- a/pkg/networkservice/stats/server.go +++ b/pkg/networkservice/stats/server.go @@ -1,4 +1,6 @@ -// Copyright (c) 2021 Doc.ai and/or its affiliates. +// Copyright (c) 2021-2022 Doc.ai and/or its affiliates. +// +// Copyright (c) 2022 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -32,19 +34,26 @@ import ( type statsServer struct { chainCtx context.Context statsConn *core.StatsConnection + statsSock string once sync.Once initErr error } // NewServer provides a NetworkServiceServer chain elements that retrieves vpp interface metrics. -func NewServer(ctx context.Context) networkservice.NetworkServiceServer { +func NewServer(ctx context.Context, options ...Option) networkservice.NetworkServiceServer { + opts := &statsOptions{} + for _, opt := range options { + opt(opts) + } + return &statsServer{ - chainCtx: ctx, + chainCtx: ctx, + statsSock: opts.socket, } } func (s *statsServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { - initErr := s.init(s.chainCtx) + initErr := s.init() if initErr != nil { log.FromContext(ctx).Errorf("%v", initErr) } @@ -68,9 +77,9 @@ func (s *statsServer) Close(ctx context.Context, conn *networkservice.Connection return &empty.Empty{}, nil } -func (s *statsServer) init(chainCtx context.Context) error { +func (s *statsServer) init() error { s.once.Do(func() { - s.statsConn, s.initErr = initFunc(chainCtx) + s.statsConn, s.initErr = initFunc(s.chainCtx, s.statsSock) }) return s.initErr }