Merge branch 'master' of sso://user/ahmetb/microservices-demo

This commit is contained in:
morganmclean 2018-07-17 15:05:24 -07:00
commit 73ef09c64d
6 changed files with 97 additions and 56 deletions

View file

@ -8,6 +8,7 @@ import (
"os"
"time"
"cloud.google.com/go/profiler"
"github.com/google/uuid"
"go.opencensus.io/exporter/stackdriver"
"go.opencensus.io/plugin/ocgrpc"
@ -18,8 +19,6 @@ import (
pb "checkoutservice/genproto"
money "checkoutservice/money"
"cloud.google.com/go/profiler"
)
const (
@ -37,22 +36,14 @@ type checkoutService struct {
}
func main() {
if err := profiler.Start(profiler.Config{
Service: "cartservice",
ServiceVersion: "1.0.0",
// ProjectID must be set if not running on GCP.
// ProjectID: "my-project",
}); err != nil {
log.Fatalf("failed to start profiler: %+v", err)
}
go initTracing()
go initProfiling("checkoutservice", "1.0.0")
port := listenPort
if os.Getenv("PORT") != "" {
port = os.Getenv("PORT")
}
go initTracing()
svc := new(checkoutService)
mustMapEnv(&svc.shippingSvcAddr, "SHIPPING_SERVICE_ADDR")
mustMapEnv(&svc.productCatalogSvcAddr, "PRODUCT_CATALOG_SERVICE_ADDR")
@ -93,6 +84,25 @@ func initTracing() {
log.Printf("warning: could not initialize stackdriver exporter after retrying, giving up")
}
func initProfiling(service, version string) {
// TODO(ahmetb) this method is duplicated in other microservices using Go
// since they are not sharing packages.
for i := 1; i <= 3; i++ {
if err := profiler.Start(profiler.Config{
Service: service,
ServiceVersion: version,
// ProjectID must be set if not running on GCP.
// ProjectID: "my-project",
}); err != nil {
log.Printf("warn: failed to start profiler: %+v", err)
}
d := time.Second * 10 * time.Duration(i)
log.Printf("sleeping %v to retry initializing stackdriver profiler", d)
time.Sleep(d)
}
log.Printf("warning: could not initialize stackdriver profiler after retrying, giving up")
}
func mustMapEnv(target *string, envKey string) {
v := os.Getenv(envKey)
if v == "" {

View file

@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"time"
@ -63,20 +62,12 @@ type frontendServer struct {
}
func main() {
if err := profiler.Start(profiler.Config{
Service: "frontendservice",
ServiceVersion: "1.0.0",
// ProjectID must be set if not running on GCP.
// ProjectID: "my-project",
}); err != nil {
log.Fatalf("failed to start profiler: %+v", err)
}
ctx := context.Background()
log := logrus.New()
log.Level = logrus.DebugLevel
log.Formatter = &logrus.TextFormatter{}
go initProfiling(log, "frontend", "1.0.0")
go initTracing(log)
srvPort := port
@ -142,6 +133,26 @@ func initTracing(log logrus.FieldLogger) {
log.Warn("could not initialize stackdriver exporter after retrying, giving up")
}
func initProfiling(log logrus.FieldLogger, service, version string) {
// TODO(ahmetb) this method is duplicated in other microservices using Go
// since they are not sharing packages.
for i := 1; i <= 3; i++ {
log = log.WithField("retry", i)
if err := profiler.Start(profiler.Config{
Service: service,
ServiceVersion: version,
// ProjectID must be set if not running on GCP.
// ProjectID: "my-project",
}); err != nil {
log.Warnf("warn: failed to start profiler: %+v", err)
}
d := time.Second * 10 * time.Duration(i)
log.Debugf("sleeping %v to retry initializing stackdriver profiler", d)
time.Sleep(d)
}
log.Warn("warning: could not initialize stackdriver profiler after retrying, giving up")
}
func mustMapEnv(target *string, envKey string) {
v := os.Getenv(envKey)
if v == "" {

View file

@ -88,18 +88,9 @@ var catalog = []*pb.Product{
}
func main() {
if err := profiler.Start(profiler.Config{
Service: "productcatalogservice",
ServiceVersion: "1.0.0",
// ProjectID must be set if not running on GCP.
// ProjectID: "my-project",
}); err != nil {
log.Fatalf("failed to start profiler: %+v", err)
}
flag.Parse()
go initTracing()
go initProfiling("productcatalogservice", "1.0.0")
flag.Parse()
log.Printf("starting grpc server at :%d", *port)
run(*port)
@ -137,6 +128,25 @@ func initTracing() {
log.Printf("warning: could not initialize stackdriver exporter after retrying, giving up")
}
func initProfiling(service, version string) {
// TODO(ahmetb) this method is duplicated in other microservices using Go
// since they are not sharing packages.
for i := 1; i <= 3; i++ {
if err := profiler.Start(profiler.Config{
Service: service,
ServiceVersion: version,
// ProjectID must be set if not running on GCP.
// ProjectID: "my-project",
}); err != nil {
log.Printf("warn: failed to start profiler: %+v", err)
}
d := time.Second * 10 * time.Duration(i)
log.Printf("sleeping %v to retry initializing stackdriver profiler", d)
time.Sleep(d)
}
log.Printf("warning: could not initialize stackdriver profiler after retrying, giving up")
}
type productCatalog struct{}
func (p *productCatalog) ListProducts(context.Context, *pb.Empty) (*pb.ListProductsResponse, error) {

View file

@ -3,15 +3,13 @@ FROM grpc/python:1.0
# show python logs as they occur
ENV PYTHONUNBUFFERED=0
# add files into working directory
COPY . /home/
WORKDIR /home
# get packages
RUN apt-get update && apt-get install python3-pip -y
RUN pip install opencensus
RUN pip install google-cloud-trace
RUN pip install google-python-cloud-debugger
WORKDIR /recommendationservice
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
# add files into working directory
COPY . .
# set listen port
ENV PORT "8080"

View file

@ -1,4 +1,5 @@
grpcio-tools
googleapis-common-protos
opencensus
google-cloud-trace
grpcio==1.0.0
grpcio-tools==1.0.0
googleapis-common-protos==1.5.3
opencensus==0.1.5
google-cloud-trace==0.19.0

View file

@ -23,23 +23,15 @@ const (
)
func main() {
go initTracing()
go initProfiling("shippingservice", "1.0.0")
port := defaultPort
if value, ok := os.LookupEnv("APP_PORT"); ok {
port = value
}
port = fmt.Sprintf(":%s", port)
if err := profiler.Start(profiler.Config{
Service: "shippingservice",
ServiceVersion: "1.0.0",
// ProjectID must be set if not running on GCP.
// ProjectID: "my-project",
}); err != nil {
log.Fatalf("failed to start profiler: %+v", err)
}
go initTracing()
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
@ -116,3 +108,22 @@ func initTracing() {
}
log.Printf("warning: could not initialize stackdriver exporter after retrying, giving up")
}
func initProfiling(service, version string) {
// TODO(ahmetb) this method is duplicated in other microservices using Go
// since they are not sharing packages.
for i := 1; i <= 3; i++ {
if err := profiler.Start(profiler.Config{
Service: service,
ServiceVersion: version,
// ProjectID must be set if not running on GCP.
// ProjectID: "my-project",
}); err != nil {
log.Printf("warn: failed to start profiler: %+v", err)
}
d := time.Second * 10 * time.Duration(i)
log.Printf("sleeping %v to retry initializing stackdriver profiler", d)
time.Sleep(d)
}
log.Printf("warning: could not initialize stackdriver profiler after retrying, giving up")
}