Skip to content

Commit

Permalink
Add an option where we can change the stats socket
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Glazychev <[email protected]>
  • Loading branch information
glazychev-art committed Mar 15, 2022
1 parent 9e8642d commit 89079d6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
19 changes: 13 additions & 6 deletions pkg/networkservice/stats/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -34,19 +34,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)
}
Expand All @@ -70,9 +77,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
}
9 changes: 6 additions & 3 deletions pkg/networkservice/stats/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -68,8 +68,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)
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/networkservice/stats/option.go
Original file line number Diff line number Diff line change
@@ -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
}
}
19 changes: 13 additions & 6 deletions pkg/networkservice/stats/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -32,19 +32,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)
}
Expand All @@ -68,9 +75,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
}

0 comments on commit 89079d6

Please sign in to comment.