Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readd optinal trace condition to logObject #1713

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 logObject(ctx context.Context, k, v interface{}) {
if !trace(ctx) {
return
}

Check warning on line 48 in pkg/registry/core/trace/traceconcise/common.go

View check run for this annotation

Codecov / codecov/patch

pkg/registry/core/trace/traceconcise/common.go#L46-L48

Added lines #L46 - L48 were not covered by tests
msg := ""
cc, err := json.Marshal(v)
if err == nil {
Expand Down
Loading