From 1799f36dc67b577a601be8dee7930d2cc0f0337f Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Wed, 11 Jul 2018 19:55:36 -0700 Subject: [PATCH] shippingsercice: export traces to stackdriver Signed-off-by: Ahmet Alp Balkan --- src/shippingservice/Dockerfile | 4 +++- src/shippingservice/main.go | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/shippingservice/Dockerfile b/src/shippingservice/Dockerfile index 06b8a51..b04855d 100644 --- a/src/shippingservice/Dockerfile +++ b/src/shippingservice/Dockerfile @@ -7,7 +7,9 @@ WORKDIR /src/microservices-demo/shippingservice RUN go get -d golang.org/x/net/context \ google.golang.org/grpc \ google.golang.org/grpc/reflection \ - go.opencensus.io/plugin/ocgrpc + go.opencensus.io/exporter/stackdriver \ + go.opencensus.io/plugin/ocgrpc \ + go.opencensus.io/trace COPY . . # get other dependencies diff --git a/src/shippingservice/main.go b/src/shippingservice/main.go index d06d117..3776bda 100644 --- a/src/shippingservice/main.go +++ b/src/shippingservice/main.go @@ -6,7 +6,9 @@ import ( "net" "os" + "go.opencensus.io/exporter/stackdriver" "go.opencensus.io/plugin/ocgrpc" + "go.opencensus.io/trace" "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/reflection" @@ -15,7 +17,7 @@ import ( ) const ( - default_port = "50051" + defaultPort = "50051" ) // server controls RPC service responses. @@ -61,12 +63,21 @@ func (s *server) ShipOrder(ctx context.Context, in *pb.ShipOrderRequest) (*pb.Sh } func main() { - port := default_port + port := defaultPort if value, ok := os.LookupEnv("APP_PORT"); ok { port = value } port = fmt.Sprintf(":%s", port) + exporter, err := stackdriver.NewExporter(stackdriver.Options{}) + if err != nil { + log.Printf("failed to initialize stackdriver exporter: %+v", err) + log.Println("skipping uploading traces to stackdriver") + } else { + trace.RegisterExporter(exporter) + log.Println("registered stackdriver tracing") + } + lis, err := net.Listen("tcp", port) if err != nil { log.Fatalf("failed to listen: %v", err)