Search code examples
google-cloud-platformgoogle-cloud-trace

minimal stackdriver trace client usage failing


Here's a minification of using the stackdriver trace go client package.

It seems like this trivial example should work but it produces an error.

package main

import (
    "context"
    "flag"
    "log"
    "net/http"

    "cloud.google.com/go/trace"
    "github.com/davecgh/go-spew/spew"
    "google.golang.org/api/option"
)

var (
    projectID         = flag.String("projectID", "", "projcect id")
    serviceAccountKey = flag.String("serviceAccountKey", "", "path to serviceacount json")
)

func main() {
    flag.Parse()
    mux := http.NewServeMux()
    log.Fatalln(http.ListenAndServe(":9000", Trace(*projectID, *serviceAccountKey, mux)))
}

func Trace(projectID string, keyPath string, h http.Handler) http.HandlerFunc {
    client, err := trace.NewClient(context.Background(), projectID, option.WithServiceAccountFile(keyPath))
    if err != nil {
        panic(err)
    }
    return func(w http.ResponseWriter, r *http.Request) {
        s := client.SpanFromRequest(r)
        defer func() { err := s.FinishWait(); spew.Dump(err) }()
        h.ServeHTTP(w, r)
    }
}

I'm running this as:

$ teststackdrivertrace -projectID ${GCLOUD_PROJECT_ID} -serviceAccountKey ${PATH_TO_SERVICEACCOUNT_JSON}

and curling it produces:

$ curl -s --header "X-Cloud-Trace-Context: 205445aa7843bc8bf206b120001000/0;o=1" localhost:9000

$ (*googleapi.Error)(0xc4203a2c80)(googleapi: Error 400: Request contains an invalid argument., badRequest)

What am I missing?


Solution

  • My traceId was 30 bytes long not 32. I took the example curl from https://cloud.google.com/trace/docs/faq which is also only 30 bytes.