Set up an OpenCensus exporter

Samples for setting up the Cloud Monitoring exporter.

Code sample

Go

// Enable OpenCensus exporters to export metrics
// to Stackdriver Monitoring.
// Exporters use Application Default Credentials to authenticate.
// See https://developers.google.com/identity/protocols/application-default-credentials
// for more details.
exporter, err := stackdriver.NewExporter(stackdriver.Options{})
if err != nil {
	log.Fatal(err)
}
// Flush must be called before main() exits to ensure metrics are recorded.
defer exporter.Flush()

if err := exporter.StartMetricsExporter(); err != nil {
	log.Fatalf("Error starting metric exporter: %v", err)
}
defer exporter.StopMetricsExporter()

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser