Skip to content

Commit 3fb53df

Browse files
authored
Merge pull request #501 from simonpasquier/go-modules
*: support Go modules
2 parents 7e9098b + f626bd2 commit 3fb53df

File tree

4 files changed

+137
-18
lines changed

4 files changed

+137
-18
lines changed

Makefile.common

Lines changed: 103 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# !!! Open PRs only against the prometheus/prometheus/Makefile.common repository!
1717

1818
# Example usage :
19-
# Create the main Makefile in the root project directory.
19+
# Create the main Makefile in the root project directory.
2020
# include Makefile.common
2121
# customTarget:
2222
# @echo ">> Running customTarget"
@@ -28,18 +28,53 @@ unexport GOBIN
2828
GO ?= go
2929
GOFMT ?= $(GO)fmt
3030
FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH)))
31+
GOOPTS ?=
32+
33+
GO_VERSION ?= $(shell $(GO) version)
34+
GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION))
35+
PRE_GO_111 ?= $(shell echo $(GO_VERSION_NUMBER) | grep -E 'go1\.(10|[0-9])\.')
36+
37+
unexport GOVENDOR
38+
ifeq (, $(PRE_GO_111))
39+
ifneq (,$(wildcard go.mod))
40+
# Enforce Go modules support just in case the directory is inside GOPATH (and for Travis CI).
41+
GO111MODULE := on
42+
43+
ifneq (,$(wildcard vendor))
44+
# Always use the local vendor/ directory to satisfy the dependencies.
45+
GOOPTS := $(GOOPTS) -mod=vendor
46+
endif
47+
endif
48+
else
49+
ifneq (,$(wildcard go.mod))
50+
ifneq (,$(wildcard vendor))
51+
$(warning This repository requires Go >= 1.11 because of Go modules)
52+
$(warning Some recipes may not work as expected as the current Go runtime is '$(GO_VERSION_NUMBER)')
53+
endif
54+
else
55+
# This repository isn't using Go modules (yet).
56+
GOVENDOR := $(FIRST_GOPATH)/bin/govendor
57+
endif
58+
59+
unexport GO111MODULE
60+
endif
3161
PROMU := $(FIRST_GOPATH)/bin/promu
3262
STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck
33-
GOVENDOR := $(FIRST_GOPATH)/bin/govendor
3463
pkgs = ./...
3564

65+
GO_VERSION ?= $(shell $(GO) version)
66+
GO_BUILD_PLATFORM ?= $(subst /,-,$(lastword $(GO_VERSION)))
67+
68+
PROMU_VERSION ?= 0.2.0
69+
PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz
70+
3671
PREFIX ?= $(shell pwd)
3772
BIN_DIR ?= $(shell pwd)
3873
DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
3974
DOCKER_REPO ?= prom
4075

4176
.PHONY: all
42-
all: style staticcheck unused build test
77+
all: precheck style staticcheck unused build test
4378

4479
# This rule is used to forward a target like "build" to "common-build". This
4580
# allows a new "build" target to be defined in a Makefile which includes this
@@ -70,37 +105,54 @@ common-check_license:
70105
.PHONY: common-test-short
71106
common-test-short:
72107
@echo ">> running short tests"
73-
$(GO) test -short $(pkgs)
108+
GO111MODULE=$(GO111MODULE) $(GO) test -short $(GOOPTS) $(pkgs)
74109

75110
.PHONY: common-test
76111
common-test:
77112
@echo ">> running all tests"
78-
$(GO) test -race $(pkgs)
113+
GO111MODULE=$(GO111MODULE) $(GO) test -race $(GOOPTS) $(pkgs)
79114

80115
.PHONY: common-format
81116
common-format:
82117
@echo ">> formatting code"
83-
$(GO) fmt $(pkgs)
118+
GO111MODULE=$(GO111MODULE) $(GO) fmt $(GOOPTS) $(pkgs)
84119

85120
.PHONY: common-vet
86121
common-vet:
87122
@echo ">> vetting code"
88-
$(GO) vet $(pkgs)
123+
GO111MODULE=$(GO111MODULE) $(GO) vet $(GOOPTS) $(pkgs)
89124

90125
.PHONY: common-staticcheck
91126
common-staticcheck: $(STATICCHECK)
92127
@echo ">> running staticcheck"
128+
ifdef GO111MODULE
129+
GO111MODULE=$(GO111MODULE) $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" -checks "SA*" $(pkgs)
130+
else
93131
$(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs)
132+
endif
94133

95134
.PHONY: common-unused
96135
common-unused: $(GOVENDOR)
136+
ifdef GOVENDOR
97137
@echo ">> running check for unused packages"
98138
@$(GOVENDOR) list +unused | grep . && exit 1 || echo 'No unused packages'
139+
else
140+
ifdef GO111MODULE
141+
@echo ">> running check for unused/missing packages in go.mod"
142+
GO111MODULE=$(GO111MODULE) $(GO) mod tidy
143+
@git diff --exit-code -- go.sum go.mod
144+
ifneq (,$(wildcard vendor))
145+
@echo ">> running check for unused packages in vendor/"
146+
GO111MODULE=$(GO111MODULE) $(GO) mod vendor
147+
@git diff --exit-code -- go.sum go.mod vendor/
148+
endif
149+
endif
150+
endif
99151

100152
.PHONY: common-build
101153
common-build: promu
102154
@echo ">> building binaries"
103-
$(PROMU) build --prefix $(PREFIX)
155+
GO111MODULE=$(GO111MODULE) $(PROMU) build --prefix $(PREFIX)
104156

105157
.PHONY: common-tarball
106158
common-tarball: promu
@@ -120,13 +172,52 @@ common-docker-tag-latest:
120172
docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):latest"
121173

122174
.PHONY: promu
123-
promu:
124-
GOOS= GOARCH= $(GO) get -u github.com/prometheus/promu
175+
promu: $(PROMU)
176+
177+
$(PROMU):
178+
curl -s -L $(PROMU_URL) | tar -xvz -C /tmp
179+
mkdir -v -p $(FIRST_GOPATH)/bin
180+
cp -v /tmp/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(PROMU)
181+
182+
.PHONY: proto
183+
proto:
184+
@echo ">> generating code from proto files"
185+
@./scripts/genproto.sh
125186

126187
.PHONY: $(STATICCHECK)
127188
$(STATICCHECK):
128-
GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck
129-
189+
ifdef GO111MODULE
190+
# Get staticcheck from a temporary directory to avoid modifying the local go.{mod,sum}.
191+
# See https://github.com/golang/go/issues/27643.
192+
# For now, we are using the next branch of staticcheck because master isn't compatible yet with Go modules.
193+
tmpModule=$$(mktemp -d 2>&1) && \
194+
mkdir -p $${tmpModule}/staticcheck && \
195+
cd "$${tmpModule}"/staticcheck && \
196+
GO111MODULE=on $(GO) mod init example.com/staticcheck && \
197+
GO111MODULE=on GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck@next && \
198+
rm -rf $${tmpModule};
199+
else
200+
GOOS= GOARCH= GO111MODULE=off $(GO) get -u honnef.co/go/tools/cmd/staticcheck
201+
endif
202+
203+
ifdef GOVENDOR
130204
.PHONY: $(GOVENDOR)
131205
$(GOVENDOR):
132206
GOOS= GOARCH= $(GO) get -u github.com/kardianos/govendor
207+
endif
208+
209+
.PHONY: precheck
210+
precheck::
211+
212+
define PRECHECK_COMMAND_template =
213+
precheck:: $(1)_precheck
214+
215+
216+
PRECHECK_COMMAND_$(1) ?= $(1) $$(strip $$(PRECHECK_OPTIONS_$(1)))
217+
.PHONE: $(1)_precheck
218+
$(1)_precheck:
219+
@if ! $$(PRECHECK_COMMAND_$(1)) 1>/dev/null 2>&1; then \
220+
echo "Execution of '$$(PRECHECK_COMMAND_$(1))' command failed. Is $(1) installed?"; \
221+
exit 1; \
222+
fi
223+
endef

api/prometheus/v1/api.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ type HealthStatus string
5656
const (
5757
// Possible values for ErrorType.
5858
ErrBadData ErrorType = "bad_data"
59-
ErrTimeout = "timeout"
60-
ErrCanceled = "canceled"
61-
ErrExec = "execution"
62-
ErrBadResponse = "bad_response"
63-
ErrServer = "server_error"
64-
ErrClient = "client_error"
59+
ErrTimeout ErrorType = "timeout"
60+
ErrCanceled ErrorType = "canceled"
61+
ErrExec ErrorType = "execution"
62+
ErrBadResponse ErrorType = "bad_response"
63+
ErrServer ErrorType = "server_error"
64+
ErrClient ErrorType = "client_error"
6565

6666
// Possible values for HealthStatus.
6767
HealthGood HealthStatus = "up"

go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/prometheus/client_golang
2+
3+
require (
4+
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973
5+
github.com/golang/protobuf v1.2.0
6+
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
7+
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910
8+
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce
9+
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d
10+
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a
11+
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f // indirect
12+
)

go.sum

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
2+
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
3+
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
4+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
5+
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
6+
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
7+
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 h1:idejC8f05m9MGOsuEi1ATq9shN03HrxNkD/luQvxCv8=
8+
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
9+
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce h1:X0jFYGnHemYDIW6jlc+fSI8f9Cg+jqCnClYP2WgZT/A=
10+
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
11+
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d h1:GoAlyOgbOEIFdaDqxJVlbOQ1DtGmZWs/Qau0hIlk+WQ=
12+
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
13+
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a h1:gOpx8G595UYyvj8UK4+OFyY4rx037g3fmfhe5SasG3U=
14+
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
15+
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ=
16+
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

0 commit comments

Comments
 (0)