Skip to content

Commit

Permalink
resty maybe works?
Browse files Browse the repository at this point in the history
  • Loading branch information
muir committed Oct 2, 2022
1 parent 8a3540b commit d73eb47
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ go 1.16

require (
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/muir/rest v0.0.0-20210822233606-6d02fb885933
github.com/phuslu/fasttime v1.0.2
github.com/muir/resty v0.0.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.0
go.opentelemetry.io/otel v1.10.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.10.0
Expand Down
19 changes: 11 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@ github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/muir/rest v0.0.0-20210822233606-6d02fb885933 h1:gBIILIoUpMUWpQNSRdVSzF9xmBqbITPEkClLSyCxsAc=
github.com/muir/rest v0.0.0-20210822233606-6d02fb885933/go.mod h1:sUUGdWPRJgmDYo14DPXmZbkyqPOuy+Ec1gaEnhRuu0I=
github.com/phuslu/fasttime v1.0.0 h1:SUkNta9tJLy54glGb/Z4+/NJ7aXiLogVuSPEXAya6i8=
github.com/phuslu/fasttime v1.0.0/go.mod h1:1zflhZFowg3RSL0IpmWQ0AprKCKbTRKV9PEpVXDBZ7A=
github.com/phuslu/fasttime v1.0.2 h1:gmD27yB6vnQuhk4vtmoBOmjaXsF+wwmyjMtayoHHUko=
github.com/phuslu/fasttime v1.0.2/go.mod h1:1zflhZFowg3RSL0IpmWQ0AprKCKbTRKV9PEpVXDBZ7A=
github.com/muir/resty v0.0.1 h1:dSWIF/uLX01/AzniDi+XukP9Q6g5QO10DPVJsZ+49n0=
github.com/muir/resty v0.0.1/go.mod h1:Vjk1Uhi1m4/Och2tNJ2nxuGJnoaI8L0U4a27TbzKMR4=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -37,8 +33,15 @@ go.opentelemetry.io/otel/sdk v1.10.0 h1:jZ6K7sVn04kk/3DNUdJ4mqRlGDiXAVuIG+MMENpT
go.opentelemetry.io/otel/sdk v1.10.0/go.mod h1:vO06iKzD5baltJz1zarxMCNHFpUlUiOy4s65ECtn6kE=
go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E=
go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb h1:pirldcYWx7rx7kE5r+9WsOXPXK0+WH5+uZ7uPmJ44uM=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7 h1:iGu644GcxtEcrInvDsQRCwJjtCIOlT2V7IRt6ah2Whw=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
26 changes: 9 additions & 17 deletions xopresty/resty.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,12 @@ func Client(client *resty.Client, opts ...ClientOpt) *resty.Client {
for _, f := range opts {
f(config)
}
log = log.Sub().Step(description)
defer func() {
if b3Sent && !response {
log.Info().Link("span.far_side_id", b3Trace).Static("span id set with B3")
log.Span().Link("span.far_side_id", b3Trace)
}
log.Done()
}()

// c := *client
// c.Header = client.Header.Clone()
// clinet = &c
return client.
OnBeforeRequest(func(_ *Client, r *resty.Request) error {
OnBeforeRequest(func(_ *resty.Client, r *resty.Request) error {
// OnBeforeRequest can execute multiple times for the same attempt if there
// are retries. It won't execute at all of the request is invalid.
ctx := r.Context()
Expand All @@ -145,18 +137,18 @@ func Client(client *resty.Client, opts ...ClientOpt) *resty.Client {
if nameRaw != nil {
name = nameRaw.(string)
} else {
name = config.RequestToName()
name = config.requestToName(r)
}
log = log.Sub().Step(name)
cv = &contextValue{
originalStartTime: time.Now(),
log: log,
}
r.SetContext(context.WithValue(ctx, contextKey, &cv))
r.SetContext(context.WithValue(ctx, contextKey, cv))
r.SetLogger(restyLogger{log: log})

log.Span().EmbeddedEnum(xopconst.SpanTypeHTTPClientRequest)
log.Span().String(xopconst.URL, r.URL.String())
log.Span().String(xopconst.URL, r.URL)
log.Span().String(xopconst.HTTPMethod, r.Method)
r.Header.Set("traceparent", log.Span().Bundle().Trace.String())
if !log.Span().TraceBaggage().IsZero() {
Expand All @@ -178,7 +170,7 @@ func Client(client *resty.Client, opts ...ClientOpt) *resty.Client {
}
return nil
}).
OnAfterResponse(func(_ *Client, resp *Response) error {
OnAfterResponse(func(_ *resty.Client, resp *resty.Response) error {
// OnAfterRequest is run for each individual request attempt
ctx := resp.Request.Context()
cvRaw := ctx.Value(contextKey)
Expand All @@ -193,15 +185,15 @@ func Client(client *resty.Client, opts ...ClientOpt) *resty.Client {
if tr != "" {
trace, ok := trace.TraceFromString(tr)
if ok {
response = true
cv.response = true
log.Info().Link(xopconst.RemoteTrace.Key(), trace).Static("traceresponse")
log.Span().Link(xopconst.RemoteTrace, trace)
} else {
log.Warn().String("header", tr).Static("invalid traceresponse received")
}
}
if res != nil {
log.Info().Any("response", r.Result())
if resp.Result() != nil {
log.Info().Any("response", resp.Result())
}
ti := resp.Request.TraceInfo()
if ti.TotalTime != 0 {
Expand Down Expand Up @@ -231,7 +223,7 @@ func Client(client *resty.Client, opts ...ClientOpt) *resty.Client {
config.extraLogging(log, cv.originalStartTime, cv.retryCount, r, nil, err)
}).
OnSuccess(func(c *resty.Client, resp *resty.Response) {
ctx := r.Context()
ctx := resp.Request.Context()
cv := ctx.Value(contextKey).(*contextValue)
log := cv.log
config.extraLogging(log, cv.originalStartTime, cv.retryCount, resp.Request, resp, nil)
Expand Down
15 changes: 9 additions & 6 deletions xopresty/resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@ import (
"github.com/muir/xop-go/xopmiddle"
"github.com/muir/xop-go/xopresty"
"github.com/muir/xop-go/xoptest"

"github.com/muir/resty"
)

func TestXopResty(t *testing.T) {
tLog := xoptest.New(t)
seed := xop.Seed(xop.WithBase(tLog))
seed := xop.NewSeed(xop.WithBase(tLog))
log := seed.Request("client")

client := resty.NewClient()
client = xopresty.Client(client)
log.Info().Msg("i am the client")
ctx := log.IntoContext(context.Background())

inbound := xopmiddle.New(seed, func(r *http.Request) string {
return r.Method
})

ts := httptest.NewServer(inbound.HandlerMiddlewareFunc()(func(w http.ResponseWriter, r *http.Request) {
ts := httptest.NewServer(inbound.HandlerFuncMiddleware()(func(w http.ResponseWriter, r *http.Request) {
log := xop.FromContextOrPanic(r.Context())
log.Info().Msg("in request handler")
http.Error(w, "generall broken", 500)
}))
defer ts.Close()

xopresty.Client(resty.New()).SetDebug(true).R().SetContext(ctx).EnableTrace().Get(ts.URL)
}

0 comments on commit d73eb47

Please sign in to comment.