Enable metric reporting

To enable metric reporting/exporting, we need to enable a metrics exporter, but before that we’ll need
to register and enable the views that match the metrics to collect. For a complete list of the available views
available please visit https://godoc.org/go.opencensus.io/plugin/ocgrpc

Exporting traces and metrics

End to end code sample

With all the steps combined, we’ll finally have this code snippet

packagemainimport("fmt""log""time""cloud.google.com/go/spanner""golang.org/x/net/context""go.opencensus.io/exporter/stackdriver""go.opencensus.io/plugin/ocgrpc""go.opencensus.io/stats/view""go.opencensus.io/trace")typePlayerstruct{FirstNamestring`spanner:"first_name"`LastNamestring`spanner:"last_name"`UUIDstring`spanner:"uuid"`Emailstring`spanner:"email"`}funcmain(){se,err:=stackdriver.NewExporter(stackdriver.Options{ProjectID:"census-demos",MetricPrefix:"spanner-oc-demo",})iferr!=nil{log.Fatalf("StatsExporter err: %v",err)}// Let's ensure that the Stackdriver exporter uploads all its data before the program exits
deferse.Flush()// Enable tracing
trace.RegisterExporter(se)// Enable metrics collection
view.RegisterExporter(se)// Register all the gRPC client views
iferr:=view.Register(ocgrpc.DefaultClientViews...);err!=nil{log.Fatalf("Failed to register gRPC default client views for metrics: %v",err)}// Register all the gRPC server views
iferr:=view.Register(ocgrpc.DefaultServerViews...);err!=nil{log.Fatalf("Failed to register gRPC default server views for metrics: %v",err)}// Enable the trace sampler.
// We are always sampling for demo purposes only: it is very high
// depending on the QPS, but sufficient for the purpose of this quick demo.
// More realistically perhaps tracing 1 in 10,000 might be more useful
trace.ApplyConfig(trace.Config{DefaultSampler:trace.AlwaysSample()})ctx:=context.Background()// The database must exist
databaseName:="projects/census-demos/instances/census-demos/databases/demo1"sessionPoolConfig:=spanner.SessionPoolConfig{MinOpened:5,WriteSessions:1}client,err:=spanner.NewClientWithConfig(ctx,databaseName,spanner.ClientConfig{SessionPoolConfig:sessionPoolConfig})iferr!=nil{log.Fatalf("SpannerClient err: %v",err)}deferclient.Close()// Warm up the spanner client session. In normal usage
// you'd have hit this point after the first operation.
_,_=client.Single().ReadRow(ctx,"Players",spanner.Key{"foo@gmail.com"},[]string{"email"})fori:=0;i<3;i++{ctx,span:=trace.StartSpan(ctx,"create-players")players:=[]*Player{{FirstName:"Poke",LastName:"Mon",Email:"poke.mon@example.org",UUID:"f1578551-eb4b-4ecd-aee2-9f97c37e164e"},{FirstName:"Go",LastName:"Census",Email:"go.census@census.io",UUID:"540868a2-a1d8-456b-a995-b324e4e7957a"},{FirstName:"Quick",LastName:"Sort",Email:"q.sort@gmail.com",UUID:"2b7e0098-a5cc-4f32-aabd-b978fc6b9710"},}up:=fmt.Sprintf("%d-%d.",i,time.Now().Unix())for_,player:=rangeplayers{player.Email=up+player.Email}iferr:=newPlayers(ctx,client,players...);err!=nil{log.Printf("Creating newPlayers err: %v",err)}span.End()}}funcnewPlayers(ctxcontext.Context,client*spanner.Client,players...*Player)error{varml[]*spanner.Mutationfor_,player:=rangeplayers{m,err:=spanner.InsertStruct("Players",player)iferr!=nil{returnerr}ml=append(ml,m)}_,err:=client.Apply(ctx,ml)returnerr}