use stringer to generate enum type
This commit is contained in:
parent
e2639a5638
commit
7d8ac22bb1
2 changed files with 33 additions and 12 deletions
18
Makefile
18
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}'
|
||||
|
|
27
main.go
27
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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue