Makefile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. # Copyright The OpenTelemetry Authors
  2. # SPDX-License-Identifier: Apache-2.0
  3. TOOLS_MOD_DIR := ./internal/tools
  4. ALL_DOCS := $(shell find . -name '*.md' -type f | sort)
  5. ALL_GO_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | sort)
  6. OTEL_GO_MOD_DIRS := $(filter-out $(TOOLS_MOD_DIR), $(ALL_GO_MOD_DIRS))
  7. ALL_COVERAGE_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | grep -E -v '^./example|^$(TOOLS_MOD_DIR)' | sort)
  8. GO = go
  9. TIMEOUT = 60
  10. # User to run as in docker images.
  11. DOCKER_USER=$(shell id -u):$(shell id -g)
  12. DEPENDENCIES_DOCKERFILE=./dependencies.Dockerfile
  13. .DEFAULT_GOAL := precommit
  14. .PHONY: precommit ci
  15. precommit: generate toolchain-check license-check misspell go-mod-tidy golangci-lint-fix verify-readmes verify-mods test-default
  16. ci: generate toolchain-check license-check lint vanity-import-check verify-readmes verify-mods build test-default check-clean-work-tree test-coverage
  17. # Tools
  18. TOOLS = $(CURDIR)/.tools
  19. $(TOOLS):
  20. @mkdir -p $@
  21. $(TOOLS)/%: $(TOOLS_MOD_DIR)/go.mod | $(TOOLS)
  22. cd $(TOOLS_MOD_DIR) && \
  23. $(GO) build -o $@ $(PACKAGE)
  24. MULTIMOD = $(TOOLS)/multimod
  25. $(TOOLS)/multimod: PACKAGE=go.opentelemetry.io/build-tools/multimod
  26. CROSSLINK = $(TOOLS)/crosslink
  27. $(TOOLS)/crosslink: PACKAGE=go.opentelemetry.io/build-tools/crosslink
  28. SEMCONVKIT = $(TOOLS)/semconvkit
  29. SEMCONVKIT_FILES := $(sort $(shell find $(TOOLS_MOD_DIR)/semconvkit -type f))
  30. $(TOOLS)/semconvkit: PACKAGE=go.opentelemetry.io/otel/$(TOOLS_MOD_DIR)/semconvkit
  31. $(TOOLS)/semconvkit: $(SEMCONVKIT_FILES)
  32. VERIFYREADMES = $(TOOLS)/verifyreadmes
  33. VERIFYREADMES_FILES := $(sort $(shell find $(TOOLS_MOD_DIR)/verifyreadmes -type f))
  34. $(TOOLS)/verifyreadmes: PACKAGE=go.opentelemetry.io/otel/$(TOOLS_MOD_DIR)/verifyreadmes
  35. $(TOOLS)/verifyreadmes: $(VERIFYREADMES_FILES)
  36. GOLANGCI_LINT = $(TOOLS)/golangci-lint
  37. $(TOOLS)/golangci-lint: PACKAGE=github.com/golangci/golangci-lint/v2/cmd/golangci-lint
  38. MISSPELL = $(TOOLS)/misspell
  39. $(TOOLS)/misspell: PACKAGE=github.com/client9/misspell/cmd/misspell
  40. GOCOVMERGE = $(TOOLS)/gocovmerge
  41. $(TOOLS)/gocovmerge: PACKAGE=github.com/wadey/gocovmerge
  42. STRINGER = $(TOOLS)/stringer
  43. $(TOOLS)/stringer: PACKAGE=golang.org/x/tools/cmd/stringer
  44. PORTO = $(TOOLS)/porto
  45. $(TOOLS)/porto: PACKAGE=github.com/jcchavezs/porto/cmd/porto
  46. GOTMPL = $(TOOLS)/gotmpl
  47. $(GOTMPL): PACKAGE=go.opentelemetry.io/build-tools/gotmpl
  48. GORELEASE = $(TOOLS)/gorelease
  49. $(GORELEASE): PACKAGE=golang.org/x/exp/cmd/gorelease
  50. GOVULNCHECK = $(TOOLS)/govulncheck
  51. $(TOOLS)/govulncheck: PACKAGE=golang.org/x/vuln/cmd/govulncheck
  52. .PHONY: tools
  53. tools: $(CROSSLINK) $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(PORTO) $(VERIFYREADMES) $(MULTIMOD) $(SEMCONVKIT) $(GOTMPL) $(GORELEASE)
  54. # Virtualized python tools via docker
  55. # The directory where the virtual environment is created.
  56. VENVDIR := venv
  57. # The directory where the python tools are installed.
  58. PYTOOLS := $(VENVDIR)/bin
  59. # The pip executable in the virtual environment.
  60. PIP := $(PYTOOLS)/pip
  61. # The directory in the docker image where the current directory is mounted.
  62. WORKDIR := /workdir
  63. # The python image to use for the virtual environment.
  64. PYTHONIMAGE := $(shell awk '$$4=="python" {print $$2}' $(DEPENDENCIES_DOCKERFILE))
  65. # Run the python image with the current directory mounted.
  66. DOCKERPY := docker run --rm -u $(DOCKER_USER) -v "$(CURDIR):$(WORKDIR)" -w $(WORKDIR) $(PYTHONIMAGE)
  67. # Create a virtual environment for Python tools.
  68. $(PYTOOLS):
  69. # The `--upgrade` flag is needed to ensure that the virtual environment is
  70. # created with the latest pip version.
  71. @$(DOCKERPY) bash -c "python3 -m venv $(VENVDIR) && $(PIP) install --upgrade --cache-dir=$(WORKDIR)/.cache/pip pip"
  72. # Install python packages into the virtual environment.
  73. $(PYTOOLS)/%: $(PYTOOLS)
  74. @$(DOCKERPY) $(PIP) install --cache-dir=$(WORKDIR)/.cache/pip -r requirements.txt
  75. CODESPELL = $(PYTOOLS)/codespell
  76. $(CODESPELL): PACKAGE=codespell
  77. # Generate
  78. .PHONY: generate
  79. generate: go-generate vanity-import-fix
  80. .PHONY: go-generate
  81. go-generate: $(OTEL_GO_MOD_DIRS:%=go-generate/%)
  82. go-generate/%: DIR=$*
  83. go-generate/%: $(STRINGER) $(GOTMPL)
  84. @echo "$(GO) generate $(DIR)/..." \
  85. && cd $(DIR) \
  86. && PATH="$(TOOLS):$${PATH}" $(GO) generate ./...
  87. .PHONY: vanity-import-fix
  88. vanity-import-fix: $(PORTO)
  89. @$(PORTO) --include-internal -w .
  90. # Generate go.work file for local development.
  91. .PHONY: go-work
  92. go-work: $(CROSSLINK)
  93. $(CROSSLINK) work --root=$(shell pwd) --go=1.22.7
  94. # Build
  95. .PHONY: build
  96. build: $(OTEL_GO_MOD_DIRS:%=build/%) $(OTEL_GO_MOD_DIRS:%=build-tests/%)
  97. build/%: DIR=$*
  98. build/%:
  99. @echo "$(GO) build $(DIR)/..." \
  100. && cd $(DIR) \
  101. && $(GO) build ./...
  102. build-tests/%: DIR=$*
  103. build-tests/%:
  104. @echo "$(GO) build tests $(DIR)/..." \
  105. && cd $(DIR) \
  106. && $(GO) list ./... \
  107. | grep -v third_party \
  108. | xargs $(GO) test -vet=off -run xxxxxMatchNothingxxxxx >/dev/null
  109. # Tests
  110. TEST_TARGETS := test-default test-bench test-short test-verbose test-race test-concurrent-safe test-fuzz
  111. .PHONY: $(TEST_TARGETS) test
  112. test-default test-race: ARGS=-race
  113. test-bench: ARGS=-run=xxxxxMatchNothingxxxxx -test.benchtime=1ms -bench=.
  114. test-short: ARGS=-short
  115. test-fuzz: ARGS=-fuzztime=10s -fuzz
  116. test-verbose: ARGS=-v -race
  117. test-concurrent-safe: ARGS=-run=ConcurrentSafe -count=100 -race
  118. test-concurrent-safe: TIMEOUT=120
  119. $(TEST_TARGETS): test
  120. test: $(OTEL_GO_MOD_DIRS:%=test/%)
  121. test/%: DIR=$*
  122. test/%:
  123. @echo "$(GO) test -timeout $(TIMEOUT)s $(ARGS) $(DIR)/..." \
  124. && cd $(DIR) \
  125. && $(GO) list ./... \
  126. | grep -v third_party \
  127. | xargs $(GO) test -timeout $(TIMEOUT)s $(ARGS)
  128. COVERAGE_MODE = atomic
  129. COVERAGE_PROFILE = coverage.out
  130. .PHONY: test-coverage
  131. test-coverage: $(GOCOVMERGE)
  132. @set -e; \
  133. printf "" > coverage.txt; \
  134. for dir in $(ALL_COVERAGE_MOD_DIRS); do \
  135. echo "$(GO) test -coverpkg=go.opentelemetry.io/otel/... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" $${dir}/..."; \
  136. (cd "$${dir}" && \
  137. $(GO) list ./... \
  138. | grep -v third_party \
  139. | grep -v 'semconv/v.*' \
  140. | xargs $(GO) test -coverpkg=./... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" && \
  141. $(GO) tool cover -html=coverage.out -o coverage.html); \
  142. done; \
  143. $(GOCOVMERGE) $$(find . -name coverage.out) > coverage.txt
  144. .PHONY: benchmark
  145. benchmark: $(OTEL_GO_MOD_DIRS:%=benchmark/%)
  146. benchmark/%:
  147. cd $* && $(GO) test -run='^$$' -bench=. $(ARGS) ./...
  148. print-sharded-benchmarks:
  149. @echo $(OTEL_GO_MOD_DIRS) | jq -cR 'split(" ")'
  150. .PHONY: golangci-lint golangci-lint-fix
  151. golangci-lint-fix: ARGS=--fix
  152. golangci-lint-fix: golangci-lint
  153. golangci-lint: $(OTEL_GO_MOD_DIRS:%=golangci-lint/%)
  154. golangci-lint/%: DIR=$*
  155. golangci-lint/%: $(GOLANGCI_LINT)
  156. @echo 'golangci-lint $(if $(ARGS),$(ARGS) ,)$(DIR)' \
  157. && cd $(DIR) \
  158. && $(GOLANGCI_LINT) run --allow-serial-runners $(ARGS)
  159. .PHONY: crosslink
  160. crosslink: $(CROSSLINK)
  161. @echo "Updating intra-repository dependencies in all go modules" \
  162. && $(CROSSLINK) --root=$(shell pwd) --prune
  163. .PHONY: go-mod-tidy
  164. go-mod-tidy: $(ALL_GO_MOD_DIRS:%=go-mod-tidy/%)
  165. go-mod-tidy/%: DIR=$*
  166. go-mod-tidy/%: crosslink
  167. @echo "$(GO) mod tidy in $(DIR)" \
  168. && cd $(DIR) \
  169. && $(GO) mod tidy -compat=1.21
  170. .PHONY: lint
  171. lint: misspell go-mod-tidy golangci-lint
  172. .PHONY: vanity-import-check
  173. vanity-import-check: $(PORTO)
  174. @$(PORTO) --include-internal -l . || ( echo "(run: make vanity-import-fix)"; exit 1 )
  175. .PHONY: misspell
  176. misspell: $(MISSPELL)
  177. @$(MISSPELL) -w $(ALL_DOCS)
  178. .PHONY: govulncheck
  179. govulncheck: $(OTEL_GO_MOD_DIRS:%=govulncheck/%)
  180. govulncheck/%: DIR=$*
  181. govulncheck/%: $(GOVULNCHECK)
  182. @echo "govulncheck ./... in $(DIR)" \
  183. && cd $(DIR) \
  184. && $(GOVULNCHECK) ./...
  185. .PHONY: codespell
  186. codespell: $(CODESPELL)
  187. @$(DOCKERPY) $(CODESPELL)
  188. .PHONY: toolchain-check
  189. toolchain-check:
  190. @toolchainRes=$$(for f in $(ALL_GO_MOD_DIRS); do \
  191. awk '/^toolchain/ { found=1; next } END { if (found) print FILENAME }' $$f/go.mod; \
  192. done); \
  193. if [ -n "$${toolchainRes}" ]; then \
  194. echo "toolchain checking failed:"; echo "$${toolchainRes}"; \
  195. exit 1; \
  196. fi
  197. .PHONY: license-check
  198. license-check:
  199. @licRes=$$(for f in $$(find . -type f \( -iname '*.go' -o -iname '*.sh' \) ! -path '**/third_party/*' ! -path './.git/*' ) ; do \
  200. awk '/Copyright The OpenTelemetry Authors|generated|GENERATED/ && NR<=4 { found=1; next } END { if (!found) print FILENAME }' $$f; \
  201. done); \
  202. if [ -n "$${licRes}" ]; then \
  203. echo "license header checking failed:"; echo "$${licRes}"; \
  204. exit 1; \
  205. fi
  206. .PHONY: check-clean-work-tree
  207. check-clean-work-tree:
  208. @if ! git diff --quiet; then \
  209. echo; \
  210. echo 'Working tree is not clean, did you forget to run "make precommit"?'; \
  211. echo; \
  212. git status; \
  213. exit 1; \
  214. fi
  215. # The weaver docker image to use for semconv-generate.
  216. WEAVER_IMAGE := $(shell awk '$$4=="weaver" {print $$2}' $(DEPENDENCIES_DOCKERFILE))
  217. SEMCONVPKG ?= "semconv/"
  218. .PHONY: semconv-generate
  219. semconv-generate: $(SEMCONVKIT)
  220. [ "$(TAG)" ] || ( echo "TAG unset: missing opentelemetry semantic-conventions tag"; exit 1 )
  221. # Ensure the target directory for source code is available.
  222. mkdir -p $(PWD)/$(SEMCONVPKG)/${TAG}
  223. # Note: We mount a home directory for downloading/storing the semconv repository.
  224. # Weaver will automatically clean the cache when finished, but the directories will remain.
  225. mkdir -p ~/.weaver
  226. docker run --rm \
  227. -u $(DOCKER_USER) \
  228. --env HOME=/tmp/weaver \
  229. --mount 'type=bind,source=$(PWD)/semconv/templates,target=/home/weaver/templates,readonly' \
  230. --mount 'type=bind,source=$(PWD)/semconv/${TAG},target=/home/weaver/target' \
  231. --mount 'type=bind,source=$(HOME)/.weaver,target=/tmp/weaver/.weaver' \
  232. $(WEAVER_IMAGE) registry generate \
  233. --registry=https://github.com/open-telemetry/semantic-conventions/archive/refs/tags/$(TAG).zip[model] \
  234. --templates=/home/weaver/templates \
  235. --param tag=$(TAG) \
  236. go \
  237. /home/weaver/target
  238. $(SEMCONVKIT) -semconv "$(SEMCONVPKG)" -tag "$(TAG)"
  239. .PHONY: gorelease
  240. gorelease: $(OTEL_GO_MOD_DIRS:%=gorelease/%)
  241. gorelease/%: DIR=$*
  242. gorelease/%:| $(GORELEASE)
  243. @echo "gorelease in $(DIR):" \
  244. && cd $(DIR) \
  245. && $(GORELEASE) \
  246. || echo ""
  247. .PHONY: verify-mods
  248. verify-mods: $(MULTIMOD)
  249. $(MULTIMOD) verify
  250. .PHONY: prerelease
  251. prerelease: verify-mods
  252. @[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
  253. $(MULTIMOD) prerelease -m ${MODSET}
  254. COMMIT ?= "HEAD"
  255. .PHONY: add-tags
  256. add-tags: verify-mods
  257. @[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
  258. $(MULTIMOD) tag -m ${MODSET} -c ${COMMIT}
  259. MARKDOWNIMAGE := $(shell awk '$$4=="markdown" {print $$2}' $(DEPENDENCIES_DOCKERFILE))
  260. .PHONY: lint-markdown
  261. lint-markdown:
  262. docker run --rm -u $(DOCKER_USER) -v "$(CURDIR):$(WORKDIR)" $(MARKDOWNIMAGE) -c $(WORKDIR)/.markdownlint.yaml $(WORKDIR)/**/*.md
  263. .PHONY: verify-readmes
  264. verify-readmes: $(VERIFYREADMES)
  265. $(VERIFYREADMES)