您好,登錄后才能下訂單哦!
小編給大家分享一下GoLang實現基于gin+jaeger的opentracing中間件的示例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
完整源碼:https://github.com/why444216978/gin-api jaeger下載地址:https://www.jaegertracing.io/download/
運行jaeger:
./jaeger-all-in-one
gin注冊中間件:
server := gin.New() server.Use(trace.OpenTracing("gin-api"))
中間件定義:
package trace import ( "github.com/gin-gonic/gin" opentracing "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/ext" ) const defaultComponentName = "net/http" const JaegerOpen = 1 const AppName = "gin-api" const JaegerHostPort = "127.0.0.1:6831" func OpenTracing(serviceName string) gin.HandlerFunc { return func(c *gin.Context) { if JaegerOpen == 1 { var parentSpan opentracing.Span tracer, closer := NewJaegerTracer(AppName, JaegerHostPort) defer closer.Close() spCtx, err := opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(c.Request.Header)) if err != nil { parentSpan = tracer.StartSpan(c.Request.URL.Path) defer parentSpan.Finish() } else { parentSpan = opentracing.StartSpan( c.Request.URL.Path, opentracing.ChildOf(spCtx), opentracing.Tag{Key: string(ext.Component), Value: "HTTP"}, ext.SpanKindRPCServer, ) defer parentSpan.Finish() } c.Set("Tracer", tracer) c.Set("ParentSpanContext", parentSpan.Context()) } c.Next() } }
http請求實現:
package http import ( "bytes" "encoding/json" "io/ioutil" "net/http" "strconv" "gin-frame/libraries/util" simplejson "github.com/bitly/go-simplejson" "github.com/gin-gonic/gin" "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/ext" opentracingLog "github.com/opentracing/opentracing-go/log" ) func HttpSend(c *gin.Context, method, url, logId string, data map[string]interface{}) map[string]interface{} { var ( err error ret = make(map[string]interface{}) req *http.Request ) client := &http.Client{} //請求數據 byteDates, err := json.Marshal(data) util.Must(err) reader := bytes.NewReader(byteDates) //url url = url + "?logid=" + logId //構建req req, err = http.NewRequest(method, url, reader) util.Must(err) //設置請求header req.Header.Add("content-type", "application/json") tracer, _ := c.Get("Tracer") parentSpanContext, _ := c.Get("ParentSpanContext") span := opentracing.StartSpan( "httpDo", opentracing.ChildOf(parentSpanContext.(opentracing.SpanContext)), opentracing.Tag{Key: string(ext.Component), Value: "HTTP"}, ext.SpanKindRPCClient, ) defer span.Finish() injectErr := tracer.(opentracing.Tracer).Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header)) if injectErr != nil { span.LogFields(opentracingLog.String("inject-error", err.Error())) } //發送請求 resp, err := client.Do(req) util.Must(err) defer resp.Body.Close() b, err := ioutil.ReadAll(resp.Body) util.Must(err) ret["code"] = resp.StatusCode ret["msg"] = "success" ret["data"] = make(map[string]interface{}) if resp.StatusCode != http.StatusOK { ret["msg"] = "http code:" + strconv.Itoa(resp.StatusCode) return ret } if b != nil { res, err := simplejson.NewJson(b) util.Must(err) ret["data"] = res } return ret }
調用代碼:
package test import ( "net/http" "github.com/gin-gonic/gin" "gin-frame/libraries/config" rpc_http "gin-frame/libraries/http" ) func Rpc(c *gin.Context) { postData := make(map[string]interface{}) logId := c.Writer.Header().Get(config.GetHeaderLogIdField()) sendUrl := "https://www.baidu.com" //urlMap := strings.Split(sendUrl, "?") //urlQueryMap := url.ParseUriQueryToMap(sendUrl) ret := rpc_http.HttpSend(c, "POST", sendUrl, logId, postData) c.JSON(http.StatusOK, gin.H{ "errno": 0, "errmsg": "success", "data": ret, }) c.Done() }
查看調用鏈路:
以上是“GoLang實現基于gin+jaeger的opentracing中間件的示例”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。