-
Notifications
You must be signed in to change notification settings - Fork 81
/
Makefile
269 lines (218 loc) · 9.49 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# Obtain an absolute path to the directory of the Makefile.
# Assume the Makefile is in the root of the repository.
REPODIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
# Used by bpf2go to generate make compatible depinfo files.
export BPF2GO_MAKEBASE := $(REPODIR)
TOOLS_MOD_DIR := ./internal/tools
TOOLS = $(CURDIR)/.tools
ALL_GO_MOD_DIRS := $(shell find . -type f -name 'go.mod' ! -path './LICENSES/*' -exec dirname {} \; | sort)
ALL_GO_MODS := $(shell find . -type f -name 'go.mod' ! -path '$(TOOLS_MOD_DIR)/*' ! -path './LICENSES/*' | sort)
# BPF compile time dependencies.
BPF2GO_CFLAGS += -I${REPODIR}/internal/include/libbpf
BPF2GO_CFLAGS += -I${REPODIR}/internal/include
export BPF2GO_CFLAGS
# Go default variables
GOCMD?= go
CGO_ENABLED?=0
.DEFAULT_GOAL := precommit
.PHONY: precommit
precommit: license-header-check dependabot-generate golangci-lint-fix test codespell
# Tools
$(TOOLS):
@mkdir -p $@
$(TOOLS)/%: | $(TOOLS)
cd $(TOOLS_MOD_DIR) && \
$(GOCMD) build -buildvcs=false -o $@ $(PACKAGE)
MULTIMOD = $(TOOLS)/multimod
$(TOOLS)/multimod: PACKAGE=go.opentelemetry.io/build-tools/multimod
GOLICENSES = $(TOOLS)/go-licenses
$(TOOLS)/go-licenses: PACKAGE=github.com/google/go-licenses/v2
DBOTCONF = $(TOOLS)/dbotconf
$(TOOLS)/dbotconf: PACKAGE=go.opentelemetry.io/build-tools/dbotconf
IMG_NAME ?= otel-go-instrumentation
IMG_NAME_BASE = $(IMG_NAME)-base
GOLANGCI_LINT = $(TOOLS)/golangci-lint
$(TOOLS)/golangci-lint: PACKAGE=github.com/golangci/golangci-lint/cmd/golangci-lint
OFFSETGEN = $(TOOLS)/offsetgen
$(TOOLS)/offsetgen: PACKAGE=go.opentelemetry.io/auto/$(TOOLS_MOD_DIR)/inspect/cmd/offsetgen
.PHONY: tools
tools: $(GOLICENSES) $(MULTIMOD) $(GOLANGCI_LINT) $(DBOTCONF) $(OFFSETGEN)
TEST_TARGETS := test-verbose test-ebpf
.PHONY: $(TEST_TARGETS) test
test-ebpf: ARGS = -tags=ebpf_test -run ^TestEBPF # These need to be run with sudo.
test-verbose: ARGS = -v
$(TEST_TARGETS): test
test: go-mod-tidy generate $(ALL_GO_MODS:%=test/%)
test/%/go.mod:
@cd $* && $(GOCMD) test $(ARGS) ./...
PROBE_ROOT = internal/pkg/instrumentation/bpf/
PROBE_GEN_GO := $(shell find $(PROBE_ROOT) -type f -name 'bpf_*_bpfe[lb].go')
PROBE_GEN_OBJ := $(PROBE_GEN_GO:.go=.o)
PROBE_GEN_ALL := $(PROBE_GEN_GO) $(PROBE_GEN_OBJ)
# Include all depinfo files to ensure we only re-generate when needed.
-include $(shell find $(PROBE_ROOT) -type f -name 'bpf_*_bpfel.go.d')
.PHONY: generate generate/all
generate: $(PROBE_GEN_ALL)
$(PROBE_GEN_ALL):
$(GOCMD) generate ./$(dir $@)...
generate/all:
$(GOCMD) generate ./...
.PHONY: docker-generate
docker-generate: docker-build-base
docker run --rm -v $(shell pwd):/app $(IMG_NAME_BASE) /bin/sh -c "cd ../app && make generate"
.PHONY: docker-test
docker-test: docker-build-base
docker run --rm -v $(shell pwd):/app $(IMG_NAME_BASE) /bin/sh -c "cd ../app && make test"
.PHONY: docker-precommit
docker-precommit: docker-build-base
docker run --rm -v $(shell pwd):/app $(IMG_NAME_BASE) /bin/sh -c "cd ../app && make precommit"
.PHONY: go-mod-tidy
go-mod-tidy: $(ALL_GO_MOD_DIRS:%=go-mod-tidy/%)
go-mod-tidy/%: DIR=$*
go-mod-tidy/%:
@cd $(DIR) && $(GOCMD) mod tidy -compat=1.20
.PHONY: golangci-lint golangci-lint-fix
golangci-lint-fix: ARGS=--fix
golangci-lint-fix: golangci-lint
golangci-lint: go-mod-tidy generate $(ALL_GO_MOD_DIRS:%=golangci-lint/%)
golangci-lint/%: DIR=$*
golangci-lint/%: | $(GOLANGCI_LINT)
@echo 'golangci-lint $(if $(ARGS),$(ARGS) ,)$(DIR)' \
&& cd $(DIR) \
&& $(GOLANGCI_LINT) run --allow-serial-runners --timeout=2m0s $(ARGS)
.PHONY: build
build: go-mod-tidy generate
CGO_ENABLED=$(CGO_ENABLED) $(GOCMD) build -o otel-go-instrumentation ./cli/...
.PHONY: docker-build
docker-build:
docker buildx build -t $(IMG_NAME) .
.PHONY: docker-build-base
docker-build-base:
docker buildx build -t $(IMG_NAME_BASE) --target base .
OFFSETS_OUTPUT_FILE="$(REPODIR)/internal/pkg/inject/offset_results.json"
.PHONY: offsets
offsets: | $(OFFSETGEN)
$(OFFSETGEN) -output=$(OFFSETS_OUTPUT_FILE) -cache=$(OFFSETS_OUTPUT_FILE)
.PHONY: docker-offsets
docker-offsets:
docker run -e DOCKER_USERNAME=$(DOCKER_USERNAME) -e DOCKER_PASSWORD=$(DOCKER_PASSWORD) --rm -v /tmp:/tmp -v /var/run/docker.sock:/var/run/docker.sock -v $(shell pwd):/app golang:1.22 /bin/sh -c "cd ../app && make offsets"
.PHONY: update-licenses
update-licenses: generate $(GOLICENSES)
rm -rf LICENSES
$(GOLICENSES) save ./cli/ --save_path LICENSES
cp -R ./internal/include/libbpf ./LICENSES
.PHONY: verify-licenses
verify-licenses: generate $(GOLICENSES)
$(GOLICENSES) save ./cli --save_path temp
cp -R ./internal/include/libbpf ./temp; \
if diff temp LICENSES > /dev/null; then \
echo "Passed"; \
rm -rf temp; \
else \
echo "LICENSES directory must be updated. Run make update-licenses"; \
rm -rf temp; \
exit 1; \
fi; \
DEPENDABOT_CONFIG = .github/dependabot.yml
.PHONY: dependabot-check
dependabot-check: | $(DBOTCONF)
@$(DBOTCONF) --ignore "/LICENSES" verify $(DEPENDABOT_CONFIG) || ( echo "(run: make dependabot-generate)"; exit 1 )
.PHONY: dependabot-generate
dependabot-generate: | $(DBOTCONF)
@$(DBOTCONF) --ignore "/LICENSES" generate > $(DEPENDABOT_CONFIG)
.PHONY: license-header-check
license-header-check:
@licRes=$$(for f in $$(find . -type f \( -iname '*.go' -o -iname '*.sh' -o -iname '*.c' -o -iname '*.h' \) ! -path '**/third_party/*' ! -path './.git/*' ! -path './LICENSES/*' ! -path './internal/include/libbpf/*' ) ; do \
awk '/Copyright The OpenTelemetry Authors|generated|GENERATED/ && NR<=3 { found=1; next } END { if (!found) print FILENAME }' $$f; \
awk '/SPDX-License-Identifier: Apache-2.0|generated|GENERATED/ && NR<=4 { found=1; next } END { if (!found) print FILENAME }' $$f; \
done); \
if [ -n "$${licRes}" ]; then \
echo "license header checking failed:"; echo "$${licRes}"; \
exit 1; \
fi
.PHONY: fixture-nethttp fixture-gin fixture-databasesql fixture-nethttp-custom fixture-otelglobal fixture-autosdk fixture-kafka-go
fixture-nethttp-custom: fixtures/nethttp_custom
fixture-nethttp: fixtures/nethttp
fixture-gin: fixtures/gin
fixture-databasesql: fixtures/databasesql
fixture-grpc: fixtures/grpc
fixture-otelglobal: fixtures/otelglobal
fixture-autosdk: fixtures/autosdk
fixture-kafka-go: fixtures/kafka-go
fixtures/%: LIBRARY=$*
fixtures/%:
$(MAKE) docker-build
if [ -f ./internal/test/e2e/$(LIBRARY)/build.sh ]; then \
./internal/test/e2e/$(LIBRARY)/build.sh; \
else \
cd internal/test/e2e/$(LIBRARY) && docker build -t sample-app . ;\
fi
kind create cluster
kind load docker-image otel-go-instrumentation sample-app
helm repo add open-telemetry https://open-telemetry.github.io/opentelemetry-helm-charts
if [ ! -d "opentelemetry-helm-charts" ]; then \
git clone https://github.com/open-telemetry/opentelemetry-helm-charts.git; \
fi
if [ -f ./internal/test/e2e/$(LIBRARY)/collector-helm-values.yml ]; then \
helm install test -f ./internal/test/e2e/$(LIBRARY)/collector-helm-values.yml opentelemetry-helm-charts/charts/opentelemetry-collector; \
else \
helm install test -f .github/workflows/e2e/k8s/collector-helm-values.yml opentelemetry-helm-charts/charts/opentelemetry-collector; \
fi
while : ; do \
kubectl get pod/test-opentelemetry-collector-0 && break; \
sleep 5; \
done
kubectl wait --for=condition=Ready --timeout=60s pod/test-opentelemetry-collector-0
kubectl -n default create -f .github/workflows/e2e/k8s/sample-job.yml
if kubectl wait --for=condition=Complete --timeout=60s job/sample-job; then \
rm -f ./internal/test/e2e/$(LIBRARY)/traces-orig.json; \
kubectl cp -c filecp default/test-opentelemetry-collector-0:tmp/trace.json ./internal/test/e2e/$(LIBRARY)/traces-orig.json; \
rm -f ./internal/test/e2e/$(LIBRARY)/traces.json; \
bats ./internal/test/e2e/$(LIBRARY)/verify.bats; \
else \
kubectl logs -l app=sample -c auto-instrumentation; \
fi
kind delete cluster
.PHONY: prerelease
prerelease: | $(MULTIMOD)
@[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
$(MULTIMOD) verify && $(MULTIMOD) prerelease -m ${MODSET}
COMMIT ?= "HEAD"
.PHONY: add-tags
add-tags: | $(MULTIMOD)
@[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
$(MULTIMOD) verify && $(MULTIMOD) tag -m ${MODSET} -c ${COMMIT}
.PHONY: check-clean-work-tree
check-clean-work-tree:
if [ -n "$$(git status --porcelain)" ]; then \
git status; \
git --no-pager diff; \
echo 'Working tree is not clean, did you forget to run "make precommit", "make generate" or "make offsets"?'; \
exit 1; \
fi
# Virtualized python tools via docker
# The directory where the virtual environment is created.
VENVDIR := venv
# The directory where the python tools are installed.
PYTOOLS := $(VENVDIR)/bin
# The pip executable in the virtual environment.
PIP := $(PYTOOLS)/pip
# The directory in the docker image where the current directory is mounted.
WORKDIR := /workdir
# The python image to use for the virtual environment.
PYTHONIMAGE := python:3.11.3-slim-bullseye
# Run the python image with the current directory mounted.
DOCKERPY := docker run --rm -v "$(CURDIR):$(WORKDIR)" -w $(WORKDIR) $(PYTHONIMAGE)
# Create a virtual environment for Python tools.
$(PYTOOLS):
# The `--upgrade` flag is needed to ensure that the virtual environment is
# created with the latest pip version.
@$(DOCKERPY) bash -c "python3 -m venv $(VENVDIR) && $(PIP) install --upgrade pip"
# Install python packages into the virtual environment.
$(PYTOOLS)/%: $(PYTOOLS)
@$(DOCKERPY) $(PIP) install -r requirements.txt
CODESPELL = $(PYTOOLS)/codespell
$(CODESPELL): PACKAGE=codespell
.PHONY: codespell
codespell: $(CODESPELL)
@$(DOCKERPY) $(CODESPELL)