Skip to content

Instantly share code, notes, and snippets.

@songy23
Created May 27, 2020 22:46
Show Gist options
  • Save songy23/001102b91293572fb3913dcfd4a84b17 to your computer and use it in GitHub Desktop.
Save songy23/001102b91293572fb3913dcfd4a84b17 to your computer and use it in GitHub Desktop.
Inspect GFE Latency in Spanner Go Client - 2
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [START spanner_quickstart]
// Sample spanner_quickstart is a basic program that uses Cloud Spanner.
package main
import (
"context"
"fmt"
"log"
"strconv"
"strings"
"time"
spanner "cloud.google.com/go/spanner/apiv1"
gax "github.com/googleapis/gax-go/v2"
sppb "google.golang.org/genproto/googleapis/spanner/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"contrib.go.opencensus.io/exporter/jaeger"
"go.opencensus.io/trace"
)
func main() {
ctx := context.Background()
// This database must exist.
databaseName := "projects/testing/instances/test-instance/databases/test-db"
client, err := spanner.NewClient(ctx)
if err != nil {
log.Fatalf("Failed to create client %v", err)
}
defer client.Close()
setupOpenCensus()
req := &sppb.CreateSessionRequest{Database: databaseName}
session, err := client.CreateSession(ctx, req)
if err != nil {
log.Fatalf("CreateSession failed with %v", err)
}
req2 := &sppb.ExecuteSqlRequest{
Session: session.Name,
Sql: "SELECT * FROM Test ORDER BY ID ASC",
QueryMode: sppb.ExecuteSqlRequest_PROFILE,
}
for i := 0; i < 20; i++ {
ctx, span := trace.StartSpan(ctx, "demo/ExecuteSql")
var md metadata.MD
resultSet, err := client.ExecuteSql(ctx, req2, gax.WithGRPCOptions(grpc.Header(&md)))
if err != nil {
log.Fatalf("ExecuteSql failed with %v", err)
}
fmt.Println("Row", resultSet.GetRows())
gfeLatency := getGFELatency(md)
span.AddAttributes(trace.Int64Attribute("GFE Latency", gfeLatency))
span.End()
time.Sleep(10 * time.Second)
}
}
func setupOpenCensus() {
// Configure 100% sample rate, otherwise, few traces will be sampled.
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
agentEndpointURI := "localhost:6831"
collectorEndpointURI := "http://localhost:14268/api/traces"
je, err := jaeger.NewExporter(jaeger.Options{
AgentEndpoint: agentEndpointURI,
CollectorEndpoint: collectorEndpointURI,
ServiceName: "demo",
})
if err != nil {
log.Fatalf("Failed to create the Jaeger exporter: %v", err)
}
// And now finally register it as a Trace Exporter
trace.RegisterExporter(je)
}
func getGFELatency(md metadata.MD) int64 {
srvTiming := md.Get("server-timing")[0]
gfeLtcy, _ := strconv.Atoi(strings.TrimPrefix(srvTiming, "gfet4t7; dur="))
return int64(gfeLtcy)
}
// [END spanner_quickstart]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment