Skip to content

Instantly share code, notes, and snippets.

@rakyll rakyll/main.go
Created Jan 16, 2018

What would you like to do?
Go Spanner Client with OpenCensus
// Copyright 2018 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Sample contains a simple program that
// uses Google Spanner Go client, and reports metrics
// and traces for the outgoing requests.
package main
import (
ss ""
ts ""
func main() {
ctx := context.Background()
// Enable OpenCensus exporters to export traces and metrics
// to Stackdriver Monitoring and Tracing.
// Exporters use Application Default Credentials to authenticate.
// See
// for more details.
statsExporter, err := ss.NewExporter(ss.Options{ProjectID: "your-project-id"})
if err != nil {
traceExporter, err := ts.NewExporter(ts.Options{ProjectID: "your-project-id"})
if err != nil {
// This database must exist.
databaseName := "projects/your-project-id/instances/your-instance-id/databases/your-database-id"
client, err := spanner.NewClient(ctx, databaseName)
if err != nil {
log.Fatalf("Failed to create client %v", err)
defer client.Close()
_, err = client.Apply(ctx, []*spanner.Mutation{
[]string{"name", "email"},
[]interface{}{"alice", ""})})
if err != nil {
log.Printf("Failed to insert: %v", err)
// Make sure data is uploaded before program finishes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.