Skip to content

Commit

Permalink
feat: add log formatting
Browse files Browse the repository at this point in the history
We introduce log formatting to support both text and json formats. This
can be configured through a CLI flag "log-format" or env var
"DCGM_EXPORTER_LOG_FORMAT".

Notably, the log format will capture any DCGM logging that is
configured.

We also move the automaxprocs call from an unused import to be
explicitly called when starting the dcgm exporter to ensure its logging
format honors the configuration.
  • Loading branch information
pintohutch committed Dec 30, 2024
1 parent 900d465 commit edde397
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 0 additions & 2 deletions cmd/dcgm-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (

"github.com/sirupsen/logrus"

_ "go.uber.org/automaxprocs"

"github.com/NVIDIA/dcgm-exporter/pkg/cmd"
)

Expand Down
27 changes: 27 additions & 0 deletions pkg/cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/NVIDIA/go-dcgm/pkg/dcgm"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"go.uber.org/automaxprocs/maxprocs"

"github.com/NVIDIA/dcgm-exporter/pkg/dcgmexporter"
"github.com/NVIDIA/dcgm-exporter/pkg/stdout"
Expand Down Expand Up @@ -72,6 +73,7 @@ const (
CLIClockEventsCountWindowSize = "clock-events-count-window-size"
CLIEnableDCGMLog = "enable-dcgm-log"
CLIDCGMLogLevel = "dcgm-log-level"
CLILogFormat = "log-format"
CLIPodResourcesKubeletSocket = "pod-resources-kubelet-socket"
CLIHPCJobMappingDir = "hpc-job-mapping-dir"
CLINvidiaResourceNames = "nvidia-resource-names"
Expand Down Expand Up @@ -226,6 +228,12 @@ func NewApp(buildVersion ...string) *cli.App {
Usage: "Specify the DCGM log verbosity level. This parameter is effective only when the '--enable-dcgm-log' option is set to 'true'. Possible values: NONE, FATAL, ERROR, WARN, INFO, DEBUG and VERB",
EnvVars: []string{"DCGM_EXPORTER_DCGM_LOG_LEVEL"},
},
&cli.StringFlag{
Name: CLILogFormat,
Value: "text",
Usage: "Specify the log output format. Possible values: text, json",
EnvVars: []string{"DCGM_EXPORTER_LOG_FORMAT"},
},
&cli.StringFlag{
Name: CLIPodResourcesKubeletSocket,
Value: "/var/lib/kubelet/pod-resources/kubelet.sock",
Expand Down Expand Up @@ -288,8 +296,27 @@ func action(c *cli.Context) (err error) {
})
}

func configureLogFormat(c *cli.Context) error {
logFormat := c.String(CLILogFormat)
switch logFormat {
case "text":
logrus.SetFormatter(&logrus.TextFormatter{})
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{})
default:
return fmt.Errorf("invalid %s parameter values: %s", CLILogFormat, logFormat)
}
return nil
}

func startDCGMExporter(c *cli.Context, cancel context.CancelFunc) error {
restart:
if err := configureLogFormat(c); err != nil {
return err
}

// Initialize automaxprocs with desired logging format.
maxprocs.Set(maxprocs.Logger(logrus.Infof))

logrus.Info("Starting dcgm-exporter")

Expand Down

0 comments on commit edde397

Please sign in to comment.