Added SLEEP_SECONDS env for product catalog
This commit is contained in:
parent
1d452f449a
commit
33921e7ddb
2 changed files with 42 additions and 4 deletions
|
@ -29,3 +29,11 @@ kubectl exec \
|
||||||
$(kubectl get pods -l app=productcatalogservice -o jsonpath='{.items[0].metadata.name}') \
|
$(kubectl get pods -l app=productcatalogservice -o jsonpath='{.items[0].metadata.name}') \
|
||||||
-c server -- kill -USR2 1
|
-c server -- kill -USR2 1
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Latency injection
|
||||||
|
|
||||||
|
This service has an "inject sleep" toggle that will sleep for `n` seconds on every call to
|
||||||
|
to the server.
|
||||||
|
|
||||||
|
You can set this mode as an environment variable. For instance, to sleep for 3 seconds,
|
||||||
|
use `SLEEP_SECONDS=3`.
|
||||||
|
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
@ -45,9 +46,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cat pb.ListProductsResponse
|
cat pb.ListProductsResponse
|
||||||
catalogMutex *sync.Mutex
|
catalogMutex *sync.Mutex
|
||||||
log *logrus.Logger
|
log *logrus.Logger
|
||||||
|
sleepDuration time.Duration
|
||||||
|
|
||||||
port = flag.Int("port", 3550, "port to listen at")
|
port = flag.Int("port", 3550, "port to listen at")
|
||||||
|
|
||||||
|
@ -73,6 +75,7 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
go initSleep()
|
||||||
go initTracing()
|
go initTracing()
|
||||||
go initProfiling("productcatalogservice", "1.0.0")
|
go initProfiling("productcatalogservice", "1.0.0")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
@ -117,7 +120,6 @@ func initJaegerTracing() {
|
||||||
log.Info("jaeger initialization disabled.")
|
log.Info("jaeger initialization disabled.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register the Jaeger exporter to be able to retrieve
|
// Register the Jaeger exporter to be able to retrieve
|
||||||
// the collected spans.
|
// the collected spans.
|
||||||
exporter, err := jaeger.NewExporter(jaeger.Options{
|
exporter, err := jaeger.NewExporter(jaeger.Options{
|
||||||
|
@ -143,6 +145,30 @@ func initStats(exporter *stackdriver.Exporter) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sets the sleepSeconds variable, if env variable provided
|
||||||
|
func initSleep() {
|
||||||
|
sleepTmp := os.Getenv("SLEEP_SECONDS")
|
||||||
|
if sleepTmp == "" {
|
||||||
|
sleepDuration = time.Duration(0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sleepInt, err := strconv.Atoi(sleepTmp)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("invalid SLEEP_SECONDS var")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if sleepInt < 1 {
|
||||||
|
log.Error("invalid SLEEP_SECONDS var, must be nonzero int")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sleepDuration = time.Second * time.Duration(sleepInt)
|
||||||
|
log.Infof("sleep enabled (%d seconds)", sleepInt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func injectSleep() {
|
||||||
|
time.Sleep(sleepDuration)
|
||||||
|
}
|
||||||
|
|
||||||
func initStackDriverTracing() {
|
func initStackDriverTracing() {
|
||||||
// TODO(ahmetb) this method is duplicated in other microservices using Go
|
// TODO(ahmetb) this method is duplicated in other microservices using Go
|
||||||
// since they are not sharing packages.
|
// since they are not sharing packages.
|
||||||
|
@ -222,14 +248,17 @@ func parseCatalog() []*pb.Product {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *productCatalog) Check(ctx context.Context, req *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) {
|
func (p *productCatalog) Check(ctx context.Context, req *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) {
|
||||||
|
injectSleep()
|
||||||
return &healthpb.HealthCheckResponse{Status: healthpb.HealthCheckResponse_SERVING}, nil
|
return &healthpb.HealthCheckResponse{Status: healthpb.HealthCheckResponse_SERVING}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *productCatalog) ListProducts(context.Context, *pb.Empty) (*pb.ListProductsResponse, error) {
|
func (p *productCatalog) ListProducts(context.Context, *pb.Empty) (*pb.ListProductsResponse, error) {
|
||||||
|
injectSleep()
|
||||||
return &pb.ListProductsResponse{Products: parseCatalog()}, nil
|
return &pb.ListProductsResponse{Products: parseCatalog()}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *productCatalog) GetProduct(ctx context.Context, req *pb.GetProductRequest) (*pb.Product, error) {
|
func (p *productCatalog) GetProduct(ctx context.Context, req *pb.GetProductRequest) (*pb.Product, error) {
|
||||||
|
injectSleep()
|
||||||
var found *pb.Product
|
var found *pb.Product
|
||||||
for i := 0; i < len(parseCatalog()); i++ {
|
for i := 0; i < len(parseCatalog()); i++ {
|
||||||
if req.Id == parseCatalog()[i].Id {
|
if req.Id == parseCatalog()[i].Id {
|
||||||
|
@ -243,6 +272,7 @@ func (p *productCatalog) GetProduct(ctx context.Context, req *pb.GetProductReque
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *productCatalog) SearchProducts(ctx context.Context, req *pb.SearchProductsRequest) (*pb.SearchProductsResponse, error) {
|
func (p *productCatalog) SearchProducts(ctx context.Context, req *pb.SearchProductsRequest) (*pb.SearchProductsResponse, error) {
|
||||||
|
injectSleep()
|
||||||
// Intepret query as a substring match in name or description.
|
// Intepret query as a substring match in name or description.
|
||||||
var ps []*pb.Product
|
var ps []*pb.Product
|
||||||
for _, p := range parseCatalog() {
|
for _, p := range parseCatalog() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue