add OpenCensus Go support for HTTP and gRPC
This commit is contained in:
parent
7101b6b4fe
commit
4a82b21b5e
8 changed files with 65 additions and 24 deletions
|
@ -6,6 +6,8 @@ import (
|
|||
"os"
|
||||
"time"
|
||||
|
||||
"microservices-demo/src/internal"
|
||||
|
||||
pb "./genproto"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
@ -20,9 +22,10 @@ func main() {
|
|||
|
||||
conn, err := grpc.DialContext(context.TODO(),
|
||||
"127.0.0.1:"+port,
|
||||
grpc.WithInsecure(),
|
||||
grpc.WithBlock(),
|
||||
grpc.WithTimeout(time.Second*3))
|
||||
grpc.WithTimeout(time.Second*3),
|
||||
internal.DefaultDialOptions(),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("probe failed: failed to connect: %+v", err)
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -7,9 +7,13 @@ import (
|
|||
"os"
|
||||
"time"
|
||||
|
||||
"microservices-demo/src/internal"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.opencensus.io/plugin/ochttp"
|
||||
"go.opencensus.io/plugin/ochttp/propagation/b3"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
|
@ -94,8 +98,13 @@ func main() {
|
|||
|
||||
log.Infof("starting server on " + addr + ":" + srvPort)
|
||||
loggedHandler := &logHandler{log: log, next: r}
|
||||
log.Fatal(http.ListenAndServe(addr+":"+srvPort,
|
||||
http.HandlerFunc(ensureSessionID(loggedHandler))))
|
||||
log.Fatal(http.ListenAndServe(
|
||||
addr+":"+srvPort,
|
||||
&ochttp.Handler{
|
||||
Handler: http.HandlerFunc(ensureSessionID(loggedHandler)),
|
||||
Propagation: &b3.HTTPFormat{},
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
func mustMapEnv(target *string, envKey string) {
|
||||
|
@ -108,7 +117,7 @@ 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.WithInsecure(), grpc.WithTimeout(time.Second*3))
|
||||
*conn, err = grpc.DialContext(ctx, addr, grpc.WithTimeout(time.Second*3), internal.DefaultDialOptions()...)
|
||||
if err != nil {
|
||||
panic(errors.Wrapf(err, "grpc: failed to connect %s", addr))
|
||||
}
|
||||
|
|
17
src/internal/internal.go
Normal file
17
src/internal/internal.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
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{}),
|
||||
}
|
||||
}
|
|
@ -8,6 +8,8 @@ import (
|
|||
"net"
|
||||
"strings"
|
||||
|
||||
"microservices-demo/src/internal"
|
||||
|
||||
pb "./genproto"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
|
@ -93,7 +95,7 @@ func run(port int) string {
|
|||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
srv := grpc.NewServer()
|
||||
srv := grpc.NewServer(internal.DefaultServerOptions()...)
|
||||
pb.RegisterProductCatalogServiceServer(srv, &productCatalog{})
|
||||
go srv.Serve(l)
|
||||
return l.Addr().String()
|
||||
|
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"microservices-demo/src/internal"
|
||||
"testing"
|
||||
|
||||
pb "./genproto"
|
||||
|
@ -15,7 +16,7 @@ import (
|
|||
func TestServer(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
addr := run(0)
|
||||
conn, err := grpc.Dial(addr, grpc.WithInsecure())
|
||||
conn, err := grpc.Dial(addr, internal.DefaultDialOptions())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@ import (
|
|||
"net"
|
||||
"os"
|
||||
|
||||
"microservices-demo/src/internal"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
|
@ -70,7 +72,7 @@ func main() {
|
|||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
s := grpc.NewServer()
|
||||
s := grpc.NewServer(internal.DefaultServerOptions()...)
|
||||
pb.RegisterShippingServiceServer(s, &server{})
|
||||
log.Printf("Shipping Service listening on port %s", port)
|
||||
|
||||
|
|
|
@ -5,7 +5,10 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
|
||||
"microservices-demo/src/internal"
|
||||
|
||||
pb "./genproto"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
|
@ -69,7 +72,7 @@ func main() {
|
|||
|
||||
func testProductCatalogService() error {
|
||||
addr := os.Getenv("PRODUCT_CATALOG_SERVICE_ADDR")
|
||||
conn, err := grpc.Dial(addr, grpc.WithInsecure())
|
||||
conn, err := grpc.Dial(addr, internal.DefaultDialOptions())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -104,7 +107,7 @@ func testProductCatalogService() error {
|
|||
|
||||
func testShippingService() error {
|
||||
addr := os.Getenv("SHIPPING_SERVICE_ADDR")
|
||||
conn, err := grpc.Dial(addr, grpc.WithInsecure())
|
||||
conn, err := grpc.Dial(addr, internal.DefaultDialOptions())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -150,7 +153,7 @@ func testShippingService() error {
|
|||
|
||||
func testRecommendationService() error {
|
||||
addr := os.Getenv("RECOMMENDATION_SERVICE_ADDR")
|
||||
conn, err := grpc.Dial(addr, grpc.WithInsecure())
|
||||
conn, err := grpc.Dial(addr, internal.DefaultDialOptions())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -172,7 +175,7 @@ func testRecommendationService() error {
|
|||
|
||||
func testPaymentService() error {
|
||||
addr := os.Getenv("PAYMENT_SERVICE_ADDR")
|
||||
conn, err := grpc.Dial(addr, grpc.WithInsecure())
|
||||
conn, err := grpc.Dial(addr, internal.DefaultDialOptions())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -200,7 +203,7 @@ func testPaymentService() error {
|
|||
|
||||
func testEmailService() error {
|
||||
addr := os.Getenv("EMAIL_SERVICE_ADDR")
|
||||
conn, err := grpc.Dial(addr, grpc.WithInsecure())
|
||||
conn, err := grpc.Dial(addr, internal.DefaultDialOptions())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -253,7 +256,7 @@ func testEmailService() error {
|
|||
|
||||
func testCurrencyService() error {
|
||||
addr := os.Getenv("CURRENCY_SERVICE_ADDR")
|
||||
conn, err := grpc.Dial(addr, grpc.WithInsecure())
|
||||
conn, err := grpc.Dial(addr, internal.DefaultDialOptions())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -284,7 +287,7 @@ func testCurrencyService() error {
|
|||
|
||||
func testCartService() error {
|
||||
addr := os.Getenv("CART_SERVICE_ADDR")
|
||||
conn, err := grpc.Dial(addr, grpc.WithInsecure())
|
||||
conn, err := grpc.Dial(addr, internal.DefaultDialOptions())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue