Skip to content

Commit

Permalink
trace [SLT-330] (#3264)
Browse files Browse the repository at this point in the history
* trace [slt-330]

[goreleaser]

* fix marshal

* marshal to bin [goreleaser]

* syntax

* another test

---------

Co-authored-by: Trajan0x <trajan0x@users.noreply.github.com>
  • Loading branch information
trajan0x and trajan0x authored Oct 10, 2024
1 parent dd5f5ac commit 2b05e51
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ethergo/util/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
gasPriceAttr = "tx.GasPrice"
gasFeeCapAttr = "tx.GasFeeCap"
gasTipCapAttr = "tx.GasTipCap"
txRawAttr = "tx.Raw"
)

// TxToAttributes converts a transaction to a slice of attribute.KeyValue.
Expand All @@ -33,6 +34,12 @@ func TxToAttributes(transaction *types.Transaction) []attribute.KeyValue {
} else {
from = call.From.Hex()
}

bin, err := transaction.MarshalBinary()
if err != nil {
bin = []byte(fmt.Sprintf("could not be marshaled: %v", err))
}

var attributes = []attribute.KeyValue{
attribute.String(hashAttr, transaction.Hash().Hex()),
attribute.String(fromAttr, from),
Expand All @@ -46,6 +53,7 @@ func TxToAttributes(transaction *types.Transaction) []attribute.KeyValue {
// nolint: gosec
attribute.Int64(gasLimitAttr, int64(transaction.Gas())),
attribute.String(chainIDAttr, BigPtrToString(transaction.ChainId())),
attribute.String(txRawAttr, common.Bytes2Hex(bin)),
}

if transaction.Type() == types.LegacyTxType && transaction.GasPrice() != nil {
Expand Down
4 changes: 4 additions & 0 deletions ethergo/util/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func (u *UtilSuite) TestTxToAttributesDynamicTX() {

u.Require().Equal(mapAttr[util.GasFeeCapAttr].AsString(), mockTX.GasFeeCap().String())
u.Require().Equal(mapAttr[util.GasTipCapAttr].AsString(), mockTX.GasTipCap().String())
marshaled, err := mockTX.MarshalBinary()
u.Require().NoError(err)

u.Require().Equal(mapAttr[util.TxRawAttr].AsString(), common.Bytes2Hex(marshaled))
_, hasGasPrice := mapAttr[util.GasPriceAttr]
u.Require().False(hasGasPrice)
u.Require().NotNil(mapAttr[util.FromAttr])
Expand Down
2 changes: 2 additions & 0 deletions ethergo/util/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ const (
GasFeeCapAttr = gasFeeCapAttr
// GasTipCapAttr exports gasTipCapAttr for testing.
GasTipCapAttr = gasTipCapAttr
// TxRawAttr exports txRawAttr for testing.
TxRawAttr = txRawAttr
)
1 change: 1 addition & 0 deletions services/omnirpc/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Client interface {
}

// Request is a request builder.
// TODO: this needs to support tracing.
type Request interface {
// SetBody sets the request body
SetBody(body []byte) Request
Expand Down
9 changes: 9 additions & 0 deletions services/omnirpc/modules/receiptsbackup/receiptsbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,18 @@ func (r *receiptsProxyImpl) ProxyRequest(c *gin.Context) (err error) {
}

func (r *receiptsProxyImpl) processRequest(ctx context.Context, rpcRequest rpc.Request, requestID []byte) (resp omniHTTP.Response, err error) {
ctx, span := r.handler.Tracer().Start(ctx, "proxyrequest")
defer func() {
metrics.EndSpanWithErr(span, err)
}()

mixins.TxSubmitMixin(ctx, r.handler, rpcRequest)

req := r.client.NewRequest()
body, err := json.Marshal(rpcRequest)

span.AddEvent("request marshaled", trace.WithAttributes(attribute.String("body", string(body))))

//nolint: exhaustive
switch client.RPCMethod(rpcRequest.Method) {
case client.TransactionReceiptByHashMethod:
Expand Down Expand Up @@ -192,6 +199,8 @@ func (r *receiptsProxyImpl) processRequest(ctx context.Context, rpcRequest rpc.R
return nil, fmt.Errorf("could not get response from RPC %s: %w", r.proxyURL, err)
}

span.AddEvent("response returned", trace.WithAttributes(attribute.String("body", string(resp.Body()))))

return resp, nil
}
}

0 comments on commit 2b05e51

Please sign in to comment.