Google Cloud Trace code

Once we have our application credentials all set up; we are off to the races to write our first trace that will be captured by our APM:

  1. First, we instantiate the necessary packages and set a server host/port constant:
package mainimport (    "context"    "fmt"    "log"    "net/http"    "os"    "time"    "contrib.go.opencensus.io/exporter/stackdriver"    "go.opencensus.io/trace")const server = ":1234"
  1. Next, in our init() function, we set up our StackDriver exporter and register our tracer to sample every web request that comes in. In production, we should probably sample fewer requests, as sampling adds additional latency to our requests:
func init() {    exporter, err := stackdriver.NewExporter(stackdriver.Options{ ProjectID: os.Getenv("GOOGLE_CLOUD_PROJECT"), ...

Get Hands-On High Performance with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.