finclip-app-manager/vendor/gitlab.finogeeks.club/finclip-backend/apm/client_http.go

89 lines
2.4 KiB
Go

package apm
import (
"context"
"errors"
"github.com/SkyAPM/go2sky"
"github.com/SkyAPM/go2sky/propagation"
"github.com/gin-gonic/gin"
"net/http"
)
func (client *Client) initHttp(config HttpConfig) {
if config.extractor == nil {
client.httpExtractor = HttpExtractor
} else {
client.httpExtractor = config.extractor
}
if config.injector == nil {
client.httpInjector = HttpInjector
} else {
client.httpInjector = config.injector
}
}
func (client *Client) InjectHttpMiddleware(engine *gin.Engine) {
if engine == nil {
panic(errors.New("engine can not be empty"))
}
if client.isEnable() {
engine.Use(HttpTraceMiddleware(engine, client.tracer))
}
}
func (client *Client) CreateHttpExitSpan(ctx context.Context, req *http.Request, host, path string) go2sky.Span {
span := client.CreateExitSpan(ctx, path, host, client.httpInjector(req))
span.Tag(go2sky.TagHTTPMethod, req.Method)
span.Tag(go2sky.TagURL, host+path)
return span
}
func (client *Client) CreateHttpExitSpanWithInjector(ctx context.Context, method, host, path string,
injector propagation.Injector) go2sky.Span {
span := client.CreateExitSpan(ctx, path, host, injector)
span.Tag(go2sky.TagHTTPMethod, method)
span.Tag(go2sky.TagURL, host+path)
return span
}
func (client *Client) CreateHttpExitSpanWithUrl(ctx context.Context, req *http.Request, url string) go2sky.Span {
if !client.isEnable() || client.isNoTraceContext(ctx) {
return nSpan
}
_, host, path := ParseURL(url)
span := client.CreateExitSpan(ctx, path, host, client.httpInjector(req))
span.Tag(go2sky.TagHTTPMethod, req.Method)
span.Tag(go2sky.TagURL, host+path)
return span
}
func (client *Client) CreateHttpExitSpanWithUrlAndInjector(ctx context.Context, method string, url string,
injector propagation.Injector) go2sky.Span {
if !client.isEnable() || client.isNoTraceContext(ctx) {
return nSpan
}
_, host, path := ParseURL(url)
span := client.CreateExitSpan(ctx, path, host, injector)
span.Tag(go2sky.TagHTTPMethod, method)
span.Tag(go2sky.TagURL, host+path)
return span
}
func (client *Client) TraceContextFromGin(c *gin.Context) context.Context {
if !client.isEnable() || c == nil || c.Request.Context() == nil {
return context.Background()
}
res := c.Request.Context().Value(ctxKeyInstance)
if res == nil {
return context.Background()
}
resCtx, ok := res.(context.Context)
if !ok {
return context.Background()
}
return resCtx
}