Skip to content

OpenCensus

JBD edited this page Apr 25, 2018 · 22 revisions

Google Cloud client libraries provide stats and traces with the use of the OpenCensus instrumentation framework.

In order to upload the collected data, users need to register a Stackdriver exporter. OpenCensus provides support for various tracing and metric collection backends including Stackdriver Trace and Monitoring. See the exporters listing to find an exporter for the backend of your choice.

For example, in order to upload stats and traces to Stackdriver, register a trace exporter in your main program. The Stackdriver exporter requires you to enable the Stackdriver Trace and Monitoring API on the Cloud Console and enable ADC for auth.

import (
	"contrib.go.opencensus.io/exporter/stackdriver"
	"go.opencensus.io/trace"
	"go.opencensus.io/stats/view"
)

exporter, err := stackdriver.NewExporter(stackdriver.Options{ProjectID: "google-cloud-project-id"})
if err != nil {
	log.Fatal(err)
}
view.RegisterExporter(exporter)
trace.RegisterExporter(exporter)

Traces

All outgoing RPCs will be traced based on the sampling rate.

Stats

OpenCensus automatically collects stats about the gRPC clients (all package except for bigquery and storage). In order to collect the recorded stats from the instrumented packages, you need to subscribe to the provided stats views.

Each package can provide its own views and all available client views provided from gRPC are at the grpcstats package. Users can also create views based on measures provided by using NewView.

For example, subscribe to collect client request count:

import (
    "go.opencensus.io/plugin/ocgrpc"
    "go.opencensus.io/stats/view"
)

if err := view.Register(ocgrpc.RPCClientRequestCountView); err != nil {
	log.Fatal(err)
}

Then, you can use the exporter of your to upload the collected stats to a metric collection backend.

An entire reference program is available as an example.

Clone this wiki locally