Makefile 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. # Copyright The OpenTelemetry Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. TOOLS_MOD_DIR := ./internal/tools
  15. ALL_DOCS := $(shell find . -name '*.md' -type f | sort)
  16. ALL_GO_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | sort)
  17. OTEL_GO_MOD_DIRS := $(filter-out $(TOOLS_MOD_DIR), $(ALL_GO_MOD_DIRS))
  18. ALL_COVERAGE_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | grep -E -v '^./example|^$(TOOLS_MOD_DIR)' | sort)
  19. GO = go
  20. TIMEOUT = 60
  21. .DEFAULT_GOAL := precommit
  22. .PHONY: precommit ci
  23. precommit: generate dependabot-generate license-check misspell go-mod-tidy golangci-lint-fix test-default
  24. ci: generate dependabot-check license-check lint vanity-import-check build test-default check-clean-work-tree test-coverage
  25. # Tools
  26. TOOLS = $(CURDIR)/.tools
  27. $(TOOLS):
  28. @mkdir -p $@
  29. $(TOOLS)/%: | $(TOOLS)
  30. cd $(TOOLS_MOD_DIR) && \
  31. $(GO) build -o $@ $(PACKAGE)
  32. MULTIMOD = $(TOOLS)/multimod
  33. $(TOOLS)/multimod: PACKAGE=go.opentelemetry.io/build-tools/multimod
  34. SEMCONVGEN = $(TOOLS)/semconvgen
  35. $(TOOLS)/semconvgen: PACKAGE=go.opentelemetry.io/build-tools/semconvgen
  36. CROSSLINK = $(TOOLS)/crosslink
  37. $(TOOLS)/crosslink: PACKAGE=go.opentelemetry.io/build-tools/crosslink
  38. SEMCONVKIT = $(TOOLS)/semconvkit
  39. $(TOOLS)/semconvkit: PACKAGE=go.opentelemetry.io/otel/$(TOOLS_MOD_DIR)/semconvkit
  40. DBOTCONF = $(TOOLS)/dbotconf
  41. $(TOOLS)/dbotconf: PACKAGE=go.opentelemetry.io/build-tools/dbotconf
  42. GOLANGCI_LINT = $(TOOLS)/golangci-lint
  43. $(TOOLS)/golangci-lint: PACKAGE=github.com/golangci/golangci-lint/cmd/golangci-lint
  44. MISSPELL = $(TOOLS)/misspell
  45. $(TOOLS)/misspell: PACKAGE=github.com/client9/misspell/cmd/misspell
  46. GOCOVMERGE = $(TOOLS)/gocovmerge
  47. $(TOOLS)/gocovmerge: PACKAGE=github.com/wadey/gocovmerge
  48. STRINGER = $(TOOLS)/stringer
  49. $(TOOLS)/stringer: PACKAGE=golang.org/x/tools/cmd/stringer
  50. PORTO = $(TOOLS)/porto
  51. $(TOOLS)/porto: PACKAGE=github.com/jcchavezs/porto/cmd/porto
  52. GOJQ = $(TOOLS)/gojq
  53. $(TOOLS)/gojq: PACKAGE=github.com/itchyny/gojq/cmd/gojq
  54. GOTMPL = $(TOOLS)/gotmpl
  55. $(GOTMPL): PACKAGE=go.opentelemetry.io/build-tools/gotmpl
  56. GORELEASE = $(TOOLS)/gorelease
  57. $(GORELEASE): PACKAGE=golang.org/x/exp/cmd/gorelease
  58. .PHONY: tools
  59. tools: $(CROSSLINK) $(DBOTCONF) $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(PORTO) $(GOJQ) $(SEMCONVGEN) $(MULTIMOD) $(SEMCONVKIT) $(GOTMPL) $(GORELEASE)
  60. # Virtualized python tools via docker
  61. # The directory where the virtual environment is created.
  62. VENVDIR := venv
  63. # The directory where the python tools are installed.
  64. PYTOOLS := $(VENVDIR)/bin
  65. # The pip executable in the virtual environment.
  66. PIP := $(PYTOOLS)/pip
  67. # The directory in the docker image where the current directory is mounted.
  68. WORKDIR := /workdir
  69. # The python image to use for the virtual environment.
  70. PYTHONIMAGE := python:3.11.3-slim-bullseye
  71. # Run the python image with the current directory mounted.
  72. DOCKERPY := docker run --rm -v "$(CURDIR):$(WORKDIR)" -w $(WORKDIR) $(PYTHONIMAGE)
  73. # Create a virtual environment for Python tools.
  74. $(PYTOOLS):
  75. # The `--upgrade` flag is needed to ensure that the virtual environment is
  76. # created with the latest pip version.
  77. @$(DOCKERPY) bash -c "python3 -m venv $(VENVDIR) && $(PIP) install --upgrade pip"
  78. # Install python packages into the virtual environment.
  79. $(PYTOOLS)/%: | $(PYTOOLS)
  80. @$(DOCKERPY) $(PIP) install -r requirements.txt
  81. CODESPELL = $(PYTOOLS)/codespell
  82. $(CODESPELL): PACKAGE=codespell
  83. # Generate
  84. .PHONY: generate
  85. generate: go-generate vanity-import-fix
  86. .PHONY: go-generate
  87. go-generate: $(OTEL_GO_MOD_DIRS:%=go-generate/%)
  88. go-generate/%: DIR=$*
  89. go-generate/%: | $(STRINGER) $(GOTMPL)
  90. @echo "$(GO) generate $(DIR)/..." \
  91. && cd $(DIR) \
  92. && PATH="$(TOOLS):$${PATH}" $(GO) generate ./...
  93. .PHONY: vanity-import-fix
  94. vanity-import-fix: | $(PORTO)
  95. @$(PORTO) --include-internal -w .
  96. # Generate go.work file for local development.
  97. .PHONY: go-work
  98. go-work: | $(CROSSLINK)
  99. $(CROSSLINK) work --root=$(shell pwd)
  100. # Build
  101. .PHONY: build
  102. build: $(OTEL_GO_MOD_DIRS:%=build/%) $(OTEL_GO_MOD_DIRS:%=build-tests/%)
  103. build/%: DIR=$*
  104. build/%:
  105. @echo "$(GO) build $(DIR)/..." \
  106. && cd $(DIR) \
  107. && $(GO) build ./...
  108. build-tests/%: DIR=$*
  109. build-tests/%:
  110. @echo "$(GO) build tests $(DIR)/..." \
  111. && cd $(DIR) \
  112. && $(GO) list ./... \
  113. | grep -v third_party \
  114. | xargs $(GO) test -vet=off -run xxxxxMatchNothingxxxxx >/dev/null
  115. # Tests
  116. TEST_TARGETS := test-default test-bench test-short test-verbose test-race
  117. .PHONY: $(TEST_TARGETS) test
  118. test-default test-race: ARGS=-race
  119. test-bench: ARGS=-run=xxxxxMatchNothingxxxxx -test.benchtime=1ms -bench=.
  120. test-short: ARGS=-short
  121. test-verbose: ARGS=-v -race
  122. $(TEST_TARGETS): test
  123. test: $(OTEL_GO_MOD_DIRS:%=test/%)
  124. test/%: DIR=$*
  125. test/%:
  126. @echo "$(GO) test -timeout $(TIMEOUT)s $(ARGS) $(DIR)/..." \
  127. && cd $(DIR) \
  128. && $(GO) list ./... \
  129. | grep -v third_party \
  130. | xargs $(GO) test -timeout $(TIMEOUT)s $(ARGS)
  131. COVERAGE_MODE = atomic
  132. COVERAGE_PROFILE = coverage.out
  133. .PHONY: test-coverage
  134. test-coverage: | $(GOCOVMERGE)
  135. @set -e; \
  136. printf "" > coverage.txt; \
  137. for dir in $(ALL_COVERAGE_MOD_DIRS); do \
  138. echo "$(GO) test -coverpkg=go.opentelemetry.io/otel/... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" $${dir}/..."; \
  139. (cd "$${dir}" && \
  140. $(GO) list ./... \
  141. | grep -v third_party \
  142. | grep -v 'semconv/v.*' \
  143. | xargs $(GO) test -coverpkg=./... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" && \
  144. $(GO) tool cover -html=coverage.out -o coverage.html); \
  145. done; \
  146. $(GOCOVMERGE) $$(find . -name coverage.out) > coverage.txt
  147. .PHONY: golangci-lint golangci-lint-fix
  148. golangci-lint-fix: ARGS=--fix
  149. golangci-lint-fix: golangci-lint
  150. golangci-lint: $(OTEL_GO_MOD_DIRS:%=golangci-lint/%)
  151. golangci-lint/%: DIR=$*
  152. golangci-lint/%: | $(GOLANGCI_LINT)
  153. @echo 'golangci-lint $(if $(ARGS),$(ARGS) ,)$(DIR)' \
  154. && cd $(DIR) \
  155. && $(GOLANGCI_LINT) run --allow-serial-runners $(ARGS)
  156. .PHONY: crosslink
  157. crosslink: | $(CROSSLINK)
  158. @echo "Updating intra-repository dependencies in all go modules" \
  159. && $(CROSSLINK) --root=$(shell pwd) --prune
  160. .PHONY: go-mod-tidy
  161. go-mod-tidy: $(ALL_GO_MOD_DIRS:%=go-mod-tidy/%)
  162. go-mod-tidy/%: DIR=$*
  163. go-mod-tidy/%: | crosslink
  164. @echo "$(GO) mod tidy in $(DIR)" \
  165. && cd $(DIR) \
  166. && $(GO) mod tidy -compat=1.20
  167. .PHONY: lint-modules
  168. lint-modules: go-mod-tidy
  169. .PHONY: lint
  170. lint: misspell lint-modules golangci-lint
  171. .PHONY: vanity-import-check
  172. vanity-import-check: | $(PORTO)
  173. @$(PORTO) --include-internal -l . || ( echo "(run: make vanity-import-fix)"; exit 1 )
  174. .PHONY: misspell
  175. misspell: | $(MISSPELL)
  176. @$(MISSPELL) -w $(ALL_DOCS)
  177. .PHONY: codespell
  178. codespell: | $(CODESPELL)
  179. @$(DOCKERPY) $(CODESPELL)
  180. .PHONY: license-check
  181. license-check:
  182. @licRes=$$(for f in $$(find . -type f \( -iname '*.go' -o -iname '*.sh' \) ! -path '**/third_party/*' ! -path './.git/*' ) ; do \
  183. awk '/Copyright The OpenTelemetry Authors|generated|GENERATED/ && NR<=4 { found=1; next } END { if (!found) print FILENAME }' $$f; \
  184. done); \
  185. if [ -n "$${licRes}" ]; then \
  186. echo "license header checking failed:"; echo "$${licRes}"; \
  187. exit 1; \
  188. fi
  189. DEPENDABOT_CONFIG = .github/dependabot.yml
  190. .PHONY: dependabot-check
  191. dependabot-check: | $(DBOTCONF)
  192. @$(DBOTCONF) verify $(DEPENDABOT_CONFIG) || ( echo "(run: make dependabot-generate)"; exit 1 )
  193. .PHONY: dependabot-generate
  194. dependabot-generate: | $(DBOTCONF)
  195. @$(DBOTCONF) generate > $(DEPENDABOT_CONFIG)
  196. .PHONY: check-clean-work-tree
  197. check-clean-work-tree:
  198. @if ! git diff --quiet; then \
  199. echo; \
  200. echo 'Working tree is not clean, did you forget to run "make precommit"?'; \
  201. echo; \
  202. git status; \
  203. exit 1; \
  204. fi
  205. SEMCONVPKG ?= "semconv/"
  206. .PHONY: semconv-generate
  207. semconv-generate: | $(SEMCONVGEN) $(SEMCONVKIT)
  208. [ "$(TAG)" ] || ( echo "TAG unset: missing opentelemetry semantic-conventions tag"; exit 1 )
  209. [ "$(OTEL_SEMCONV_REPO)" ] || ( echo "OTEL_SEMCONV_REPO unset: missing path to opentelemetry semantic-conventions repo"; exit 1 )
  210. $(SEMCONVGEN) -i "$(OTEL_SEMCONV_REPO)/model/." --only=span -p conventionType=trace -f trace.go -t "$(SEMCONVPKG)/template.j2" -s "$(TAG)"
  211. $(SEMCONVGEN) -i "$(OTEL_SEMCONV_REPO)/model/." --only=attribute_group -p conventionType=trace -f attribute_group.go -t "$(SEMCONVPKG)/template.j2" -s "$(TAG)"
  212. $(SEMCONVGEN) -i "$(OTEL_SEMCONV_REPO)/model/." --only=event -p conventionType=event -f event.go -t "$(SEMCONVPKG)/template.j2" -s "$(TAG)"
  213. $(SEMCONVGEN) -i "$(OTEL_SEMCONV_REPO)/model/." --only=resource -p conventionType=resource -f resource.go -t "$(SEMCONVPKG)/template.j2" -s "$(TAG)"
  214. $(SEMCONVKIT) -output "$(SEMCONVPKG)/$(TAG)" -tag "$(TAG)"
  215. .PHONY: gorelease
  216. gorelease: $(OTEL_GO_MOD_DIRS:%=gorelease/%)
  217. gorelease/%: DIR=$*
  218. gorelease/%:| $(GORELEASE)
  219. @echo "gorelease in $(DIR):" \
  220. && cd $(DIR) \
  221. && $(GORELEASE) \
  222. || echo ""
  223. .PHONY: prerelease
  224. prerelease: | $(MULTIMOD)
  225. @[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
  226. $(MULTIMOD) verify && $(MULTIMOD) prerelease -m ${MODSET}
  227. COMMIT ?= "HEAD"
  228. .PHONY: add-tags
  229. add-tags: | $(MULTIMOD)
  230. @[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
  231. $(MULTIMOD) verify && $(MULTIMOD) tag -m ${MODSET} -c ${COMMIT}