带有Google App Engine Go 1.11运行时的Stackdriver Trace

时间:2019-02-25 05:26:52

标签: google-app-engine go tracing stackdriver

我想让Stackdriver Trace在新的Go 1.11 standard runtime on Google App Engine上工作。这些都仍被标记为beta,因此也许它还不能完全正常工作。但是,我遵循了step-by-step directions,但是它不起作用。我(几乎)完全按照链接中列出的方式部署了代码,并且跟踪是平坦的(即不包括我期望的瀑布视图,其中传入请求位于顶部,传出请求嵌套在下方)。

示例代码

package main

import (
    "fmt"
    "log"
    "net/http"
    "os"

    "contrib.go.opencensus.io/exporter/stackdriver"
    "contrib.go.opencensus.io/exporter/stackdriver/propagation"
    "go.opencensus.io/plugin/ochttp"
    "go.opencensus.io/trace"
)

func main() {
    // Create and register a OpenCensus Stackdriver Trace exporter.
    exporter, err := stackdriver.NewExporter(stackdriver.Options{
        ProjectID: os.Getenv("GOOGLE_CLOUD_PROJECT"),
    })
    if err != nil {
        log.Fatal(err)
    }
    trace.RegisterExporter(exporter)

    client := &http.Client{
        Transport: &ochttp.Transport{
            // Use Google Cloud propagation format.
            Propagation: &propagation.HTTPFormat{},
        },
    }

    handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        req, _ := http.NewRequest("GET", "https://www.google.com/robots.txt", nil)

        // The trace ID from the incoming request will be
        // propagated to the outgoing request.
        req = req.WithContext(r.Context())

        // The outgoing request will be traced with r's trace ID.
        resp, err := client.Do(req)
        if err != nil {
            log.Fatal(err)
        }
        defer resp.Body.Close()

        fmt.Fprint(w, "OK")
    })
    http.Handle("/foo", handler)
    log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), &ochttp.Handler{}))
}

样本跟踪: enter image description here

1 个答案:

答案 0 :(得分:0)

如原始问题的回复注释中所述,您可以尝试进行采样配置吗:trace.AlwaysSample()

您可以在OpenCensus Trace documentationgodoc of OpenCensus Trace library中找到有关采样率的一些注释:

  

默认情况下,跟踪的采样相对很少。若要更改整个程序的采样频率,请调用ApplyConfig。使用ProbabilitySampler采样跟踪的子集,或使用AlwaysSample在每次运行时收集跟踪: