Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ratelimit): Adding principal metrics #402

Merged
merged 1 commit into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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