-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to enable DCGM library log output
Signed-off-by: Vadym Fedorov <[email protected]>
- Loading branch information
1 parent
9d2d633
commit 80f08f9
Showing
13 changed files
with
670 additions
and
147 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,4 +94,4 @@ validate-modules: | |
tools: ## Install required tools and utilities | ||
go install github.com/golangci/golangci-lint/cmd/[email protected] | ||
go install github.com/axw/gocov/gocov@latest | ||
|
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,72 @@ | ||
/* | ||
* Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
* | ||
* 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 logging | ||
|
||
import ( | ||
"fmt" | ||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// LogrusAdapter is an adapter that allows logrus Logger to be used as a go-kit/log Logger. | ||
type LogrusAdapter struct { | ||
Logger *logrus.Logger | ||
} | ||
|
||
// NewLogrusAdapter creates a new LogrusAdapter with the provided logrus.Logger. | ||
func NewLogrusAdapter(logger *logrus.Logger) log.Logger { | ||
return &LogrusAdapter{ | ||
Logger: logger, | ||
} | ||
} | ||
|
||
// Log implements the go-kit/log Logger interface. | ||
func (a *LogrusAdapter) Log(keyvals ...interface{}) error { | ||
if len(keyvals)%2 != 0 { | ||
keyvals = append(keyvals, "MISSING") | ||
} | ||
|
||
fields := logrus.Fields{} | ||
for i := 0; i < len(keyvals); i += 2 { | ||
key, ok := keyvals[i].(string) | ||
if !ok { | ||
// If the key is not a string, use a default key | ||
key = "missing_key" | ||
} | ||
fields[key] = keyvals[i+1] | ||
} | ||
|
||
msg, exists := fields["msg"] | ||
if exists { | ||
delete(fields, "msg") | ||
} | ||
|
||
lvl, exists := fields["level"] | ||
if !exists { | ||
fields["level"] = level.InfoValue() | ||
} | ||
delete(fields, "level") | ||
parsedLvl, err := logrus.ParseLevel(fmt.Sprint(lvl)) | ||
if err != nil { | ||
parsedLvl = logrus.InfoLevel | ||
} | ||
|
||
a.Logger.WithFields(fields).Log(parsedLvl, msg) | ||
|
||
return nil | ||
} |
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,113 @@ | ||
/* | ||
* Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
* | ||
* 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 logging | ||
|
||
import ( | ||
"github.com/go-kit/log/level" | ||
"github.com/sirupsen/logrus" | ||
"github.com/sirupsen/logrus/hooks/test" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestLogrusAdapter_Log(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
keyvals []interface{} | ||
assert func(*testing.T, *logrus.Entry) | ||
} | ||
|
||
//"msg", "Listening on", "address" | ||
testCases := []testCase{ | ||
{ | ||
name: "Success", | ||
keyvals: []interface{}{ | ||
"level", | ||
level.InfoValue, | ||
"msg", | ||
"Listening on", | ||
"address", | ||
"127.0.0.0.1:8080", | ||
}, | ||
assert: func(t *testing.T, entry *logrus.Entry) { | ||
t.Helper() | ||
require.NotNil(t, entry) | ||
assert.Equal(t, "Listening on", entry.Message) | ||
require.Contains(t, entry.Data, "address") | ||
assert.Equal(t, "127.0.0.0.1:8080", entry.Data["address"]) | ||
}, | ||
}, | ||
{ | ||
name: "When no Level", | ||
keyvals: []interface{}{ | ||
"msg", | ||
"Listening on", | ||
"address", | ||
"127.0.0.0.1:8080", | ||
}, | ||
assert: func(t *testing.T, entry *logrus.Entry) { | ||
t.Helper() | ||
require.NotNil(t, entry) | ||
assert.Equal(t, "Listening on", entry.Message) | ||
require.Contains(t, entry.Data, "address") | ||
assert.Equal(t, "127.0.0.0.1:8080", entry.Data["address"]) | ||
}, | ||
}, | ||
{ | ||
name: "When key is not string", | ||
keyvals: []interface{}{ | ||
"msg", | ||
"Listening on", | ||
42, | ||
"127.0.0.0.1:8080", | ||
}, | ||
assert: func(t *testing.T, entry *logrus.Entry) { | ||
t.Helper() | ||
require.NotNil(t, entry) | ||
assert.Equal(t, "Listening on", entry.Message) | ||
require.Contains(t, entry.Data, "missing_key") | ||
assert.Equal(t, "127.0.0.0.1:8080", entry.Data["missing_key"]) | ||
}, | ||
}, | ||
{ | ||
name: "When value is missing", | ||
keyvals: []interface{}{ | ||
"msg", | ||
"Listening on", | ||
"address", | ||
}, | ||
assert: func(t *testing.T, entry *logrus.Entry) { | ||
t.Helper() | ||
require.NotNil(t, entry) | ||
assert.Equal(t, "Listening on", entry.Message) | ||
require.Contains(t, entry.Data, "address") | ||
assert.Equal(t, "MISSING", entry.Data["address"]) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
logrusLogger, logHook := test.NewNullLogger() | ||
logger := NewLogrusAdapter(logrusLogger) | ||
err := logger.Log(tc.keyvals...) | ||
require.NoError(t, err) | ||
tc.assert(t, logHook.LastEntry()) | ||
}) | ||
} | ||
} |
Oops, something went wrong.