49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package zipkinHttp
|
||
|
||
import (
|
||
"encoding/json"
|
||
"strings"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/zzfup/go-fetch"
|
||
)
|
||
|
||
/*c,需要在上层(application、domain、表现层等)传递*/
|
||
func SendByFetch(c *gin.Context, url, method string, body interface{}) (fetch.Resp, error) {
|
||
var headers = map[string]string{
|
||
"Accept": "application/json, text/plain, */*",
|
||
"Content-Type": "application/json",
|
||
}
|
||
|
||
traceSpanId, exists := c.Get("zipkin_span_id")
|
||
if exists {
|
||
value, exists := c.Get("zipkin_trace_span_id_" + traceSpanId.(string))
|
||
if exists && value != nil {
|
||
headers["Zipkin-Span-Context"] = value.(string)
|
||
}
|
||
}
|
||
|
||
options := fetch.Options{
|
||
Method: strings.ToUpper(method),
|
||
Header: headers,
|
||
}
|
||
|
||
if body != nil {
|
||
options.Body, _ = json.Marshal(body)
|
||
}
|
||
|
||
return fetch.Fetch(url, options)
|
||
}
|
||
|
||
func SendByFetchOptions(c *gin.Context, url string, options fetch.Options) (fetch.Resp, error) {
|
||
traceSpanId, exists := c.Get("zipkin_span_id")
|
||
if exists {
|
||
value, exists := c.Get("zipkin_trace_span_id_" + traceSpanId.(string))
|
||
if exists && value != nil {
|
||
options.Header["Zipkin-Span-Context"] = value.(string)
|
||
}
|
||
}
|
||
|
||
return fetch.Fetch(url, options)
|
||
}
|