Makefile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. GOVULNCHECK = $(TOOLS)/govulncheck
  59. $(TOOLS)/govulncheck: PACKAGE=golang.org/x/vuln/cmd/govulncheck
  60. .PHONY: tools
  61. tools: $(CROSSLINK) $(DBOTCONF) $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(PORTO) $(GOJQ) $(SEMCONVGEN) $(MULTIMOD) $(SEMCONVKIT) $(GOTMPL) $(GORELEASE)
  62. # Virtualized python tools via docker
  63. # The directory where the virtual environment is created.
  64. VENVDIR := venv
  65. # The directory where the python tools are installed.
  66. PYTOOLS := $(VENVDIR)/bin
  67. # The pip executable in the virtual environment.
  68. PIP := $(PYTOOLS)/pip
  69. # The directory in the docker image where the current directory is mounted.
  70. WORKDIR := /workdir
  71. # The python image to use for the virtual environment.
  72. PYTHONIMAGE := python:3.11.3-slim-bullseye
  73. # Run the python image with the current directory mounted.
  74. DOCKERPY := docker run --rm -v "$(CURDIR):$(WORKDIR)" -w $(WORKDIR) $(PYTHONIMAGE)
  75. # Create a virtual environment for Python tools.
  76. $(PYTOOLS):
  77. # The `--upgrade` flag is needed to ensure that the virtual environment is
  78. # created with the latest pip version.
  79. @$(DOCKERPY) bash -c "python3 -m venv $(VENVDIR) && $(PIP) install --upgrade pip"
  80. # Install python packages into the virtual environment.
  81. $(PYTOOLS)/%: | $(PYTOOLS)
  82. @$(DOCKERPY) $(PIP) install -r requirements.txt
  83. CODESPELL = $(PYTOOLS)/codespell
  84. $(CODESPELL): PACKAGE=codespell
  85. # Generate
  86. .PHONY: generate
  87. generate: go-generate vanity-import-fix
  88. .PHONY: go-generate
  89. go-generate: $(OTEL_GO_MOD_DIRS:%=go-generate/%)
  90. go-generate/%: DIR=$*
  91. go-generate/%: | $(STRINGER) $(GOTMPL)
  92. @echo "$(GO) generate $(DIR)/..." \
  93. && cd $(DIR) \
  94. && PATH="$(TOOLS):$${PATH}" $(GO) generate ./...
  95. .PHONY: vanity-import-fix
  96. vanity-import-fix: | $(PORTO)
  97. @$(PORTO) --include-internal -w .
  98. # Generate go.work file for local development.
  99. .PHONY: go-work
  100. go-work: | $(CROSSLINK)
  101. $(CROSSLINK) work --root=$(shell pwd)
  102. # Build
  103. .PHONY: build
  104. build: $(OTEL_GO_MOD_DIRS:%=build/%) $(OTEL_GO_MOD_DIRS:%=build-tests/%)
  105. build/%: DIR=$*
  106. build/%:
  107. @echo "$(GO) build $(DIR)/..." \
  108. && cd $(DIR) \
  109. && $(GO) build ./...
  110. build-tests/%: DIR=$*
  111. build-tests/%:
  112. @echo "$(GO) build tests $(DIR)/..." \
  113. && cd $(DIR) \
  114. && $(GO) list ./... \
  115. | grep -v third_party \
  116. | xargs $(GO) test -vet=off -run xxxxxMatchNothingxxxxx >/dev/null
  117. # Tests
  118. TEST_TARGETS := test-default test-bench test-short test-verbose test-race
  119. .PHONY: $(TEST_TARGETS) test
  120. test-default test-race: ARGS=-race
  121. test-bench: ARGS=-run=xxxxxMatchNothingxxxxx -test.benchtime=1ms -bench=.
  122. test-short: ARGS=-short
  123. test-verbose: ARGS=-v -race
  124. $(TEST_TARGETS): test
  125. test: $(OTEL_GO_MOD_DIRS:%=test/%)
  126. test/%: DIR=$*
  127. test/%:
  128. @echo "$(GO) test -timeout $(TIMEOUT)s $(ARGS) $(DIR)/..." \
  129. && cd $(DIR) \
  130. && $(GO) list ./... \
  131. | grep -v third_party \
  132. | xargs $(GO) test -timeout $(TIMEOUT)s $(ARGS)
  133. COVERAGE_MODE = atomic
  134. COVERAGE_PROFILE = coverage.out
  135. .PHONY: test-coverage
  136. test-coverage: | $(GOCOVMERGE)
  137. @set -e; \
  138. printf "" > coverage.txt; \
  139. for dir in $(ALL_COVERAGE_MOD_DIRS); do \
  140. echo "$(GO) test -coverpkg=go.opentelemetry.io/otel/... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" $${dir}/..."; \
  141. (cd "$${dir}" && \
  142. $(GO) list ./... \
  143. | grep -v third_party \
  144. | grep -v 'semconv/v.*' \
  145. | xargs $(GO) test -coverpkg=./... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" && \
  146. $(GO) tool cover -html=coverage.out -o coverage.html); \
  147. done; \
  148. $(GOCOVMERGE) $$(find . -name coverage.out) > coverage.txt
  149. # Adding a directory will include all benchmarks in that direcotry if a filter is not specified.
  150. BENCHMARK_TARGETS := sdk/trace
  151. .PHONY: benchmark
  152. benchmark: $(BENCHMARK_TARGETS:%=benchmark/%)
  153. BENCHMARK_FILTER = .
  154. # You can override the filter for a particular directory by adding a rule here.
  155. benchmark/sdk/trace: BENCHMARK_FILTER = SpanWithAttributes_8/AlwaysSample
  156. benchmark/%:
  157. @echo "$(GO) test -timeout $(TIMEOUT)s -run=xxxxxMatchNothingxxxxx -bench=$(BENCHMARK_FILTER) $*..." \
  158. && cd $* \
  159. $(foreach filter, $(BENCHMARK_FILTER), && $(GO) test -timeout $(TIMEOUT)s -run=xxxxxMatchNothingxxxxx -bench=$(filter))
  160. .PHONY: golangci-lint golangci-lint-fix
  161. golangci-lint-fix: ARGS=--fix
  162. golangci-lint-fix: golangci-lint
  163. golangci-lint: $(OTEL_GO_MOD_DIRS:%=golangci-lint/%)
  164. golangci-lint/%: DIR=$*
  165. golangci-lint/%: | $(GOLANGCI_LINT)
  166. @echo 'golangci-lint $(if $(ARGS),$(ARGS) ,)$(DIR)' \
  167. && cd $(DIR) \
  168. && $(GOLANGCI_LINT) run --allow-serial-runners $(ARGS)
  169. .PHONY: crosslink
  170. crosslink: | $(CROSSLINK)
  171. @echo "Updating intra-repository dependencies in all go modules" \
  172. && $(CROSSLINK) --root=$(shell pwd) --prune
  173. .PHONY: go-mod-tidy
  174. go-mod-tidy: $(ALL_GO_MOD_DIRS:%=go-mod-tidy/%)
  175. go-mod-tidy/%: DIR=$*
  176. go-mod-tidy/%: | crosslink
  177. @echo "$(GO) mod tidy in $(DIR)" \
  178. && cd $(DIR) \
  179. && $(GO) mod tidy -compat=1.20
  180. .PHONY: lint-modules
  181. lint-modules: go-mod-tidy
  182. .PHONY: lint
  183. lint: misspell lint-modules golangci-lint govulncheck
  184. .PHONY: vanity-import-check
  185. vanity-import-check: | $(PORTO)
  186. @$(PORTO) --include-internal -l . || ( echo "(run: make vanity-import-fix)"; exit 1 )
  187. .PHONY: misspell
  188. misspell: | $(MISSPELL)
  189. @$(MISSPELL) -w $(ALL_DOCS)
  190. .PHONY: govulncheck
  191. govulncheck: $(OTEL_GO_MOD_DIRS:%=govulncheck/%)
  192. govulncheck/%: DIR=$*
  193. govulncheck/%: | $(GOVULNCHECK)
  194. @echo "govulncheck ./... in $(DIR)" \
  195. && cd $(DIR) \
  196. && $(GOVULNCHECK) ./...
  197. .PHONY: codespell
  198. codespell: | $(CODESPELL)
  199. @$(DOCKERPY) $(CODESPELL)
  200. .PHONY: license-check
  201. license-check:
  202. @licRes=$$(for f in $$(find . -type f \( -iname '*.go' -o -iname '*.sh' \) ! -path '**/third_party/*' ! -path './.git/*' ) ; do \
  203. awk '/Copyright The OpenTelemetry Authors|generated|GENERATED/ && NR<=4 { found=1; next } END { if (!found) print FILENAME }' $$f; \
  204. done); \
  205. if [ -n "$${licRes}" ]; then \
  206. echo "license header checking failed:"; echo "$${licRes}"; \
  207. exit 1; \
  208. fi
  209. DEPENDABOT_CONFIG = .github/dependabot.yml
  210. .PHONY: dependabot-check
  211. dependabot-check: | $(DBOTCONF)
  212. @$(DBOTCONF) verify $(DEPENDABOT_CONFIG) || ( echo "(run: make dependabot-generate)"; exit 1 )
  213. .PHONY: dependabot-generate
  214. dependabot-generate: | $(DBOTCONF)
  215. @$(DBOTCONF) generate > $(DEPENDABOT_CONFIG)
  216. .PHONY: check-clean-work-tree
  217. check-clean-work-tree:
  218. @if ! git diff --quiet; then \
  219. echo; \
  220. echo 'Working tree is not clean, did you forget to run "make precommit"?'; \
  221. echo; \
  222. git status; \
  223. exit 1; \
  224. fi
  225. SEMCONVPKG ?= "semconv/"
  226. .PHONY: semconv-generate
  227. semconv-generate: | $(SEMCONVGEN) $(SEMCONVKIT)
  228. [ "$(TAG)" ] || ( echo "TAG unset: missing opentelemetry semantic-conventions tag"; exit 1 )
  229. [ "$(OTEL_SEMCONV_REPO)" ] || ( echo "OTEL_SEMCONV_REPO unset: missing path to opentelemetry semantic-conventions repo"; exit 1 )
  230. $(SEMCONVGEN) -i "$(OTEL_SEMCONV_REPO)/model/." --only=span -p conventionType=trace -f trace.go -t "$(SEMCONVPKG)/template.j2" -s "$(TAG)"
  231. $(SEMCONVGEN) -i "$(OTEL_SEMCONV_REPO)/model/." --only=attribute_group -p conventionType=trace -f attribute_group.go -t "$(SEMCONVPKG)/template.j2" -s "$(TAG)"
  232. $(SEMCONVGEN) -i "$(OTEL_SEMCONV_REPO)/model/." --only=event -p conventionType=event -f event.go -t "$(SEMCONVPKG)/template.j2" -s "$(TAG)"
  233. $(SEMCONVGEN) -i "$(OTEL_SEMCONV_REPO)/model/." --only=resource -p conventionType=resource -f resource.go -t "$(SEMCONVPKG)/template.j2" -s "$(TAG)"
  234. $(SEMCONVKIT) -output "$(SEMCONVPKG)/$(TAG)" -tag "$(TAG)"
  235. .PHONY: gorelease
  236. gorelease: $(OTEL_GO_MOD_DIRS:%=gorelease/%)
  237. gorelease/%: DIR=$*
  238. gorelease/%:| $(GORELEASE)
  239. @echo "gorelease in $(DIR):" \
  240. && cd $(DIR) \
  241. && $(GORELEASE) \
  242. || echo ""
  243. .PHONY: prerelease
  244. prerelease: | $(MULTIMOD)
  245. @[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
  246. $(MULTIMOD) verify && $(MULTIMOD) prerelease -m ${MODSET}
  247. COMMIT ?= "HEAD"
  248. .PHONY: add-tags
  249. add-tags: | $(MULTIMOD)
  250. @[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
  251. $(MULTIMOD) verify && $(MULTIMOD) tag -m ${MODSET} -c ${COMMIT}
  252. .PHONY: lint-markdown
  253. lint-markdown:
  254. docker run -v "$(CURDIR):$(WORKDIR)" docker://avtodev/markdown-lint:v1 -c $(WORKDIR)/.markdownlint.yaml $(WORKDIR)/**/*.md