最小的stackdriver跟踪客户端使用失败

时间:2016-11-10 20:45:38

标签: google-cloud-platform google-cloud-trace

这是使用stackdriver trace go客户端软件包的缩小。

看起来这个简单的例子应该可行,但它会产生错误。

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)
    }
}

我的运作方式如下:

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

并卷曲它会产生:

$ 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)

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我的traceId长度为30个字节而不是32个。我从exact implementation is browser-dependent获取了示例curl,它也只有30个字节。