Skip to content

Commit 93b8f1a

Browse files
authored
Generate protobuf code for Go and Python services (open-telemetry#1794)
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. The dockerfiles for the Go and Python services have been updated to not generate the protobuf files during the build process. Instead, this function has been refactored into the genproto directory of each service. Signed-off-by: Charlie Le <[email protected]>
1 parent bb71866 commit 93b8f1a

22 files changed

+10588
-47
lines changed

.github/workflows/component-build-images.yml

+12
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,20 @@ on:
2626
type: string
2727

2828
jobs:
29+
protobufcheck:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- name: Checkout
33+
uses: actions/checkout@v4
34+
- name: Generate
35+
run: make clean docker-generate-protobuf
36+
- name: Check Clean Work Tree
37+
run: make check-clean-work-tree
38+
2939
build_and_push_images:
3040
runs-on: ubuntu-latest
41+
needs: protobufcheck
42+
3143
permissions:
3244
contents: read
3345
packages: write

.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

.licenserc.json

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
"src/featureflagservice/assets/vendor/",
4646
"src/featureflagservice/priv/",
4747
"src/productcatalogservice/genproto/",
48+
"src/recommendationservice/demo_pb2.py",
49+
"src/recommendationservice/demo_pb2_grpc.py",
4850
"internal/tools/"
4951
]
5052
}

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ the release.
2121
([#1786](https://github.com/open-telemetry/opentelemetry-demo/pull/1786))
2222
* [chore] Add multi-platform build support
2323
([#1785](https://github.com/open-telemetry/opentelemetry-demo/pull/1785))
24+
* [chore] Generate protobuf code for Go and Python services
25+
([#1794](https://github.com/open-telemetry/opentelemetry-demo/pull/1784))
2426

2527
## 1.12.0
2628

Makefile

+19
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,25 @@ generate-kubernetes-manifests:
133133
echo " name: otel-demo" >> kubernetes/opentelemetry-demo.yaml
134134
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
135135

136+
.PHONY: docker-generate-protobuf
137+
docker-generate-protobuf:
138+
./docker-gen-proto.sh
139+
140+
.PHONY: clean
141+
clean:
142+
rm -rf ./src/{checkoutservice,productcatalogservice}/genproto/oteldemo/
143+
rm -rf ./src/recommendationservice/{demo_pb2,demo_pb2_grpc}.py
144+
145+
.PHONY: check-clean-work-tree
146+
check-clean-work-tree:
147+
@if ! git diff --quiet; then \
148+
echo; \
149+
echo 'Working tree is not clean, did you forget to run "make docker-generate-protobuf"?'; \
150+
echo; \
151+
git status; \
152+
exit 1; \
153+
fi
154+
136155
.PHONY: start
137156
start:
138157
$(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 --rm -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 --rm -v $(pwd):/build "$1-genproto" \
21+
python -m grpc_tools.protoc -I /build/pb/ --python_out="./src/$1/" --grpc_python_out="./src/$1/" /build/pb/demo.proto
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

internal/tools/sanitycheck.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def sanitycheck(pattern, allow_utf8 = False, allow_eol = (CRLF, LF), indent = 1)
8484
retval += sanitycheck('**/*.md', allow_eol = (LF,))
8585
retval += sanitycheck('**/*.proj', allow_eol = (LF,), indent = 2)
8686
retval += sanitycheck('**/*.props', allow_eol = (LF,), indent = 2)
87-
retval += sanitycheck('**/*.py', allow_eol = (LF,), indent = 4)
87+
retval += sanitycheck('**/[!demo_pb2]*.py', allow_eol = (LF,), indent = 4)
8888
retval += sanitycheck('**/*.sln', allow_utf8 = True, indent = 4)
8989
retval += sanitycheck('**/*.targets', allow_eol = (LF,), indent = 2)
9090
retval += sanitycheck('**/*.xml', allow_eol = (LF,), indent = 2)

src/checkoutservice/Dockerfile

+2-9
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,15 @@ 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

2619
FROM alpine
2720

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)