finclip-app-manager/infrastructure/zipkin-http/http.go

49 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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