-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17c078b
commit 65774e2
Showing
8 changed files
with
907 additions
and
401 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,106 @@ | ||
package stats | ||
|
||
import ( | ||
"github.com/rudderlabs/rudder-server/services/stats" | ||
) | ||
|
||
type SourceStat struct { | ||
Source string | ||
|
||
WriteKey string | ||
ReqType string | ||
SourceID string | ||
WorkspaceID string | ||
|
||
reason string | ||
|
||
requests struct { | ||
total int | ||
|
||
succeeded int | ||
failed int | ||
dropped int | ||
suppressed int | ||
} | ||
events struct { | ||
total int | ||
|
||
succeeded int | ||
failed int | ||
} | ||
} | ||
|
||
// RequestSucceeded increments the requests total & succeeded counters by one | ||
func (ss *SourceStat) RequestSucceeded() { | ||
ss.requests.total++ | ||
ss.requests.succeeded++ | ||
} | ||
|
||
// RequestDropped increments the requests total & dropped counters by one | ||
func (ss *SourceStat) RequestDropped() { | ||
ss.requests.total++ | ||
ss.requests.dropped++ | ||
} | ||
|
||
// RequestSuppressed increments the requests total & suppressed counters by one | ||
func (ss *SourceStat) RequestSuppressed() { | ||
ss.requests.total++ | ||
ss.requests.suppressed++ | ||
} | ||
|
||
// RequestFailed increments the requests total & failed counters by one | ||
func (ss *SourceStat) RequestFailed(reason string) { | ||
ss.requests.total++ | ||
ss.requests.failed++ | ||
ss.reason = reason | ||
} | ||
|
||
// RequestEventsSucceeded increments the requests total & succeeded counters by one, and the events total & succeeded counters by num | ||
func (ss *SourceStat) RequestEventsSucceeded(num int) { | ||
ss.events.succeeded += num | ||
ss.events.total += num | ||
ss.requests.total++ | ||
ss.requests.succeeded++ | ||
} | ||
|
||
// RequestEventsSucceeded increments the requests total & failed counters by one, and the events total & failed counters by num | ||
func (ss *SourceStat) RequestEventsFailed(num int, reason string) { | ||
ss.requests.total++ | ||
ss.requests.failed++ | ||
ss.events.failed += num | ||
ss.events.total += num | ||
ss.reason = reason | ||
} | ||
|
||
// Reports captured stats | ||
func (ss *SourceStat) Report(s stats.Stats) { | ||
tags := map[string]string{ | ||
"source": ss.Source, | ||
"writeKey": ss.WriteKey, | ||
"reqType": ss.ReqType, | ||
"workspaceId": ss.WorkspaceID, | ||
"sourceID": ss.SourceID, | ||
} | ||
|
||
failedTags := map[string]string{ | ||
"source": ss.Source, | ||
"writeKey": ss.WriteKey, | ||
"reqType": ss.ReqType, | ||
"workspaceId": ss.WorkspaceID, | ||
"sourceID": ss.SourceID, | ||
} | ||
if ss.reason != "" { | ||
failedTags["reason"] = ss.reason | ||
} | ||
|
||
s.NewTaggedStat("gateway.write_key_requests", stats.CountType, tags).Count(ss.requests.total) | ||
s.NewTaggedStat("gateway.write_key_successful_requests", stats.CountType, tags).Count(ss.requests.succeeded) | ||
s.NewTaggedStat("gateway.write_key_failed_requests", stats.CountType, failedTags).Count(ss.requests.failed) | ||
s.NewTaggedStat("gateway.write_key_dropped_requests", stats.CountType, tags).Count(ss.requests.dropped) | ||
s.NewTaggedStat("gateway.write_key_suppressed_requests", stats.CountType, tags).Count(ss.requests.suppressed) | ||
if ss.events.total > 0 { | ||
s.NewTaggedStat("gateway.write_key_events", stats.CountType, tags).Count(ss.events.total) | ||
s.NewTaggedStat("gateway.write_key_successful_events", stats.CountType, tags).Count(ss.events.succeeded) | ||
s.NewTaggedStat("gateway.write_key_failed_events", stats.CountType, failedTags).Count(ss.events.failed) | ||
} | ||
} |
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,168 @@ | ||
package stats | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
"github.com/rudderlabs/rudder-server/services/stats/memstats" | ||
trand "github.com/rudderlabs/rudder-server/testhelper/rand" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func getSourceStat(statMap map[string]*SourceStat, sourceTag string) { | ||
statMap[sourceTag] = &SourceStat{ | ||
Source: trand.String(10), | ||
SourceID: trand.String(10), | ||
WorkspaceID: trand.String(10), | ||
WriteKey: trand.String(10), | ||
ReqType: trand.String(10), | ||
} | ||
} | ||
|
||
func TestReport(t *testing.T) { | ||
// populate some SourceStats | ||
statMap := make(map[string]*SourceStat) | ||
for i := 0; i < 10; i++ { | ||
getSourceStat(statMap, fmt.Sprint(i)) | ||
} | ||
|
||
// populate some request, event counts | ||
// keep track using some counters | ||
counterMap := make(map[string]*counter) | ||
for i := 0; i < 10; i++ { | ||
counterMap[fmt.Sprint(i)] = &counter{} | ||
} | ||
rand.Seed(time.Now().UnixNano()) | ||
for i := 0; i < 10; i++ { | ||
sourceTag := fmt.Sprint(i) | ||
randInt := rand.Int() % 10 // skipcq: GSC-G404 | ||
for j := 0; j < randInt; j++ { | ||
statMap[sourceTag].RequestSucceeded() | ||
} | ||
counterMap[sourceTag].succeeded += randInt | ||
counterMap[sourceTag].total += randInt | ||
randInt = rand.Int() % 10 // skipcq: GSC-G404 | ||
for j := 0; j < randInt; j++ { | ||
statMap[sourceTag].RequestDropped() | ||
} | ||
counterMap[sourceTag].dropped += randInt | ||
counterMap[sourceTag].total += randInt | ||
randInt = rand.Int() % 10 // skipcq: GSC-G404 | ||
for j := 0; j < randInt; j++ { | ||
statMap[sourceTag].RequestSuppressed() | ||
} | ||
counterMap[sourceTag].suppressed += randInt | ||
counterMap[sourceTag].total += randInt | ||
randInt = rand.Int() % 10 // skipcq: GSC-G404 | ||
for j := 0; j < randInt; j++ { | ||
statMap[sourceTag].RequestFailed("reason") | ||
} | ||
counterMap[sourceTag].failed += randInt | ||
counterMap[sourceTag].total += randInt | ||
randInt = rand.Int() % 10 // skipcq: GSC-G404 | ||
for j := 0; j < randInt; j++ { | ||
statMap[sourceTag].RequestEventsSucceeded(10) | ||
} | ||
counterMap[sourceTag].eventsSucceeded += randInt * 10 | ||
counterMap[sourceTag].eventsTotal += randInt * 10 | ||
counterMap[sourceTag].total += randInt | ||
counterMap[sourceTag].succeeded += randInt | ||
randInt = rand.Int() % 10 // skipcq: GSC-G404 | ||
for j := 0; j < randInt; j++ { | ||
statMap[sourceTag].RequestEventsFailed(10, "reason") | ||
} | ||
counterMap[sourceTag].eventsFailed += randInt * 10 | ||
counterMap[sourceTag].eventsTotal += randInt * 10 | ||
counterMap[sourceTag].total += randInt | ||
counterMap[sourceTag].failed += randInt | ||
} | ||
|
||
// report | ||
statsStore := memstats.New() | ||
for _, v := range statMap { | ||
v.Report(statsStore) | ||
} | ||
|
||
// check | ||
for i := 0; i < 10; i++ { | ||
sourceTag := fmt.Sprint(i) | ||
tags := map[string]string{ | ||
"source": statMap[sourceTag].Source, | ||
"sourceID": statMap[sourceTag].SourceID, | ||
"workspaceId": statMap[sourceTag].WorkspaceID, | ||
"writeKey": statMap[sourceTag].WriteKey, | ||
"reqType": statMap[sourceTag].ReqType, | ||
} | ||
failedTags := map[string]string{ | ||
"source": statMap[sourceTag].Source, | ||
"sourceID": statMap[sourceTag].SourceID, | ||
"workspaceId": statMap[sourceTag].WorkspaceID, | ||
"writeKey": statMap[sourceTag].WriteKey, | ||
"reqType": statMap[sourceTag].ReqType, | ||
"reason": "reason", | ||
} | ||
require.Equal(t, | ||
float64(counterMap[sourceTag].total), | ||
statsStore.Get( | ||
"gateway.write_key_requests", | ||
tags, | ||
).LastValue(), | ||
) | ||
require.Equal(t, | ||
float64(counterMap[sourceTag].succeeded), | ||
statsStore.Get( | ||
"gateway.write_key_successful_requests", | ||
tags, | ||
).LastValue(), | ||
) | ||
require.Equal(t, | ||
float64(counterMap[sourceTag].dropped), | ||
statsStore.Get( | ||
"gateway.write_key_dropped_requests", | ||
tags, | ||
).LastValue(), | ||
) | ||
require.Equal(t, | ||
float64(counterMap[sourceTag].suppressed), | ||
statsStore.Get( | ||
"gateway.write_key_suppressed_requests", | ||
tags, | ||
).LastValue(), | ||
) | ||
require.Equal(t, | ||
float64(counterMap[sourceTag].failed), | ||
statsStore.Get( | ||
"gateway.write_key_failed_requests", | ||
failedTags, | ||
).LastValue(), | ||
) | ||
require.Equal(t, | ||
float64(counterMap[sourceTag].eventsTotal), | ||
statsStore.Get( | ||
"gateway.write_key_events", | ||
tags, | ||
).LastValue(), | ||
) | ||
require.Equal(t, | ||
float64(counterMap[sourceTag].eventsSucceeded), | ||
statsStore.Get( | ||
"gateway.write_key_successful_events", | ||
tags, | ||
).LastValue(), | ||
) | ||
require.Equal(t, | ||
float64(counterMap[sourceTag].eventsFailed), | ||
statsStore.Get( | ||
"gateway.write_key_failed_events", | ||
failedTags, | ||
).LastValue(), | ||
) | ||
} | ||
} | ||
|
||
type counter struct { | ||
total, succeeded, failed, dropped, suppressed int | ||
eventsTotal, eventsSucceeded, eventsFailed int | ||
} |
31 changes: 17 additions & 14 deletions
31
mocks/gateway/webhook/mock_webhook.go → gateway/mocks/mockwebhook.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.