-
Notifications
You must be signed in to change notification settings - Fork 1.3k
OpenCensus
Google Cloud client libraries provide stats and traces with the use of OpenCensus instrumentation framework. Stats and trace collection is available from the following packages.
In order to upload the collected traces, users need to register a trace exporter. OpenCensus provides support for various tracing backends including Stackdriver Trace. See the trace exporters listing to an exporter for the backend of your choice.
For example, in order to upload traces to the Stackdriver Trace, register a trace exporter in your main program. The Stackdriver exporter requires you to enable the Stackdriver Trace API on the Cloud Console and enable ADC for auth.
import (
"go.opencensus.io/exporter/trace/stackdriver"
"go.opencensus.io/trace"
)
exporter, err := stackdriver.NewExporter(stackdriver.Options{ProjectID: "google-cloud-project-id"})
if err != nil {
log.Fatal(err)
}
trace.RegisterExporter(exporter)
OpenCensus automatically collects stats about the gRPC clients. In order to collect the recorded stats from the instrumented packages, you need to subscribe to the stats views provided from the gRPC integrations for the gRPC clients.
For example, subscribe to collect client request count:
if err := grpcstats.RPCClientRequestCountView.Subscribe(); err != nil {
log.Fatal(err)
}
Then, you can use the exporter of your choice to upload the collected stats to a metric collection backend.
As an example, in order to upload data to Stackdriver Monitoring, register the Stackdriver Monitoring exporter in your main program. The Stackdriver exporter requires you to enable the Stackdriver Monitoring API on the Cloud Console and enable ADC for auth.
import (
"go.opencensus.io/exporter/stats/stackdriver"
"go.opencensus.io/stats"
)
exporter, err := stackdriver.NewExporter(stackdriver.Options{ProjectID: "google-cloud-project-id"})
if err != nil {
log.Fatal(err)
}
stats.RegisterExporter(exporter)