Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change log format to JSON and log destination to stdout #48

Merged
merged 5 commits into from Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/checkoutservice/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/checkoutservice/Gopkg.toml
Expand Up @@ -41,6 +41,10 @@
name = "github.com/google/uuid"
version = "1.0.0"

[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.0.6"

[[constraint]]
name = "go.opencensus.io"
version = "0.16.0"
Expand Down
53 changes: 35 additions & 18 deletions src/checkoutservice/main.go
Expand Up @@ -17,14 +17,14 @@ package main
import (
"context"
"fmt"
"log"
"net"
"os"
"time"

"cloud.google.com/go/profiler"
"contrib.go.opencensus.io/exporter/stackdriver"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"go.opencensus.io/plugin/ocgrpc"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
Expand All @@ -42,6 +42,22 @@ const (
usdCurrency = "USD"
)

var log *logrus.Logger

func init() {
log = logrus.New()
log.Level = logrus.DebugLevel
log.Formatter = &logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "timestamp",
logrus.FieldKeyLevel: "severity",
logrus.FieldKeyMsg: "message",
},
TimestampFormat: time.RFC3339Nano,
}
log.Out = os.Stdout
}

type checkoutService struct {
productCatalogSvcAddr string
cartSvcAddr string
Expand All @@ -68,7 +84,7 @@ func main() {
mustMapEnv(&svc.emailSvcAddr, "EMAIL_SERVICE_ADDR")
mustMapEnv(&svc.paymentSvcAddr, "PAYMENT_SERVICE_ADDR")

log.Printf("service config: %+v", svc)
log.Infof("service config: %+v", svc)

lis, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
if err != nil {
Expand All @@ -77,16 +93,17 @@ func main() {
srv := grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{}))
pb.RegisterCheckoutServiceServer(srv, svc)
healthpb.RegisterHealthServer(srv, svc)
log.Printf("starting to listen on tcp: %q", lis.Addr().String())
log.Fatal(srv.Serve(lis))
log.Infof("starting to listen on tcp: %q", lis.Addr().String())
err = srv.Serve(lis)
log.Fatal(err)
}

func initStats(exporter *stackdriver.Exporter) {
view.RegisterExporter(exporter)
if err := view.Register(ocgrpc.DefaultServerViews...); err != nil {
log.Printf("Error registering default server views")
log.Warn("Error registering default server views")
} else {
log.Printf("Registered default server views")
log.Info("Registered default server views")
}
}

Expand All @@ -96,21 +113,21 @@ func initTracing() {
for i := 1; i <= 3; i++ {
exporter, err := stackdriver.NewExporter(stackdriver.Options{})
if err != nil {
log.Printf("info: failed to initialize stackdriver exporter: %+v", err)
log.Infof("failed to initialize stackdriver exporter: %+v", err)
} else {
trace.RegisterExporter(exporter)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
log.Print("registered stackdriver tracing")
log.Info("registered stackdriver tracing")

// Register the views to collect server stats.
initStats(exporter)
return
}
d := time.Second * 10 * time.Duration(i)
log.Printf("sleeping %v to retry initializing stackdriver exporter", d)
log.Infof("sleeping %v to retry initializing stackdriver exporter", d)
time.Sleep(d)
}
log.Printf("warning: could not initialize stackdriver exporter after retrying, giving up")
log.Warn("could not initialize stackdriver exporter after retrying, giving up")
}

func initProfiling(service, version string) {
Expand All @@ -123,16 +140,16 @@ func initProfiling(service, version string) {
// ProjectID must be set if not running on GCP.
// ProjectID: "my-project",
}); err != nil {
log.Printf("warn: failed to start profiler: %+v", err)
log.Warnf("failed to start profiler: %+v", err)
} else {
log.Print("started stackdriver profiler")
log.Info("started stackdriver profiler")
return
}
d := time.Second * 10 * time.Duration(i)
log.Printf("sleeping %v to retry initializing stackdriver profiler", d)
log.Infof("sleeping %v to retry initializing stackdriver profiler", d)
time.Sleep(d)
}
log.Printf("warning: could not initialize stackdriver profiler after retrying, giving up")
log.Warn("could not initialize stackdriver profiler after retrying, giving up")
}

func mustMapEnv(target *string, envKey string) {
Expand All @@ -148,7 +165,7 @@ func (cs *checkoutService) Check(ctx context.Context, req *healthpb.HealthCheckR
}

func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderRequest) (*pb.PlaceOrderResponse, error) {
log.Printf("[PlaceOrder] user_id=%q user_currency=%q", req.UserId, req.UserCurrency)
log.Infof("[PlaceOrder] user_id=%q user_currency=%q", req.UserId, req.UserCurrency)

orderID, err := uuid.NewUUID()
if err != nil {
Expand All @@ -172,7 +189,7 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to charge card: %+v", err)
}
log.Printf("payment went through (transaction_id: %s)", txID)
log.Infof("payment went through (transaction_id: %s)", txID)

shippingTrackingID, err := cs.shipOrder(ctx, req.Address, prep.cartItems)
if err != nil {
Expand All @@ -190,9 +207,9 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq
}

if err := cs.sendOrderConfirmation(ctx, req.Email, orderResult); err != nil {
log.Printf("failed to send order confirmation to %q: %+v", req.Email, err)
log.Warnf("failed to send order confirmation to %q: %+v", req.Email, err)
} else {
log.Printf("order confirmation email sent to %q", req.Email)
log.Infof("order confirmation email sent to %q", req.Email)
}
resp := &pb.PlaceOrderResponse{Order: orderResult}
return resp, nil
Expand Down
10 changes: 9 additions & 1 deletion src/frontend/main.go
Expand Up @@ -83,7 +83,15 @@ func main() {
ctx := context.Background()
log := logrus.New()
log.Level = logrus.DebugLevel
log.Formatter = &logrus.TextFormatter{}
log.Formatter = &logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "timestamp",
logrus.FieldKeyLevel: "severity",
logrus.FieldKeyMsg: "message",
},
TimestampFormat: time.RFC3339Nano,
}
log.Out = os.Stdout

go initProfiling(log, "frontend", "1.0.0")
go initTracing(log)
Expand Down
16 changes: 16 additions & 0 deletions src/productcatalogservice/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/productcatalogservice/Gopkg.toml
Expand Up @@ -41,6 +41,10 @@
name = "github.com/google/go-cmp"
version = "0.2.0"

[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.0.6"

[[constraint]]
name = "go.opencensus.io"
version = "0.16.0"
Expand Down
40 changes: 26 additions & 14 deletions src/productcatalogservice/server.go
Expand Up @@ -20,8 +20,8 @@ import (
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"strings"
"time"

Expand All @@ -31,6 +31,7 @@ import (
"cloud.google.com/go/profiler"
"contrib.go.opencensus.io/exporter/stackdriver"
"github.com/golang/protobuf/jsonpb"
"github.com/sirupsen/logrus"
"go.opencensus.io/plugin/ocgrpc"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
Expand All @@ -41,6 +42,7 @@ import (

var (
catalogJSON []byte
log *logrus.Logger

port = flag.Int("port", 3550, "port to listen at")
)
Expand All @@ -51,15 +53,25 @@ func init() {
log.Fatalf("failed to open product catalog json file: %v", err)
}
catalogJSON = c
log.Printf("successfully parsed product catalog json")
log = logrus.New()
log.Formatter = &logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "timestamp",
logrus.FieldKeyLevel: "severity",
logrus.FieldKeyMsg: "message",
},
TimestampFormat: time.RFC3339Nano,
}
log.Out = os.Stdout
log.Info("successfully parsed product catalog json")
}

func main() {
go initTracing()
go initProfiling("productcatalogservice", "1.0.0")
flag.Parse()

log.Printf("starting grpc server at :%d", *port)
log.Infof("starting grpc server at :%d", *port)
run(*port)
select {}
}
Expand All @@ -80,9 +92,9 @@ func run(port int) string {
func initStats(exporter *stackdriver.Exporter) {
view.RegisterExporter(exporter)
if err := view.Register(ocgrpc.DefaultServerViews...); err != nil {
log.Printf("Error registering default server views")
log.Info("Error registering default server views")
} else {
log.Printf("Registered default server views")
log.Info("Registered default server views")
}
}

Expand All @@ -92,21 +104,21 @@ func initTracing() {
for i := 1; i <= 3; i++ {
exporter, err := stackdriver.NewExporter(stackdriver.Options{})
if err != nil {
log.Printf("info: failed to initialize stackdriver exporter: %+v", err)
log.Warnf("failed to initialize stackdriver exporter: %+v", err)
} else {
trace.RegisterExporter(exporter)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
log.Print("registered stackdriver tracing")
log.Info("registered stackdriver tracing")

// Register the views to collect server stats.
initStats(exporter)
return
}
d := time.Second * 10 * time.Duration(i)
log.Printf("sleeping %v to retry initializing stackdriver exporter", d)
log.Infof("sleeping %v to retry initializing stackdriver exporter", d)
time.Sleep(d)
}
log.Printf("warning: could not initialize stackdriver exporter after retrying, giving up")
log.Warn("could not initialize stackdriver exporter after retrying, giving up")
}

func initProfiling(service, version string) {
Expand All @@ -119,16 +131,16 @@ func initProfiling(service, version string) {
// ProjectID must be set if not running on GCP.
// ProjectID: "my-project",
}); err != nil {
log.Printf("warn: failed to start profiler: %+v", err)
log.Warnf("failed to start profiler: %+v", err)
} else {
log.Print("started stackdriver profiler")
log.Info("started stackdriver profiler")
return
}
d := time.Second * 10 * time.Duration(i)
log.Printf("sleeping %v to retry initializing stackdriver profiler", d)
log.Infof("sleeping %v to retry initializing stackdriver profiler", d)
time.Sleep(d)
}
log.Printf("warning: could not initialize stackdriver profiler after retrying, giving up")
log.Warn("could not initialize stackdriver profiler after retrying, giving up")
}

type productCatalog struct{}
Expand All @@ -137,7 +149,7 @@ func parseCatalog() []*pb.Product {
var cat pb.ListProductsResponse

if err := jsonpb.Unmarshal(bytes.NewReader(catalogJSON), &cat); err != nil {
log.Printf("warning: failed to parse the catalog JSON: %v", err)
log.Warnf("failed to parse the catalog JSON: %v", err)
return nil
}
return cat.Products
Expand Down
16 changes: 16 additions & 0 deletions src/shippingservice/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.