Skip to content
Snippets Groups Projects
Unverified Commit 43072dda authored by Will Rouesnel's avatar Will Rouesnel
Browse files

Improve support code quality.

parent 456ac724
No related branches found
No related tags found
No related merge requests found
COVERDIR = .coverage
TOOLDIR = tools
BINDIR = bin
RELEASEDIR = release
DIRS = $(BINDIR) $(RELEASEDIR)
GO_SRC := $(shell find . -name '*.go' ! -path '*/vendor/*' ! -path 'tools/*' ! -path 'bin/*' ! -path 'release/*' )
GO_DIRS := $(shell find . -type d -name '*.go' ! -path '*/vendor/*' ! -path 'tools/*' ! -path 'bin/*' ! -path 'release/*' )
GO_PKGS := $(shell go list ./... | grep -v '/vendor/')
CONTAINER_NAME ?= wrouesnel/postgres_exporter:latest
BINARY := $(shell basename $(shell pwd))
VERSION ?= $(shell git describe --dirty 2>/dev/null)
VERSION_SHORT ?= $(shell git describe --abbrev=0 2>/dev/null)
ifeq ($(VERSION),)
VERSION := v0.0.0
endif
ifeq ($(VERSION_SHORT),)
VERSION_SHORT := v0.0.0
endif
# By default this list is filtered down to some common platforms.
platforms := $(subst /,-,$(shell go tool dist list | grep -e linux -e windows -e darwin | grep -e 386 -e amd64))
PLATFORM_BINS_TMP := $(patsubst %,$(BINDIR)/$(BINARY)_$(VERSION_SHORT)_%/$(BINARY),$(platforms))
PLATFORM_BINS := $(patsubst $(BINDIR)/$(BINARY)_$(VERSION_SHORT)_windows-%/$(BINARY),$(BINDIR)/$(BINARY)_$(VERSION_SHORT)_windows-%/$(BINARY).exe,$(PLATFORM_BINS_TMP))
PLATFORM_DIRS := $(patsubst %,$(BINDIR)/$(BINARY)_$(VERSION_SHORT)_%,$(platforms))
PLATFORM_TARS := $(patsubst %,$(RELEASEDIR)/$(BINARY)_$(VERSION_SHORT)_%.tar.gz,$(platforms))
# These are evaluated on use, and so will have the correct values in the build
# rule (https://vic.demuzere.be/articles/golang-makefile-crosscompile/)
PLATFORMS_TEMP = $(subst /, ,$(subst -, ,$(patsubst $(BINDIR)/$(BINARY)_$(VERSION_SHORT)_%,%,$@)))
GOOS = $(word 1, $(PLATFORMS_TEMP))
GOARCH = $(word 2, $(PLATFORMS_TEMP))
CURRENT_PLATFORM_TMP := $(BINDIR)/$(BINARY)_$(VERSION_SHORT)_$(shell go env GOOS)-$(shell go env GOARCH)/$(BINARY)
CURRENT_PLATFORM := $(patsubst $(BINDIR)/$(BINARY)_$(VERSION_SHORT)_windows-%/$(BINARY),$(BINDIR)/$(BINARY)_$(VERSION_SHORT)_windows-%/$(BINARY).exe,$(CURRENT_PLATFORM_TMP))
CONCURRENT_LINTERS ?=
ifeq ($(CONCURRENT_LINTERS),)
CONCURRENT_LINTERS = $(shell gometalinter --help | grep -o 'concurrency=\w*' | cut -d= -f2 | cut -d' ' -f1)
endif
LINTER_DEADLINE ?= 30s
$(shell mkdir -p $(DIRS))
export PATH := $(TOOLDIR)/bin:$(PATH)
SHELL := env PATH=$(PATH) /bin/bash
all: style lint test binary
binary: $(BINARY)
$(BINARY): $(CURRENT_PLATFORM)
ln -sf $< $@
$(PLATFORM_BINS): $(GO_SRC)
CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -a \
-ldflags "-extldflags '-static' -X main.Version=$(VERSION)" \
-o $@ .
$(PLATFORM_DIRS): $(PLATFORM_BINS)
$(PLATFORM_TARS): $(RELEASEDIR)/%.tar.gz : $(BINDIR)/%
tar -czf $@ -C $(BINDIR) $$(basename $<)
release-bin: $(PLATFORM_BINS)
release: $(PLATFORM_TARS)
# Take a go build and turn it into a minimal container
docker: $(CURRENT_PLATFORM)
docker build --build-arg=binary=$(CURRENT_PLATFORM) -t $(CONTAINER_NAME) .
style: tools
gometalinter --disable-all --enable=gofmt --vendor
lint: tools
@echo Using $(CONCURRENT_LINTERS) processes
gometalinter -j $(CONCURRENT_LINTERS) --deadline=$(LINTER_DEADLINE) --disable=gotype --disable=gocyclo $(GO_DIRS)
fmt: tools
gofmt -s -w $(GO_SRC)
postgres_exporter_integration_test: $(GO_SRC)
CGO_ENABLED=0 go test -c -tags integration \
-a -ldflags "-extldflags '-static' -X main.Version=$(VERSION)" \
-o postgres_exporter_integration_test -cover -covermode count .
test: tools
@mkdir -p $(COVERDIR)
@rm -f $(COVERDIR)/*
for pkg in $(GO_PKGS) ; do \
go test -v -covermode count -coverprofile=$(COVERDIR)/$$(echo $$pkg | tr '/' '-').out $$pkg || exit 1 ; \
done
gocovmerge $(shell find $(COVERDIR) -name '*.out') > cover.test.out
test-integration: postgres_exporter postgres_exporter_integration_test
tests/test-smoke "$(shell pwd)/postgres_exporter" "$(shell pwd)/postgres_exporter_integration_test_script $(shell pwd)/postgres_exporter_integration_test $(shell pwd)/cover.integration.out"
cover.out: tools
gocovmerge cover.*.out > cover.out
clean:
[ ! -z $(BINDIR) ] && [ -e $(BINDIR) ] && find $(BINDIR) -print -delete || /bin/true
[ ! -z $(COVERDIR) ] && [ -e $(COVERDIR) ] && find $(COVERDIR) -print -delete || /bin/true
[ ! -z $(RELEASEDIR) ] && [ -e $(RELEASEDIR) ] && find $(RELEASEDIR) -print -delete || /bin/true
rm -f postgres_exporter postgres_exporter_integration_test
tools:
$(MAKE) -C $(TOOLDIR)
.PHONY: tools style fmt test all release binary clean
Loading
Loading
@@ -88,7 +88,7 @@ if [ "$1" = 'postgres' ]; then
: ${POSTGRES_DB:=$POSTGRES_USER}
export POSTGRES_USER POSTGRES_DB
 
psql=( psql -v ON_ERROR_STOP=1 )
psql=( "psql" "-v" "ON_ERROR_STOP=1" )
 
if [ "$POSTGRES_DB" != 'postgres' ]; then
"${psql[@]}" --username postgres <<-EOSQL
Loading
Loading
Loading
Loading
@@ -23,7 +23,7 @@ echo "Test Binary: $test_binary" 1>&2
[ -z "$postgres_exporter" ] && echo "Missing exporter binary" && exit 1
[ -z "$test_binary" ] && echo "Missing test binary" && exit 1
 
cd $DIR
cd "$DIR" || exit 1
 
VERSIONS=( \
9.1 \
Loading
Loading
@@ -48,7 +48,8 @@ wait_for_postgres(){
exit 1
fi
local wait_start=$(date +%s)
local wait_start
wait_start=$(date +%s) || exit 1
echo "Waiting for postgres to start listening..."
while ! pg_isready --host=$ip --port=$port &> /dev/null; do
if [ $(( $(date +%s) - $wait_start )) -gt $TIMEOUT ]; then
Loading
Loading
@@ -60,7 +61,8 @@ wait_for_postgres(){
}
 
wait_for_exporter() {
local wait_start=$(date +%s)
local wait_start
wait_start=$(date +%s) || exit 1
echo "Waiting for exporter to start..."
while ! nc -z localhost $exporter_port ; do
if [ $(( $(date +%s) - $wait_start )) -gt $TIMEOUT ]; then
Loading
Loading
@@ -101,8 +103,7 @@ smoketest_postgres() {
wait_for_exporter
 
# Dump the metrics to a file.
wget -q -O - http://localhost:$exporter_port/metrics 1> $METRICS_DIR/.metrics.single.$version.prom
if [ "$?" != "0" ]; then
if wget -q -O - http://localhost:$exporter_port/metrics 1> "$METRICS_DIR/.metrics.single.$version.prom" ; then
echo "Failed on postgres $version ($DOCKER_IMAGE)" 1>&2
kill $exporter_pid
exit 1
Loading
Loading
@@ -124,35 +125,34 @@ smoketest_postgres() {
echo "Replicated Postgres $version"
echo "#######################"
old_pwd=$(pwd)
cd docker-postgres-replication
cd docker-postgres-replication || exit 1
VERSION=$version p2 -t Dockerfile.p2 -o Dockerfile
if [ "$?" != "0" ]; then
if VERSION="$version" p2 -t Dockerfile.p2 -o Dockerfile ; then
echo "Templating failed" 1>&2
exit 1
fi
trap "docker-compose logs; docker-compose down ; docker-compose rm -v; exit 1" EXIT INT TERM
local compose_cmd="POSTGRES_PASSWORD=$POSTGRES_PASSWORD docker-compose up -d --force-recreate --build"
echo "Compose Cmd: $compose_cmd"
eval $compose_cmd
eval "$compose_cmd"
master_container=$(docker-compose ps -q pg-master)
slave_container=$(docker-compose ps -q pg-slave)
master_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $master_container)
slave_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $slave_container)
master_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$master_container")
slave_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$slave_container")
echo "Got master IP: $master_ip"
wait_for_postgres $master_ip 5432
wait_for_postgres $slave_ip 5432
wait_for_postgres "$master_ip" 5432
wait_for_postgres "$slave_ip" 5432
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$master_ip:5432/?sslmode=disable" $test_binary || exit $?
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$master_ip:5432/?sslmode=disable" $postgres_exporter --log.level=debug --web.listen-address=:$exporter_port &
exporter_pid=$!
# shellcheck disable=SC2064
trap "docker-compose logs; docker-compose down ; docker-compose rm -v ; kill $exporter_pid; exit 1" EXIT INT TERM
wait_for_exporter
 
wget -q -O - http://localhost:$exporter_port/metrics 1> $METRICS_DIR/.metrics.replicated.$version.prom
if [ "$?" != "0" ]; then
if wget -q -O - http://localhost:$exporter_port/metrics 1> "$METRICS_DIR/.metrics.replicated.$version.prom" ; then
echo "Failed on postgres $version ($DOCKER_IMAGE)" 1>&2
exit 1
fi
Loading
Loading
@@ -162,15 +162,15 @@ smoketest_postgres() {
docker-compose rm -v
trap - EXIT INT TERM
cd $old_pwd
cd "$old_pwd" || exit 1
}
 
# Start pulling the docker images in advance
for version in ${VERSIONS[@]}; do
docker pull postgres:$version > /dev/null &
for version in "${VERSIONS[@]}"; do
docker pull "postgres:$version" > /dev/null &
done
 
for version in ${VERSIONS[@]}; do
for version in "${VERSIONS[@]}"; do
echo "Testing postgres version $version"
smoketest_postgres $version
smoketest_postgres "$version"
done
Loading
Loading
@@ -2,10 +2,10 @@
# Script to determine added and removed metrics.
# Not currently used in CI but useful for inspecting complicated changes.
 
type=$1
version=$2
old_version=$3
new_version=$4
type="$1"
version="$2"
old_version="$3"
new_version="$4"
 
comm -23 $old_version $new_version > .metrics.${type}.${version}.removed
comm -13 $old_version $new_version > .metrics.${type}.${version}.added
comm -23 "$old_version" "$new_version" > ".metrics.${type}.${version}.removed"
comm -13 "$old_version" "$new_version" > ".metrics.${type}.${version}.added"
Loading
Loading
@@ -7,10 +7,9 @@
 
for raw_prom in $(echo .*.prom) ; do
# Strip, sort and deduplicate the label names
cat $raw_prom | grep -v '#' | \
grep -v '#' "$raw_prom" | \
rev | cut -d' ' -f2- | \
rev | cut -d'{' -f1 | \
sort | \
uniq > ${raw_prom}.unique
uniq > "${raw_prom}.unique"
done
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment