0

这个问题与以下问题直接相关。他们都没有解决这个问题:

使用 golang http.PostForm 进行分布式跟踪

使用 http.NewRequest POST 数据失败

如何使用 json 传递 opentracing 数据

在微服务的提取载体中找不到 SpanContext

这是我目前的形象。

在第一个微服务(API-网关)中,我有以下代码:

func startTracing(service string) (opentracing.Tracer, io.Closer) {
    cfg := &config.Configuration{
        Sampler: &config.SamplerConfig{
            Type:  "const",
            Param: 1,
        },
        Reporter: &config.ReporterConfig{
            LogSpans: true,
        },
    }
    tracer, closer, err := cfg.New(service, config.Logger(jaeger.StdLogger))
    if err != nil {
        panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
    }
    return tracer, closer
}

func main() {

    http.HandleFunc("/getemail", validatemail)
    http.ListenAndServe(":7070", nil)
}

func validatemail(res http.ResponseWriter, req *http.Request) {

    gatewayTracer, gatewayTracerClose := startTracing("Validate Email")

    validateEmailSpan := gatewayTracer.StartSpan("Email address checking")

    // Check method
    if req.Method != "POST" {
        log.Panic("Email form data is not Post")

    }

    email := req.FormValue("email") 

    validEmail := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`) // regex to validate email address

    if validEmail.MatchString(email) {
        httpClient := http.Client{}

        form := url.Values{}
        form.Set("email", email)

        userserviceUrl := "http://user:7071/checkemail"

        checkEmailReq, err := http.NewRequest("POST", userserviceUrl, strings.NewReader(form.Encode()))
        if err != nil {
            log.Println(err)
        }

        ext.SpanKindRPCClient.Set(validateEmailSpan)
        ext.HTTPUrl.Set(validateEmailSpan, userserviceUrl)
        ext.HTTPMethod.Set(validateEmailSpan, "POST")

        validateEmailSpan.Tracer().Inject(
            validateEmailSpan.Context(),
            opentracing.HTTPHeaders,
            opentracing.HTTPHeadersCarrier(req.Header),
        )
        req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

        resp, err := httpClient.Do(checkEmailReq)

        if err != nil {
            log.Println("Couldnt verify email address user service sends an error : ", err)
        }
        defer resp.Body.Close()

    } else {
        log.Println("Wrong email address format")
    }

    defer gatewayTracerClose.Close()
    defer validateEmailSpan.Finish()
}

在我的第二个微服务(用户服务)

func CheckEmail(res http.ResponseWriter, req *http.Request) {

    checkEmailTracer, checkemailtracerClose := startTracing("Registering Service")  // Same startTracing function as API-Gateway

    spanCtx, _ := checkEmailTracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
    span := checkEmailTracer.StartSpan("format", ext.RPCServerOption(spanCtx))

    defer span.Finish()
    req.ParseForm()

    log.Println("Start from context :", spanCtx)

}

这里来自 **Start from context ** 的输出:nil

我在这里想念什么?我真的很陌生opentracing。这里发布的代码取自opentracing-tutorial-yurishkur

4

0 回答 0