From 7d8ac22bb17fed78f15adf8b6de1bf246ef28892 Mon Sep 17 00:00:00 2001 From: Andre Kully Date: Thu, 23 Dec 2021 16:46:18 +0100 Subject: [PATCH] use stringer to generate enum type --- Makefile | 18 +++++++++++++----- main.go | 27 ++++++++++++++++++++------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 365589e..3d3d494 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ ## # .DEFAULT_GOAL := help +.PHONY: generate version := $(shell git describe --tags --always) revision := $(shell git rev-parse HEAD) @@ -18,17 +19,24 @@ LDFLAGS := -w -s \ -X $(versionPkgPrefix).BuildDate=${builddate} GOFLAGS := -v -linux: ## builds the linux version of the exporter +linux: generate ## builds the linux version of the exporter GOOS=linux GOARCH=amd64 go build $(GOFLAGS) -ldflags '$(LDFLAGS)' -mac: ## builds the macos version of the exporter +mac: generate ## builds the macos version of the exporter GOOS=darwin GOARCH=amd64 go build $(GOFLAGS) -ldflags '$(LDFLAGS)' -mac-arm: ## builds the macos (m1) version of the exporter +mac-arm: generate ## builds the macos (m1) version of the exporter GOOS=darwin GOARCH=arm64 go build $(GOFLAGS) -ldflags '$(LDFLAGS)' -arm64: +arm64: generate GOOS=linux GOARCH=arm64 go build $(GOFLAGS) -ldflags '$(LDFLAGS)' -arm: +arm: generate GOOS=linux GOARCH=arm go build $(GOFLAGS) -ldflags '$(LDFLAGS)' +# -- see more info on https://pkg.go.dev/golang.org/x/tools/cmd/stringer +generate: $(GOPATH)/bin/stringer + go generate ./... + +$(GOPATH)/bin/stringer: + go install golang.org/x/tools/cmd/stringer@latest + # -- help: @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' diff --git a/main.go b/main.go index fd58ca8..dc8ae82 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,4 @@ +//go:generate stringer -type MystromReqStatus main.go package main import ( @@ -16,14 +17,15 @@ import ( "mystrom-exporter/pkg/version" ) -type switchReport struct { - Power float64 `json:"power"` - WattPerSec float64 `json:"Ws"` - Relay bool `json:"relay"` - Temperature float64 `json:"temperature"` -} +// -- MystromRequestStatusType represents the request to MyStrom device status +type MystromReqStatus uint32 -const namespace = "mystrom" +const ( + OK MystromReqStatus = iota + ERROR_SOCKET + ERROR_TIMEOUT + ERROR_PARSING_VALUE +) var ( listenAddress = flag.String("web.listen-address", ":9452", @@ -66,6 +68,12 @@ func main() { }, []string{"target"}) telemetryRegistry.MustRegister(mystromDurationCounterVec) + mystromRequestsCounterVec = prometheus.NewCounterVec(prometheus.CounterOpts{ + Name: "mystrom_requests_total", + Help: "Number of mystrom request by status and target", + }, []string{"target", "status"}) + telemetryRegistry.MustRegister(mystromRequestsCounterVec) + // -- make the build information is available through a metric buildInfo := prometheus.NewGaugeVec( prometheus.GaugeOpts{ @@ -115,7 +123,11 @@ func scrapeHandler(e *mystrom.Exporter, w http.ResponseWriter, r *http.Request) duration := time.Since(start).Seconds() if err != nil { if strings.Contains(fmt.Sprintf("%v", err), "unable to connect with target") { + mystromRequestsCounterVec.WithLabelValues(target, ERROR_SOCKET.String()).Inc() } else if strings.Contains(fmt.Sprintf("%v", err), "i/o timeout") { + mystromRequestsCounterVec.WithLabelValues(target, ERROR_TIMEOUT.String()).Inc() + } else { + mystromRequestsCounterVec.WithLabelValues(target, ERROR_PARSING_VALUE.String()).Inc() } http.Error( w, @@ -126,6 +138,7 @@ func scrapeHandler(e *mystrom.Exporter, w http.ResponseWriter, r *http.Request) return } mystromDurationCounterVec.WithLabelValues(target).Add(duration) + mystromRequestsCounterVec.WithLabelValues(target, OK.String()).Inc() promhttp.HandlerFor(gatherer, promhttp.HandlerOpts{}).ServeHTTP(w, r)