frontend/checkoutservice: add tracing
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
89618dbeb6
commit
555c8a4c05
4 changed files with 39 additions and 2 deletions
|
@ -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 ./...
|
||||
|
|
|
@ -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 == "" {
|
||||
|
|
|
@ -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 ./
|
||||
|
|
|
@ -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 == "" {
|
||||
|
|
Loading…
Reference in a new issue