add opencensus option for go services
This commit is contained in:
parent
595bd21d38
commit
8bc3a86b97
6 changed files with 216 additions and 11 deletions
17
src/adservice/.project
Normal file
17
src/adservice/.project
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>adservice</name>
|
||||||
|
<comment>Project adservice created by Buildship.</comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
2
src/adservice/.settings/org.eclipse.buildship.core.prefs
Normal file
2
src/adservice/.settings/org.eclipse.buildship.core.prefs
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
connection.project.dir=
|
||||||
|
eclipse.preferences.version=1
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"cloud.google.com/go/profiler"
|
"cloud.google.com/go/profiler"
|
||||||
|
@ -69,8 +70,30 @@ type checkoutService struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
go initTracing()
|
ocStats, err := getenvBool("OC_STATS")
|
||||||
go initProfiling("checkoutservice", "1.0.0")
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
ocStats = false
|
||||||
|
|
||||||
|
ocTrace, err := getenvBool("OC_TRACE")
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
ocTrace = false
|
||||||
|
|
||||||
|
ocProfiling, err := getenvBool("OC_PROFILING")
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
ocProfiling = false
|
||||||
|
|
||||||
|
if ocTrace == true {
|
||||||
|
go initTracing()
|
||||||
|
}
|
||||||
|
if ocProfiling == true {
|
||||||
|
go initProfiling("checkoutservice", "1.0.0")
|
||||||
|
}
|
||||||
|
|
||||||
port := listenPort
|
port := listenPort
|
||||||
if os.Getenv("PORT") != "" {
|
if os.Getenv("PORT") != "" {
|
||||||
|
@ -91,7 +114,13 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
srv := grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{}))
|
|
||||||
|
var srv *grpc.Server
|
||||||
|
if ocStats == true {
|
||||||
|
srv = grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{}))
|
||||||
|
} else {
|
||||||
|
srv = grpc.NewServer()
|
||||||
|
}
|
||||||
pb.RegisterCheckoutServiceServer(srv, svc)
|
pb.RegisterCheckoutServiceServer(srv, svc)
|
||||||
healthpb.RegisterHealthServer(srv, svc)
|
healthpb.RegisterHealthServer(srv, svc)
|
||||||
log.Infof("starting to listen on tcp: %q", lis.Addr().String())
|
log.Infof("starting to listen on tcp: %q", lis.Addr().String())
|
||||||
|
@ -409,3 +438,23 @@ func (cs *checkoutService) shipOrder(ctx context.Context, address *pb.Address, i
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Dial and create client once, reuse.
|
// TODO: Dial and create client once, reuse.
|
||||||
|
|
||||||
|
func getenvBool(key string) (bool, error) {
|
||||||
|
s, err := getenvStr(key)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseBool(s)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getenvStr(key string) (string, error) {
|
||||||
|
v := os.Getenv(key)
|
||||||
|
if v == "" {
|
||||||
|
return v, fmt.Errorf("empty var")
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"cloud.google.com/go/profiler"
|
"cloud.google.com/go/profiler"
|
||||||
|
@ -94,8 +95,24 @@ func main() {
|
||||||
}
|
}
|
||||||
log.Out = os.Stdout
|
log.Out = os.Stdout
|
||||||
|
|
||||||
go initProfiling(log, "frontend", "1.0.0")
|
ocTrace, err := getenvBool("OC_TRACE")
|
||||||
go initTracing(log)
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
ocTrace = false
|
||||||
|
|
||||||
|
ocProfiling, err := getenvBool("OC_PROFILING")
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
ocProfiling = false
|
||||||
|
|
||||||
|
if ocTrace == true {
|
||||||
|
go initTracing(log)
|
||||||
|
}
|
||||||
|
if ocProfiling == true {
|
||||||
|
go initProfiling(log, "frontend", "1.0.0")
|
||||||
|
}
|
||||||
|
|
||||||
srvPort := port
|
srvPort := port
|
||||||
if os.Getenv("PORT") != "" {
|
if os.Getenv("PORT") != "" {
|
||||||
|
@ -260,3 +277,23 @@ func mustConnGRPC(ctx context.Context, conn **grpc.ClientConn, addr string) {
|
||||||
panic(errors.Wrapf(err, "grpc: failed to connect %s", addr))
|
panic(errors.Wrapf(err, "grpc: failed to connect %s", addr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getenvBool(key string) (bool, error) {
|
||||||
|
s, err := getenvStr(key)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseBool(s)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getenvStr(key string) (string, error) {
|
||||||
|
v := os.Getenv(key)
|
||||||
|
if v == "" {
|
||||||
|
return v, fmt.Errorf("empty var")
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
|
@ -74,8 +74,32 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
go initTracing()
|
var err error
|
||||||
go initProfiling("productcatalogservice", "1.0.0")
|
ocStats, err = getenvBool("OC_STATS")
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
ocStats = false
|
||||||
|
|
||||||
|
ocTrace, err := getenvBool("OC_TRACE")
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
ocTrace = false
|
||||||
|
|
||||||
|
ocProfiling, err := getenvBool("OC_PROFILING")
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
ocProfiling = false
|
||||||
|
|
||||||
|
if ocTrace == true {
|
||||||
|
go initTracing()
|
||||||
|
}
|
||||||
|
if ocProfiling == true {
|
||||||
|
go initProfiling("productcatalogservice", "1.0.0")
|
||||||
|
}
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// set injected latency
|
// set injected latency
|
||||||
|
@ -119,8 +143,15 @@ func run(port string) string {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
srv := grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{}))
|
var srv *grpc.Server
|
||||||
|
if ocStats == true {
|
||||||
|
srv = grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{}))
|
||||||
|
} else {
|
||||||
|
srv = grpc.NewServer()
|
||||||
|
}
|
||||||
|
|
||||||
svc := &productCatalog{}
|
svc := &productCatalog{}
|
||||||
|
|
||||||
pb.RegisterProductCatalogServiceServer(srv, svc)
|
pb.RegisterProductCatalogServiceServer(srv, svc)
|
||||||
healthpb.RegisterHealthServer(srv, svc)
|
healthpb.RegisterHealthServer(srv, svc)
|
||||||
go srv.Serve(l)
|
go srv.Serve(l)
|
||||||
|
@ -275,3 +306,23 @@ func (p *productCatalog) SearchProducts(ctx context.Context, req *pb.SearchProdu
|
||||||
}
|
}
|
||||||
return &pb.SearchProductsResponse{Results: ps}, nil
|
return &pb.SearchProductsResponse{Results: ps}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getenvBool(key string) (bool, error) {
|
||||||
|
s, err := getenvStr(key)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseBool(s)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getenvStr(key string) (string, error) {
|
||||||
|
v := os.Getenv(key)
|
||||||
|
if v == "" {
|
||||||
|
return v, fmt.Errorf("empty var")
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"cloud.google.com/go/profiler"
|
"cloud.google.com/go/profiler"
|
||||||
|
@ -58,8 +59,30 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
go initTracing()
|
ocStats, err := getenvBool("OC_STATS")
|
||||||
go initProfiling("shippingservice", "1.0.0")
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
ocStats = false
|
||||||
|
|
||||||
|
ocTrace, err := getenvBool("OC_TRACE")
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
ocTrace = false
|
||||||
|
|
||||||
|
ocProfiling, err := getenvBool("OC_PROFILING")
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
ocProfiling = false
|
||||||
|
|
||||||
|
if ocTrace == true {
|
||||||
|
go initTracing()
|
||||||
|
}
|
||||||
|
if ocProfiling == true {
|
||||||
|
go initProfiling("shippingservice", "1.0.0")
|
||||||
|
}
|
||||||
|
|
||||||
port := defaultPort
|
port := defaultPort
|
||||||
if value, ok := os.LookupEnv("PORT"); ok {
|
if value, ok := os.LookupEnv("PORT"); ok {
|
||||||
|
@ -71,7 +94,13 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to listen: %v", err)
|
log.Fatalf("failed to listen: %v", err)
|
||||||
}
|
}
|
||||||
srv := grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{}))
|
|
||||||
|
var srv *grpc.Server
|
||||||
|
if ocStats == true {
|
||||||
|
srv = grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{}))
|
||||||
|
} else {
|
||||||
|
srv = grpc.NewServer()
|
||||||
|
}
|
||||||
svc := &server{}
|
svc := &server{}
|
||||||
pb.RegisterShippingServiceServer(srv, svc)
|
pb.RegisterShippingServiceServer(srv, svc)
|
||||||
healthpb.RegisterHealthServer(srv, svc)
|
healthpb.RegisterHealthServer(srv, svc)
|
||||||
|
@ -216,3 +245,23 @@ func initProfiling(service, version string) {
|
||||||
}
|
}
|
||||||
log.Warn("could not initialize Stackdriver profiler after retrying, giving up")
|
log.Warn("could not initialize Stackdriver profiler after retrying, giving up")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getenvBool(key string) (bool, error) {
|
||||||
|
s, err := getenvStr(key)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseBool(s)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getenvStr(key string) (string, error) {
|
||||||
|
v := os.Getenv(key)
|
||||||
|
if v == "" {
|
||||||
|
return v, fmt.Errorf("empty var")
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue