Skip to content

Commit 0a05a48

Browse files
committed
chore: tidy things up
Signed-off-by: Utku Ozdemir <[email protected]>
1 parent e7ececf commit 0a05a48

File tree

13 files changed

+69
-68
lines changed

13 files changed

+69
-68
lines changed

.golangci.yml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22
linters:
33
enable-all: true
44
disable:
5-
# breaking things: https://github.com/atc0005/go-ci/issues/1024
6-
- depguard
75
# not useful
8-
- exhaustivestruct
96
- exhaustruct
10-
- goerr113
7+
- err113
118

129
linters-settings:
1310
gci:
1411
sections:
1512
- standard
1613
- default
17-
- prefix(github.com/utkuozdemir/nvidia_gpu_exporter)
18-
- blank
19-
- dot
20-
- alias
14+
- localmodule
2115
goimports:
2216
local-prefixes: github.com/utkuozdemir/nvidia_gpu_exporter
2317
goconst:
2418
ignore-tests: true
19+
depguard:
20+
rules:
21+
main:
22+
list-mode: lax
23+
files:
24+
- $all
25+
deny:
26+
- pkg: io/ioutil
27+
desc: "replaced by io and os packages since Go 1.16: https://tip.golang.org/doc/go1.16#ioutil"

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM ubuntu:22.04
1+
FROM ubuntu:24.04
22

33
COPY nvidia_gpu_exporter /usr/bin/nvidia_gpu_exporter
44

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ See [METRICS.md](METRICS.md) for details.
6363

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

66-
# Star History
66+
## Star History
6767

68+
<!-- markdownlint-disable no-inline-html -->
6869
<a href="https://star-history.com/#utkuozdemir/nvidia_gpu_exporter&Date">
6970
<picture>
7071
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=utkuozdemir/nvidia_gpu_exporter&type=Date&theme=dark" />
7172
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=utkuozdemir/nvidia_gpu_exporter&type=Date" />
7273
<img alt="Star History Chart" src="https://api.star-history.com/svg?repos=utkuozdemir/nvidia_gpu_exporter&type=Date" />
7374
</picture>
7475
</a>
76+
<!-- markdownlint-enable no-inline-html -->

cmd/nvidia_gpu_exporter/main.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,25 @@ import (
1212
"github.com/go-kit/log"
1313
"github.com/go-kit/log/level"
1414
"github.com/prometheus/client_golang/prometheus"
15+
clientversion "github.com/prometheus/client_golang/prometheus/collectors/version"
1516
"github.com/prometheus/client_golang/prometheus/promhttp"
1617
"github.com/prometheus/common/promlog"
1718
"github.com/prometheus/common/promlog/flag"
1819
"github.com/prometheus/common/version"
1920
"github.com/prometheus/exporter-toolkit/web"
21+
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
2022

2123
"github.com/utkuozdemir/nvidia_gpu_exporter/internal/exporter"
22-
23-
clientversion "github.com/prometheus/client_golang/prometheus/collectors/version"
24-
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
2524
)
2625

27-
const (
28-
redirectPageTemplate = `<html lang="en">
26+
const redirectPageTemplate = `<html lang="en">
2927
<head><title>Nvidia GPU Exporter</title></head>
3028
<body>
3129
<h1>Nvidia GPU Exporter</h1>
3230
<p><a href="%s">Metrics</a></p>
3331
</body>
3432
</html>
3533
`
36-
)
3734

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

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

@@ -157,7 +154,7 @@ func listenAndServe(server *http.Server, flags *web.FlagConfig, network string,
157154
defer func() {
158155
for _, listener := range listeners {
159156
if err := listener.Close(); err != nil {
160-
level.Error(logger).Log("msg", "Error closing listener", "err", err) //nolint:errcheck
157+
retErr = errors.Join(retErr, fmt.Errorf("failed to close listener: %w", err))
161158
}
162159
}
163160
}()

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/utkuozdemir/nvidia_gpu_exporter
22

3-
go 1.22
3+
go 1.22.4
44

55
require (
66
github.com/alecthomas/kingpin/v2 v2.4.0
@@ -14,7 +14,7 @@ require (
1414
)
1515

1616
require (
17-
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
17+
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 // indirect
1818
github.com/beorn7/perks v1.0.1 // indirect
1919
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2020
github.com/davecgh/go-spew v1.1.1 // indirect

go.sum

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/alecthomas/kingpin/v2 v2.4.0 h1:f48lwail6p8zpO1bC4TxtqACaGqHYA22qkHjHpqDjYY=
22
github.com/alecthomas/kingpin/v2 v2.4.0/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE=
3-
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 h1:ez/4by2iGztzR4L0zgAOR8lTQK9VlyBVVd7G4omaOQs=
4-
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
3+
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 h1:t3eaIm0rUkzbrIewtiFmMK5RXHej2XnoXNhxVsAYUfg=
4+
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30/go.mod h1:fvzegU4vN3H1qMT+8wDmzjAcDONcgo2/SZ/TyfdUOFs=
55
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
66
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
77
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
@@ -43,7 +43,12 @@ github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoG
4343
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
4444
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
4545
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
46-
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
46+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
47+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
48+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
49+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
50+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
51+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
4752
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
4853
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
4954
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
@@ -67,8 +72,8 @@ google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWn
6772
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6873
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
6974
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
70-
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7175
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
7276
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
77+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7378
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
7479
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/exporter/csv.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ import (
55
"strings"
66
)
77

8-
type Table[T any] struct {
9-
Rows []Row[T]
8+
type Table struct {
9+
Rows []Row
1010
RFields []RField
11-
QFieldToCells map[QField][]Cell[T]
11+
QFieldToCells map[QField][]Cell
1212
}
1313

14-
type Row[T any] struct {
15-
QFieldToCells map[QField]Cell[T]
16-
Cells []Cell[T]
14+
type Row struct {
15+
QFieldToCells map[QField]Cell
16+
Cells []Cell
1717
}
1818

19-
type Cell[T any] struct {
19+
type Cell struct {
2020
QField QField
2121
RField RField
22-
RawValue T
22+
RawValue string
2323
}
2424

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

34-
rows := make([]Row[string], numRows)
34+
rows := make([]Row, numRows)
3535

36-
qFieldToCells := make(map[QField][]Cell[string])
36+
qFieldToCells := make(map[QField][]Cell)
3737
for _, q := range qFields {
38-
qFieldToCells[q] = make([]Cell[string], numRows)
38+
qFieldToCells[q] = make([]Cell, numRows)
3939
}
4040

4141
for rowIndex, valuesLine := range valuesLines {
42-
qFieldToCell := make(map[QField]Cell[string], numCols)
43-
cells := make([]Cell[string], numCols)
42+
qFieldToCell := make(map[QField]Cell, numCols)
43+
cells := make([]Cell, numCols)
4444
rawValues := parseCSVLine(valuesLine)
4545

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

5151
for colIndex, rawValue := range rawValues {
5252
currentQField := qFields[colIndex]
5353
currentRField := rFields[colIndex]
54-
tableCell := Cell[string]{
54+
tableCell := Cell{
5555
QField: currentQField,
5656
RField: currentRField,
5757
RawValue: rawValue,
@@ -61,15 +61,15 @@ func ParseCSVIntoTable(queryResult string, qFields []QField) (Table[string], err
6161
qFieldToCells[currentQField][rowIndex] = tableCell
6262
}
6363

64-
tableRow := Row[string]{
64+
tableRow := Row{
6565
QFieldToCells: qFieldToCell,
6666
Cells: cells,
6767
}
6868

6969
rows[rowIndex] = tableRow
7070
}
7171

72-
return Table[string]{
72+
return Table{
7373
Rows: rows,
7474
RFields: rFields,
7575
QFieldToCells: qFieldToCells,

internal/exporter/csv_test.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import (
99
"github.com/utkuozdemir/nvidia_gpu_exporter/internal/exporter"
1010
)
1111

12-
const (
13-
testCsv = `
12+
const testCsv = `
1413
name, power.draw [W]
1514
NVIDIA GeForce RTX 2080 SUPER, 30.14 W
1615
Some Dummy GPU, 12.34 W
1716
`
18-
)
1917

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

29-
cell00 := exporter.Cell[string]{QField: "name", RField: "name", RawValue: "NVIDIA GeForce RTX 2080 SUPER"}
30-
cell01 := exporter.Cell[string]{QField: "power.draw", RField: "power.draw [W]", RawValue: "30.14 W"}
31-
cell10 := exporter.Cell[string]{QField: "name", RField: "name", RawValue: "Some Dummy GPU"}
32-
cell11 := exporter.Cell[string]{QField: "power.draw", RField: "power.draw [W]", RawValue: "12.34 W"}
27+
cell00 := exporter.Cell{QField: "name", RField: "name", RawValue: "NVIDIA GeForce RTX 2080 SUPER"}
28+
cell01 := exporter.Cell{QField: "power.draw", RField: "power.draw [W]", RawValue: "30.14 W"}
29+
cell10 := exporter.Cell{QField: "name", RField: "name", RawValue: "Some Dummy GPU"}
30+
cell11 := exporter.Cell{QField: "power.draw", RField: "power.draw [W]", RawValue: "12.34 W"}
3331

34-
row0 := exporter.Row[string]{
35-
QFieldToCells: map[exporter.QField]exporter.Cell[string]{"name": cell00, "power.draw": cell01},
36-
Cells: []exporter.Cell[string]{cell00, cell01},
32+
row0 := exporter.Row{
33+
QFieldToCells: map[exporter.QField]exporter.Cell{"name": cell00, "power.draw": cell01},
34+
Cells: []exporter.Cell{cell00, cell01},
3735
}
3836

39-
row1 := exporter.Row[string]{
40-
QFieldToCells: map[exporter.QField]exporter.Cell[string]{"name": cell10, "power.draw": cell11},
41-
Cells: []exporter.Cell[string]{cell10, cell11},
37+
row1 := exporter.Row{
38+
QFieldToCells: map[exporter.QField]exporter.Cell{"name": cell10, "power.draw": cell11},
39+
Cells: []exporter.Cell{cell10, cell11},
4240
}
4341

44-
expected := exporter.Table[string]{
45-
Rows: []exporter.Row[string]{row0, row1},
42+
expected := exporter.Table{
43+
Rows: []exporter.Row{row0, row1},
4644
RFields: []exporter.RField{"name", "power.draw [W]"},
47-
QFieldToCells: map[exporter.QField][]exporter.Cell[string]{
45+
QFieldToCells: map[exporter.QField][]exporter.Cell{
4846
"name": {cell00, cell10},
4947
"power.draw": {cell01, cell11},
5048
},

internal/exporter/exporter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (e *GPUExporter) Collect(metricCh chan<- prometheus.Metric) {
214214
}
215215
}
216216

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

220220
cmdAndArgs := strings.Fields(nvidiaSmiCommand)

internal/exporter/exporter_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package exporter_test
22

33
import (
4+
_ "embed"
45
"fmt"
56
"os"
67
"os/exec"
@@ -13,15 +14,11 @@ import (
1314
"github.com/stretchr/testify/require"
1415

1516
"github.com/utkuozdemir/nvidia_gpu_exporter/internal/exporter"
16-
17-
_ "embed"
1817
)
1918

20-
const (
21-
delta = 1e-9
22-
)
19+
const delta = 1e-9
2320

24-
//go:embed _query-test.txt
21+
//go:embed testdata/query.txt
2522
var queryTest string
2623

2724
func assertFloat(t *testing.T, expected, actual float64) {

internal/exporter/fields_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package exporter_test
22

33
import (
4+
_ "embed"
45
"os/exec"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
910

1011
"github.com/utkuozdemir/nvidia_gpu_exporter/internal/exporter"
11-
12-
_ "embed"
1312
)
1413

1514
var (
16-
//go:embed _fields-test.txt
15+
//go:embed testdata/fields.txt
1716
fieldsTest string
1817

1918
//nolint:gochecknoglobals

0 commit comments

Comments
 (0)