Skip to content

Commit

Permalink
Merge pull request #254 from multiversx/fix-panic-and-comm-version
Browse files Browse the repository at this point in the history
Extra checks and bug-fix
  • Loading branch information
iulianpascalau authored Nov 20, 2023
2 parents e18418b + 2b59967 commit 2114730
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
8 changes: 7 additions & 1 deletion client/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ func (m *metricsTransport) RoundTrip(req *http.Request) (*http.Response, error)

startTime := time.Now()
size := req.ContentLength

var statusCode int
resp, err := m.transport.RoundTrip(req)
if err == nil {
statusCode = resp.StatusCode
}

duration := time.Since(startTime)

valueFromCtx := req.Context().Value(request.ContextKey)
Expand All @@ -50,7 +56,7 @@ func (m *metricsTransport) RoundTrip(req *http.Request) (*http.Response, error)
topic := fmt.Sprintf("%s", valueFromCtx)

m.statusMetrics.AddIndexingData(metrics.ArgsAddIndexingData{
StatusCode: resp.StatusCode,
StatusCode: statusCode,
GotError: err != nil,
MessageLen: uint64(size),
Topic: topic,
Expand Down
25 changes: 25 additions & 0 deletions client/transport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package transport
import (
"bytes"
"context"
"errors"
"net/http"
"testing"

Expand Down Expand Up @@ -34,6 +35,30 @@ func TestMetricsTransport_NilRequest(t *testing.T) {
require.Equal(t, errNilRequest, err)
}

func TestMetricsTransport_RoundTripNilResponseShouldWork(t *testing.T) {
t.Parallel()

metricsHandler := metrics.NewStatusMetrics()
transportHandler, _ := NewMetricsTransport(metricsHandler)

testErr := errors.New("test")
transportHandler.transport = &mock.TransportMock{
Response: nil,
Err: testErr,
}

testTopic := "test"
contextWithValue := context.WithValue(context.Background(), request.ContextKey, testTopic)
req, _ := http.NewRequestWithContext(contextWithValue, http.MethodGet, "dummy", bytes.NewBuffer([]byte("test")))

_, _ = transportHandler.RoundTrip(req)

metricsMap := metricsHandler.GetMetrics()
require.Equal(t, uint64(1), metricsMap[testTopic].OperationsCount)
require.Equal(t, uint64(1), metricsMap[testTopic].TotalErrorsCount)
require.Equal(t, uint64(4), metricsMap[testTopic].TotalData)
}

func TestMetricsTransport_RoundTrip(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/elastic/go-elasticsearch/v7 v7.12.0
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.9.1
github.com/multiversx/mx-chain-communication-go v1.0.7
github.com/multiversx/mx-chain-communication-go v1.0.12
github.com/multiversx/mx-chain-core-go v1.2.16
github.com/multiversx/mx-chain-logger-go v1.0.13
github.com/multiversx/mx-chain-vm-common-go v1.5.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/multiversx/mx-chain-communication-go v1.0.7 h1:7qeDBcqmGYYhSqFcpwv0qKkR3ahOfIMbRwXYEnOt/do=
github.com/multiversx/mx-chain-communication-go v1.0.7/go.mod h1:+oaUowpq+SqrEmAsMPGwhz44g7L81loWb6AiNQU9Ms4=
github.com/multiversx/mx-chain-communication-go v1.0.12 h1:67WOaf87gpwouydD1AAOHw5LMGZh7NfITrp/KqFY3Tw=
github.com/multiversx/mx-chain-communication-go v1.0.12/go.mod h1:+oaUowpq+SqrEmAsMPGwhz44g7L81loWb6AiNQU9Ms4=
github.com/multiversx/mx-chain-core-go v1.2.16 h1:m0hUNmZQjGJxKDLQOHoM9jSaeDfVTbyd+mqiS8+NckE=
github.com/multiversx/mx-chain-core-go v1.2.16/go.mod h1:BILOGHUOIG5dNNX8cgkzCNfDaVtoYrJRYcPnpxRMH84=
github.com/multiversx/mx-chain-crypto-go v1.2.8 h1:wOgVlUaO5X4L8iEbFjcQcL8SZvv6WZ7LqH73BiRPhxU=
Expand Down

0 comments on commit 2114730

Please sign in to comment.