From 8aa583e7105cfde7adafe7786f7f3d251c78c5c1 Mon Sep 17 00:00:00 2001 From: Rob Zienert Date: Wed, 31 May 2017 14:16:02 -0700 Subject: [PATCH] feat(ratelimit): Adding principal metrics --- .../gate/ratelimit/RateLimitingInterceptor.java | 14 ++++++++++++++ .../ratelimit/RateLimitingInterceptorSpec.groovy | 6 ++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/ratelimit/RateLimitingInterceptor.java b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/ratelimit/RateLimitingInterceptor.java index 1b396f14ca..b3c9d67e54 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/ratelimit/RateLimitingInterceptor.java +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/ratelimit/RateLimitingInterceptor.java @@ -15,8 +15,10 @@ */ package com.netflix.spinnaker.gate.ratelimit; +import com.netflix.spectator.api.BasicTag; import com.netflix.spectator.api.Counter; import com.netflix.spectator.api.Registry; +import com.netflix.spectator.api.Tag; import com.netflix.spinnaker.security.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,6 +31,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.time.ZonedDateTime; +import java.util.Collections; public class RateLimitingInterceptor extends HandlerInterceptorAdapter { @@ -38,6 +41,7 @@ public class RateLimitingInterceptor extends HandlerInterceptorAdapter { private RateLimiter rateLimiter; private RateLimitPrincipalProvider rateLimitPrincipalProvider; + private Registry registry; private Counter throttlingCounter; private Counter learningThrottlingCounter; @@ -45,6 +49,7 @@ public class RateLimitingInterceptor extends HandlerInterceptorAdapter { public RateLimitingInterceptor(RateLimiter rateLimiter, Registry registry, RateLimitPrincipalProvider rateLimitPrincipalProvider) { this.rateLimiter = rateLimiter; this.rateLimitPrincipalProvider = rateLimitPrincipalProvider; + this.registry = registry; throttlingCounter = registry.counter("rateLimit.throttling"); learningThrottlingCounter = registry.counter("rateLimit.throttlingLearning"); } @@ -69,6 +74,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons Rate rate = rateLimiter.incrementAndGetRate(principal); rate.assignHttpHeaders(response, principal.isLearning()); + recordPrincipalMetrics(principal, rate); if (principal.isLearning()) { if (rate.isThrottled()) { @@ -123,4 +129,12 @@ private String sourceIpAddress(HttpServletRequest request) { } return ip; } + + private void recordPrincipalMetrics(RateLimitPrincipal principal, Rate rate) { + Iterable tags = Collections.singletonList(new BasicTag("principal", principal.getName())); + if (rate.isThrottled()) { + registry.counter("rateLimit.principal.throttled", tags).increment(); + } + registry.gauge("rateLimit.principal.remaining", tags, rate.remaining); + } } diff --git a/gate-web/src/test/groovy/com/netflix/spinnaker/gate/ratelimit/RateLimitingInterceptorSpec.groovy b/gate-web/src/test/groovy/com/netflix/spinnaker/gate/ratelimit/RateLimitingInterceptorSpec.groovy index 5304f8d8a0..19ef0499c5 100644 --- a/gate-web/src/test/groovy/com/netflix/spinnaker/gate/ratelimit/RateLimitingInterceptorSpec.groovy +++ b/gate-web/src/test/groovy/com/netflix/spinnaker/gate/ratelimit/RateLimitingInterceptorSpec.groovy @@ -15,7 +15,7 @@ */ package com.netflix.spinnaker.gate.ratelimit -import com.netflix.spectator.api.Counter +import com.netflix.spectator.api.NoopRegistry import com.netflix.spectator.api.Registry import com.netflix.spinnaker.gate.config.RateLimiterConfiguration import com.netflix.spinnaker.security.User @@ -30,9 +30,7 @@ import javax.servlet.http.HttpServletResponse class RateLimitingInterceptorSpec extends Specification { - Registry registry = Mock() { - counter(_) >> Mock(Counter) - } + Registry registry = new NoopRegistry() RateLimiter rateLimiter = Mock()