-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored setlogoption chain element to not use adapters
Signed-off-by: Mikhail Avramenko <[email protected]>
- Loading branch information
1 parent
d801027
commit 58199f6
Showing
13 changed files
with
233 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2021 Doc.ai 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 setlogoption | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next" | ||
"github.com/networkservicemesh/sdk/pkg/tools/log" | ||
) | ||
|
||
type setLogOptionClient struct { | ||
options map[string]string | ||
} | ||
|
||
// NewClient - construct a new set log option server to override some logging capabilities for context. | ||
func NewClient(options map[string]string) networkservice.NetworkServiceClient { | ||
return &setLogOptionClient{ | ||
options: options, | ||
} | ||
} | ||
|
||
func (s *setLogOptionClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) { | ||
ctx = s.withFields(ctx) | ||
return next.Client(ctx).Request(ctx, request, opts...) | ||
} | ||
|
||
func (s *setLogOptionClient) withFields(ctx context.Context) context.Context { | ||
ctxFields := log.Fields(ctx) | ||
fields := make(map[string]interface{}) | ||
for k, v := range ctxFields { | ||
fields[k] = v | ||
} | ||
|
||
fields["type"] = networkService | ||
for k, v := range s.options { | ||
fields[k] = v | ||
} | ||
if len(fields) > 0 { | ||
ctx = log.WithFields(ctx, fields) | ||
} | ||
return ctx | ||
} | ||
|
||
func (s *setLogOptionClient) Close(ctx context.Context, connection *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) { | ||
ctx = s.withFields(ctx) | ||
return next.Client(ctx).Close(ctx, connection, opts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2021 Doc.ai 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 setlogoption | ||
|
||
const ( | ||
networkService = "NetworkService" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2020-2021 Doc.ai 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 setlogoption | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
"github.com/networkservicemesh/api/pkg/api/registry" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/registry/core/next" | ||
) | ||
|
||
const ( | ||
nsRegistryClient = "NetworkServiceRegistryClient" | ||
) | ||
|
||
type setNSLogOptionClient struct { | ||
options map[string]string | ||
} | ||
|
||
func (s *setNSLogOptionClient) Register(ctx context.Context, ns *registry.NetworkService, opts ...grpc.CallOption) (*registry.NetworkService, error) { | ||
ctx = withFields(ctx, s.options, nsRegistryClient) | ||
return next.NetworkServiceRegistryClient(ctx).Register(ctx, ns, opts...) | ||
} | ||
|
||
func (s *setNSLogOptionClient) Find(ctx context.Context, query *registry.NetworkServiceQuery, opts ...grpc.CallOption) (registry.NetworkServiceRegistry_FindClient, error) { | ||
ctx = withFields(ctx, s.options, nsRegistryClient) | ||
return next.NetworkServiceRegistryClient(ctx).Find(ctx, query, opts...) | ||
} | ||
|
||
func (s *setNSLogOptionClient) Unregister(ctx context.Context, ns *registry.NetworkService, opts ...grpc.CallOption) (*empty.Empty, error) { | ||
ctx = withFields(ctx, s.options, nsRegistryClient) | ||
return next.NetworkServiceRegistryClient(ctx).Unregister(ctx, ns, opts...) | ||
} | ||
|
||
// NewNetworkServiceRegistryClient creates new instance of NetworkServiceRegistryClient which sets the passed options | ||
func NewNetworkServiceRegistryClient(options map[string]string) registry.NetworkServiceRegistryClient { | ||
return &setNSLogOptionClient{ | ||
options: options, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) 2020-2021 Doc.ai 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 setlogoption implements a chain element to set log options before full chain | ||
package setlogoption | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
"github.com/networkservicemesh/api/pkg/api/registry" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/registry/core/next" | ||
) | ||
|
||
const ( | ||
nseRegistryClient = "NetworkServiceEndpointRegistryClient" | ||
) | ||
|
||
type setNSELogOptionClient struct { | ||
options map[string]string | ||
} | ||
|
||
func (s *setNSELogOptionClient) Register(ctx context.Context, endpoint *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*registry.NetworkServiceEndpoint, error) { | ||
ctx = withFields(ctx, s.options, nseRegistryClient) | ||
return next.NetworkServiceEndpointRegistryClient(ctx).Register(ctx, endpoint, opts...) | ||
} | ||
|
||
func (s *setNSELogOptionClient) Find(ctx context.Context, query *registry.NetworkServiceEndpointQuery, opts ...grpc.CallOption) (registry.NetworkServiceEndpointRegistry_FindClient, error) { | ||
ctx = withFields(ctx, s.options, nseRegistryClient) | ||
return next.NetworkServiceEndpointRegistryClient(ctx).Find(ctx, query, opts...) | ||
} | ||
|
||
func (s *setNSELogOptionClient) Unregister(ctx context.Context, endpoint *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*empty.Empty, error) { | ||
ctx = withFields(ctx, s.options, nseRegistryClient) | ||
return next.NetworkServiceEndpointRegistryClient(ctx).Unregister(ctx, endpoint, opts...) | ||
} | ||
|
||
// NewNetworkServiceEndpointRegistryClient creates new instance of NetworkServiceEndpointRegistryClient which sets the passed options | ||
func NewNetworkServiceEndpointRegistryClient(options map[string]string) registry.NetworkServiceEndpointRegistryClient { | ||
return &setNSELogOptionClient{ | ||
options: options, | ||
} | ||
} |
Oops, something went wrong.