-
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.
Merge pull request #276 from NVIDIA/enable-dcgm-logging-options
Added ability to enable DCGM library log output
- Loading branch information
Showing
13 changed files
with
679 additions
and
117 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,3 @@ 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,75 @@ | ||
/* | ||
* 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 (la *LogrusAdapter) Log(keyvals ...interface{}) error { | ||
// keyvals is a slice of interfaces, that represents a key-value pairs. | ||
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 la string, use la default key | ||
key = "missing_key" | ||
} | ||
fields[key] = keyvals[i+1] | ||
} | ||
|
||
// The go-kit/log uses msg field to keep log message, we don't want to use message as field in the logrus. | ||
msg, exists := fields["msg"] | ||
if exists { | ||
delete(fields, "msg") | ||
} | ||
|
||
// The go-kit/log uses level fields to keep log level. We need to convert this field into logrus value. | ||
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 | ||
} | ||
|
||
la.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.