Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8ccbcd8

Browse files
committedNov 26, 2024·
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. 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 <charlie_le@apple.com>
1 parent bb71866 commit 8ccbcd8

File tree

22 files changed

+10595
-47
lines changed

22 files changed

+10595
-47
lines changed
 

‎.github/workflows/gen-check.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
name: Clean Generation
5+
on:
6+
push:
7+
branches:
8+
- main
9+
pull_request:
10+
jobs:
11+
protobufcheck:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
- name: Generate
17+
run: make clean docker-generate-protobuf
18+
- name: Check Clean Work Tree
19+
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

‎.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

‎src/checkoutservice/genproto/oteldemo/demo.pb.go

+3,343
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/checkoutservice/genproto/oteldemo/demo_grpc.pb.go

+1,323
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/productcatalogservice/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/productcatalogservice/go.sum,target=go.sum \
1411
--mount=type=bind,source=./src/productcatalogservice/go.mod,target=go.mod \
15-
--mount=type=bind,source=./src/productcatalogservice/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/productcatalogservice,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/productcatalogservice/ ./
17+
go build -ldflags "-s -w" -o /go/bin/productcatalogservice/ ./
2518

2619
FROM alpine AS release
2720

‎src/productcatalogservice/README.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@ docker compose build productcatalogservice
2525

2626
## Regenerate protos
2727

28-
> [!NOTE]
29-
> [`protoc`](https://grpc.io/docs/protoc-installation/) is required.
30-
31-
To regenerate gRPC code run:
28+
To build the protos, run from the root directory:
3229

3330
```sh
34-
go generate
31+
make docker-generate-protobuf
3532
```
3633

3734
## Bump dependencies
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/productcatalogservice/go.mod ./
11+
COPY ./src/productcatalogservice/go.sum ./
12+
COPY ./src/productcatalogservice/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 \

‎src/productcatalogservice/genproto/oteldemo/demo.pb.go

+3,343
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/productcatalogservice/genproto/oteldemo/demo_grpc.pb.go

+1,323
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/recommendationservice/Dockerfile

-11
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,12 @@ COPY ./src/recommendationservice/requirements.txt ./
1818
RUN pip install --upgrade pip
1919
RUN pip install --prefix="/reqs" -r requirements.txt
2020

21-
#
22-
# Build gRPC files
23-
#
24-
FROM base AS grpc-builder
25-
WORKDIR /usr/src/app/
26-
COPY ./pb/ ./proto/
27-
28-
RUN python -m pip install grpcio-tools==1.59.2
29-
RUN python -m grpc_tools.protoc -I=./proto/ --python_out=./ --grpc_python_out=./ ./proto/demo.proto
30-
3121
#
3222
# Runtime
3323
#
3424
FROM base AS runtime
3525
WORKDIR /usr/src/app/
3626
COPY --from=builder /reqs /usr/local
37-
COPY --from=grpc-builder /usr/src/app/ .
3827
COPY ./src/recommendationservice/ ./
3928

4029
RUN opentelemetry-bootstrap -a install

‎src/recommendationservice/README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ selected product.
55

66
## Local Build
77

8-
To build the protos, run:
8+
To build the protos, run from the root directory:
99

1010
```sh
11-
pip install -r requirements.txt
12-
python -m pip install grpcio-tools==1.48.2
13-
python -m grpc_tools.protoc -I=../pb/ --python_out=./ --grpc_python_out=./ ../pb/demo.proto
11+
make docker-generate-protobuf
1412
```
1513

1614
## Docker Build

‎src/recommendationservice/demo_pb2.py

+130
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/recommendationservice/demo_pb2_grpc.py

+1,005
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
FROM python:3.12-slim-bookworm
5+
6+
WORKDIR /build
7+
8+
RUN python -m pip install grpcio-tools==1.59.2

0 commit comments

Comments
 (0)
Please sign in to comment.