diff --git a/src/checkoutservice/Dockerfile b/src/checkoutservice/Dockerfile index abe10c2..56578bd 100644 --- a/src/checkoutservice/Dockerfile +++ b/src/checkoutservice/Dockerfile @@ -7,7 +7,9 @@ RUN go get -d github.com/google/uuid \ google.golang.org/grpc \ google.golang.org/grpc/codes \ google.golang.org/grpc/status \ - go.opencensus.io/plugin/ocgrpc + go.opencensus.io/plugin/ocgrpc \ + go.opencensus.io/exporter/stackdriver \ + go.opencensus.io/trace COPY . . # get remaining dependencies RUN go get -d ./... diff --git a/src/checkoutservice/main.go b/src/checkoutservice/main.go index e99be0c..63bd08e 100644 --- a/src/checkoutservice/main.go +++ b/src/checkoutservice/main.go @@ -8,7 +8,9 @@ import ( "os" "github.com/google/uuid" + "go.opencensus.io/exporter/stackdriver" "go.opencensus.io/plugin/ocgrpc" + "go.opencensus.io/trace" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -37,6 +39,8 @@ func main() { port = os.Getenv("PORT") } + initTracing() + svc := new(checkoutService) mustMapEnv(&svc.shippingSvcAddr, "SHIPPING_SERVICE_ADDR") mustMapEnv(&svc.productCatalogSvcAddr, "PRODUCT_CATALOG_SERVICE_ADDR") @@ -57,6 +61,18 @@ func main() { log.Fatal(srv.Serve(lis)) } +func initTracing() { + 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) + trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()}) + log.Println("registered stackdriver tracing") + } +} + func mustMapEnv(target *string, envKey string) { v := os.Getenv(envKey) if v == "" { diff --git a/src/frontend/Dockerfile b/src/frontend/Dockerfile index cb08630..6dff288 100644 --- a/src/frontend/Dockerfile +++ b/src/frontend/Dockerfile @@ -11,7 +11,9 @@ RUN go get -d github.com/google/uuid \ google.golang.org/grpc/codes \ google.golang.org/grpc/status \ go.opencensus.io/plugin/ochttp \ - go.opencensus.io/plugin/ochttp/propagation/b3 + go.opencensus.io/plugin/ochttp/propagation/b3 \ + go.opencensus.io/exporter/stackdriver \ + go.opencensus.io/trace # copy go-only part of the build COPY *.go ./ diff --git a/src/frontend/main.go b/src/frontend/main.go index 0a486ff..c616d47 100644 --- a/src/frontend/main.go +++ b/src/frontend/main.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "log" "net/http" "os" "time" @@ -10,9 +11,11 @@ import ( "github.com/gorilla/mux" "github.com/pkg/errors" "github.com/sirupsen/logrus" + "go.opencensus.io/exporter/stackdriver" "go.opencensus.io/plugin/ocgrpc" "go.opencensus.io/plugin/ochttp" "go.opencensus.io/plugin/ochttp/propagation/b3" + "go.opencensus.io/trace" "google.golang.org/grpc" ) @@ -64,6 +67,8 @@ func main() { log.Level = logrus.DebugLevel log.Formatter = &logrus.TextFormatter{} + initTracing() + srvPort := port if os.Getenv("PORT") != "" { srvPort = os.Getenv("PORT") @@ -106,6 +111,18 @@ func main() { log.Fatal(http.ListenAndServe(addr+":"+srvPort, handler)) } +func initTracing() { + 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) + trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()}) + log.Println("registered stackdriver tracing") + } +} + func mustMapEnv(target *string, envKey string) { v := os.Getenv(envKey) if v == "" {