Skip to content

Commit

Permalink
Satisfy golint (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir Mofasser authored Mar 7, 2019
1 parent 5f0e503 commit 6340134
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
27 changes: 8 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}; \
Expand Down Expand Up @@ -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
Expand All @@ -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
rm -rf ${BUILD_DIR}/out/
17 changes: 12 additions & 5 deletions cmd/prometheus-cert-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion pkg/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 6340134

Please sign in to comment.