Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from MysteriumNetwork/feature/MYST-269-statis…
…tics-endpoint Add statistics endpoint with bytes sent/received
- Loading branch information
Showing
15 changed files
with
223 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
openvpn/middlewares/client/bytescount/composite_stats_handler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package bytescount | ||
|
||
// NewCompositeStatsHandler composes multiple stats handlers into single one, which executes all handlers sequentially | ||
func NewCompositeStatsHandler(statsHandlers ...SessionStatsHandler) SessionStatsHandler { | ||
return func(stats SessionStats) error { | ||
for _, handler := range statsHandlers { | ||
err := handler(stats) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
openvpn/middlewares/client/bytescount/composite_stats_handler_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package bytescount | ||
|
||
import ( | ||
"errors" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
var stats = SessionStats{BytesSent: 1, BytesReceived: 1} | ||
|
||
func TestCompositeHandlerWithNoHandlers(t *testing.T) { | ||
stats := SessionStats{BytesSent: 1, BytesReceived: 1} | ||
|
||
compositeHandler := NewCompositeStatsHandler() | ||
assert.NoError(t, compositeHandler(stats)) | ||
} | ||
|
||
func TestCompositeHandlerWithSuccessfulHandler(t *testing.T) { | ||
statsRecorder := fakeStatsRecorder{} | ||
compositeHandler := NewCompositeStatsHandler(statsRecorder.record) | ||
assert.NoError(t, compositeHandler(stats)) | ||
assert.Equal(t, stats, statsRecorder.LastSessionStats) | ||
} | ||
|
||
func TestCompositeHandlerWithFailingHandler(t *testing.T) { | ||
failingHandler := func(stats SessionStats) error { return errors.New("fake error") } | ||
compositeHandler := NewCompositeStatsHandler(failingHandler) | ||
assert.Error(t, compositeHandler(stats), "fake error") | ||
} | ||
|
||
func TestCompositeHandlerWithMultipleHandlers(t *testing.T) { | ||
recorder1 := fakeStatsRecorder{} | ||
recorder2 := fakeStatsRecorder{} | ||
|
||
compositeHandler := NewCompositeStatsHandler(recorder1.record, recorder2.record) | ||
assert.NoError(t, compositeHandler(stats)) | ||
|
||
assert.Equal(t, stats, recorder1.LastSessionStats) | ||
assert.Equal(t, stats, recorder2.LastSessionStats) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package bytescount | ||
|
||
// SessionStats represents statistics, generated by bytescount middleware | ||
type SessionStats struct { | ||
BytesSent, BytesReceived int | ||
} |
10 changes: 10 additions & 0 deletions
10
openvpn/middlewares/client/bytescount/fake_stats_handler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package bytescount | ||
|
||
type fakeStatsRecorder struct { | ||
LastSessionStats SessionStats | ||
} | ||
|
||
func (sender *fakeStatsRecorder) record(sessionStats SessionStats) error { | ||
sender.LastSessionStats = sessionStats | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package bytescount | ||
|
||
// SessionStatsKeeper keeps session stats | ||
type SessionStatsKeeper struct { | ||
sessionStats SessionStats | ||
} | ||
|
||
// Save saves session stats to keeper | ||
func (keeper *SessionStatsKeeper) Save(stats SessionStats) { | ||
keeper.sessionStats = stats | ||
} | ||
|
||
// Retrieve retrieves session stats from keeper | ||
func (keeper *SessionStatsKeeper) Retrieve() SessionStats { | ||
return keeper.sessionStats | ||
} |
14 changes: 14 additions & 0 deletions
14
openvpn/middlewares/client/bytescount/stats_keeper_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package bytescount | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestSessionStatsStoreWorks(t *testing.T) { | ||
statsKeeper := &SessionStatsKeeper{} | ||
stats := SessionStats{BytesSent: 1, BytesReceived: 2} | ||
|
||
statsKeeper.Save(stats) | ||
assert.Equal(t, stats, statsKeeper.Retrieve()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package bytescount | ||
|
||
// NewSessionStatsSaver returns stats handler, which saves stats stats keeper | ||
func NewSessionStatsSaver(statsKeeper *SessionStatsKeeper) SessionStatsHandler { | ||
return func(sessionStats SessionStats) error { | ||
statsKeeper.Save(sessionStats) | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package bytescount | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestNewSessionStatsSaver(t *testing.T) { | ||
statsKeeper := &SessionStatsKeeper{} | ||
|
||
saver := NewSessionStatsSaver(statsKeeper) | ||
stats := SessionStats{BytesSent: 1, BytesReceived: 2} | ||
saver(stats) | ||
assert.Equal(t, stats, statsKeeper.Retrieve()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.