| 41 | namespace internal { |
| 42 | |
| 43 | MetricsProcess* MetricsProcess::create( |
| 44 | const Option<string>& authenticationRealm) |
| 45 | { |
| 46 | Option<string> limit = |
| 47 | os::getenv("LIBPROCESS_METRICS_SNAPSHOT_ENDPOINT_RATE_LIMIT"); |
| 48 | |
| 49 | Option<Owned<RateLimiter>> limiter; |
| 50 | |
| 51 | // By default, we apply a rate limit of 2 requests |
| 52 | // per second to the metrics snapshot endpoint in |
| 53 | // order to maintain backwards compatibility (before |
| 54 | // this was made configurable, we hard-coded a limit |
| 55 | // of 2 requests per second). |
| 56 | if (limit.isNone()) { |
| 57 | limiter = Owned<RateLimiter>(new RateLimiter(2, Seconds(1))); |
| 58 | } else if (limit->empty()) { |
| 59 | limiter = None(); |
| 60 | } else { |
| 61 | // TODO(vinod): Move this parsing logic to flags |
| 62 | // once we have a 'Rate' abstraction in stout. |
| 63 | Option<Error> reason; |
| 64 | vector<string> tokens = strings::tokenize(limit.get(), "/"); |
| 65 | |
| 66 | if (tokens.size() == 2) { |
| 67 | Try<int> requests = numify<int>(tokens[0]); |
| 68 | Try<Duration> interval = Duration::parse(tokens[1]); |
| 69 | |
| 70 | if (requests.isError()) { |
| 71 | reason = Error( |
| 72 | "Failed to parse the number of requests: " + requests.error()); |
| 73 | } else if (interval.isError()) { |
| 74 | reason = Error( |
| 75 | "Failed to parse the interval: " + interval.error()); |
| 76 | } else { |
| 77 | limiter = Owned<RateLimiter>( |
| 78 | new RateLimiter(requests.get(), interval.get())); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (limiter.isNone()) { |
| 83 | EXIT(EXIT_FAILURE) |
| 84 | << "Failed to parse LIBPROCESS_METRICS_SNAPSHOT_ENDPOINT_RATE_LIMIT " |
| 85 | << "'" << limit.get() << "'" |
| 86 | << " (format is <number of requests>/<interval duration>)" |
| 87 | << (reason.isSome() ? ": " + reason->message : ""); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return new MetricsProcess(limiter, authenticationRealm); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void MetricsProcess::initialize() |