Skip to content

Commit

Permalink
extrac check metrics response
Browse files Browse the repository at this point in the history
  • Loading branch information
miiu96 committed Nov 6, 2023
1 parent e18418b commit 2f139c0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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

0 comments on commit 2f139c0

Please sign in to comment.