Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate protobuf code for Go and Python services #67

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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>
CharlieTLe committed Nov 25, 2024
commit d7727eeabfd0336446a91d39a7f31d93b7e4b162
16 changes: 16 additions & 0 deletions .github/workflows/gen-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Clean Generation
on:
push:
branches:
- main
pull_request:
jobs:
protobufcheck:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Generate
run: make clean docker-generate-protobuf
- name: Check Clean Work Tree
run: make check-clean-work-tree
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -52,9 +52,6 @@ test/tracetesting/tracetesting-vars.yaml
/src/frontend/pb/
/src/frontend/protos/
/src/paymentservice/demo.proto
/src/recommendationservice/demo_pb2*.py
/src/shippingservice/proto/
/src/productcatalogservice/genproto
/src/currencyservice/proto
/src/checkoutservice/genproto
/src/accountingservice/genproto
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -125,6 +125,25 @@ generate-kubernetes-manifests:
echo " name: otel-demo" >> kubernetes/opentelemetry-demo.yaml
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

.PHONY: docker-generate-protobuf
docker-generate-protobuf:
./docker-gen-proto.sh

.PHONY: clean
clean:
rm -rf ./src/{checkoutservice,productcatalogservice}/genproto/oteldemo/
rm -rf ./src/recommendationservice/{demo_pb2,demo_pb2_grpc}.py

.PHONY: check-clean-work-tree
check-clean-work-tree:
@if ! git diff --quiet; then \
echo; \
echo 'Working tree is not clean, did you forget to run "make docker-generate-protobuf"?'; \
echo; \
git status; \
exit 1; \
fi

.PHONY: start
start:
$(DOCKER_COMPOSE_CMD) $(DOCKER_COMPOSE_ENV) up --force-recreate --remove-orphans --detach
35 changes: 35 additions & 0 deletions docker-gen-proto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

set -e # Exit immediately if a command exits with a non-zero status.
set -x # Print commands and their arguments as they are executed

# This script is used to generate protobuf files for all services with Docker.

gen_proto_go() {
echo "Generating Go protobuf files for $1"
docker build -f "src/$1/genproto/Dockerfile" -t "$1-genproto" .
docker run -v $(pwd):/build "$1-genproto" \
protoc -I /build/pb /build/pb/demo.proto --go_out="./src/$1/" --go-grpc_out="./src/$1/"
}

gen_proto_python() {
echo "Generating Python protobuf files for $1"
docker build -f "src/$1/genproto/Dockerfile" -t "$1-genproto" .
docker run -v $(pwd):/build "$1-genproto" \
python -m grpc_tools.protoc -I /build/pb/ /build/pb/demo.proto --python_out="./src/$1/" --grpc_python_out="./src/$1/"
}

#gen_proto_dotnet accountingservice
#gen_proto_java adservice
#gen_proto_dotnet cartservice
gen_proto_go checkoutservice
#gen_proto_cpp currencyservice
#gen_proto_ruby emailservice
#gen_proto_ts frontend
#gen_proto_js paymentservice
gen_proto_go productcatalogservice
#gen_proto_php quoteservice
gen_proto_python recommendationservice
#gen_proto_rust shippingservice
14 changes: 4 additions & 10 deletions src/checkoutservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -6,27 +6,21 @@ FROM golang:1.22-alpine AS builder

WORKDIR /usr/src/app/

RUN apk update \
&& apk add --no-cache make protobuf-dev

RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,source=./src/checkoutservice/go.sum,target=go.sum \
--mount=type=bind,source=./src/checkoutservice/go.mod,target=go.mod \
--mount=type=bind,source=./src/checkoutservice/tools.go,target=tools.go \
go mod download \
&& go list -e -f '{{range .Imports}}{{.}} {{end}}' tools.go | CGO_ENABLED=0 xargs go install -mod=readonly
go mod download

RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=bind,rw,source=./src/checkoutservice,target=. \
--mount=type=bind,rw,source=./pb,target=./pb \
protoc -I ./pb ./pb/demo.proto --go_out=./ --go-grpc_out=./ \
&& go build -ldflags "-s -w" -o /go/bin/checkoutservice/ ./
go build -ldflags "-s -w" -o /go/bin/checkoutservice/ ./

FROM alpine
FROM alpine AS release

WORKDIR /usr/src/app/

COPY ./src/checkoutservice/products/ ./products/
COPY --from=builder /go/bin/checkoutservice/ ./

EXPOSE ${CHECKOUT_SERVICE_PORT}
7 changes: 2 additions & 5 deletions src/checkoutservice/README.md
Original file line number Diff line number Diff line change
@@ -20,13 +20,10 @@ docker compose build checkoutservice

## Regenerate protos

> [!NOTE]
> [`protoc`](https://grpc.io/docs/protoc-installation/) is required.

To regenerate gRPC code run:
To build the protos, run from the root directory:

```sh
go generate
make docker-generate-protobuf
```

## Bump dependencies
16 changes: 16 additions & 0 deletions src/checkoutservice/genproto/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

FROM golang:1.22-alpine

WORKDIR /build

RUN apk add --no-cache protobuf-dev

COPY ./src/checkoutservice/go.mod ./
COPY ./src/checkoutservice/go.sum ./
COPY ./src/checkoutservice/tools.go ./

RUN go env -w GOMODCACHE=/root/.cache/go-build
RUN --mount=type=cache,target=/root/.cache/go-build \
go list -e -f '{{range .Imports}}{{.}} {{end}}' tools.go | CGO_ENABLED=0 xargs go install -mod=readonly
Loading