Skip to content

Commit

Permalink
Readd optinal trace condition to logObject (#1713)
Browse files Browse the repository at this point in the history
Signed-off-by: Arpad Kiss <[email protected]>
  • Loading branch information
arp-est authored Feb 13, 2025
1 parent 422abe9 commit e11e3c3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pkg/networkservice/core/trace/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// Copyright (c) 2024 Nordix Foundation.
//
// Copyright (c) 2025 OpenInfra Foundation Europe. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -238,6 +240,43 @@ func TestOutput_Info(t *testing.T) {
require.Equal(t, expectedOutput, result)
}

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

// Create a chain with modifying elements
ch := chain.NewNetworkServiceServer(
&testutil.LabelChangerFirstServer{},
&testutil.LabelChangerSecondServer{},
)

request := testutil.NewConnection()

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

e, err := ch.Close(context.Background(), conn)
require.NoError(t, err)
require.NotNil(t, e)
logrus.Info("Random log line")

expectedOutput := "[INFO] Random log line\n"

result := testutil.TrimLogTime(&buff)
result = testutil.Normalize(result)
expectedOutput = testutil.Normalize(expectedOutput)

require.Equal(t, expectedOutput, result)
}

func TestErrorOutput_Info(t *testing.T) {
// Configure logging
// Set output to buffer
Expand Down
5 changes: 5 additions & 0 deletions pkg/networkservice/core/trace/traceconcise/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2023-2024 Cisco and/or its affiliates.
//
// Copyright (c) 2025 OpenInfra Foundation Europe. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -51,6 +53,9 @@ func logError(ctx context.Context, err error, operation string) error {
}

func logObject(ctx context.Context, k, v interface{}) {
if !trace(ctx) {
return
}
s := log.FromContext(ctx)
msg := ""
cc, err := json.Marshal(v)
Expand Down
5 changes: 5 additions & 0 deletions pkg/registry/core/trace/traceconcise/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2023-2024 Cisco and/or its affiliates.
//
// Copyright (c) 2025 OpenInfra Foundation Europe. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -41,6 +43,9 @@ func logError(ctx context.Context, err error, operation string) error {
}

func logObject(ctx context.Context, k, v interface{}) {
if !trace(ctx) {
return
}
msg := ""
cc, err := json.Marshal(v)
if err == nil {
Expand Down

0 comments on commit e11e3c3

Please sign in to comment.