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 io.opencensus.contrib.grpc.metrics.RpcViews

End to end code sample

packagecom.opencensus.examples;importcom.google.cloud.spanner.DatabaseClient;importcom.google.cloud.spanner.DatabaseId;importcom.google.cloud.spanner.Key;importcom.google.cloud.spanner.Mutation;importcom.google.cloud.spanner.ResultSet;importcom.google.cloud.spanner.Spanner;importcom.google.cloud.spanner.SpannerOptions;importcom.google.cloud.spanner.Statement;importio.opencensus.common.Scope;importio.opencensus.contrib.grpc.metrics.RpcViews;importio.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter;importio.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter;importio.opencensus.trace.Tracing;importio.opencensus.trace.samplers.Samplers;importjava.util.Arrays;importjava.util.List;publicclassSpannerOpenCensusTutorial{privateDatabaseClientdbClient;privateSpannerspanner;privatestaticStringparentSpanName="create-players";publicSpannerOpenCensusTutorial(StringinstanceId,StringdatabaseId)throwsException{// Instantiate the client.
SpannerOptionsoptions=SpannerOptions.getDefaultInstance();this.spanner=options.getService();// And then create the Spanner database client.
this.dbClient=this.spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(),instanceId,databaseId));// Next up let's install the exporter for Stackdriver tracing.
StackdriverTraceExporter.createAndRegister();Tracing.getExportComponent().getSampledSpanStore().registerSpanNamesForCollection(Arrays.asList(parentSpanName));// Then the exporter for Stackdriver monitoring/metrics.
StackdriverStatsExporter.createAndRegister();RpcViews.registerAllGrpcViews();}publicvoidclose(){this.spanner.close();}publicvoidwarmUpRead(){this.dbClient.singleUse().readRow("Players",Key.of("foo@gmail.com"),Arrays.asList("email"));}publicstaticvoidmain(String...args)throwsException{if(args.length<2){System.err.println("Usage: ZeuSports <instance_id> <database_id>");return;}try{SpannerOpenCensusTutorialzdb=newSpannerOpenCensusTutorial(args[0],args[1]);// Warm up the spanner client session. In normal usage
// you'd have hit this point after the first operation.
zdb.warmUpRead();for(inti=0;i<3;i++){Stringup=i+"-"+(System.currentTimeMillis()/1000)+".";List<Mutation>mutations=Arrays.asList(playerMutation("Poke","Mon",up+"poke.mon@example.org","f1578551-eb4b-4ecd-aee2-9f97c37e164e"),playerMutation("Go","Census",up+"go.census@census.io","540868a2-a1d8-456b-a995-b324e4e7957a"),playerMutation("Quick","Sort",up+"q.sort@gmail.com","2b7e0098-a5cc-4f32-aabd-b978fc6b9710"));zdb.insertPlayers(mutations);}zdb.close();}catch(Exceptione){System.out.printf("Exception while adding player: "+e);}finally{System.out.println("Bye!");}}publicstaticMutationplayerMutation(StringfirstName,StringlastName,Stringemail,Stringuuid){returnMutation.newInsertBuilder("Players").set("first_name").to(firstName).set("last_name").to(lastName).set("uuid").to(uuid).set("email").to(email).build();}publicvoidinsertPlayers(List<Mutation>players)throwsException{try(Scopess=Tracing.getTracer().spanBuilderWithExplicitParent(parentSpanName,null)// Enable the trace sampler.
// We are always sampling for demo purposes only: this is a very high sampling
// rate, but sufficient for the purpose of this quick demo.
// More realistically perhaps tracing 1 in 10,000 might be more useful
.setSampler(Samplers.alwaysSample()).startScopedSpan()){this.dbClient.write(players);}finally{}}}