From 305dde459359b01d60e8437d3515093a0685c90f Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Mon, 14 Oct 2024 15:14:39 -0500 Subject: [PATCH 01/10] add tracing support --- services/omnirpc/http/capture_client.go | 71 ++++++++++++++++++++++++- services/omnirpc/http/client.go | 4 ++ services/omnirpc/http/fasthttp.go | 65 +++++++++++++++++++++- services/omnirpc/http/resty.go | 8 +++ services/omnirpc/proxy/forward.go | 8 +-- services/omnirpc/proxy/forwarder.go | 18 ++++--- services/omnirpc/proxy/standardize.go | 3 +- 7 files changed, 161 insertions(+), 16 deletions(-) diff --git a/services/omnirpc/http/capture_client.go b/services/omnirpc/http/capture_client.go index 2a791a2d6e..e1808a59e1 100644 --- a/services/omnirpc/http/capture_client.go +++ b/services/omnirpc/http/capture_client.go @@ -3,7 +3,11 @@ package http import ( "context" + "github.com/ethereum/go-ethereum/common" "github.com/synapsecns/sanguine/core/bytemap" + "github.com/synapsecns/sanguine/core/metrics" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" ) // CaptureClient is a mock client used for checking response values. @@ -56,44 +60,107 @@ type CapturedRequest struct { // RequestURIBytes is the request uri bytes. Notably, this will not include // RequestURI's set by SetRequestURI RequestURIBytes []byte + // Metrics is the metrics handler + Handler metrics.Handler } var _ Client = &CaptureClient{} // SetBody stores the body for testing. func (c *CapturedRequest) SetBody(body []byte) Request { + _, span := c.Handler.Tracer().Start( + c.Context, + "SetBody", + trace.WithAttributes(attribute.String("SetBody", common.Bytes2Hex(body))), + ) + defer func() { + metrics.EndSpan(span) + }() c.Body = body return c } // SetContext stores the context for testing. func (c *CapturedRequest) SetContext(ctx context.Context) Request { + _, span := c.Handler.Tracer().Start( + c.Context, + "SetContext", + ) + span.AddEvent("SetContext") + defer func() { + metrics.EndSpan(span) + }() c.Context = ctx return c } // SetHeader sets the header for testing. func (c *CapturedRequest) SetHeader(key, value string) Request { + _, span := c.Handler.Tracer().Start( + c.Context, + "SetHeader", + trace.WithAttributes(attribute.String("SetHeader", key)), + trace.WithAttributes(attribute.String("value", value)), + ) + defer func() { + metrics.EndSpan(span) + }() c.StringHeaders[key] = value return c } // SetHeaderBytes sets header bytes for testing. func (c *CapturedRequest) SetHeaderBytes(key, value []byte) Request { + _, span := c.Handler.Tracer().Start( + c.Context, + "SetHeaderBytes", + trace.WithAttributes(attribute.String("key", common.Bytes2Hex(key))), + trace.WithAttributes(attribute.String("value", common.Bytes2Hex(value))), + ) + defer func() { + metrics.EndSpan(span) + }() c.ByteHeaders.Put(key, value) return c } // SetRequestURI stores the request uri. func (c *CapturedRequest) SetRequestURI(uri string) Request { + _, span := c.Handler.Tracer().Start( + c.Context, + "SetRequestURI", + trace.WithAttributes(attribute.String("uri", uri)), + ) + defer func() { + metrics.EndSpan(span) + }() c.RequestURI = uri return c } // Do calls responseFunc for testing. func (c *CapturedRequest) Do() (Response, error) { - //nolint: wrapcheck - return c.Client.responseFunc(c) + _, span := c.Handler.Tracer().Start( + c.Context, + "Do", + ) + defer func() { + metrics.EndSpan(span) + }() + + resp, err := c.Client.responseFunc(c) + if err != nil { + return nil, err + } + + span.SetAttributes(attribute.String("response", common.Bytes2Hex(resp.Body()))) + + return resp, err +} + +func (c *CapturedRequest) WithMetrics(metrics metrics.Handler) Request { + c.Handler = metrics + return c } var _ Request = &CapturedRequest{} diff --git a/services/omnirpc/http/client.go b/services/omnirpc/http/client.go index c07ffaeb77..b4b2a0bde3 100644 --- a/services/omnirpc/http/client.go +++ b/services/omnirpc/http/client.go @@ -3,6 +3,8 @@ package http import ( "context" "strings" + + "github.com/synapsecns/sanguine/core/metrics" ) // Client contains a post client for interacting with json rpc servers. @@ -26,6 +28,8 @@ type Request interface { SetRequestURI(uri string) Request // Do makes the actual request Do() (Response, error) + // WithMetrics sets the metrics for the request + WithMetrics(handler metrics.Handler) Request } // Response is a standardized response interface. diff --git a/services/omnirpc/http/fasthttp.go b/services/omnirpc/http/fasthttp.go index 7b7a3d856e..807748c704 100644 --- a/services/omnirpc/http/fasthttp.go +++ b/services/omnirpc/http/fasthttp.go @@ -3,12 +3,17 @@ package http import ( "context" "fmt" + "sync" + "time" + "github.com/ImVexed/fasturl" + "github.com/ethereum/go-ethereum/common" "github.com/puzpuzpuz/xsync" http2 "github.com/synapsecns/fasthttp-http2" + "github.com/synapsecns/sanguine/core/metrics" "github.com/valyala/fasthttp" - "sync" - "time" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" ) // dialer is an allocated fasthttp dialer for increasing dns cache time. @@ -138,6 +143,7 @@ func (f *fastHTTPClient) AcquireRequest() *fastHTTPRequest { &fasthttp.Request{}, f, nil, + nil, } } //nolint: forcetypeassert @@ -158,6 +164,7 @@ type fastHTTPRequest struct { // we need to respect context cancellation even after response //nolint: containedctx context context.Context + handler metrics.Handler } // Reset clears request contents. @@ -167,32 +174,81 @@ func (f *fastHTTPRequest) Reset() { } func (f *fastHTTPRequest) SetBody(body []byte) Request { + _, span := f.handler.Tracer().Start( + f.context, + "SetBody", + trace.WithAttributes(attribute.String("body", common.Bytes2Hex(body))), + ) + defer func() { + metrics.EndSpan(span) + }() f.Request.SetBodyRaw(body) return f } // SetContext does nothing on fasthttp request. func (f *fastHTTPRequest) SetContext(ctx context.Context) Request { + _, span := f.handler.Tracer().Start( + f.context, + "SetContext", + ) + span.AddEvent("SetContext") + defer func() { + metrics.EndSpan(span) + }() f.context = ctx return f } func (f *fastHTTPRequest) SetHeader(key, value string) Request { + _, span := f.handler.Tracer().Start( + f.context, + "SetHeader", + trace.WithAttributes(attribute.String("SetHeader", key)), + trace.WithAttributes(attribute.String("value", value)), + ) + defer func() { + metrics.EndSpan(span) + }() f.Request.Header.Set(key, value) return f } func (f *fastHTTPRequest) SetHeaderBytes(key, value []byte) Request { + _, span := f.handler.Tracer().Start( + f.context, + "SetHeaderBytes", + trace.WithAttributes(attribute.String("key", common.Bytes2Hex(key))), + trace.WithAttributes(attribute.String("value", common.Bytes2Hex(value))), + ) + defer func() { + metrics.EndSpan(span) + }() f.Request.Header.SetBytesKV(key, value) return f } func (f *fastHTTPRequest) SetRequestURI(uri string) Request { + _, span := f.handler.Tracer().Start( + f.context, + "SetRequestURI", + trace.WithAttributes(attribute.String("uri", uri)), + ) + defer func() { + metrics.EndSpan(span) + }() f.Request.SetRequestURI(uri) return f } func (f *fastHTTPRequest) Do() (Response, error) { + _, span := f.handler.Tracer().Start( + f.context, + "Do", + ) + defer func() { + metrics.EndSpan(span) + }() defer f.Reset() uri := f.Request.URI() @@ -225,6 +281,11 @@ func (f *fastHTTPRequest) Do() (Response, error) { } } +func (f *fastHTTPRequest) WithMetrics(metrics metrics.Handler) Request { + f.handler = metrics + return f +} + func (f *fastHTTPClient) NewRequest() Request { req := f.AcquireRequest() return req diff --git a/services/omnirpc/http/resty.go b/services/omnirpc/http/resty.go index 58db412bb7..2fb37dfde7 100644 --- a/services/omnirpc/http/resty.go +++ b/services/omnirpc/http/resty.go @@ -2,7 +2,9 @@ package http import ( "context" + "github.com/go-resty/resty/v2" + "github.com/synapsecns/sanguine/core/metrics" ) // RestyClient is a resty client for making requests to the http client. @@ -19,6 +21,7 @@ func NewRestyClient() Client { type restyRequest struct { *resty.Request endpoint string + handler metrics.Handler } // NewRequest create a new request. @@ -59,4 +62,9 @@ func (r *restyRequest) Do() (Response, error) { return r.Request.Post(r.endpoint) } +func (r *restyRequest) WithMetrics(metrics metrics.Handler) Request { + r.handler = metrics + return r +} + var _ Client = &RestyClient{} diff --git a/services/omnirpc/proxy/forward.go b/services/omnirpc/proxy/forward.go index ffdf2d37dc..dfc3c05159 100644 --- a/services/omnirpc/proxy/forward.go +++ b/services/omnirpc/proxy/forward.go @@ -5,6 +5,9 @@ import ( "context" "crypto/sha256" "fmt" + goHTTP "net/http" + "strings" + "github.com/ImVexed/fasturl" "github.com/goccy/go-json" "github.com/jftuga/ellipsis" @@ -14,8 +17,6 @@ import ( "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" "golang.org/x/exp/slices" - goHTTP "net/http" - "strings" ) type rawResponse struct { @@ -129,7 +130,7 @@ const ( ) func (f *Forwarder) forwardRequest(parentCtx context.Context, endpoint string) (_ *rawResponse, err error) { - ctx, span := f.tracer.Start(parentCtx, "forwardRequest", + ctx, span := f.handler.Tracer().Start(parentCtx, "forwardRequest", trace.WithAttributes(attribute.String("endpoint", endpoint)), ) @@ -158,6 +159,7 @@ func (f *Forwarder) forwardRequest(parentCtx context.Context, endpoint string) ( SetHeaderBytes(http.XForwardedFor, http.OmniRPCValue). SetHeaderBytes(http.ContentType, http.JSONType). SetHeaderBytes(http.Accept, http.JSONType). + WithMetrics(f.handler). Do() if err != nil { return nil, fmt.Errorf("could not get response from %s: %w", endpoint, err) diff --git a/services/omnirpc/proxy/forwarder.go b/services/omnirpc/proxy/forwarder.go index 4d45abd944..d77a9545c6 100644 --- a/services/omnirpc/proxy/forwarder.go +++ b/services/omnirpc/proxy/forwarder.go @@ -3,19 +3,21 @@ package proxy import ( "context" "fmt" - "github.com/synapsecns/sanguine/ethergo/parser/rpc" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/trace" "io" "net/http" "strconv" "strings" "sync" + "github.com/synapsecns/sanguine/ethergo/parser/rpc" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" + "github.com/Soft/iter" "github.com/gin-gonic/gin" "github.com/goccy/go-json" "github.com/puzpuzpuz/xsync" + "github.com/synapsecns/sanguine/core/metrics" "github.com/synapsecns/sanguine/core/threaditer" "github.com/synapsecns/sanguine/services/omnirpc/chainmanager" omniHTTP "github.com/synapsecns/sanguine/services/omnirpc/http" @@ -50,8 +52,8 @@ type Forwarder struct { mux sync.RWMutex // span is the span for the request span trace.Span - // tracer is the tracer for the request - tracer trace.Tracer + // handler is the metrics handler + handler metrics.Handler } // Reset resets the forwarder so it can be reused. @@ -77,9 +79,9 @@ func (r *RPCProxy) AcquireForwarder() *Forwarder { v := r.forwarderPool.Get() if v == nil { return &Forwarder{ - r: r, - client: r.client, - tracer: r.tracer, + r: r, + client: r.client, + handler: r.handler, } } //nolint: forcetypeassert diff --git a/services/omnirpc/proxy/standardize.go b/services/omnirpc/proxy/standardize.go index 31904ec6c1..2911f7ea68 100644 --- a/services/omnirpc/proxy/standardize.go +++ b/services/omnirpc/proxy/standardize.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "github.com/synapsecns/sanguine/ethergo/client" "github.com/synapsecns/sanguine/ethergo/parser/rpc" @@ -34,7 +35,7 @@ type JSONError struct { // rpcTransaction is an eth rpc transaction (copied from ethclient). type rpcTransaction struct { - //nolint: unused + //lint:ignore U1000 it's okay. tx *types.Transaction txExtraInfo } From 275dfaa7fe60b6ad35a7432186f4928d1c6b58f0 Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Mon, 14 Oct 2024 15:18:10 -0500 Subject: [PATCH 02/10] add tracing support --- services/omnirpc/http/resty.go | 63 ++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/services/omnirpc/http/resty.go b/services/omnirpc/http/resty.go index 2fb37dfde7..6510dfb6aa 100644 --- a/services/omnirpc/http/resty.go +++ b/services/omnirpc/http/resty.go @@ -3,8 +3,11 @@ package http import ( "context" + "github.com/ethereum/go-ethereum/common" "github.com/go-resty/resty/v2" "github.com/synapsecns/sanguine/core/metrics" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" ) // RestyClient is a resty client for making requests to the http client. @@ -33,33 +36,87 @@ func (r RestyClient) NewRequest() Request { // SetHeaderBytes is a wrapper around SetHeadre for bytes. func (r *restyRequest) SetHeaderBytes(key, value []byte) Request { + _, span := r.handler.Tracer().Start( + r.Request.Context(), + "SetHeaderBytes", + trace.WithAttributes( + attribute.String("key", common.Bytes2Hex(key)), + attribute.String("value", common.Bytes2Hex(value)), + )) + defer func() { + metrics.EndSpan(span) + }() r.Request.SetHeader(string(key), string(value)) return r } func (r *restyRequest) SetBody(body []byte) Request { + _, span := r.handler.Tracer().Start( + r.Request.Context(), + "SetBody", + trace.WithAttributes(attribute.String("body", common.Bytes2Hex(body))), + ) + defer func() { + metrics.EndSpan(span) + }() r.Request.SetBody(body) return r } func (r *restyRequest) SetContext(ctx context.Context) Request { + _, span := r.handler.Tracer().Start( + r.Request.Context(), + "SetContext", + ) + span.AddEvent("SetContext") + defer func() { + metrics.EndSpan(span) + }() r.Request.SetContext(ctx) return r } func (r *restyRequest) SetHeader(key, value string) Request { + _, span := r.handler.Tracer().Start( + r.Request.Context(), + "SetHeader", + trace.WithAttributes( + attribute.String("SetHeader", key), + attribute.String("value", value), + )) + defer func() { + metrics.EndSpan(span) + }() r.Request.SetHeader(key, value) return r } func (r *restyRequest) SetRequestURI(uri string) Request { + _, span := r.handler.Tracer().Start( + r.Request.Context(), + "SetRequestURI", + trace.WithAttributes(attribute.String("uri", uri)), + ) + defer func() { + metrics.EndSpan(span) + }() r.endpoint = uri return r } -func (r *restyRequest) Do() (Response, error) { - //nolint: wrapcheck - return r.Request.Post(r.endpoint) +func (r *restyRequest) Do() (_ Response, err error) { + _, span := r.handler.Tracer().Start( + r.Request.Context(), + "Do", + ) + defer func() { + metrics.EndSpanWithErr(span, err) + }() + resp, err := r.Request.Post(r.endpoint) + if err != nil { + return nil, err + } + return resp, nil } func (r *restyRequest) WithMetrics(metrics metrics.Handler) Request { From fc96c213a63c7b1939e8c25ba19bdf8231640db2 Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Mon, 14 Oct 2024 15:19:31 -0500 Subject: [PATCH 03/10] fix lint --- services/omnirpc/http/capture_client.go | 1 + services/omnirpc/http/resty.go | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/services/omnirpc/http/capture_client.go b/services/omnirpc/http/capture_client.go index e1808a59e1..9a1a8033f5 100644 --- a/services/omnirpc/http/capture_client.go +++ b/services/omnirpc/http/capture_client.go @@ -158,6 +158,7 @@ func (c *CapturedRequest) Do() (Response, error) { return resp, err } +// WithMetrics sets the metrics handler. func (c *CapturedRequest) WithMetrics(metrics metrics.Handler) Request { c.Handler = metrics return c diff --git a/services/omnirpc/http/resty.go b/services/omnirpc/http/resty.go index 6510dfb6aa..39cc2aec60 100644 --- a/services/omnirpc/http/resty.go +++ b/services/omnirpc/http/resty.go @@ -2,6 +2,7 @@ package http import ( "context" + "fmt" "github.com/ethereum/go-ethereum/common" "github.com/go-resty/resty/v2" @@ -114,7 +115,7 @@ func (r *restyRequest) Do() (_ Response, err error) { }() resp, err := r.Request.Post(r.endpoint) if err != nil { - return nil, err + return nil, fmt.Errorf("could not get response from %s: %w", r.endpoint, err) } return resp, nil } From 81a732ef4cd6db1fb47646913ec40ae673deaed8 Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Tue, 15 Oct 2024 10:13:46 -0500 Subject: [PATCH 04/10] fix nil pointer --- services/omnirpc/http/capture_client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/omnirpc/http/capture_client.go b/services/omnirpc/http/capture_client.go index 9a1a8033f5..2889028bcd 100644 --- a/services/omnirpc/http/capture_client.go +++ b/services/omnirpc/http/capture_client.go @@ -83,7 +83,7 @@ func (c *CapturedRequest) SetBody(body []byte) Request { // SetContext stores the context for testing. func (c *CapturedRequest) SetContext(ctx context.Context) Request { _, span := c.Handler.Tracer().Start( - c.Context, + ctx, "SetContext", ) span.AddEvent("SetContext") From 9055ff642ba9ae47e40c9aa3578ca824e253510c Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Tue, 15 Oct 2024 10:21:32 -0500 Subject: [PATCH 05/10] fix nil pointer --- services/omnirpc/http/fasthttp.go | 2 +- services/omnirpc/http/resty.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/omnirpc/http/fasthttp.go b/services/omnirpc/http/fasthttp.go index 807748c704..49a30d1bbf 100644 --- a/services/omnirpc/http/fasthttp.go +++ b/services/omnirpc/http/fasthttp.go @@ -189,7 +189,7 @@ func (f *fastHTTPRequest) SetBody(body []byte) Request { // SetContext does nothing on fasthttp request. func (f *fastHTTPRequest) SetContext(ctx context.Context) Request { _, span := f.handler.Tracer().Start( - f.context, + ctx, "SetContext", ) span.AddEvent("SetContext") diff --git a/services/omnirpc/http/resty.go b/services/omnirpc/http/resty.go index 39cc2aec60..6384abc40c 100644 --- a/services/omnirpc/http/resty.go +++ b/services/omnirpc/http/resty.go @@ -66,7 +66,7 @@ func (r *restyRequest) SetBody(body []byte) Request { func (r *restyRequest) SetContext(ctx context.Context) Request { _, span := r.handler.Tracer().Start( - r.Request.Context(), + ctx, "SetContext", ) span.AddEvent("SetContext") From 54eacfa2f70dcc165592d0c9d08d12d7353cea59 Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Tue, 15 Oct 2024 12:02:01 -0500 Subject: [PATCH 06/10] fix nil pointer --- services/omnirpc/http/capture_client.go | 12 ++---- services/omnirpc/http/capture_client_test.go | 16 +++++--- services/omnirpc/http/client.go | 10 ++--- services/omnirpc/http/client_test.go | 5 ++- services/omnirpc/http/fasthttp.go | 38 +++++++++---------- services/omnirpc/http/resty.go | 13 +++---- services/omnirpc/http/suite_test.go | 6 ++- .../confirmedtofinalized/finalizedproxy.go | 11 +++--- .../modules/harmonyproxy/harmonyproxy.go | 2 +- .../modules/receiptsbackup/receiptsbackup.go | 5 ++- services/omnirpc/proxy/forward.go | 1 - services/omnirpc/proxy/forward_test.go | 25 ++++++------ services/omnirpc/proxy/forwarder.go | 2 +- services/omnirpc/proxy/server.go | 15 +++----- 14 files changed, 80 insertions(+), 81 deletions(-) diff --git a/services/omnirpc/http/capture_client.go b/services/omnirpc/http/capture_client.go index 2889028bcd..748d5d20bc 100644 --- a/services/omnirpc/http/capture_client.go +++ b/services/omnirpc/http/capture_client.go @@ -14,14 +14,15 @@ import ( type CaptureClient struct { requests []*CapturedRequest responseFunc MakeResponseFunc + handler metrics.Handler } // MakeResponseFunc is used for mocking responses. type MakeResponseFunc func(c *CapturedRequest) (Response, error) // NewCaptureClient creates anew client for testing. -func NewCaptureClient(responseFunc MakeResponseFunc) *CaptureClient { - return &CaptureClient{requests: []*CapturedRequest{}, responseFunc: responseFunc} +func NewCaptureClient(handler metrics.Handler, responseFunc MakeResponseFunc) *CaptureClient { + return &CaptureClient{requests: []*CapturedRequest{}, responseFunc: responseFunc, handler: handler} } // Requests turns a list of sent requests. These are not mutation safe. @@ -34,6 +35,7 @@ func (c *CaptureClient) NewRequest() Request { request := CapturedRequest{ Client: c, StringHeaders: make(map[string]string), + Handler: c.handler, } c.requests = append(c.requests, &request) return &request @@ -158,10 +160,4 @@ func (c *CapturedRequest) Do() (Response, error) { return resp, err } -// WithMetrics sets the metrics handler. -func (c *CapturedRequest) WithMetrics(metrics metrics.Handler) Request { - c.Handler = metrics - return c -} - var _ Request = &CapturedRequest{} diff --git a/services/omnirpc/http/capture_client_test.go b/services/omnirpc/http/capture_client_test.go index b99b520624..ed1ecde653 100644 --- a/services/omnirpc/http/capture_client_test.go +++ b/services/omnirpc/http/capture_client_test.go @@ -2,23 +2,27 @@ package http_test import ( "context" + "testing" + "github.com/brianvoe/gofakeit/v6" . "github.com/stretchr/testify/assert" + "github.com/synapsecns/sanguine/core/metrics" "github.com/synapsecns/sanguine/services/omnirpc/http" "github.com/synapsecns/sanguine/services/omnirpc/http/mocks" - "testing" ) func TestCaptureClient(t *testing.T) { testRes := gofakeit.ImageJpeg(50, 50) testBody := gofakeit.ImageJpeg(50, 50) - client := http.NewCaptureClient(func(c *http.CapturedRequest) (http.Response, error) { - bodyRes := new(mocks.Response) - bodyRes.On("Body").Return(testRes) + client := http.NewCaptureClient( + metrics.NewNullHandler(), + func(c *http.CapturedRequest) (http.Response, error) { + bodyRes := new(mocks.Response) + bodyRes.On("Body").Return(testRes) - return bodyRes, nil - }) + return bodyRes, nil + }) testCtx := context.Background() diff --git a/services/omnirpc/http/client.go b/services/omnirpc/http/client.go index b4b2a0bde3..f228646b9f 100644 --- a/services/omnirpc/http/client.go +++ b/services/omnirpc/http/client.go @@ -28,8 +28,6 @@ type Request interface { SetRequestURI(uri string) Request // Do makes the actual request Do() (Response, error) - // WithMetrics sets the metrics for the request - WithMetrics(handler metrics.Handler) Request } // Response is a standardized response interface. @@ -66,14 +64,14 @@ func init() { // NewClient creates a client from the client type // defaults to fast http. -func NewClient(clientType ClientType) Client { +func NewClient(handler metrics.Handler, clientType ClientType) Client { switch clientType { case FastHTTP: - return NewFastHTTPClient() + return NewFastHTTPClient(handler) case Resty: - return NewRestyClient() + return NewRestyClient(handler) default: - return NewRestyClient() + return NewRestyClient(handler) } } diff --git a/services/omnirpc/http/client_test.go b/services/omnirpc/http/client_test.go index 2e1314ef83..cef1b0d6ca 100644 --- a/services/omnirpc/http/client_test.go +++ b/services/omnirpc/http/client_test.go @@ -1,11 +1,12 @@ package http_test import ( - "github.com/brianvoe/gofakeit/v6" - . "github.com/stretchr/testify/assert" "io" "net/http" "net/http/httptest" + + "github.com/brianvoe/gofakeit/v6" + . "github.com/stretchr/testify/assert" ) var jsonOptions = &gofakeit.JSONOptions{ diff --git a/services/omnirpc/http/fasthttp.go b/services/omnirpc/http/fasthttp.go index 49a30d1bbf..42ecc6339a 100644 --- a/services/omnirpc/http/fasthttp.go +++ b/services/omnirpc/http/fasthttp.go @@ -31,6 +31,7 @@ type fastHTTPClient struct { // no longer needed. This allows Request recycling, reduces GC pressure // and usually improves performance. reqPool sync.Pool + handler metrics.Handler } // FastClient is an interface for storing both fasthttp.Clients and fasthttp.HostClients. @@ -47,16 +48,20 @@ var _ FastClient = &fasthttp.HostClient{} // while substantially faster than resty, this can be a bad choice in certain cases: // - Context Cancellation not respected: fasthttp does not support context cancellation, so we hardcode a timeout here // this is less than ideal and puts additional load on both the application and rpc servers since we pessimistically fetch -func NewFastHTTPClient() Client { - return &fastHTTPClient{clients: xsync.NewMapOf[FastClient](), defaultClient: &fasthttp.Client{ - NoDefaultUserAgentHeader: true, - Dial: dialer.Dial, - DialDualStack: false, - ReadTimeout: time.Second * 30, - WriteTimeout: time.Second * 30, - DisableHeaderNamesNormalizing: true, - DisablePathNormalizing: true, - }} +func NewFastHTTPClient(handler metrics.Handler) Client { + return &fastHTTPClient{ + clients: xsync.NewMapOf[FastClient](), + defaultClient: &fasthttp.Client{ + NoDefaultUserAgentHeader: true, + Dial: dialer.Dial, + DialDualStack: false, + ReadTimeout: time.Second * 30, + WriteTimeout: time.Second * 30, + DisableHeaderNamesNormalizing: true, + DisablePathNormalizing: true, + }, + handler: handler, + } } type rawResponse struct { @@ -140,10 +145,10 @@ func (f *fastHTTPClient) AcquireRequest() *fastHTTPRequest { v := f.reqPool.Get() if v == nil { return &fastHTTPRequest{ - &fasthttp.Request{}, - f, - nil, - nil, + Request: &fasthttp.Request{}, + client: f, + context: nil, + handler: f.handler, } } //nolint: forcetypeassert @@ -281,11 +286,6 @@ func (f *fastHTTPRequest) Do() (Response, error) { } } -func (f *fastHTTPRequest) WithMetrics(metrics metrics.Handler) Request { - f.handler = metrics - return f -} - func (f *fastHTTPClient) NewRequest() Request { req := f.AcquireRequest() return req diff --git a/services/omnirpc/http/resty.go b/services/omnirpc/http/resty.go index 6384abc40c..802120c62a 100644 --- a/services/omnirpc/http/resty.go +++ b/services/omnirpc/http/resty.go @@ -13,13 +13,14 @@ import ( // RestyClient is a resty client for making requests to the http client. type RestyClient struct { - client *resty.Client + client *resty.Client + metrics metrics.Handler } // NewRestyClient creates a resty client. // while much slower than fasthttp, this client requests context cancellation. -func NewRestyClient() Client { - return &RestyClient{client: resty.New()} +func NewRestyClient(metrics metrics.Handler) Client { + return &RestyClient{client: resty.New(), metrics: metrics} } type restyRequest struct { @@ -32,6 +33,7 @@ type restyRequest struct { func (r RestyClient) NewRequest() Request { return &restyRequest{ Request: r.client.R(), + handler: r.metrics, } } @@ -120,9 +122,4 @@ func (r *restyRequest) Do() (_ Response, err error) { return resp, nil } -func (r *restyRequest) WithMetrics(metrics metrics.Handler) Request { - r.handler = metrics - return r -} - var _ Client = &RestyClient{} diff --git a/services/omnirpc/http/suite_test.go b/services/omnirpc/http/suite_test.go index b197306eea..89b65cb516 100644 --- a/services/omnirpc/http/suite_test.go +++ b/services/omnirpc/http/suite_test.go @@ -1,11 +1,13 @@ package http_test import ( + "testing" + "github.com/brianvoe/gofakeit/v6" "github.com/stretchr/testify/suite" + "github.com/synapsecns/sanguine/core/metrics" "github.com/synapsecns/sanguine/core/testsuite" "github.com/synapsecns/sanguine/services/omnirpc/http" - "testing" ) // clientSuite defines the basic test suite. @@ -27,7 +29,7 @@ func (c *HTTPSuite) SetupTest() { c.TestSuite.SetupTest() for _, clientType := range http.AllClientTypes { - c.clients = append(c.clients, http.NewClient(clientType)) + c.clients = append(c.clients, http.NewClient(metrics.NewNullHandler(), clientType)) } } diff --git a/services/omnirpc/modules/confirmedtofinalized/finalizedproxy.go b/services/omnirpc/modules/confirmedtofinalized/finalizedproxy.go index ce974daa3f..55471ebba9 100644 --- a/services/omnirpc/modules/confirmedtofinalized/finalizedproxy.go +++ b/services/omnirpc/modules/confirmedtofinalized/finalizedproxy.go @@ -6,6 +6,11 @@ import ( "encoding/json" "errors" "fmt" + "io" + "math/big" + "net/http" + "time" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/params" @@ -22,10 +27,6 @@ import ( "github.com/synapsecns/sanguine/services/omnirpc/swagger" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" - "io" - "math/big" - "net/http" - "time" ) // FinalizedProxy is the interface for the finalized proxy. @@ -58,7 +59,7 @@ func NewProxy(proxyURL string, handler metrics.Handler, port, maxSubmitAhead, ch proxyURL: proxyURL, handler: handler, port: uint16(port), - client: omniHTTP.NewRestyClient(), + client: omniHTTP.NewRestyClient(handler), logger: handler.ExperimentalLogger(), maxSubmitAhead: maxSubmitAhead, chainID: chainID, diff --git a/services/omnirpc/modules/harmonyproxy/harmonyproxy.go b/services/omnirpc/modules/harmonyproxy/harmonyproxy.go index 023d0ab3ea..32f1540c30 100644 --- a/services/omnirpc/modules/harmonyproxy/harmonyproxy.go +++ b/services/omnirpc/modules/harmonyproxy/harmonyproxy.go @@ -54,7 +54,7 @@ func NewHarmonyProxy(proxyURL string, handler metrics.Handler, port int) *Harmon proxyURL: proxyURL, handler: handler, port: uint16(port), - client: omniHTTP.NewRestyClient(), + client: omniHTTP.NewRestyClient(handler), tracer: handler.Tracer(), } } diff --git a/services/omnirpc/modules/receiptsbackup/receiptsbackup.go b/services/omnirpc/modules/receiptsbackup/receiptsbackup.go index f472d40105..fc0fe85943 100644 --- a/services/omnirpc/modules/receiptsbackup/receiptsbackup.go +++ b/services/omnirpc/modules/receiptsbackup/receiptsbackup.go @@ -5,11 +5,12 @@ import ( "encoding/json" "errors" "fmt" - "github.com/synapsecns/sanguine/services/omnirpc/modules/mixins" "io" "net/http" "time" + "github.com/synapsecns/sanguine/services/omnirpc/modules/mixins" + "github.com/flowchartsman/swaggerui" "github.com/gin-gonic/gin" "github.com/synapsecns/sanguine/core/ginhelper" @@ -57,7 +58,7 @@ func NewProxy(proxyURL, backupURL string, receiptTimeout time.Duration, handler backupURL: backupURL, handler: handler, port: uint16(port), - client: omniHTTP.NewRestyClient(), + client: omniHTTP.NewRestyClient(handler), logger: handler.ExperimentalLogger(), chainID: chainID, receiptTimeout: receiptTimeout, diff --git a/services/omnirpc/proxy/forward.go b/services/omnirpc/proxy/forward.go index dfc3c05159..f053b6060c 100644 --- a/services/omnirpc/proxy/forward.go +++ b/services/omnirpc/proxy/forward.go @@ -159,7 +159,6 @@ func (f *Forwarder) forwardRequest(parentCtx context.Context, endpoint string) ( SetHeaderBytes(http.XForwardedFor, http.OmniRPCValue). SetHeaderBytes(http.ContentType, http.JSONType). SetHeaderBytes(http.Accept, http.JSONType). - WithMetrics(f.handler). Do() if err != nil { return nil, fmt.Errorf("could not get response from %s: %w", endpoint, err) diff --git a/services/omnirpc/proxy/forward_test.go b/services/omnirpc/proxy/forward_test.go index 6eea0570f8..39b3dbc95e 100644 --- a/services/omnirpc/proxy/forward_test.go +++ b/services/omnirpc/proxy/forward_test.go @@ -5,6 +5,11 @@ import ( "encoding/json" "errors" "fmt" + "net/http" + "net/http/httptest" + "net/url" + "strconv" + "github.com/brianvoe/gofakeit/v6" "github.com/gin-gonic/gin" "github.com/google/uuid" @@ -18,10 +23,6 @@ import ( "github.com/synapsecns/sanguine/services/omnirpc/http/mocks" "github.com/synapsecns/sanguine/services/omnirpc/proxy" proxyMocks "github.com/synapsecns/sanguine/services/omnirpc/proxy/mocks" - "net/http" - "net/http/httptest" - "net/url" - "strconv" ) func (p *ProxySuite) TestServeRequestNoChain() { @@ -84,7 +85,7 @@ func (p *ProxySuite) TestAcquireReleaseForwarder() { forwarder := prxy.AcquireForwarder() forwarder.SetChain(new(chainManagerMocks.Chain)) forwarder.SetC(&gin.Context{}) - forwarder.SetClient(omniHTTP.NewClient(omniHTTP.Resty)) + forwarder.SetClient(omniHTTP.NewClient(p.metrics, omniHTTP.Resty)) forwarder.SetR(prxy) forwarder.SetBody(gofakeit.ImagePng(5, 5)) forwarder.SetRequestID([]byte(uuid.New().String())) @@ -144,12 +145,14 @@ func (p *ProxySuite) TestForwardRequest() { Result: nil, }) - captureClient := omniHTTP.NewCaptureClient(func(c *omniHTTP.CapturedRequest) (omniHTTP.Response, error) { - bodyRes := new(mocks.Response) - bodyRes.On("Body").Return(testRes) - bodyRes.On("StatusCode").Return(200) - return bodyRes, nil - }) + captureClient := omniHTTP.NewCaptureClient( + p.metrics, + func(c *omniHTTP.CapturedRequest) (omniHTTP.Response, error) { + bodyRes := new(mocks.Response) + bodyRes.On("Body").Return(testRes) + bodyRes.On("StatusCode").Return(200) + return bodyRes, nil + }) prxy.SetClient(captureClient) testURL := gofakeit.URL() diff --git a/services/omnirpc/proxy/forwarder.go b/services/omnirpc/proxy/forwarder.go index d77a9545c6..d0b4776f3d 100644 --- a/services/omnirpc/proxy/forwarder.go +++ b/services/omnirpc/proxy/forwarder.go @@ -97,7 +97,7 @@ func (r *RPCProxy) ReleaseForwarder(f *Forwarder) { // Forward forwards the rpc request to the servers and makes assertions around confirmation thresholds. // required confirmations can be used to override the required confirmations count. func (r *RPCProxy) Forward(c *gin.Context, chainID uint32, requiredConfirmationsOverride *uint16) { - ctx, span := r.tracer.Start(c, "rpcRequest", + ctx, span := r.handler.Tracer().Start(c, "rpcRequest", trace.WithAttributes(attribute.Int("chainID", int(chainID))), ) diff --git a/services/omnirpc/proxy/server.go b/services/omnirpc/proxy/server.go index a454767746..e99ec2801f 100644 --- a/services/omnirpc/proxy/server.go +++ b/services/omnirpc/proxy/server.go @@ -3,6 +3,11 @@ package proxy import ( "context" "fmt" + "net/http" + "strconv" + "sync" + "time" + "github.com/flowchartsman/swaggerui" "github.com/gin-gonic/gin" "github.com/synapsecns/sanguine/core/ginhelper" @@ -12,17 +17,10 @@ import ( "github.com/synapsecns/sanguine/services/omnirpc/config" omniHTTP "github.com/synapsecns/sanguine/services/omnirpc/http" "github.com/synapsecns/sanguine/services/omnirpc/swagger" - "go.opentelemetry.io/otel/trace" - "net/http" - "strconv" - "sync" - "time" ) // RPCProxy proxies rpc request to the fastest endpoint. Requests fallback in cases where data is not available. type RPCProxy struct { - // tracer is the tracer for the proxy - tracer trace.Tracer // chainManager contains a list of chains and latency ordered rpcs chainManager chainmanager.ChainManager // config contains the config for each chain @@ -50,9 +48,8 @@ func NewProxy(config config.Config, handler metrics.Handler) *RPCProxy { chainManager: chainmanager.NewChainManagerFromConfig(config, handler), refreshInterval: time.Second * time.Duration(config.RefreshInterval), port: config.Port, - client: omniHTTP.NewClient(omniHTTP.ClientTypeFromString(config.ClientType)), + client: omniHTTP.NewClient(handler, omniHTTP.ClientTypeFromString(config.ClientType)), handler: handler, - tracer: handler.Tracer(), } } From 151d6cd8e12267e6e305d6b9e0fa60ef95e5bbf0 Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Tue, 15 Oct 2024 14:43:59 -0500 Subject: [PATCH 07/10] fix tracing --- services/omnirpc/http/capture_client_test.go | 2 +- services/omnirpc/http/client_test.go | 2 +- services/omnirpc/http/resty.go | 8 +++---- services/omnirpc/http/suite_test.go | 22 ++++++++++++++++++++ 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/services/omnirpc/http/capture_client_test.go b/services/omnirpc/http/capture_client_test.go index ed1ecde653..e5a20b9879 100644 --- a/services/omnirpc/http/capture_client_test.go +++ b/services/omnirpc/http/capture_client_test.go @@ -35,8 +35,8 @@ func TestCaptureClient(t *testing.T) { testURL := gofakeit.URL() testReq := client.NewRequest() - testReq.SetBody(testBody) testReq.SetContext(testCtx) + testReq.SetBody(testBody) testReq.SetHeaderBytes(byteHeaderK, byteHeaderV) testReq.SetHeader(strHeaderK, strHeaderV) testReq.SetRequestURI(testURL) diff --git a/services/omnirpc/http/client_test.go b/services/omnirpc/http/client_test.go index cef1b0d6ca..b3cf2e6001 100644 --- a/services/omnirpc/http/client_test.go +++ b/services/omnirpc/http/client_test.go @@ -43,9 +43,9 @@ func (c *HTTPSuite) TestClient() { })) req := client.NewRequest() + req.SetContext(c.GetTestContext()) req.SetRequestURI(svr.URL) req.SetBody(mockBody) - req.SetContext(c.GetTestContext()) for key, val := range headers { if gofakeit.Bool() { req.SetHeader(key, val) diff --git a/services/omnirpc/http/resty.go b/services/omnirpc/http/resty.go index 802120c62a..55287d81ce 100644 --- a/services/omnirpc/http/resty.go +++ b/services/omnirpc/http/resty.go @@ -14,13 +14,13 @@ import ( // RestyClient is a resty client for making requests to the http client. type RestyClient struct { client *resty.Client - metrics metrics.Handler + handler metrics.Handler } // NewRestyClient creates a resty client. // while much slower than fasthttp, this client requests context cancellation. -func NewRestyClient(metrics metrics.Handler) Client { - return &RestyClient{client: resty.New(), metrics: metrics} +func NewRestyClient(handler metrics.Handler) Client { + return &RestyClient{client: resty.New(), handler: handler} } type restyRequest struct { @@ -33,7 +33,7 @@ type restyRequest struct { func (r RestyClient) NewRequest() Request { return &restyRequest{ Request: r.client.R(), - handler: r.metrics, + handler: r.handler, } } diff --git a/services/omnirpc/http/suite_test.go b/services/omnirpc/http/suite_test.go index 89b65cb516..5f376b7175 100644 --- a/services/omnirpc/http/suite_test.go +++ b/services/omnirpc/http/suite_test.go @@ -5,15 +5,21 @@ import ( "github.com/brianvoe/gofakeit/v6" "github.com/stretchr/testify/suite" + "github.com/synapsecns/sanguine/core" + "github.com/synapsecns/sanguine/core/config" "github.com/synapsecns/sanguine/core/metrics" + "github.com/synapsecns/sanguine/core/metrics/localmetrics" "github.com/synapsecns/sanguine/core/testsuite" "github.com/synapsecns/sanguine/services/omnirpc/http" ) +var buildInfo = config.NewBuildInfo(config.DefaultVersion, config.DefaultCommit, "omnirpc", config.DefaultDate) + // clientSuite defines the basic test suite. type HTTPSuite struct { *testsuite.TestSuite clients []http.Client + metrics metrics.Handler } // NewTestSuite creates a new test suite and performs some basic checks afterward. @@ -33,6 +39,22 @@ func (c *HTTPSuite) SetupTest() { } } +func (c *HTTPSuite) SetupSuite() { + c.TestSuite.SetupSuite() + // don't use metrics on ci for integration tests + isCI := core.GetEnvBool("CI", false) + useMetrics := !isCI + metricsHandler := metrics.Null + + if useMetrics { + localmetrics.SetupTestJaeger(c.GetSuiteContext(), c.T()) + metricsHandler = metrics.Jaeger + } + var err error + c.metrics, err = metrics.NewByType(c.GetSuiteContext(), buildInfo, metricsHandler) + c.Require().NoError(err) +} + func TestCommonSuite(t *testing.T) { suite.Run(t, NewClientSuite(t)) } From 7eb74489169eba5cfffc104552168fd7bcfbd00a Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Thu, 17 Oct 2024 11:26:20 -0500 Subject: [PATCH 08/10] span name --- services/omnirpc/proxy/forwarder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/omnirpc/proxy/forwarder.go b/services/omnirpc/proxy/forwarder.go index d0b4776f3d..03841fa0e0 100644 --- a/services/omnirpc/proxy/forwarder.go +++ b/services/omnirpc/proxy/forwarder.go @@ -97,7 +97,7 @@ func (r *RPCProxy) ReleaseForwarder(f *Forwarder) { // Forward forwards the rpc request to the servers and makes assertions around confirmation thresholds. // required confirmations can be used to override the required confirmations count. func (r *RPCProxy) Forward(c *gin.Context, chainID uint32, requiredConfirmationsOverride *uint16) { - ctx, span := r.handler.Tracer().Start(c, "rpcRequest", + ctx, span := r.handler.Tracer().Start(c, "Forward", trace.WithAttributes(attribute.Int("chainID", int(chainID))), ) From fc15e547fe327024ed1f55333d08decd1f522624 Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Thu, 17 Oct 2024 11:35:09 -0500 Subject: [PATCH 09/10] go generate --- services/rfq/api/db/activequoterequeststatus_string.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/rfq/api/db/activequoterequeststatus_string.go b/services/rfq/api/db/activequoterequeststatus_string.go index cb9e64a4d6..3ebff97ae4 100644 --- a/services/rfq/api/db/activequoterequeststatus_string.go +++ b/services/rfq/api/db/activequoterequeststatus_string.go @@ -16,7 +16,7 @@ func _() { const _ActiveQuoteRequestStatus_name = "ReceivedPendingExpiredClosed" -var _ActiveQuoteRequestStatus_index = [...]uint8{0, 8, 15, 22, 31} +var _ActiveQuoteRequestStatus_index = [...]uint8{0, 8, 15, 22, 28} func (i ActiveQuoteRequestStatus) String() string { i -= 1 From cf9763fb729041ae1652c2eee7ab5348314ed310 Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Fri, 18 Oct 2024 12:24:48 -0500 Subject: [PATCH 10/10] trace in Do() --- services/omnirpc/http/capture_client.go | 53 +++++-------------------- services/omnirpc/http/fasthttp.go | 48 +++------------------- services/omnirpc/http/resty.go | 50 +++-------------------- 3 files changed, 20 insertions(+), 131 deletions(-) diff --git a/services/omnirpc/http/capture_client.go b/services/omnirpc/http/capture_client.go index 748d5d20bc..606a067791 100644 --- a/services/omnirpc/http/capture_client.go +++ b/services/omnirpc/http/capture_client.go @@ -2,6 +2,7 @@ package http import ( "context" + "fmt" "github.com/ethereum/go-ethereum/common" "github.com/synapsecns/sanguine/core/bytemap" @@ -70,72 +71,30 @@ var _ Client = &CaptureClient{} // SetBody stores the body for testing. func (c *CapturedRequest) SetBody(body []byte) Request { - _, span := c.Handler.Tracer().Start( - c.Context, - "SetBody", - trace.WithAttributes(attribute.String("SetBody", common.Bytes2Hex(body))), - ) - defer func() { - metrics.EndSpan(span) - }() c.Body = body return c } // SetContext stores the context for testing. func (c *CapturedRequest) SetContext(ctx context.Context) Request { - _, span := c.Handler.Tracer().Start( - ctx, - "SetContext", - ) - span.AddEvent("SetContext") - defer func() { - metrics.EndSpan(span) - }() c.Context = ctx return c } // SetHeader sets the header for testing. func (c *CapturedRequest) SetHeader(key, value string) Request { - _, span := c.Handler.Tracer().Start( - c.Context, - "SetHeader", - trace.WithAttributes(attribute.String("SetHeader", key)), - trace.WithAttributes(attribute.String("value", value)), - ) - defer func() { - metrics.EndSpan(span) - }() c.StringHeaders[key] = value return c } // SetHeaderBytes sets header bytes for testing. func (c *CapturedRequest) SetHeaderBytes(key, value []byte) Request { - _, span := c.Handler.Tracer().Start( - c.Context, - "SetHeaderBytes", - trace.WithAttributes(attribute.String("key", common.Bytes2Hex(key))), - trace.WithAttributes(attribute.String("value", common.Bytes2Hex(value))), - ) - defer func() { - metrics.EndSpan(span) - }() c.ByteHeaders.Put(key, value) return c } // SetRequestURI stores the request uri. func (c *CapturedRequest) SetRequestURI(uri string) Request { - _, span := c.Handler.Tracer().Start( - c.Context, - "SetRequestURI", - trace.WithAttributes(attribute.String("uri", uri)), - ) - defer func() { - metrics.EndSpan(span) - }() c.RequestURI = uri return c } @@ -145,6 +104,11 @@ func (c *CapturedRequest) Do() (Response, error) { _, span := c.Handler.Tracer().Start( c.Context, "Do", + trace.WithAttributes( + attribute.String("uri", c.RequestURI), + attribute.String("headers", fmt.Sprintf("%v", c.StringHeaders)), + attribute.String("body", common.Bytes2Hex(c.Body)), + ), ) defer func() { metrics.EndSpan(span) @@ -155,7 +119,10 @@ func (c *CapturedRequest) Do() (Response, error) { return nil, err } - span.SetAttributes(attribute.String("response", common.Bytes2Hex(resp.Body()))) + span.SetAttributes( + attribute.String("response", common.Bytes2Hex(resp.Body())), + attribute.Int("status", resp.StatusCode()), + ) return resp, err } diff --git a/services/omnirpc/http/fasthttp.go b/services/omnirpc/http/fasthttp.go index 42ecc6339a..9560270e79 100644 --- a/services/omnirpc/http/fasthttp.go +++ b/services/omnirpc/http/fasthttp.go @@ -7,7 +7,6 @@ import ( "time" "github.com/ImVexed/fasturl" - "github.com/ethereum/go-ethereum/common" "github.com/puzpuzpuz/xsync" http2 "github.com/synapsecns/fasthttp-http2" "github.com/synapsecns/sanguine/core/metrics" @@ -179,69 +178,27 @@ func (f *fastHTTPRequest) Reset() { } func (f *fastHTTPRequest) SetBody(body []byte) Request { - _, span := f.handler.Tracer().Start( - f.context, - "SetBody", - trace.WithAttributes(attribute.String("body", common.Bytes2Hex(body))), - ) - defer func() { - metrics.EndSpan(span) - }() f.Request.SetBodyRaw(body) return f } // SetContext does nothing on fasthttp request. func (f *fastHTTPRequest) SetContext(ctx context.Context) Request { - _, span := f.handler.Tracer().Start( - ctx, - "SetContext", - ) - span.AddEvent("SetContext") - defer func() { - metrics.EndSpan(span) - }() f.context = ctx return f } func (f *fastHTTPRequest) SetHeader(key, value string) Request { - _, span := f.handler.Tracer().Start( - f.context, - "SetHeader", - trace.WithAttributes(attribute.String("SetHeader", key)), - trace.WithAttributes(attribute.String("value", value)), - ) - defer func() { - metrics.EndSpan(span) - }() f.Request.Header.Set(key, value) return f } func (f *fastHTTPRequest) SetHeaderBytes(key, value []byte) Request { - _, span := f.handler.Tracer().Start( - f.context, - "SetHeaderBytes", - trace.WithAttributes(attribute.String("key", common.Bytes2Hex(key))), - trace.WithAttributes(attribute.String("value", common.Bytes2Hex(value))), - ) - defer func() { - metrics.EndSpan(span) - }() f.Request.Header.SetBytesKV(key, value) return f } func (f *fastHTTPRequest) SetRequestURI(uri string) Request { - _, span := f.handler.Tracer().Start( - f.context, - "SetRequestURI", - trace.WithAttributes(attribute.String("uri", uri)), - ) - defer func() { - metrics.EndSpan(span) - }() f.Request.SetRequestURI(uri) return f } @@ -250,6 +207,11 @@ func (f *fastHTTPRequest) Do() (Response, error) { _, span := f.handler.Tracer().Start( f.context, "Do", + trace.WithAttributes( + attribute.String("uri", f.Request.URI().String()), + attribute.String("headers", fmt.Sprintf("%v", f.Request.Header.String())), + attribute.String("body", string(f.Request.Body())), + ), ) defer func() { metrics.EndSpan(span) diff --git a/services/omnirpc/http/resty.go b/services/omnirpc/http/resty.go index 55287d81ce..5d69d9edcf 100644 --- a/services/omnirpc/http/resty.go +++ b/services/omnirpc/http/resty.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - "github.com/ethereum/go-ethereum/common" "github.com/go-resty/resty/v2" "github.com/synapsecns/sanguine/core/metrics" "go.opentelemetry.io/otel/attribute" @@ -39,70 +38,26 @@ func (r RestyClient) NewRequest() Request { // SetHeaderBytes is a wrapper around SetHeadre for bytes. func (r *restyRequest) SetHeaderBytes(key, value []byte) Request { - _, span := r.handler.Tracer().Start( - r.Request.Context(), - "SetHeaderBytes", - trace.WithAttributes( - attribute.String("key", common.Bytes2Hex(key)), - attribute.String("value", common.Bytes2Hex(value)), - )) - defer func() { - metrics.EndSpan(span) - }() r.Request.SetHeader(string(key), string(value)) return r } func (r *restyRequest) SetBody(body []byte) Request { - _, span := r.handler.Tracer().Start( - r.Request.Context(), - "SetBody", - trace.WithAttributes(attribute.String("body", common.Bytes2Hex(body))), - ) - defer func() { - metrics.EndSpan(span) - }() r.Request.SetBody(body) return r } func (r *restyRequest) SetContext(ctx context.Context) Request { - _, span := r.handler.Tracer().Start( - ctx, - "SetContext", - ) - span.AddEvent("SetContext") - defer func() { - metrics.EndSpan(span) - }() r.Request.SetContext(ctx) return r } func (r *restyRequest) SetHeader(key, value string) Request { - _, span := r.handler.Tracer().Start( - r.Request.Context(), - "SetHeader", - trace.WithAttributes( - attribute.String("SetHeader", key), - attribute.String("value", value), - )) - defer func() { - metrics.EndSpan(span) - }() r.Request.SetHeader(key, value) return r } func (r *restyRequest) SetRequestURI(uri string) Request { - _, span := r.handler.Tracer().Start( - r.Request.Context(), - "SetRequestURI", - trace.WithAttributes(attribute.String("uri", uri)), - ) - defer func() { - metrics.EndSpan(span) - }() r.endpoint = uri return r } @@ -111,6 +66,11 @@ func (r *restyRequest) Do() (_ Response, err error) { _, span := r.handler.Tracer().Start( r.Request.Context(), "Do", + trace.WithAttributes( + attribute.String("uri", r.endpoint), + attribute.String("headers", fmt.Sprintf("%v", r.Request.Header)), + attribute.String("body", r.Request.Body.(string)), + ), ) defer func() { metrics.EndSpanWithErr(span, err)