Skip to content

Commit

Permalink
chore: tidy things up
Browse files Browse the repository at this point in the history
Signed-off-by: Utku Ozdemir <utkuozdemir@gmail.com>
  • Loading branch information
utkuozdemir committed Jun 28, 2024
1 parent e7ececf commit 0a05a48
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 68 deletions.
19 changes: 11 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@
linters:
enable-all: true
disable:
# breaking things: https://github.com/atc0005/go-ci/issues/1024
- depguard
# not useful
- exhaustivestruct
- exhaustruct
- goerr113
- err113

linters-settings:
gci:
sections:
- standard
- default
- prefix(github.com/utkuozdemir/nvidia_gpu_exporter)
- blank
- dot
- alias
- localmodule
goimports:
local-prefixes: github.com/utkuozdemir/nvidia_gpu_exporter
goconst:
ignore-tests: true
depguard:
rules:
main:
list-mode: lax
files:
- $all
deny:
- pkg: io/ioutil
desc: "replaced by io and os packages since Go 1.16: https://tip.golang.org/doc/go1.16#ioutil"
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:22.04
FROM ubuntu:24.04

COPY nvidia_gpu_exporter /usr/bin/nvidia_gpu_exporter

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ See [METRICS.md](METRICS.md) for details.

See [CONTRIBUTING.md](CONTRIBUTING.md) for details.

# Star History
## Star History

<!-- markdownlint-disable no-inline-html -->
<a href="https://star-history.com/#utkuozdemir/nvidia_gpu_exporter&Date">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=utkuozdemir/nvidia_gpu_exporter&type=Date&theme=dark" />
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=utkuozdemir/nvidia_gpu_exporter&type=Date" />
<img alt="Star History Chart" src="https://api.star-history.com/svg?repos=utkuozdemir/nvidia_gpu_exporter&type=Date" />
</picture>
</a>
<!-- markdownlint-enable no-inline-html -->
13 changes: 5 additions & 8 deletions cmd/nvidia_gpu_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,25 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
clientversion "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"

"github.com/utkuozdemir/nvidia_gpu_exporter/internal/exporter"

clientversion "github.com/prometheus/client_golang/prometheus/collectors/version"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
)

const (
redirectPageTemplate = `<html lang="en">
const redirectPageTemplate = `<html lang="en">
<head><title>Nvidia GPU Exporter</title></head>
<body>
<h1>Nvidia GPU Exporter</h1>
<p><a href="%s">Metrics</a></p>
</body>
</html>
`
)

// main is the entrypoint of the application.
//
Expand Down Expand Up @@ -123,7 +120,7 @@ func (r *RootHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
}

// listenAndServe is same as web.ListenAndServe but supports passing network stack as an argument.
func listenAndServe(server *http.Server, flags *web.FlagConfig, network string, logger log.Logger) error {
func listenAndServe(server *http.Server, flags *web.FlagConfig, network string, logger log.Logger) (retErr error) {
if *flags.WebSystemdSocket {
level.Info(logger).Log("msg", "Listening on systemd activated listeners instead of port listeners.") //nolint:errcheck

Expand Down Expand Up @@ -157,7 +154,7 @@ func listenAndServe(server *http.Server, flags *web.FlagConfig, network string,
defer func() {
for _, listener := range listeners {
if err := listener.Close(); err != nil {
level.Error(logger).Log("msg", "Error closing listener", "err", err) //nolint:errcheck
retErr = errors.Join(retErr, fmt.Errorf("failed to close listener: %w", err))
}
}
}()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/utkuozdemir/nvidia_gpu_exporter

go 1.22
go 1.22.4

require (
github.com/alecthomas/kingpin/v2 v2.4.0
Expand All @@ -14,7 +14,7 @@ require (
)

require (
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
13 changes: 9 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/alecthomas/kingpin/v2 v2.4.0 h1:f48lwail6p8zpO1bC4TxtqACaGqHYA22qkHjHpqDjYY=
github.com/alecthomas/kingpin/v2 v2.4.0/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE=
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 h1:ez/4by2iGztzR4L0zgAOR8lTQK9VlyBVVd7G4omaOQs=
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 h1:t3eaIm0rUkzbrIewtiFmMK5RXHej2XnoXNhxVsAYUfg=
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30/go.mod h1:fvzegU4vN3H1qMT+8wDmzjAcDONcgo2/SZ/TyfdUOFs=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
Expand Down Expand Up @@ -43,7 +43,12 @@ github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoG
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
Expand All @@ -67,8 +72,8 @@ google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWn
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
36 changes: 18 additions & 18 deletions internal/exporter/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import (
"strings"
)

type Table[T any] struct {
Rows []Row[T]
type Table struct {
Rows []Row
RFields []RField
QFieldToCells map[QField][]Cell[T]
QFieldToCells map[QField][]Cell
}

type Row[T any] struct {
QFieldToCells map[QField]Cell[T]
Cells []Cell[T]
type Row struct {
QFieldToCells map[QField]Cell
Cells []Cell
}

type Cell[T any] struct {
type Cell struct {
QField QField
RField RField
RawValue T
RawValue string
}

func ParseCSVIntoTable(queryResult string, qFields []QField) (Table[string], error) {
func ParseCSVIntoTable(queryResult string, qFields []QField) (Table, error) {
lines := strings.Split(strings.TrimSpace(queryResult), "\n")
titlesLine := lines[0]
valuesLines := lines[1:]
Expand All @@ -31,27 +31,27 @@ func ParseCSVIntoTable(queryResult string, qFields []QField) (Table[string], err
numCols := len(qFields)
numRows := len(valuesLines)

rows := make([]Row[string], numRows)
rows := make([]Row, numRows)

qFieldToCells := make(map[QField][]Cell[string])
qFieldToCells := make(map[QField][]Cell)
for _, q := range qFields {
qFieldToCells[q] = make([]Cell[string], numRows)
qFieldToCells[q] = make([]Cell, numRows)
}

for rowIndex, valuesLine := range valuesLines {
qFieldToCell := make(map[QField]Cell[string], numCols)
cells := make([]Cell[string], numCols)
qFieldToCell := make(map[QField]Cell, numCols)
cells := make([]Cell, numCols)
rawValues := parseCSVLine(valuesLine)

if len(qFields) != len(rFields) {
return Table[string]{}, fmt.Errorf("field count mismatch: query fields: %d, returned fields: %d",
return Table{}, fmt.Errorf("field count mismatch: query fields: %d, returned fields: %d",
len(qFields), len(rFields))
}

for colIndex, rawValue := range rawValues {
currentQField := qFields[colIndex]
currentRField := rFields[colIndex]
tableCell := Cell[string]{
tableCell := Cell{
QField: currentQField,
RField: currentRField,
RawValue: rawValue,
Expand All @@ -61,15 +61,15 @@ func ParseCSVIntoTable(queryResult string, qFields []QField) (Table[string], err
qFieldToCells[currentQField][rowIndex] = tableCell
}

tableRow := Row[string]{
tableRow := Row{
QFieldToCells: qFieldToCell,
Cells: cells,
}

rows[rowIndex] = tableRow
}

return Table[string]{
return Table{
Rows: rows,
RFields: rFields,
QFieldToCells: qFieldToCells,
Expand Down
30 changes: 14 additions & 16 deletions internal/exporter/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import (
"github.com/utkuozdemir/nvidia_gpu_exporter/internal/exporter"
)

const (
testCsv = `
const testCsv = `
name, power.draw [W]
NVIDIA GeForce RTX 2080 SUPER, 30.14 W
Some Dummy GPU, 12.34 W
`
)

func TestParseCsvIntoTable(t *testing.T) {
t.Parallel()
Expand All @@ -26,25 +24,25 @@ func TestParseCsvIntoTable(t *testing.T) {
assert.Len(t, parsed.Rows, 2)
assert.Equal(t, []exporter.RField{"name", "power.draw [W]"}, parsed.RFields)

cell00 := exporter.Cell[string]{QField: "name", RField: "name", RawValue: "NVIDIA GeForce RTX 2080 SUPER"}
cell01 := exporter.Cell[string]{QField: "power.draw", RField: "power.draw [W]", RawValue: "30.14 W"}
cell10 := exporter.Cell[string]{QField: "name", RField: "name", RawValue: "Some Dummy GPU"}
cell11 := exporter.Cell[string]{QField: "power.draw", RField: "power.draw [W]", RawValue: "12.34 W"}
cell00 := exporter.Cell{QField: "name", RField: "name", RawValue: "NVIDIA GeForce RTX 2080 SUPER"}
cell01 := exporter.Cell{QField: "power.draw", RField: "power.draw [W]", RawValue: "30.14 W"}
cell10 := exporter.Cell{QField: "name", RField: "name", RawValue: "Some Dummy GPU"}
cell11 := exporter.Cell{QField: "power.draw", RField: "power.draw [W]", RawValue: "12.34 W"}

row0 := exporter.Row[string]{
QFieldToCells: map[exporter.QField]exporter.Cell[string]{"name": cell00, "power.draw": cell01},
Cells: []exporter.Cell[string]{cell00, cell01},
row0 := exporter.Row{
QFieldToCells: map[exporter.QField]exporter.Cell{"name": cell00, "power.draw": cell01},
Cells: []exporter.Cell{cell00, cell01},
}

row1 := exporter.Row[string]{
QFieldToCells: map[exporter.QField]exporter.Cell[string]{"name": cell10, "power.draw": cell11},
Cells: []exporter.Cell[string]{cell10, cell11},
row1 := exporter.Row{
QFieldToCells: map[exporter.QField]exporter.Cell{"name": cell10, "power.draw": cell11},
Cells: []exporter.Cell{cell10, cell11},
}

expected := exporter.Table[string]{
Rows: []exporter.Row[string]{row0, row1},
expected := exporter.Table{
Rows: []exporter.Row{row0, row1},
RFields: []exporter.RField{"name", "power.draw [W]"},
QFieldToCells: map[exporter.QField][]exporter.Cell[string]{
QFieldToCells: map[exporter.QField][]exporter.Cell{
"name": {cell00, cell10},
"power.draw": {cell01, cell11},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (e *GPUExporter) Collect(metricCh chan<- prometheus.Metric) {
}
}

func scrape(qFields []QField, nvidiaSmiCommand string, command runCmd) (int, *Table[string], error) {
func scrape(qFields []QField, nvidiaSmiCommand string, command runCmd) (int, *Table, error) {
qFieldsJoined := strings.Join(QFieldSliceToStringSlice(qFields), ",")

cmdAndArgs := strings.Fields(nvidiaSmiCommand)
Expand Down
9 changes: 3 additions & 6 deletions internal/exporter/exporter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package exporter_test

import (
_ "embed"
"fmt"
"os"
"os/exec"
Expand All @@ -13,15 +14,11 @@ import (
"github.com/stretchr/testify/require"

"github.com/utkuozdemir/nvidia_gpu_exporter/internal/exporter"

_ "embed"
)

const (
delta = 1e-9
)
const delta = 1e-9

//go:embed _query-test.txt
//go:embed testdata/query.txt
var queryTest string

func assertFloat(t *testing.T, expected, actual float64) {
Expand Down
5 changes: 2 additions & 3 deletions internal/exporter/fields_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package exporter_test

import (
_ "embed"
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/utkuozdemir/nvidia_gpu_exporter/internal/exporter"

_ "embed"
)

var (
//go:embed _fields-test.txt
//go:embed testdata/fields.txt
fieldsTest string

//nolint:gochecknoglobals
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 0a05a48

Please sign in to comment.