Skip to content

Commit

Permalink
feat: add stat for domain not found (#624)
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Jaques <[email protected]>
  • Loading branch information
JDeuce authored Jun 20, 2024
1 parent 0ddd444 commit db7fc78
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/config/config_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ func (this *rateLimitConfigImpl) GetLimit(
value := this.domains[domain]
if value == nil {
logger.Debugf("unknown domain '%s'", domain)
domainStats := this.statsManager.NewDomainStats(domain)
domainStats.NotFound.Inc()
return rateLimit
}

Expand Down
9 changes: 9 additions & 0 deletions src/stats/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ type Manager interface {
// NewStats provides a RateLimitStats structure associated with a given descriptorKey.
// Multiple calls with the same descriptorKey argument are guaranteed to be equivalent.
NewStats(descriptorKey string) RateLimitStats
// Gets stats for a domain (when no descriptors are found)
// Multiple calls with the same domain argument are guaranteed to be equivalent.
NewDomainStats(domain string) DomainStats
// Initializes a ShouldRateLimitStats structure.
// Multiple calls to this method are idempotent.
NewShouldRateLimitStats() ShouldRateLimitStats
Expand Down Expand Up @@ -52,3 +55,9 @@ type RateLimitStats struct {
WithinLimit gostats.Counter
ShadowMode gostats.Counter
}

// Stats for a domain entry
type DomainStats struct {
Key string
NotFound gostats.Counter
}
7 changes: 7 additions & 0 deletions src/stats/manager_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func (this *ManagerImpl) NewStats(key string) RateLimitStats {
return ret
}

func (this *ManagerImpl) NewDomainStats(domain string) DomainStats {
ret := DomainStats{}
domain = utils.SanitizeStatName(domain)
ret.NotFound = this.rlStatsScope.NewCounter(domain + ".domain_not_found")
return ret
}

func (this *ManagerImpl) NewShouldRateLimitStats() ShouldRateLimitStats {
ret := ShouldRateLimitStats{}
ret.RedisError = this.shouldRateLimitScope.NewCounter("redis_error")
Expand Down
3 changes: 3 additions & 0 deletions test/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ func TestBasicConfig(t *testing.T) {
rlConfig := config.NewRateLimitConfigImpl(loadFile("basic_config.yaml"), mockstats.NewMockStatManager(stats), false)
rlConfig.Dump()
assert.Equal(rlConfig.IsEmptyDomains(), false)
assert.EqualValues(0, stats.NewCounter("foo_domain.domain_not_found").Value())
assert.Nil(rlConfig.GetLimit(context.TODO(), "foo_domain", &pb_struct.RateLimitDescriptor{}))
assert.EqualValues(1, stats.NewCounter("foo_domain.domain_not_found").Value())
assert.Nil(rlConfig.GetLimit(context.TODO(), "test-domain", &pb_struct.RateLimitDescriptor{}))
assert.EqualValues(0, stats.NewCounter("test-domain.domain_not_found").Value())

rl := rlConfig.GetLimit(
context.TODO(), "test-domain",
Expand Down
9 changes: 9 additions & 0 deletions test/mocks/stats/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func (m *MockStatManager) NewStats(key string) stats.RateLimitStats {
return ret
}

func (m *MockStatManager) NewDomainStats(key string) stats.DomainStats {
ret := stats.DomainStats{}
logger.Debugf("outputing test domain stats %s", key)
ret.Key = key
ret.NotFound = m.store.NewCounter(key + ".domain_not_found")

return ret
}

func NewMockStatManager(store gostats.Store) stats.Manager {
return &MockStatManager{store: store}
}

0 comments on commit db7fc78

Please sign in to comment.