Skip to content

Commit d7727ee

Browse files
committed
Generate protobuf code for Go and Python services
Part of open-telemetry#1787 to generate profobuf files for all services. This should help unblock open-telemetry#1754 to allow dependabot to manage dependecy upgrades for the Go services. There are new Makefile recipes for managing the protobuf files. 1. docker-generate-protobuf - to generate the protobuf files with docker so that the only dependency on the machine needed is docker. 2. clean - to remove the protobuf files generated 3. check-clean-work-tree - to check that the working tree is clean and to help with verifying that the protobuf files are updated for all of the services when there are changes to the protobuf definition. There's a new check in the GitHub Actions workflow to verify that the protobuf code is generated. It is only verifying that the protobuf code is generated for Go and Python, but other services can apply the same workflow by updating the docker-gen-proto.sh script to uncomment the function call for the service. Signed-off-by: Charlie Le <charlie_le@apple.com>
1 parent d953c81 commit d7727ee

File tree

19 files changed

+10589
-46
lines changed

19 files changed

+10589
-46
lines changed

.github/workflows/gen-check.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Clean Generation
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
jobs:
8+
protobufcheck:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v4
13+
- name: Generate
14+
run: make clean docker-generate-protobuf
15+
- name: Check Clean Work Tree
16+
run: make check-clean-work-tree

.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ test/tracetesting/tracetesting-vars.yaml
5252
/src/frontend/pb/
5353
/src/frontend/protos/
5454
/src/paymentservice/demo.proto
55-
/src/recommendationservice/demo_pb2*.py
5655
/src/shippingservice/proto/
57-
/src/productcatalogservice/genproto
5856
/src/currencyservice/proto
59-
/src/checkoutservice/genproto
6057
/src/accountingservice/genproto

Makefile

+19
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ generate-kubernetes-manifests:
125125
echo " name: otel-demo" >> kubernetes/opentelemetry-demo.yaml
126126
helm template opentelemetry-demo open-telemetry/opentelemetry-demo --namespace otel-demo | sed '/helm.sh\/chart\:/d' | sed '/helm.sh\/hook/d' | sed '/managed-by\: Helm/d' >> kubernetes/opentelemetry-demo.yaml
127127

128+
.PHONY: docker-generate-protobuf
129+
docker-generate-protobuf:
130+
./docker-gen-proto.sh
131+
132+
.PHONY: clean
133+
clean:
134+
rm -rf ./src/{checkoutservice,productcatalogservice}/genproto/oteldemo/
135+
rm -rf ./src/recommendationservice/{demo_pb2,demo_pb2_grpc}.py
136+
137+
.PHONY: check-clean-work-tree
138+
check-clean-work-tree:
139+
@if ! git diff --quiet; then \
140+
echo; \
141+
echo 'Working tree is not clean, did you forget to run "make docker-generate-protobuf"?'; \
142+
echo; \
143+
git status; \
144+
exit 1; \
145+
fi
146+
128147
.PHONY: start
129148
start:
130149
$(DOCKER_COMPOSE_CMD) $(DOCKER_COMPOSE_ENV) up --force-recreate --remove-orphans --detach

docker-gen-proto.sh

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
# Copyright The OpenTelemetry Authors
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
set -e # Exit immediately if a command exits with a non-zero status.
6+
set -x # Print commands and their arguments as they are executed
7+
8+
# This script is used to generate protobuf files for all services with Docker.
9+
10+
gen_proto_go() {
11+
echo "Generating Go protobuf files for $1"
12+
docker build -f "src/$1/genproto/Dockerfile" -t "$1-genproto" .
13+
docker run -v $(pwd):/build "$1-genproto" \
14+
protoc -I /build/pb /build/pb/demo.proto --go_out="./src/$1/" --go-grpc_out="./src/$1/"
15+
}
16+
17+
gen_proto_python() {
18+
echo "Generating Python protobuf files for $1"
19+
docker build -f "src/$1/genproto/Dockerfile" -t "$1-genproto" .
20+
docker run -v $(pwd):/build "$1-genproto" \
21+
python -m grpc_tools.protoc -I /build/pb/ /build/pb/demo.proto --python_out="./src/$1/" --grpc_python_out="./src/$1/"
22+
}
23+
24+
#gen_proto_dotnet accountingservice
25+
#gen_proto_java adservice
26+
#gen_proto_dotnet cartservice
27+
gen_proto_go checkoutservice
28+
#gen_proto_cpp currencyservice
29+
#gen_proto_ruby emailservice
30+
#gen_proto_ts frontend
31+
#gen_proto_js paymentservice
32+
gen_proto_go productcatalogservice
33+
#gen_proto_php quoteservice
34+
gen_proto_python recommendationservice
35+
#gen_proto_rust shippingservice

src/checkoutservice/Dockerfile

+4-10
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,21 @@ FROM golang:1.22-alpine AS builder
66

77
WORKDIR /usr/src/app/
88

9-
RUN apk update \
10-
&& apk add --no-cache make protobuf-dev
11-
129
RUN --mount=type=cache,target=/go/pkg/mod/ \
1310
--mount=type=bind,source=./src/checkoutservice/go.sum,target=go.sum \
1411
--mount=type=bind,source=./src/checkoutservice/go.mod,target=go.mod \
15-
--mount=type=bind,source=./src/checkoutservice/tools.go,target=tools.go \
16-
go mod download \
17-
&& go list -e -f '{{range .Imports}}{{.}} {{end}}' tools.go | CGO_ENABLED=0 xargs go install -mod=readonly
12+
go mod download
1813

1914
RUN --mount=type=cache,target=/go/pkg/mod/ \
2015
--mount=type=cache,target=/root/.cache/go-build \
2116
--mount=type=bind,rw,source=./src/checkoutservice,target=. \
22-
--mount=type=bind,rw,source=./pb,target=./pb \
23-
protoc -I ./pb ./pb/demo.proto --go_out=./ --go-grpc_out=./ \
24-
&& go build -ldflags "-s -w" -o /go/bin/checkoutservice/ ./
17+
go build -ldflags "-s -w" -o /go/bin/checkoutservice/ ./
2518

26-
FROM alpine
19+
FROM alpine AS release
2720

2821
WORKDIR /usr/src/app/
2922

23+
COPY ./src/checkoutservice/products/ ./products/
3024
COPY --from=builder /go/bin/checkoutservice/ ./
3125

3226
EXPOSE ${CHECKOUT_SERVICE_PORT}

src/checkoutservice/README.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ docker compose build checkoutservice
2020

2121
## Regenerate protos
2222

23-
> [!NOTE]
24-
> [`protoc`](https://grpc.io/docs/protoc-installation/) is required.
25-
26-
To regenerate gRPC code run:
23+
To build the protos, run from the root directory:
2724

2825
```sh
29-
go generate
26+
make docker-generate-protobuf
3027
```
3128

3229
## Bump dependencies
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
FROM golang:1.22-alpine
5+
6+
WORKDIR /build
7+
8+
RUN apk add --no-cache protobuf-dev
9+
10+
COPY ./src/checkoutservice/go.mod ./
11+
COPY ./src/checkoutservice/go.sum ./
12+
COPY ./src/checkoutservice/tools.go ./
13+
14+
RUN go env -w GOMODCACHE=/root/.cache/go-build
15+
RUN --mount=type=cache,target=/root/.cache/go-build \
16+
go list -e -f '{{range .Imports}}{{.}} {{end}}' tools.go | CGO_ENABLED=0 xargs go install -mod=readonly

0 commit comments

Comments
 (0)