Skip to content

Commit

Permalink
feat: go1.18 & improved error messages (#69)
Browse files Browse the repository at this point in the history
* feat: improve logs of cmd outputs

Signed-off-by: Utku Ozdemir <[email protected]>

* refactor: workflows

Signed-off-by: Utku Ozdemir <[email protected]>

* refactor: enable linters

Signed-off-by: Utku Ozdemir <[email protected]>

* ci: remove gofumpt since it is checked via golangci-lint & remove deprecated config on golangci-lint action

Signed-off-by: Utku Ozdemir <[email protected]>
  • Loading branch information
utkuozdemir authored Apr 6, 2022
1 parent 5b48550 commit f04728f
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 117 deletions.
26 changes: 7 additions & 19 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
name: build

on:
push:
branches:
Expand All @@ -22,21 +23,13 @@ on:
- "grafana/**"
- "systemd/**"
- "renovate.json"

concurrency:
group: build-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
pre_job:
continue-on-error: true
runs-on: ubuntu-20.04
outputs:
should_skip: ${{ steps.skip_check.outputs.should_skip }}
steps:
- id: skip_check
uses: fkirc/[email protected]
with:
cancel_others: true
concurrent_skipping: same_content
build:
needs: pre_job
if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
runs-on: ubuntu-20.04
steps:
- name: Checkout
Expand All @@ -45,19 +38,14 @@ jobs:
uses: actions/[email protected]
with:
# renovate: go
go-version: 1.17.5
go-version: 1.18
- name: Ensure go.mod is already tidied
run: go mod tidy && git diff --no-patch --exit-code
- name: Install gofumpt
run: go install mvdan.cc/[email protected]
- name: Ensure code is properly formatted using gofumpt
run: test -z "$(gofumpt -d .)"
- name: Run linters
uses: golangci/[email protected]
with:
# renovate: golangci-lint
version: v1.45.2
skip-go-installation: true
args: --timeout=3m0s
- name: Install go-acc
# renovate: go-acc
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: release
on:
push:
tags:
- 'v*.*.*'
- "v*.*.*"
jobs:
release:
runs-on: ubuntu-20.04
Expand All @@ -18,7 +18,7 @@ jobs:
uses: actions/[email protected]
with:
# renovate: go
go-version: 1.17.5
go-version: 1.18
- name: Login to DockerHub
uses: docker/[email protected]
with:
Expand Down
75 changes: 75 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# https://golangci-lint.run/usage/linters/
linters:
enable:
- asciicheck
- bidichk
- containedctx
- contextcheck
- cyclop
- decorder
- dogsled
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- forbidigo
- forcetypeassert
- funlen
- gci
# - gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- goerr113
- gofumpt
- goheader
# - goimports # inconsistent with gci
- gomnd
- gomoddirectives
# - gomodguard # no need
- goprintffuncname
- gosec
- grouper
- ifshort
- importas
- ireturn
- lll
- maintidx
- makezero
- misspell
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- paralleltest
- prealloc
- predeclared
- revive
- rowserrcheck
# - sqlclosecheck
- stylecheck
- tagliatelle
- tenv
# - testpackage # TODO: visit
- thelper
- tparallel
- unconvert
- unparam
- varnamelen
- wastedassign
- whitespace
- wrapcheck
- wsl

linters-settings:
goconst:
ignore-tests: true
10 changes: 6 additions & 4 deletions cmd/nvidia_gpu_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ func main() {
kingpin.Parse()

logger := promlog.New(promlogConfig)
e, err := exporter.New(exporter.DefaultPrefix, *nvidiaSmiCommand, *qFields, logger)

exp, err := exporter.New(exporter.DefaultPrefix, *nvidiaSmiCommand, *qFields, logger)
if err != nil {
_ = level.Error(logger).Log("msg", "Error on creating exporter", "err", err)

os.Exit(1)
}

prometheus.MustRegister(e)
prometheus.MustRegister(exp)
prometheus.MustRegister(version.NewCollector("nvidia_gpu_exporter"))

_ = level.Info(logger).Log("msg", "Listening on address", "address", listenAddress)
Expand All @@ -72,6 +74,7 @@ func main() {
srv := &http.Server{Addr: *listenAddress}
if err := web.ListenAndServe(srv, *webConfig, logger); err != nil {
_ = level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)

os.Exit(1)
}
}
Expand All @@ -89,8 +92,7 @@ func NewRootHandler(logger log.Logger, metricsPath string) *RootHandler {
}

func (r *RootHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write(r.response)
if err != nil {
if _, err := w.Write(r.response); err != nil {
_ = level.Error(r.logger).Log("msg", "Error writing redirect", "err", err)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/utkuozdemir/nvidia_gpu_exporter

// renovate: go
go 1.17
go 1.18

require (
github.com/go-kit/log v0.2.0
Expand Down
29 changes: 17 additions & 12 deletions internal/exporter/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type cell struct {
rawValue string
}

var ErrFieldCountMismatch = fmt.Errorf("field count mismatch")

func parseCSVIntoTable(queryResult string, qFields []qField) (table, error) {
lines := strings.Split(strings.TrimSpace(queryResult), "\n")
titlesLine := lines[0]
Expand All @@ -42,30 +44,31 @@ func parseCSVIntoTable(queryResult string, qFields []qField) (table, error) {
qFieldToCell := make(map[qField]cell, numCols)
cells := make([]cell, numCols)
rawValues := parseCSVLine(valuesLine)

if len(qFields) != len(rFields) {
return table{}, fmt.Errorf("query fields (%d) and returned fields (%d) have different sizes", len(qFields), len(rFields))
return table{}, fmt.Errorf("%w: query fields: %d, returned fields: %d",
ErrFieldCountMismatch, len(qFields), len(rFields))
}

for colIndex, rawValue := range rawValues {
q := qFields[colIndex]
r := rFields[colIndex]
gm := cell{
qField: q,
rField: r,
currentQField := qFields[colIndex]
currentRField := rFields[colIndex]
tableCell := cell{
qField: currentQField,
rField: currentRField,
rawValue: rawValue,
}
qFieldToCell[q] = gm
cells[colIndex] = gm
qFieldToCells[q][rowIndex] = gm
qFieldToCell[currentQField] = tableCell
cells[colIndex] = tableCell
qFieldToCells[currentQField][rowIndex] = tableCell
}

gmc := row{
tableRow := row{
qFieldToCells: qFieldToCell,
cells: cells,
}

rows[rowIndex] = gmc

rows[rowIndex] = tableRow
}

return table{
Expand All @@ -78,8 +81,10 @@ func parseCSVIntoTable(queryResult string, qFields []qField) (table, error) {
func parseCSVLine(line string) []string {
values := strings.Split(line, ",")
result := make([]string, len(values))

for i, field := range values {
result[i] = strings.TrimSpace(field)
}

return result
}
3 changes: 3 additions & 0 deletions internal/exporter/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ Some Dummy GPU, 12.34 W
)

func TestParseCsvIntoTable(t *testing.T) {
t.Parallel()

parsed, err := parseCSVIntoTable(testCsv, []qField{"name", "power.draw"})

assert.NoError(t, err)
assert.Len(t, parsed.rows, 2)
assert.Equal(t, []rField{"name", "power.draw [W]"}, parsed.rFields)
Expand Down
Loading

0 comments on commit f04728f

Please sign in to comment.