Skip to content

Commit

Permalink
Added ability to enable DCGM library log output
Browse files Browse the repository at this point in the history
Signed-off-by: Vadym Fedorov <[email protected]>
  • Loading branch information
nvvfedorov committed Mar 5, 2024
1 parent 9d2d633 commit 80f08f9
Show file tree
Hide file tree
Showing 13 changed files with 670 additions and 147 deletions.
4 changes: 3 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"args": [
"-f",
"./etc/default-counters.csv",
"--debug"
"--debug",
"--enable-dcgm-log",
"--dcgm-log-level=INFO"
]
}
]
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/NVIDIA/go-nvml v0.12.0-2
github.com/avast/retry-go/v4 v4.5.1
github.com/bits-and-blooms/bitset v1.13.0
github.com/go-kit/log v0.2.1
github.com/gorilla/mux v1.8.1
github.com/prometheus/client_model v0.6.0
github.com/prometheus/common v0.47.0
Expand All @@ -31,7 +32,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
Expand Down
72 changes: 72 additions & 0 deletions internal/pkg/logging/logger_adapter.go
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
}
113 changes: 113 additions & 0 deletions internal/pkg/logging/logger_adapter_test.go
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())
})
}
}
Loading

0 comments on commit 80f08f9

Please sign in to comment.