From cc6efcc2c7dddd0adca272f18e3c3b1b4099bd2f Mon Sep 17 00:00:00 2001 From: "Edward.Yang" Date: Wed, 25 Jul 2018 14:13:19 +0800 Subject: [PATCH 1/6] modify func New() -> New(context.Context) --- gorequest.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gorequest.go b/gorequest.go index 8edb0fc..4225ec9 100644 --- a/gorequest.go +++ b/gorequest.go @@ -3,6 +3,7 @@ package gorequest import ( "bytes" + "context" "crypto/tls" "encoding/json" "io" @@ -90,12 +91,13 @@ type SuperAgent struct { } //If true prevents clearing Superagent data and makes it possible to reuse it for the next requests DoNotClearSuperAgent bool + ctx context.Context } var DisableTransportSwap = false // Used to create a new SuperAgent object. -func New() *SuperAgent { +func New(ctx context.Context) *SuperAgent { cookiejarOptions := cookiejar.Options{ PublicSuffixList: publicsuffix.List, } @@ -121,6 +123,7 @@ func New() *SuperAgent { Debug: debug, CurlCommand: false, logger: log.New(os.Stderr, "[gorequest]", log.LstdFlags), + ctx: context.Context, } // disable keep alives by default, see this issue https://github.com/parnurzeal/gorequest/issues/75 s.Transport.DisableKeepAlives = true @@ -1257,6 +1260,7 @@ func (s *SuperAgent) MakeRequest() (*http.Request, error) { if req, err = http.NewRequest(s.Method, s.Url, contentReader); err != nil { return nil, err } + req = req.WithContext(s.ctx) for k, vals := range s.Header { for _, v := range vals { From 8007ef2b5ccef1f73ac345bea9f0cbfe2c435b67 Mon Sep 17 00:00:00 2001 From: "Edward.Yang" Date: Wed, 25 Jul 2018 14:19:05 +0800 Subject: [PATCH 2/6] modify func New() -> New(context.Context) --- gorequest.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gorequest.go b/gorequest.go index 4225ec9..ca7f6ee 100644 --- a/gorequest.go +++ b/gorequest.go @@ -123,7 +123,7 @@ func New(ctx context.Context) *SuperAgent { Debug: debug, CurlCommand: false, logger: log.New(os.Stderr, "[gorequest]", log.LstdFlags), - ctx: context.Context, + ctx: ctx, } // disable keep alives by default, see this issue https://github.com/parnurzeal/gorequest/issues/75 s.Transport.DisableKeepAlives = true From 88b8a16f3a8b6b1a0efb33574f4baa1207d928cc Mon Sep 17 00:00:00 2001 From: "Edward.Yang" Date: Wed, 25 Jul 2018 15:09:27 +0800 Subject: [PATCH 3/6] add OptracingAgent --- gorequest.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/gorequest.go b/gorequest.go index ca7f6ee..09ae01d 100644 --- a/gorequest.go +++ b/gorequest.go @@ -3,7 +3,6 @@ package gorequest import ( "bytes" - "context" "crypto/tls" "encoding/json" "io" @@ -20,6 +19,9 @@ import ( "strings" "time" + "github.com/opentracing/opentracing-go" + "gitlab.goiot.net/sde-base/golib/tracing" + "github.com/pkg/errors" "mime/multipart" @@ -91,13 +93,12 @@ type SuperAgent struct { } //If true prevents clearing Superagent data and makes it possible to reuse it for the next requests DoNotClearSuperAgent bool - ctx context.Context } var DisableTransportSwap = false // Used to create a new SuperAgent object. -func New(ctx context.Context) *SuperAgent { +func New() *SuperAgent { cookiejarOptions := cookiejar.Options{ PublicSuffixList: publicsuffix.List, } @@ -123,7 +124,6 @@ func New(ctx context.Context) *SuperAgent { Debug: debug, CurlCommand: false, logger: log.New(os.Stderr, "[gorequest]", log.LstdFlags), - ctx: ctx, } // disable keep alives by default, see this issue https://github.com/parnurzeal/gorequest/issues/75 s.Transport.DisableKeepAlives = true @@ -1260,7 +1260,6 @@ func (s *SuperAgent) MakeRequest() (*http.Request, error) { if req, err = http.NewRequest(s.Method, s.Url, contentReader); err != nil { return nil, err } - req = req.WithContext(s.ctx) for k, vals := range s.Header { for _, v := range vals { @@ -1314,3 +1313,14 @@ func (s *SuperAgent) AsCurlCommand() (string, error) { } return cmd.String(), nil } + +type SuperOpentracingAgent struct { + SpanContext opentracing.SpanContext + SuperAgent +} + +func (s *SuperOpentracingAgent) MakeRequest() (*http.Request, error) { + req, err := s.SuperAgent.MakeRequest() + tracing.InjectTraceID(s.SpanContext, req.Header) + return req, err +} From c39c2503e773b5f6c60e93b4861a74c2f46f1350 Mon Sep 17 00:00:00 2001 From: "Edward.Yang" Date: Wed, 25 Jul 2018 19:17:38 +0800 Subject: [PATCH 4/6] bugfixed --- gorequest.go | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/gorequest.go b/gorequest.go index 09ae01d..2eee1cb 100644 --- a/gorequest.go +++ b/gorequest.go @@ -91,6 +91,7 @@ type SuperAgent struct { Attempt int Enable bool } + SpanContext opentracing.SpanContext //If true prevents clearing Superagent data and makes it possible to reuse it for the next requests DoNotClearSuperAgent bool } @@ -1261,6 +1262,11 @@ func (s *SuperAgent) MakeRequest() (*http.Request, error) { return nil, err } + if s.SpanContext != nil { + fmt.Println("reject header") + tracing.InjectTraceID(s.SpanContext, req.Header) + } + for k, vals := range s.Header { for _, v := range vals { req.Header.Add(k, v) @@ -1313,14 +1319,3 @@ func (s *SuperAgent) AsCurlCommand() (string, error) { } return cmd.String(), nil } - -type SuperOpentracingAgent struct { - SpanContext opentracing.SpanContext - SuperAgent -} - -func (s *SuperOpentracingAgent) MakeRequest() (*http.Request, error) { - req, err := s.SuperAgent.MakeRequest() - tracing.InjectTraceID(s.SpanContext, req.Header) - return req, err -} From a9dc4d6f8c3ffc01fc7cf74c2d47a03e9cbcb24d Mon Sep 17 00:00:00 2001 From: "Edward.Yang" Date: Thu, 26 Jul 2018 15:58:54 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=88=A0=E9=99=A4debug=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gorequest.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gorequest.go b/gorequest.go index 2eee1cb..9c3f749 100644 --- a/gorequest.go +++ b/gorequest.go @@ -1263,7 +1263,7 @@ func (s *SuperAgent) MakeRequest() (*http.Request, error) { } if s.SpanContext != nil { - fmt.Println("reject header") + // fmt.Println("reject header") tracing.InjectTraceID(s.SpanContext, req.Header) } From dbcf1f10a40a9f9f02fbf342c4c9757761caec2e Mon Sep 17 00:00:00 2001 From: "edward.yang" Date: Wed, 4 Nov 2020 17:02:34 +0800 Subject: [PATCH 6/6] delete gitlab tracing package --- gorequest.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gorequest.go b/gorequest.go index 9c3f749..f179a54 100644 --- a/gorequest.go +++ b/gorequest.go @@ -20,7 +20,6 @@ import ( "time" "github.com/opentracing/opentracing-go" - "gitlab.goiot.net/sde-base/golib/tracing" "github.com/pkg/errors" @@ -1264,7 +1263,10 @@ func (s *SuperAgent) MakeRequest() (*http.Request, error) { if s.SpanContext != nil { // fmt.Println("reject header") - tracing.InjectTraceID(s.SpanContext, req.Header) + opentracing.GlobalTracer().Inject( + s.SpanContext, + opentracing.HTTPHeaders, + opentracing.HTTPHeadersCarrier(req.Header)) } for k, vals := range s.Header {