diff --git a/src/cartservice/Dockerfile b/src/cartservice/Dockerfile index 38db5ae..ff3dffc 100644 --- a/src/cartservice/Dockerfile +++ b/src/cartservice/Dockerfile @@ -8,7 +8,7 @@ RUN go build -o /cartservice_probe . # cartservice FROM gcr.io/google-appengine/aspnetcore:2.1.0 COPY --from=builder /cartservice_probe /cartservice_probe -RUN apt update && apt install net-tools telnet +RUN apt-get update && apt-get install net-tools telnet WORKDIR /app COPY . . RUN dotnet restore && \ diff --git a/src/cartservice/probe/main.go b/src/cartservice/probe/main.go index d9d69db..b937ff2 100644 --- a/src/cartservice/probe/main.go +++ b/src/cartservice/probe/main.go @@ -6,10 +6,9 @@ import ( "os" "time" - "microservices-demo/src/internal" - pb "./genproto" + "go.opencensus.io/plugin/ocgrpc" "google.golang.org/grpc" ) @@ -24,7 +23,8 @@ func main() { "127.0.0.1:"+port, grpc.WithBlock(), grpc.WithTimeout(time.Second*3), - internal.DefaultDialOptions(), + grpc.WithInsecure(), + grpc.WithStatsHandler(&ocgrpc.ClientHandler{}), ) if err != nil { log.Fatalf("probe failed: failed to connect: %+v", err) diff --git a/src/checkoutservice/Dockerfile b/src/checkoutservice/Dockerfile index ef6e45f..abe10c2 100644 --- a/src/checkoutservice/Dockerfile +++ b/src/checkoutservice/Dockerfile @@ -6,7 +6,8 @@ WORKDIR /go/src/checkoutservice RUN go get -d github.com/google/uuid \ google.golang.org/grpc \ google.golang.org/grpc/codes \ - google.golang.org/grpc/status + google.golang.org/grpc/status \ + go.opencensus.io/plugin/ocgrpc COPY . . # get remaining dependencies RUN go get -d ./... diff --git a/src/checkoutservice/main.go b/src/checkoutservice/main.go index 032af77..e99be0c 100644 --- a/src/checkoutservice/main.go +++ b/src/checkoutservice/main.go @@ -7,9 +7,8 @@ import ( "net" "os" - "microservices-demo/src/internal" - "github.com/google/uuid" + "go.opencensus.io/plugin/ocgrpc" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -52,7 +51,7 @@ func main() { if err != nil { log.Fatal(err) } - srv := grpc.NewServer(internal.DefaultServerOptions()...) + srv := grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{})) pb.RegisterCheckoutServiceServer(srv, svc) log.Printf("starting to listen on tcp: %q", lis.Addr().String()) log.Fatal(srv.Serve(lis)) @@ -149,7 +148,9 @@ func (cs *checkoutService) prepareOrderItemsAndShippingQuoteFromCart(ctx context } func (cs *checkoutService) quoteShipping(ctx context.Context, address *pb.Address, items []*pb.CartItem) (*pb.Money, error) { - conn, err := grpc.DialContext(ctx, cs.shippingSvcAddr, internal.DefaultDialOptions()...) + conn, err := grpc.DialContext(ctx, cs.shippingSvcAddr, + grpc.WithInsecure(), + grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return nil, fmt.Errorf("could not connect shipping service: %+v", err) } @@ -166,7 +167,7 @@ func (cs *checkoutService) quoteShipping(ctx context.Context, address *pb.Addres } func (cs *checkoutService) getUserCart(ctx context.Context, userID string) ([]*pb.CartItem, error) { - conn, err := grpc.DialContext(ctx, cs.cartSvcAddr, internal.DefaultDialOptions()...) + conn, err := grpc.DialContext(ctx, cs.cartSvcAddr, grpc.WithInsecure(), grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return nil, fmt.Errorf("could not connect cart service: %+v", err) } @@ -180,7 +181,7 @@ func (cs *checkoutService) getUserCart(ctx context.Context, userID string) ([]*p } func (cs *checkoutService) emptyUserCart(ctx context.Context, userID string) error { - conn, err := grpc.DialContext(ctx, cs.cartSvcAddr, internal.DefaultDialOptions()...) + conn, err := grpc.DialContext(ctx, cs.cartSvcAddr, grpc.WithInsecure(), grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return fmt.Errorf("could not connect cart service: %+v", err) } @@ -195,7 +196,7 @@ func (cs *checkoutService) emptyUserCart(ctx context.Context, userID string) err func (cs *checkoutService) prepOrderItems(ctx context.Context, items []*pb.CartItem, userCurrency string) ([]*pb.OrderItem, error) { out := make([]*pb.OrderItem, len(items)) - conn, err := grpc.DialContext(ctx, cs.productCatalogSvcAddr, internal.DefaultDialOptions()...) + conn, err := grpc.DialContext(ctx, cs.productCatalogSvcAddr, grpc.WithInsecure(), grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return nil, fmt.Errorf("could not connect product catalog service: %+v", err) } @@ -219,7 +220,7 @@ func (cs *checkoutService) prepOrderItems(ctx context.Context, items []*pb.CartI } func (cs *checkoutService) convertCurrency(ctx context.Context, from *pb.Money, toCurrency string) (*pb.Money, error) { - conn, err := grpc.DialContext(ctx, cs.currencySvcAddr, internal.DefaultDialOptions()...) + conn, err := grpc.DialContext(ctx, cs.currencySvcAddr, grpc.WithInsecure(), grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return nil, fmt.Errorf("could not connect currency service: %+v", err) } @@ -234,7 +235,7 @@ func (cs *checkoutService) convertCurrency(ctx context.Context, from *pb.Money, } func (cs *checkoutService) chargeCard(ctx context.Context, amount *pb.Money, paymentInfo *pb.CreditCardInfo) (string, error) { - conn, err := grpc.DialContext(ctx, cs.paymentSvcAddr, internal.DefaultDialOptions()...) + conn, err := grpc.DialContext(ctx, cs.paymentSvcAddr, grpc.WithInsecure(), grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return "", fmt.Errorf("failed to connect payment service: %+v", err) } @@ -250,7 +251,7 @@ func (cs *checkoutService) chargeCard(ctx context.Context, amount *pb.Money, pay } func (cs *checkoutService) sendOrderConfirmation(ctx context.Context, email string, order *pb.OrderResult) error { - conn, err := grpc.DialContext(ctx, cs.emailSvcAddr, internal.DefaultDialOptions()...) + conn, err := grpc.DialContext(ctx, cs.emailSvcAddr, grpc.WithInsecure(), grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return fmt.Errorf("failed to connect email service: %+v", err) } @@ -262,7 +263,7 @@ func (cs *checkoutService) sendOrderConfirmation(ctx context.Context, email stri } func (cs *checkoutService) shipOrder(ctx context.Context, address *pb.Address, items []*pb.CartItem) (string, error) { - conn, err := grpc.DialContext(ctx, cs.shippingSvcAddr, internal.DefaultDialOptions()...) + conn, err := grpc.DialContext(ctx, cs.shippingSvcAddr, grpc.WithInsecure(), grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return "", fmt.Errorf("failed to connect email service: %+v", err) } diff --git a/src/frontend/Dockerfile b/src/frontend/Dockerfile index 7ef2fe5..cb08630 100644 --- a/src/frontend/Dockerfile +++ b/src/frontend/Dockerfile @@ -9,7 +9,9 @@ RUN go get -d github.com/google/uuid \ github.com/sirupsen/logrus \ google.golang.org/grpc \ google.golang.org/grpc/codes \ - google.golang.org/grpc/status + google.golang.org/grpc/status \ + go.opencensus.io/plugin/ochttp \ + go.opencensus.io/plugin/ochttp/propagation/b3 # copy go-only part of the build COPY *.go ./ diff --git a/src/frontend/main.go b/src/frontend/main.go index 4caa5d4..863c469 100644 --- a/src/frontend/main.go +++ b/src/frontend/main.go @@ -7,11 +7,10 @@ import ( "os" "time" - "microservices-demo/src/internal" - "github.com/gorilla/mux" "github.com/pkg/errors" "github.com/sirupsen/logrus" + "go.opencensus.io/plugin/ocgrpc" "go.opencensus.io/plugin/ochttp" "go.opencensus.io/plugin/ochttp/propagation/b3" "google.golang.org/grpc" @@ -117,7 +116,10 @@ func mustMapEnv(target *string, envKey string) { func mustConnGRPC(ctx context.Context, conn **grpc.ClientConn, addr string) { var err error - *conn, err = grpc.DialContext(ctx, addr, grpc.WithTimeout(time.Second*3), internal.DefaultDialOptions()...) + *conn, err = grpc.DialContext(ctx, addr, + grpc.WithInsecure(), + grpc.WithTimeout(time.Second*3), + grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { panic(errors.Wrapf(err, "grpc: failed to connect %s", addr)) } diff --git a/src/internal/internal.go b/src/internal/internal.go deleted file mode 100644 index f952f68..0000000 --- a/src/internal/internal.go +++ /dev/null @@ -1,17 +0,0 @@ -package internal - -import "google.golang.org/grpc" -import "go.opencensus.io/plugin/ocgrpc" - -func DefaultDialOptions() []grpc.DialOption { - return []grpc.DialOption{ - grpc.WithInsecure(), - grpc.WithStatsHandler(&ocgrpc.ClientHandler{}), - } -} - -func DefaultServerOptions() []grpc.ServerOption { - return []grpc.ServerOption{ - grpc.StatsHandler(&ocgrpc.ServerHandler{}), - } -} diff --git a/src/productcatalogservice/Dockerfile b/src/productcatalogservice/Dockerfile index 1e4d411..0d20b54 100644 --- a/src/productcatalogservice/Dockerfile +++ b/src/productcatalogservice/Dockerfile @@ -6,7 +6,8 @@ WORKDIR /src/microservices-demo/productcatalogservice # get known dependencies RUN go get -d google.golang.org/grpc \ google.golang.org/grpc/codes \ - google.golang.org/grpc/status + google.golang.org/grpc/status \ + go.opencensus.io/plugin/ocgrpc COPY . . # get remaining dependencies diff --git a/src/productcatalogservice/server.go b/src/productcatalogservice/server.go index 702359e..755dacf 100644 --- a/src/productcatalogservice/server.go +++ b/src/productcatalogservice/server.go @@ -8,9 +8,8 @@ import ( "net" "strings" - "microservices-demo/src/internal" - pb "./genproto" + "go.opencensus.io/plugin/ocgrpc" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -95,7 +94,7 @@ func run(port int) string { if err != nil { log.Fatal(err) } - srv := grpc.NewServer(internal.DefaultServerOptions()...) + srv := grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{})) pb.RegisterProductCatalogServiceServer(srv, &productCatalog{}) go srv.Serve(l) return l.Addr().String() diff --git a/src/productcatalogservice/server_test.go b/src/productcatalogservice/server_test.go index ad137c6..5b3163b 100644 --- a/src/productcatalogservice/server_test.go +++ b/src/productcatalogservice/server_test.go @@ -2,12 +2,12 @@ package main import ( "context" - "microservices-demo/src/internal" "testing" pb "./genproto" "github.com/golang/protobuf/proto" "github.com/google/go-cmp/cmp" + "go.opencensus.io/plugin/ocgrpc" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -16,7 +16,9 @@ import ( func TestServer(t *testing.T) { ctx := context.Background() addr := run(0) - conn, err := grpc.Dial(addr, internal.DefaultDialOptions()) + conn, err := grpc.Dial(addr, + grpc.WithInsecure(), + grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { t.Fatal(err) } diff --git a/src/shippingservice/Dockerfile b/src/shippingservice/Dockerfile index b2c4c13..06b8a51 100644 --- a/src/shippingservice/Dockerfile +++ b/src/shippingservice/Dockerfile @@ -5,8 +5,10 @@ RUN apk add --no-cache \ WORKDIR /src/microservices-demo/shippingservice # get known dependencies RUN go get -d golang.org/x/net/context \ -google.golang.org/grpc \ -google.golang.org/grpc/reflection + google.golang.org/grpc \ + google.golang.org/grpc/reflection \ + go.opencensus.io/plugin/ocgrpc + COPY . . # get other dependencies RUN go get -d ./... diff --git a/src/shippingservice/main.go b/src/shippingservice/main.go index f2be6f0..d06d117 100644 --- a/src/shippingservice/main.go +++ b/src/shippingservice/main.go @@ -6,8 +6,7 @@ import ( "net" "os" - "microservices-demo/src/internal" - + "go.opencensus.io/plugin/ocgrpc" "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/reflection" @@ -72,7 +71,7 @@ func main() { if err != nil { log.Fatalf("failed to listen: %v", err) } - s := grpc.NewServer(internal.DefaultServerOptions()...) + s := grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{})) pb.RegisterShippingServiceServer(s, &server{}) log.Printf("Shipping Service listening on port %s", port) diff --git a/src/test-cli/main.go b/src/test-cli/main.go index d8c4c33..3c8c548 100644 --- a/src/test-cli/main.go +++ b/src/test-cli/main.go @@ -5,9 +5,8 @@ import ( "log" "os" - "microservices-demo/src/internal" - pb "./genproto" + "go.opencensus.io/plugin/ocgrpc" "google.golang.org/grpc" ) @@ -72,7 +71,8 @@ func main() { func testProductCatalogService() error { addr := os.Getenv("PRODUCT_CATALOG_SERVICE_ADDR") - conn, err := grpc.Dial(addr, internal.DefaultDialOptions()) + conn, err := grpc.Dial(addr, + grpc.WithInsecure(), grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return err } @@ -107,7 +107,8 @@ func testProductCatalogService() error { func testShippingService() error { addr := os.Getenv("SHIPPING_SERVICE_ADDR") - conn, err := grpc.Dial(addr, internal.DefaultDialOptions()) + conn, err := grpc.Dial(addr, + grpc.WithInsecure(), grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return err } @@ -153,7 +154,9 @@ func testShippingService() error { func testRecommendationService() error { addr := os.Getenv("RECOMMENDATION_SERVICE_ADDR") - conn, err := grpc.Dial(addr, internal.DefaultDialOptions()) + conn, err := grpc.Dial(addr, + grpc.WithInsecure(), + grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return err } @@ -175,7 +178,8 @@ func testRecommendationService() error { func testPaymentService() error { addr := os.Getenv("PAYMENT_SERVICE_ADDR") - conn, err := grpc.Dial(addr, internal.DefaultDialOptions()) + conn, err := grpc.Dial(addr, + grpc.WithInsecure(), grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return err } @@ -203,7 +207,8 @@ func testPaymentService() error { func testEmailService() error { addr := os.Getenv("EMAIL_SERVICE_ADDR") - conn, err := grpc.Dial(addr, internal.DefaultDialOptions()) + conn, err := grpc.Dial(addr, + grpc.WithInsecure(), grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return err } @@ -256,7 +261,8 @@ func testEmailService() error { func testCurrencyService() error { addr := os.Getenv("CURRENCY_SERVICE_ADDR") - conn, err := grpc.Dial(addr, internal.DefaultDialOptions()) + conn, err := grpc.Dial(addr, + grpc.WithInsecure(), grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return err } @@ -287,7 +293,8 @@ func testCurrencyService() error { func testCartService() error { addr := os.Getenv("CART_SERVICE_ADDR") - conn, err := grpc.Dial(addr, internal.DefaultDialOptions()) + conn, err := grpc.Dial(addr, + grpc.WithInsecure(), grpc.WithStatsHandler(&ocgrpc.ClientHandler{})) if err != nil { return err }