Skip to content

Commit 0608e04

Browse files
authored
Merge pull request #67 from grafana/grpc
Add gRPC support + two gRPC endpoints + misc fixes
2 parents 3d57b10 + 14f3739 commit 0608e04

40 files changed

+596
-4720
lines changed

.github/workflows/k6-tests.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jobs:
1010
image: ghcr.io/grafana/quickpizza-local:latest
1111
ports:
1212
- 3333:3333
13+
- 3334:3334
1314

1415
steps:
1516
- name: Checkout
@@ -51,11 +52,11 @@ jobs:
5152
# act -W .github/workflows/k6-tests.yaml --container-architecture linux/amd64
5253

5354
- name: Run k6 foundations tests
54-
run: ./run-tests.sh -t **/k6/foundations/*.js -u http://localhost:3333
55+
run: ./scripts/run-tests.sh -t **/k6/foundations/*.js -u http://localhost:3333
5556
env:
5657
ACT: ${{ env.ACT }}
5758

5859
- name: Run k6 browser tests
59-
run: ./run-tests.sh -t **/k6/browser/*.js -u http://localhost:3333
60+
run: ./scripts/run-tests.sh -t **/k6/browser/*.js -u http://localhost:3333
6061
env:
6162
ACT: ${{ env.ACT }}

Makefile

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
GO_SOURCES=$(shell find . -type f -name '*.go' -not -path "./vendor/*")
2+
13
.PHONY: run
24
run:
35
go generate pkg/web/web.go
@@ -8,3 +10,15 @@ run:
810
build:
911
go generate pkg/web/web.go
1012
CGO_ENABLED=0 go build -o bin/quickpizza ./cmd
13+
14+
.PHONY: proto
15+
proto:
16+
protoc --go_out=. --go-grpc_out=. proto/quickpizza.proto
17+
18+
.PHONY: format
19+
format:
20+
@goimports -w -l $(GO_SOURCES)
21+
22+
.PHONY: format-check
23+
format-check:
24+
@out=$$(goimports -l $(GO_SOURCES)) && echo "$$out" && test -z "$$out"

cmd/main.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import (
99
"strings"
1010
"time"
1111

12+
"log/slog"
13+
1214
"github.com/grafana/pyroscope-go"
1315
"github.com/grafana/quickpizza/pkg/database"
16+
qpgrpc "github.com/grafana/quickpizza/pkg/grpc"
1417
qphttp "github.com/grafana/quickpizza/pkg/http"
1518
"github.com/hashicorp/go-retryablehttp"
1619
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1720
"go.opentelemetry.io/otel/propagation"
18-
"golang.org/x/exp/slog"
1921
)
2022

2123
func main() {
@@ -126,6 +128,17 @@ func main() {
126128
server = server.WithRecommendations(catalogClient, copyClient)
127129
}
128130

131+
if envServe("QUICKPIZZA_GRPC") {
132+
grpcServer := qpgrpc.NewServer(":3334")
133+
go func() {
134+
err := grpcServer.ListenAndServe()
135+
if err != nil {
136+
slog.Error("Running gRPC server", "err", err)
137+
os.Exit(1)
138+
}
139+
}()
140+
}
141+
129142
listen := ":3333"
130143
slog.Info("Starting QuickPizza", "listenAddress", listen)
131144
err = http.ListenAndServe(listen, server)

go.mod

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ require (
2727
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0
2828
go.opentelemetry.io/otel/sdk v1.28.0
2929
go.opentelemetry.io/otel/trace v1.28.0
30-
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63
30+
google.golang.org/grpc v1.65.0
31+
google.golang.org/protobuf v1.34.2
3132
)
3233

3334
require (
@@ -69,8 +70,6 @@ require (
6970
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
7071
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
7172
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
72-
google.golang.org/grpc v1.65.0 // indirect
73-
google.golang.org/protobuf v1.34.2 // indirect
7473
gopkg.in/yaml.v3 v3.0.1 // indirect
7574
lukechampine.com/uint128 v1.3.0 // indirect
7675
mellium.im/sasl v0.3.1 // indirect

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,6 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
326326
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
327327
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
328328
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
329-
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ=
330-
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8=
331329
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
332330
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
333331
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

k6/foundations/16.grpc.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Client, StatusOK } from 'k6/net/grpc';
2+
import { check, sleep } from 'k6';
3+
4+
const BASE_URL = 'localhost:3334';
5+
6+
const client = new Client();
7+
client.load(['definitions'], '../../../proto/quickpizza.proto');
8+
9+
export default () => {
10+
client.connect(BASE_URL, {
11+
plaintext: true
12+
});
13+
14+
const data = { ingredients: ["Pepperoni", "Mozzarella"], dough: "Stuffed" };
15+
const response = client.invoke('quickpizza.GRPC/EvaluatePizza', data);
16+
17+
check(response, {
18+
'status is OK': (r) => r && r.status === StatusOK,
19+
});
20+
21+
console.log(JSON.stringify(response.message));
22+
23+
client.close();
24+
sleep(1);
25+
};

pkg/database/catalog.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package database
33
import (
44
"context"
55

6+
"log/slog"
7+
68
"github.com/uptrace/bun"
79
"github.com/uptrace/bun/migrate"
8-
"golang.org/x/exp/slog"
910

1011
"github.com/grafana/quickpizza/pkg/database/migrations"
1112
"github.com/grafana/quickpizza/pkg/errorinjector"

pkg/database/copy.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package database
33
import (
44
"context"
55

6+
"log/slog"
7+
68
"github.com/grafana/quickpizza/pkg/database/migrations"
79
"github.com/grafana/quickpizza/pkg/model"
8-
"golang.org/x/exp/slog"
910

1011
"github.com/uptrace/bun"
1112
"github.com/uptrace/bun/migrate"

pkg/database/database.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import (
66
"runtime"
77
"strings"
88

9+
"log/slog"
10+
911
"github.com/grafana/quickpizza/pkg/logging"
1012
"github.com/uptrace/bun"
1113
"github.com/uptrace/bun/dialect/pgdialect"
1214
"github.com/uptrace/bun/dialect/sqlitedialect"
1315
"github.com/uptrace/bun/driver/pgdriver"
1416
"github.com/uptrace/bun/driver/sqliteshim"
1517
"github.com/uptrace/bun/extra/bunotel"
16-
"golang.org/x/exp/slog"
1718
)
1819

1920
func initializeDB(connString string) (*bun.DB, error) {

pkg/grpc/grpc.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package grpc
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log/slog"
7+
"math/rand"
8+
"net"
9+
10+
pb "github.com/grafana/quickpizza/pkg/grpc/quickpizza"
11+
"google.golang.org/grpc"
12+
)
13+
14+
type serverImplementation struct {
15+
pb.UnimplementedGRPCServer
16+
}
17+
18+
type Server struct {
19+
grpcServer *grpc.Server
20+
listen string
21+
}
22+
23+
func (s *serverImplementation) Status(_ context.Context, in *pb.StatusRequest) (*pb.StatusResponse, error) {
24+
return &pb.StatusResponse{Ready: true}, nil
25+
}
26+
27+
func (s *serverImplementation) EvaluatePizza(_ context.Context, in *pb.PizzaEvaluationRequest) (*pb.PizzaEvaluationResponse, error) {
28+
var rating int32
29+
if len(in.Ingredients) > 0 && in.Dough != "" {
30+
rating = rand.Int31n(6)
31+
}
32+
return &pb.PizzaEvaluationResponse{
33+
StarsRating: rating,
34+
}, nil
35+
}
36+
37+
func NewServer(listen string) *Server {
38+
s := grpc.NewServer()
39+
pb.RegisterGRPCServer(s, &serverImplementation{})
40+
41+
return &Server{grpcServer: s, listen: listen}
42+
}
43+
44+
func (s *Server) ListenAndServe() error {
45+
lis, err := net.Listen("tcp", s.listen)
46+
if err != nil {
47+
return fmt.Errorf("failed to listen on port: %w", err)
48+
}
49+
50+
slog.Info("Starting QuickPizza gRPC", "listenAddress", s.listen)
51+
return s.grpcServer.Serve(lis)
52+
}

0 commit comments

Comments
 (0)