Skip to content

Commit

Permalink
use the logrus logger by default to not miss useful logs.
Browse files Browse the repository at this point in the history
Signed-off-by: denis-tingaikin <denis.tingajkin@xored.com>
  • Loading branch information
denis-tingaikin committed Dec 19, 2024
1 parent 7dfb136 commit 9586bdd
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/networkservice/core/trace/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (

"github.com/networkservicemesh/sdk/pkg/networkservice/core/trace/traceconcise"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/trace/traceverbose"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger"
)

type traceClient struct {
Expand All @@ -48,16 +50,23 @@ func NewNetworkServiceClient(traced networkservice.NetworkServiceClient) network

func (t *traceClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
if logrus.GetLevel() <= logrus.WarnLevel {
if log.FromContext(ctx) == log.L() {
ctx = log.WithLog(ctx, logruslogger.New(ctx))
}
return t.original.Request(ctx, request)
}
if logrus.GetLevel() >= logrus.DebugLevel {
return t.verbose.Request(ctx, request, opts...)
}

return t.concise.Request(ctx, request, opts...)
}

func (t *traceClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
if logrus.GetLevel() <= logrus.WarnLevel {
if log.FromContext(ctx) == log.L() {
ctx = log.WithLog(ctx, logruslogger.New(ctx))
}
return t.original.Close(ctx, conn)
}
if logrus.GetLevel() >= logrus.DebugLevel {
Expand Down
8 changes: 8 additions & 0 deletions pkg/networkservice/core/trace/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (

"github.com/networkservicemesh/sdk/pkg/networkservice/core/trace/traceconcise"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/trace/traceverbose"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger"

"github.com/golang/protobuf/ptypes/empty"

Expand All @@ -44,6 +46,9 @@ func NewNetworkServiceServer(traced networkservice.NetworkServiceServer) network

func (t *traceServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
if logrus.GetLevel() <= logrus.WarnLevel {
if log.FromContext(ctx) == log.L() {
ctx = log.WithLog(ctx, logruslogger.New(ctx))
}
return t.original.Request(ctx, request)
}
if logrus.GetLevel() >= logrus.DebugLevel {
Expand All @@ -55,6 +60,9 @@ func (t *traceServer) Request(ctx context.Context, request *networkservice.Netwo

func (t *traceServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
if logrus.GetLevel() <= logrus.WarnLevel {
if log.FromContext(ctx) == log.L() {
ctx = log.WithLog(ctx, logruslogger.New(ctx))
}
return t.original.Close(ctx, conn)
}
if logrus.GetLevel() >= logrus.DebugLevel {
Expand Down
33 changes: 33 additions & 0 deletions pkg/networkservice/core/trace/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/trace/testutil"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkcontext"
"github.com/networkservicemesh/sdk/pkg/tools/log"
)

Expand Down Expand Up @@ -165,6 +166,38 @@ func TestErrorOutput(t *testing.T) {
require.Equal(t, expectedOutput, result)
}

func Test_ErrorOutput_InfoLevel(t *testing.T) {
// Configure logging
// Set output to buffer
var buff bytes.Buffer
logrus.SetOutput(&buff)
logrus.SetFormatter(&logrus.TextFormatter{
DisableTimestamp: true,
})
logrus.SetLevel(logrus.WarnLevel)
log.EnableTracing(true)

// Create a chain with modifying elements
ch := chain.NewNetworkServiceServer(
checkcontext.NewServer(t, func(t *testing.T, ctx context.Context) {
log.FromContext(ctx).Error("error details")
}),
)

request := testutil.NewConnection()

_, err := ch.Request(context.Background(), request)
require.NoError(t, err)

expectedOutput := "[ERRO] error details\n"
result := testutil.TrimLogTime(&buff)

result = testutil.Normalize(result)
expectedOutput = testutil.Normalize(expectedOutput)

require.Equal(t, expectedOutput, result)
}

func TestOutput_Info(t *testing.T) {
// Configure logging
// Set output to buffer
Expand Down
17 changes: 17 additions & 0 deletions pkg/registry/core/trace/ns_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (

"github.com/networkservicemesh/sdk/pkg/registry/core/trace/traceconcise"
"github.com/networkservicemesh/sdk/pkg/registry/core/trace/traceverbose"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger"
)

type traceNetworkServiceRegistryClient struct {
Expand All @@ -50,6 +52,9 @@ func (t *traceNetworkServiceRegistryClient) Register(ctx context.Context, in *re
if logrus.GetLevel() >= logrus.DebugLevel {
return t.verbose.Register(ctx, in, opts...)
}
if log.FromContext(ctx) == log.L() {
ctx = log.WithLog(ctx, logruslogger.New(ctx))
}
return t.original.Register(ctx, in, opts...)
}
func (t *traceNetworkServiceRegistryClient) Find(ctx context.Context, in *registry.NetworkServiceQuery, opts ...grpc.CallOption) (registry.NetworkServiceRegistry_FindClient, error) {
Expand All @@ -59,6 +64,9 @@ func (t *traceNetworkServiceRegistryClient) Find(ctx context.Context, in *regist
if logrus.GetLevel() <= logrus.WarnLevel {
return t.concise.Find(ctx, in, opts...)
}
if log.FromContext(ctx) == log.L() {
ctx = log.WithLog(ctx, logruslogger.New(ctx))
}
return t.original.Find(ctx, in, opts...)
}

Expand All @@ -69,6 +77,9 @@ func (t *traceNetworkServiceRegistryClient) Unregister(ctx context.Context, in *
if logrus.GetLevel() <= logrus.WarnLevel {
return t.concise.Unregister(ctx, in, opts...)
}
if log.FromContext(ctx) == log.L() {
ctx = log.WithLog(ctx, logruslogger.New(ctx))
}
return t.original.Unregister(ctx, in, opts...)
}

Expand All @@ -92,6 +103,9 @@ func (t *traceNetworkServiceRegistryServer) Register(ctx context.Context, in *re
if logrus.GetLevel() <= logrus.WarnLevel {
return t.concise.Register(ctx, in)
}
if log.FromContext(ctx) == log.L() {
ctx = log.WithLog(ctx, logruslogger.New(ctx))
}
return t.original.Register(ctx, in)
}

Expand All @@ -112,5 +126,8 @@ func (t *traceNetworkServiceRegistryServer) Unregister(ctx context.Context, in *
if logrus.GetLevel() <= logrus.WarnLevel {
return t.concise.Unregister(ctx, in)
}
if log.FromContext(ctx) == log.L() {
ctx = log.WithLog(ctx, logruslogger.New(ctx))
}
return t.original.Unregister(ctx, in)
}
14 changes: 14 additions & 0 deletions pkg/registry/core/trace/nse_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (

"github.com/networkservicemesh/sdk/pkg/registry/core/trace/traceconcise"
"github.com/networkservicemesh/sdk/pkg/registry/core/trace/traceverbose"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger"
)

type traceNetworkServiceEndpointRegistryClient struct {
Expand Down Expand Up @@ -59,6 +61,9 @@ func (t *traceNetworkServiceEndpointRegistryClient) Find(ctx context.Context, in
if logrus.GetLevel() <= logrus.WarnLevel {
return t.concise.Find(ctx, in, opts...)
}
if log.FromContext(ctx) == log.L() {
ctx = log.WithLog(ctx, logruslogger.New(ctx))
}
return t.original.Find(ctx, in, opts...)
}

Expand All @@ -69,6 +74,9 @@ func (t *traceNetworkServiceEndpointRegistryClient) Unregister(ctx context.Conte
if logrus.GetLevel() <= logrus.WarnLevel {
return t.concise.Unregister(ctx, in, opts...)
}
if log.FromContext(ctx) == log.L() {
ctx = log.WithLog(ctx, logruslogger.New(ctx))
}
return t.original.Unregister(ctx, in, opts...)
}

Expand All @@ -92,6 +100,9 @@ func (t *traceNetworkServiceEndpointRegistryServer) Register(ctx context.Context
if logrus.GetLevel() <= logrus.WarnLevel {
return t.concise.Register(ctx, in)
}
if log.FromContext(ctx) == log.L() {
ctx = log.WithLog(ctx, logruslogger.New(ctx))
}
return t.original.Register(ctx, in)
}

Expand All @@ -112,5 +123,8 @@ func (t *traceNetworkServiceEndpointRegistryServer) Unregister(ctx context.Conte
if logrus.GetLevel() <= logrus.WarnLevel {
return t.concise.Unregister(ctx, in)
}
if log.FromContext(ctx) == log.L() {
ctx = log.WithLog(ctx, logruslogger.New(ctx))
}
return t.original.Unregister(ctx, in)
}

0 comments on commit 9586bdd

Please sign in to comment.