go services: do not fail (+retry) profiler init
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
cca50463ee
commit
5bb1a3f400
4 changed files with 86 additions and 35 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"cloud.google.com/go/profiler"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"go.opencensus.io/exporter/stackdriver"
|
"go.opencensus.io/exporter/stackdriver"
|
||||||
"go.opencensus.io/plugin/ocgrpc"
|
"go.opencensus.io/plugin/ocgrpc"
|
||||||
|
@ -18,8 +19,6 @@ import (
|
||||||
|
|
||||||
pb "checkoutservice/genproto"
|
pb "checkoutservice/genproto"
|
||||||
money "checkoutservice/money"
|
money "checkoutservice/money"
|
||||||
|
|
||||||
"cloud.google.com/go/profiler"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -37,22 +36,14 @@ type checkoutService struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := profiler.Start(profiler.Config{
|
go initTracing()
|
||||||
Service: "cartservice",
|
go initProfiling("checkoutservice", "1.0.0")
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
port := listenPort
|
port := listenPort
|
||||||
if os.Getenv("PORT") != "" {
|
if os.Getenv("PORT") != "" {
|
||||||
port = os.Getenv("PORT")
|
port = os.Getenv("PORT")
|
||||||
}
|
}
|
||||||
|
|
||||||
go initTracing()
|
|
||||||
|
|
||||||
svc := new(checkoutService)
|
svc := new(checkoutService)
|
||||||
mustMapEnv(&svc.shippingSvcAddr, "SHIPPING_SERVICE_ADDR")
|
mustMapEnv(&svc.shippingSvcAddr, "SHIPPING_SERVICE_ADDR")
|
||||||
mustMapEnv(&svc.productCatalogSvcAddr, "PRODUCT_CATALOG_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")
|
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) {
|
func mustMapEnv(target *string, envKey string) {
|
||||||
v := os.Getenv(envKey)
|
v := os.Getenv(envKey)
|
||||||
if v == "" {
|
if v == "" {
|
||||||
|
|
|
@ -63,22 +63,14 @@ type frontendServer struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := profiler.Start(profiler.Config{
|
go initProfiling(frontend, "1.0.0")
|
||||||
Service: "frontendservice",
|
go initTracing(log)
|
||||||
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()
|
ctx := context.Background()
|
||||||
log := logrus.New()
|
log := logrus.New()
|
||||||
log.Level = logrus.DebugLevel
|
log.Level = logrus.DebugLevel
|
||||||
log.Formatter = &logrus.TextFormatter{}
|
log.Formatter = &logrus.TextFormatter{}
|
||||||
|
|
||||||
go initTracing(log)
|
|
||||||
|
|
||||||
srvPort := port
|
srvPort := port
|
||||||
if os.Getenv("PORT") != "" {
|
if os.Getenv("PORT") != "" {
|
||||||
srvPort = os.Getenv("PORT")
|
srvPort = os.Getenv("PORT")
|
||||||
|
@ -142,6 +134,25 @@ func initTracing(log logrus.FieldLogger) {
|
||||||
log.Warn("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) {
|
||||||
|
// 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) {
|
func mustMapEnv(target *string, envKey string) {
|
||||||
v := os.Getenv(envKey)
|
v := os.Getenv(envKey)
|
||||||
if v == "" {
|
if v == "" {
|
||||||
|
|
|
@ -88,18 +88,9 @@ var catalog = []*pb.Product{
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
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 initTracing()
|
||||||
|
go initProfiling("productcatalogservice", "1.0.0")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
log.Printf("starting grpc server at :%d", *port)
|
log.Printf("starting grpc server at :%d", *port)
|
||||||
run(*port)
|
run(*port)
|
||||||
|
@ -137,6 +128,25 @@ func initTracing() {
|
||||||
log.Printf("warning: could not initialize stackdriver exporter after retrying, giving up")
|
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{}
|
type productCatalog struct{}
|
||||||
|
|
||||||
func (p *productCatalog) ListProducts(context.Context, *pb.Empty) (*pb.ListProductsResponse, error) {
|
func (p *productCatalog) ListProducts(context.Context, *pb.Empty) (*pb.ListProductsResponse, error) {
|
||||||
|
|
|
@ -23,6 +23,9 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
go initTracing()
|
||||||
|
go initProfiling("shippingservice", "1.0.0")
|
||||||
|
|
||||||
port := defaultPort
|
port := defaultPort
|
||||||
if value, ok := os.LookupEnv("APP_PORT"); ok {
|
if value, ok := os.LookupEnv("APP_PORT"); ok {
|
||||||
port = value
|
port = value
|
||||||
|
@ -38,8 +41,6 @@ func main() {
|
||||||
log.Fatalf("failed to start profiler: %+v", err)
|
log.Fatalf("failed to start profiler: %+v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go initTracing()
|
|
||||||
|
|
||||||
lis, err := net.Listen("tcp", port)
|
lis, err := net.Listen("tcp", port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to listen: %v", err)
|
log.Fatalf("failed to listen: %v", err)
|
||||||
|
@ -116,3 +117,22 @@ func initTracing() {
|
||||||
}
|
}
|
||||||
log.Printf("warning: could not initialize stackdriver exporter after retrying, giving up")
|
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")
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue