Skip to content

Commit

Permalink
feat(ratelimit): Adding principal metrics (spinnaker#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
robzienert authored and danielpeach committed May 24, 2018
1 parent ce710f1 commit 589d182
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -38,13 +41,15 @@ public class RateLimitingInterceptor extends HandlerInterceptorAdapter {

private RateLimiter rateLimiter;
private RateLimitPrincipalProvider rateLimitPrincipalProvider;
private Registry registry;

private Counter throttlingCounter;
private Counter learningThrottlingCounter;

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");
}
Expand All @@ -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()) {
Expand Down Expand Up @@ -123,4 +129,12 @@ private String sourceIpAddress(HttpServletRequest request) {
}
return ip;
}

private void recordPrincipalMetrics(RateLimitPrincipal principal, Rate rate) {
Iterable<Tag> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down

0 comments on commit 589d182

Please sign in to comment.