草庐IT

google-app-engine - Stackdriver Trace 与 Google App Engine Go 1.11 运行时

coder 2024-07-09 原文

我想让 Stackdriver Trace 处理新的 Go 1.11 standard runtime on Google App Engine .这一切仍然被标记为测试版,所以它可能还没有完全奏效。但是,我遵循了 step-by-step directions它不工作。我(几乎)完全按照链接中列出的方式部署了代码,并且我的跟踪是平坦的(即不包括我期望的瀑布 View ,传入请求位于顶部,传出请求嵌套在下方)。

示例代码

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

样本跟踪:

最佳答案

如原问题的回复评论中所写,您可以尝试采样的配置吗?:trace.AlwaysSample()

您可以在 OpenCensus Trace documentation 中找到有关采样率的一些评论和 godoc of OpenCensus Trace library :

By default, traces will be sampled relatively rarely. To change the sampling frequency for your entire program, call ApplyConfig. Use a ProbabilitySampler to sample a subset of traces, or use AlwaysSample to collect a trace on every run:

关于google-app-engine - Stackdriver Trace 与 Google App Engine Go 1.11 运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54859976/

有关google-app-engine - Stackdriver Trace 与 Google App Engine Go 1.11 运行时的更多相关文章

随机推荐