gpt4 book ai didi

go - 使用 zipkin-go 完成追踪

转载 作者:行者123 更新时间:2023-12-03 03:15:46 24 4
gpt4 key购买 nike

我有一些服务。我想使用 zipkin-go 跟踪这些服务。在每个服务中,我都会调用一些其他内部服务或数据库调用。我想跟踪每个事件,例如调用内部服务或数据库花费了多少时间。我已经使用互联网上的可用教程来实现。下面是我的代码:

package main

import (
"fmt"
"os"
"net/http"
"log"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"Upload-Image-API/services/controllers/updatemodrefid"
"Upload-Image-API/services/controllers/uploadimage"
"Upload-Image-API/services/utils"
"github.com/openzipkin/zipkin-go"
"github.com/openzipkin/zipkin-go/model"
zipkinhttp "github.com/openzipkin/zipkin-go/middleware/http"
reporterhttp "github.com/openzipkin/zipkin-go/reporter/http"
)

const endpointURL = "http://localhost:9411/api/v2/spans"

func newTracer() (*zipkin.Tracer, error) {
// The reporter sends traces to zipkin server
reporter := reporterhttp.NewReporter(endpointURL)
port := utils.GetString("serviceListeningPort")
if port == "" {
fmt.Println("Port is not defined in configuration ...!!!")
os.Exit(1)
}
// Local endpoint represent the local service information
localEndpoint := &model.Endpoint{ServiceName: "Upload-Image-API", Port: 7795}

// Sampler tells you which traces are going to be sampled or not. In this case we will record 100% (1.00) of traces.
sampler, err := zipkin.NewCountingSampler(1)
if err != nil {
return nil, err
}

t, err := zipkin.NewTracer(
reporter,
zipkin.WithSampler(sampler),
zipkin.WithLocalEndpoint(localEndpoint),
)
if err != nil {
return nil, err
}

return t, err
}


func main() {
var err error
var environment = os.Args[1]
err = utils.LoadDefaultConfig(environment)
tracer, err := newTracer()
if err != nil {
log.Fatal(err)
}
// We add the instrumented transport to the defaultClient
// that comes with the zipkin-go library
http.DefaultClient.Transport, err = zipkinhttp.NewTransport(
tracer,
zipkinhttp.TransportTrace(true),
)
if err != nil {
log.Fatal(err)
}
if err != nil {
fmt.Println("Could not load the configuration because of following err " + err.Error() + " ...!!!")
os.Exit(1)
} else {
fmt.Println("Configurations sucessfully loaded for environment " + environment + " ...!!!")
}
allowedHeaders := handlers.AllowedHeaders([]string{"X-Requested-With"})
allowedOrigins := handlers.AllowedOrigins([]string{"*"})
allowedMethods := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"})

port := utils.GetString("serviceListeningPort")
if port == "" {
fmt.Println("Port is not defined in configuration ...!!!")
os.Exit(1)
}
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/uploadimage", uploadimage.UploadImages).Methods("POST", "GET")
router.HandleFunc("/updatemodrefid", updatemodrefid.UpdateModRefId).Methods("POST", "GET")
router.Use(zipkinhttp.NewServerMiddleware(
tracer,
zipkinhttp.SpanName("request")), // name for request span
)
err = http.ListenAndServe(port, handlers.CORS(allowedHeaders, allowedOrigins, allowedMethods)(router))
if err != nil {
fmt.Println("Count not start the server because of following err " + err.Error())
return
}
}

我正在跟踪我的请求,但无法跟踪 uploadimage Controller 内发生的情况。下面是我的 zipkin UI 的屏幕截图:

Zipkin UI

我想跟踪 uploadimage Controller 内发生的所有事件。我需要传递什么才能追踪所有内容。

最佳答案

我是 zipkin 和 golang 的新手,如果你想跟踪内部进程,那么你可以从上下文创建 span

示例:假设您有一个名为 Login 的 api,在登录中您可以执行数据库操作或任何其他操作

func Login(res http.ResponseWriter, req *http.Request) {
span, _ := tracer.StartSpanFromContext(req.Context(), "database")
databaseOperation()
span.Finish()
}

关于go - 使用 zipkin-go 完成追踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57703884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com