Skip to content

Commit

Permalink
Modularize the project and use golang project style
Browse files Browse the repository at this point in the history
Introduce a golang based project approach for the prometheus exporter.
Create a seperate cmd with cli parsing by cobra, a server instance with
logging using logrus.
  • Loading branch information
jelly committed May 13, 2020
1 parent 8ef5b9d commit fe94d56
Show file tree
Hide file tree
Showing 19 changed files with 1,254 additions and 166 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
prometheus-kopano-exporter
/vendor
/bin
/golint.txt
/govet.txt
/dist
/test/tests.*
/3rdparty-LICENSES.md
/.vscode
74 changes: 74 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
run:
modules-download-mode: vendor

linters-settings:
govet:
check-shadowing: true
settings:
printf:
funcs:
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
golint:
min-confidence: 0
gocyclo:
min-complexity: 10
maligned:
suggest-new: true
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
misspell:
locale: US
lll:
line-length: 140
goimports:
local-prefixes: stash.kopano.io/kc/kapi
gocritic:
enabled-tags:
- performance
- style
- experimental

linters:
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
disable-all: true
enable:
- bodyclose
- deadcode
- dupl
- errcheck
- funlen
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- golint
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- lll
- misspell
- nakedret
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck

# don't enable:
# - depguard - until https://github.com/OpenPeeDeeP/depguard/issues/7 gets fixed
# - maligned,prealloc
# - gochecknoglobals
4 changes: 4 additions & 0 deletions .license-ranger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"mode": "mod-vendor",
"header": "# Kopano Prometheus Exporter 3rd party licenses\n\nCopyright 2020 Kopano and its licensors. See LICENSE.txt for license information. This document contains a list of open source components used in this project.\n\n## Kopano Prometheus Exporter prometheus-kopano-exporter\n"
}
44 changes: 44 additions & 0 deletions Dockerfile.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# Copyright 2019 Kopano and its licensors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3 or
# later, as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

FROM golang:1.14.0-buster

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

ARG GOLANGCI_LINT_TAG=v1.23.8
RUN curl -sfL \
https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \
sh -s -- -b /usr/local/bin ${GOLANGCI_LINT_TAG}

RUN GOBIN=/usr/local/bin go get -v \
github.com/tebeka/go2xunit \
&& go clean -cache && rm -rf /root/go

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
build-essential \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /build

ENV GOCACHE=/tmp/go-build
ENV GOPATH=""
ENV HOME=/tmp

CMD ["make", "DATE=reproducible"]
51 changes: 51 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env groovy

pipeline {
agent {
dockerfile {
filename 'Dockerfile.build'
}
}
stages {
stage('Bootstrap') {
steps {
echo 'Bootstrapping..'
sh 'go version'
}
}
stage('Lint') {
steps {
echo 'Linting..'
sh 'make lint-checkstyle'
checkstyle pattern: 'test/tests.lint.xml', canComputeNew: false, unstableTotalHigh: '100'
}
}
stage('Vendor') {
steps {
echo 'Fetching vendor dependencies..'
sh 'make vendor'
}
}
stage('Build') {
steps {
echo 'Building..'
sh 'make DATE=reproducible'
sh './bin/promtheus-kopano-exporter version && sha256sum ./bin/prometheus-kopano-exporter'
}
}
stage('Dist') {
steps {
echo 'Dist..'
sh 'test -z "$(git diff --shortstat 2>/dev/null |tail -n1)" && echo "Clean check passed."'
sh 'make check'
sh 'make dist'
}
}
}
post {
always {
archiveArtifacts 'dist/*.tar.gz'
cleanWs()
}
}
}
13 changes: 13 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2020 Kopano and its licensors

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License, version 3,
as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
119 changes: 119 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
PACKAGE = stash.kopano.io/kgol/prometheus-kopano-exporter
PACKAGE_NAME = prometheus-kopano-exporter

# Tools

GO ?= go
GOFMT ?= gofmt
GOLINT ?= golangci-lint

# Cgo

CGO_ENABLED ?= 0

# Go modules

GO111MODULE ?= on

# Variables

export CGO_ENABLED GO111MODULE
unexport GOPATH

ARGS ?=
PWD := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
DATE ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
VERSION ?= $(shell git describe --tags --always --dirty --match=v* 2>/dev/null | sed 's/^v//' || \
cat $(CURDIR)/.version 2> /dev/null || echo 0.0.0-unreleased)
PKGS = $(or $(PKG),$(shell $(GO) list -mod=readonly ./... | grep -v "^$(PACKAGE)/vendor/"))
TESTPKGS = $(shell $(GO) list -mod=readonly -f '{{ if or .TestGoFiles .XTestGoFiles }}{{ .ImportPath }}{{ end }}' $(PKGS) 2>/dev/null)
CMDS = $(or $(CMD),$(addprefix cmd/,$(notdir $(shell find "$(PWD)/cmd/" -type d))))
TIMEOUT = 30

# Build

.PHONY: all
all: fmt | $(CMDS) $(PLUGINS)

plugins: fmt | $(PLUGINS)

.PHONY: $(CMDS)
$(CMDS): vendor ; $(info building $@ ...) @
CGO_ENABLED=$(CGO_ENABLED) $(GO) build \
-mod=vendor \
-trimpath \
-tags release \
-buildmode=exe \
-ldflags '-s -w -buildid=reproducible/$(VERSION) -X $(PACKAGE)/version.Version=$(VERSION) -X $(PACKAGE)/version.BuildDate=$(DATE) -extldflags -static' \
-o bin/$(notdir $@) ./$@

# Helpers

.PHONY: lint
lint: vendor ; $(info running $(GOLINT) ...) @
$(GOLINT) run

.PHONY: lint-checkstyle
lint-checkstyle: vendor ; $(info running $(GOLINT) checkstyle ...) @
@mkdir -p test
$(GOLINT) run --out-format checkstyle --issues-exit-code 0 > test/tests.lint.xml

.PHONY: fmt
fmt: ; $(info running gofmt ...) @
@ret=0 && for d in $$($(GO) list -mod=readonly -f '{{.Dir}}' ./... | grep -v /vendor/); do \
$(GOFMT) -l -w $$d/*.go || ret=$$? ; \
done ; exit $$ret

.PHONY: check
check: ; $(info checking dependencies ...) @
@$(GO) mod verify && echo OK

# Mod

go.sum: go.mod ; $(info updating dependencies ...)
@$(GO) mod tidy -v
@touch $@

.PHONY: vendor
vendor: go.sum ; $(info retrieving dependencies ...)
@$(GO) mod vendor -v
@touch $@

# Dist

.PHONY: licenses
licenses: vendor ; $(info building licenses files ...)
$(CURDIR)/scripts/go-license-ranger.py > $(CURDIR)/3rdparty-LICENSES.md

3rdparty-LICENSES.md: licenses

.PHONY: dist
dist: 3rdparty-LICENSES.md ; $(info building dist tarball ...)
@rm -rf "dist/${PACKAGE_NAME}-${VERSION}"
@mkdir -p "dist/${PACKAGE_NAME}-${VERSION}"
@mkdir -p "dist/${PACKAGE_NAME}-${VERSION}/scripts"
@cd dist && \
cp -avf ../LICENSE.txt "${PACKAGE_NAME}-${VERSION}" && \
cp -avf ../README.md "${PACKAGE_NAME}-${VERSION}" && \
cp -avf ../3rdparty-LICENSES.md "${PACKAGE_NAME}-${VERSION}" && \
cp -avf ../bin/* "${PACKAGE_NAME}-${VERSION}" && \
cp -avf ../scripts/prometheus-kopano-exporter.binscript "${PACKAGE_NAME}-${VERSION}/scripts" && \
cp -avf ../scripts/prometheus-kopano-exporter.service "${PACKAGE_NAME}-${VERSION}/scripts" && \
cp -avf ../scripts/prometheus-kopano-exporter.cfg "${PACKAGE_NAME}-${VERSION}/scripts" && \
tar --owner=0 --group=0 -czvf ${PACKAGE_NAME}-${VERSION}.tar.gz "${PACKAGE_NAME}-${VERSION}" && \
cd ..

.PHONE: changelog
changelog: ; $(info updating changelog ...)
$(CHGLOG) --output CHANGELOG.md $(ARGS)

# Rest

.PHONY: clean
clean: ; $(info cleaning ...) @
@rm -rf bin
@rm -rf test/test.*

.PHONY: version
version:
@echo $(VERSION)
27 changes: 27 additions & 0 deletions cmd/prometheus-kopano-exporter/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
* Copyright 2020 Kopano and its licensors
*/

package main

import (
"os"

"github.com/sirupsen/logrus"
)

func newLogger(disableTimestamp bool, logLevelString string) (logrus.FieldLogger, error) {
logLevel, err := logrus.ParseLevel(logLevelString)
if err != nil {
return nil, err
}

return &logrus.Logger{
Out: os.Stderr,
Formatter: &logrus.TextFormatter{
DisableTimestamp: disableTimestamp,
},
Level: logLevel,
}, nil
}
5 changes: 3 additions & 2 deletions main.go → cmd/prometheus-kopano-exporter/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
* Copyright 2019 Kopano and its licensors
* Copyright 2020 Kopano and its licensors
*/

package main
Expand All @@ -10,5 +10,6 @@ import (
)

func main() {
cmd.Execute()
cmd.RootCmd.AddCommand(commandServe())
cmd.RootCmd.Execute()
}
Loading

0 comments on commit fe94d56

Please sign in to comment.