From 63401341a1882611701b6826d628123b3e3a37c9 Mon Sep 17 00:00:00 2001 From: Amir Mofasser Date: Thu, 7 Mar 2019 14:57:47 +0100 Subject: [PATCH] Satisfy golint (#3) --- Makefile | 27 ++++++++------------------- cmd/prometheus-cert-exporter/main.go | 17 ++++++++++++----- pkg/exporter/exporter.go | 11 ++++++++++- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index c596b1e..c03cb81 100644 --- a/Makefile +++ b/Makefile @@ -17,9 +17,8 @@ LDFLAGS = -ldflags "-X main.VERSION=${VERSION} -X main.COMMIT=${COMMIT} -X main. # Build the project all: build -test: - cd ${BUILD_DIR}; \ - go test ${PKG_LIST}; \ +dep: + go get -v -d ./cmd/prometheus-cert-exporter/... ; fmt: cd ${BUILD_DIR}; \ @@ -49,8 +48,11 @@ misspell: cd ${BUILD_DIR}; \ find . -type f -not -path "./vendor/*" -not -path "./.git/*" -print0 | xargs -0 ${GOPATH}/bin/misspell; \ -dep: - go get -v -d ./cmd/prometheus-cert-exporter/... ; +ci: fmt vet gocyclo golint ineffassign misspell + +test: dep + cd ${BUILD_DIR}; \ + go test ${PKG_LIST}; \ linux: dep CGO_ENABLED=0 GOOS=linux GOARCH=${GOARCH} go build ${LDFLAGS} -o ${BUILD_DIR}/out/${BINARY}-linux-${GOARCH} cmd/prometheus-cert-exporter/main.go @@ -64,20 +66,7 @@ darwin: dep windows: dep CGO_ENABLED=0 GOOS=windows GOARCH=${GOARCH} go build ${LDFLAGS} -o ${BUILD_DIR}/out/${BINARY}-windows-${GOARCH}.exe cmd/prometheus-cert-exporter/main.go -docker_build: - docker run --rm -v "${PWD}":/go/src/github.com/amimof/prometheus-cert-exporter -w /go/src/github.com/amimof/prometheus-cert-exporter golang:${GOVERSION} make fmt test - docker build -t amimof/prometheus-cert-exporter:${VERSION} . - docker tag amimof/prometheus-cert-exporter:${VERSION} amimof/prometheus-cert-exporter:latest - -docker_push: - docker push amimof/prometheus-cert-exporter:${VERSION} - docker push amimof/prometheus-cert-exporter:latest - -docker: docker_build docker_push - build: linux darwin rpi windows clean: - -rm -rf ${BUILD_DIR}/out/ - -.PHONY: linux darwin windows test fmt clean \ No newline at end of file + rm -rf ${BUILD_DIR}/out/ \ No newline at end of file diff --git a/cmd/prometheus-cert-exporter/main.go b/cmd/prometheus-cert-exporter/main.go index ac3c7b7..ba277ae 100644 --- a/cmd/prometheus-cert-exporter/main.go +++ b/cmd/prometheus-cert-exporter/main.go @@ -12,12 +12,19 @@ import ( "net/http" ) -var ( - VERSION string - COMMIT string - BRANCH string - GOVERSION string +// VERSION is generated during compile as is never to be set here +var VERSION string + +// COMMIT is the Git commit hash and is generated during compile as is never to be set here +var COMMIT string + +// BRANCH is the Git branch name and is generated during compile as is never to be set here +var BRANCH string +// GOVERSION is the Go version used to compile and is generated during compile as is never to be set here +var GOVERSION string + +var ( host string port int listen string diff --git a/pkg/exporter/exporter.go b/pkg/exporter/exporter.go index 9bfc92a..eb4a42c 100644 --- a/pkg/exporter/exporter.go +++ b/pkg/exporter/exporter.go @@ -12,7 +12,7 @@ import ( "sync" ) -var extensions []string = []string{".pem", "crt", "cert", "cer"} +var extensions = []string{".pem", "crt", "cert", "cer"} func findCertPaths(p string) ([]string, error) { paths := []string{} @@ -40,26 +40,34 @@ func isCertFile(p string) bool { return false } +// Exporter implements prometheus.Collector interface type Exporter struct { mux sync.Mutex roots []string certExpiry *prometheus.GaugeVec } +// SetRoots sets the list of file paths that the exporter should search for certificates in func (e *Exporter) SetRoots(p []string) { e.roots = p } +// Collect satisfies prometheus.Collector interface func (e *Exporter) Collect(ch chan<- prometheus.Metric) { e.mux.Lock() defer e.mux.Unlock() e.Scrape(ch) } +// Describe satisfies prometheus.Collector interface func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { ch <- e.certExpiry.WithLabelValues("path", "issuer", "alg", "version", "subject", "dns_names", "email_addresses").Desc() } +// Scrape iterates over the list of file paths (set by SetRoot) and parses any found x509 certificates. +// Certificates are parsed and the fields are mapped to prometheus labels which attached to a Gauge. +// Scrape will create a new time series for each certificate file with its associated labels. The value +// of the series equals the expiry of the certificate in UNIX timestamp. func (e *Exporter) Scrape(ch chan<- prometheus.Metric) { for _, root := range e.roots { paths, err := findCertPaths(root) @@ -103,6 +111,7 @@ func (e *Exporter) Scrape(ch chan<- prometheus.Metric) { } +// New creates an instance of Exporter and returns it func New() *Exporter { return &Exporter{ certExpiry: prometheus.NewGaugeVec(prometheus.GaugeOpts{