Skip to content

Commit

Permalink
Merge pull request #67 from grafana/grpc
Browse files Browse the repository at this point in the history
Add gRPC support + two gRPC endpoints + misc fixes
  • Loading branch information
federicotdn authored Nov 25, 2024
2 parents 3d57b10 + 14f3739 commit 0608e04
Show file tree
Hide file tree
Showing 40 changed files with 596 additions and 4,720 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/k6-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
image: ghcr.io/grafana/quickpizza-local:latest
ports:
- 3333:3333
- 3334:3334

steps:
- name: Checkout
Expand Down Expand Up @@ -51,11 +52,11 @@ jobs:
# act -W .github/workflows/k6-tests.yaml --container-architecture linux/amd64

- name: Run k6 foundations tests
run: ./run-tests.sh -t **/k6/foundations/*.js -u http://localhost:3333
run: ./scripts/run-tests.sh -t **/k6/foundations/*.js -u http://localhost:3333
env:
ACT: ${{ env.ACT }}

- name: Run k6 browser tests
run: ./run-tests.sh -t **/k6/browser/*.js -u http://localhost:3333
run: ./scripts/run-tests.sh -t **/k6/browser/*.js -u http://localhost:3333
env:
ACT: ${{ env.ACT }}
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
GO_SOURCES=$(shell find . -type f -name '*.go' -not -path "./vendor/*")

.PHONY: run
run:
go generate pkg/web/web.go
Expand All @@ -8,3 +10,15 @@ run:
build:
go generate pkg/web/web.go
CGO_ENABLED=0 go build -o bin/quickpizza ./cmd

.PHONY: proto
proto:
protoc --go_out=. --go-grpc_out=. proto/quickpizza.proto

.PHONY: format
format:
@goimports -w -l $(GO_SOURCES)

.PHONY: format-check
format-check:
@out=$$(goimports -l $(GO_SOURCES)) && echo "$$out" && test -z "$$out"
15 changes: 14 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"strings"
"time"

"log/slog"

"github.com/grafana/pyroscope-go"
"github.com/grafana/quickpizza/pkg/database"
qpgrpc "github.com/grafana/quickpizza/pkg/grpc"
qphttp "github.com/grafana/quickpizza/pkg/http"
"github.com/hashicorp/go-retryablehttp"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/propagation"
"golang.org/x/exp/slog"
)

func main() {
Expand Down Expand Up @@ -126,6 +128,17 @@ func main() {
server = server.WithRecommendations(catalogClient, copyClient)
}

if envServe("QUICKPIZZA_GRPC") {
grpcServer := qpgrpc.NewServer(":3334")
go func() {
err := grpcServer.ListenAndServe()
if err != nil {
slog.Error("Running gRPC server", "err", err)
os.Exit(1)
}
}()
}

listen := ":3333"
slog.Info("Starting QuickPizza", "listenAddress", listen)
err = http.ListenAndServe(listen, server)
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0
go.opentelemetry.io/otel/sdk v1.28.0
go.opentelemetry.io/otel/trace v1.28.0
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
)

require (
Expand Down Expand Up @@ -69,8 +70,6 @@ require (
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.3.0 // indirect
mellium.im/sasl v0.3.1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,6 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ=
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
25 changes: 25 additions & 0 deletions k6/foundations/16.grpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Client, StatusOK } from 'k6/net/grpc';
import { check, sleep } from 'k6';

const BASE_URL = 'localhost:3334';

const client = new Client();
client.load(['definitions'], '../../../proto/quickpizza.proto');

export default () => {
client.connect(BASE_URL, {
plaintext: true
});

const data = { ingredients: ["Pepperoni", "Mozzarella"], dough: "Stuffed" };
const response = client.invoke('quickpizza.GRPC/EvaluatePizza', data);

check(response, {
'status is OK': (r) => r && r.status === StatusOK,
});

console.log(JSON.stringify(response.message));

client.close();
sleep(1);
};
3 changes: 2 additions & 1 deletion pkg/database/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package database
import (
"context"

"log/slog"

"github.com/uptrace/bun"
"github.com/uptrace/bun/migrate"
"golang.org/x/exp/slog"

"github.com/grafana/quickpizza/pkg/database/migrations"
"github.com/grafana/quickpizza/pkg/errorinjector"
Expand Down
3 changes: 2 additions & 1 deletion pkg/database/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package database
import (
"context"

"log/slog"

"github.com/grafana/quickpizza/pkg/database/migrations"
"github.com/grafana/quickpizza/pkg/model"
"golang.org/x/exp/slog"

"github.com/uptrace/bun"
"github.com/uptrace/bun/migrate"
Expand Down
3 changes: 2 additions & 1 deletion pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"runtime"
"strings"

"log/slog"

"github.com/grafana/quickpizza/pkg/logging"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/dialect/sqlitedialect"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/uptrace/bun/driver/sqliteshim"
"github.com/uptrace/bun/extra/bunotel"
"golang.org/x/exp/slog"
)

func initializeDB(connString string) (*bun.DB, error) {
Expand Down
52 changes: 52 additions & 0 deletions pkg/grpc/grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package grpc

import (
"context"
"fmt"
"log/slog"
"math/rand"
"net"

pb "github.com/grafana/quickpizza/pkg/grpc/quickpizza"
"google.golang.org/grpc"
)

type serverImplementation struct {
pb.UnimplementedGRPCServer
}

type Server struct {
grpcServer *grpc.Server
listen string
}

func (s *serverImplementation) Status(_ context.Context, in *pb.StatusRequest) (*pb.StatusResponse, error) {
return &pb.StatusResponse{Ready: true}, nil
}

func (s *serverImplementation) EvaluatePizza(_ context.Context, in *pb.PizzaEvaluationRequest) (*pb.PizzaEvaluationResponse, error) {
var rating int32
if len(in.Ingredients) > 0 && in.Dough != "" {
rating = rand.Int31n(6)
}
return &pb.PizzaEvaluationResponse{
StarsRating: rating,
}, nil
}

func NewServer(listen string) *Server {
s := grpc.NewServer()
pb.RegisterGRPCServer(s, &serverImplementation{})

return &Server{grpcServer: s, listen: listen}
}

func (s *Server) ListenAndServe() error {
lis, err := net.Listen("tcp", s.listen)
if err != nil {
return fmt.Errorf("failed to listen on port: %w", err)
}

slog.Info("Starting QuickPizza gRPC", "listenAddress", s.listen)
return s.grpcServer.Serve(lis)
}
Loading

0 comments on commit 0608e04

Please sign in to comment.