add OpenCensus Go support for HTTP and gRPC

This commit is contained in:
JBD 2018-07-10 17:36:55 -07:00
parent 7101b6b4fe
commit 4a82b21b5e
8 changed files with 65 additions and 24 deletions

View file

@ -7,6 +7,8 @@ import (
"net"
"os"
"microservices-demo/src/internal"
"github.com/google/uuid"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
@ -50,7 +52,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
srv := grpc.NewServer()
srv := grpc.NewServer(internal.DefaultServerOptions()...)
pb.RegisterCheckoutServiceServer(srv, svc)
log.Printf("starting to listen on tcp: %q", lis.Addr().String())
log.Fatal(srv.Serve(lis))
@ -147,7 +149,7 @@ 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, grpc.WithInsecure())
conn, err := grpc.DialContext(ctx, cs.shippingSvcAddr, internal.DefaultDialOptions()...)
if err != nil {
return nil, fmt.Errorf("could not connect shipping service: %+v", err)
}
@ -164,7 +166,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, grpc.WithInsecure())
conn, err := grpc.DialContext(ctx, cs.cartSvcAddr, internal.DefaultDialOptions()...)
if err != nil {
return nil, fmt.Errorf("could not connect cart service: %+v", err)
}
@ -178,7 +180,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, grpc.WithInsecure())
conn, err := grpc.DialContext(ctx, cs.cartSvcAddr, internal.DefaultDialOptions()...)
if err != nil {
return fmt.Errorf("could not connect cart service: %+v", err)
}
@ -193,7 +195,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, grpc.WithInsecure())
conn, err := grpc.DialContext(ctx, cs.productCatalogSvcAddr, internal.DefaultDialOptions()...)
if err != nil {
return nil, fmt.Errorf("could not connect product catalog service: %+v", err)
}
@ -217,7 +219,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, grpc.WithInsecure())
conn, err := grpc.DialContext(ctx, cs.currencySvcAddr, internal.DefaultDialOptions()...)
if err != nil {
return nil, fmt.Errorf("could not connect currency service: %+v", err)
}
@ -232,7 +234,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, grpc.WithInsecure())
conn, err := grpc.DialContext(ctx, cs.paymentSvcAddr, internal.DefaultDialOptions()...)
if err != nil {
return "", fmt.Errorf("failed to connect payment service: %+v", err)
}
@ -248,7 +250,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, grpc.WithInsecure())
conn, err := grpc.DialContext(ctx, cs.emailSvcAddr, internal.DefaultDialOptions()...)
if err != nil {
return fmt.Errorf("failed to connect email service: %+v", err)
}
@ -260,7 +262,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, grpc.WithInsecure())
conn, err := grpc.DialContext(ctx, cs.shippingSvcAddr, internal.DefaultDialOptions()...)
if err != nil {
return "", fmt.Errorf("failed to connect email service: %+v", err)
}
@ -273,3 +275,5 @@ func (cs *checkoutService) shipOrder(ctx context.Context, address *pb.Address, i
}
return resp.GetTrackingId(), nil
}
// TODO: Dial and create client once, reuse.