diff --git a/contrib/endpoints/src/api_manager/config.cc b/contrib/endpoints/src/api_manager/config.cc index b0a1b85d119..bcc0e3babee 100644 --- a/contrib/endpoints/src/api_manager/config.cc +++ b/contrib/endpoints/src/api_manager/config.cc @@ -113,6 +113,23 @@ MethodInfoImpl *Config::GetOrCreateMethodInfoImpl(const string &name, return i->second.get(); } +bool Config::LoadQuotaRule(ApiManagerEnvInterface *env) { + for (const auto &rule : service_.quota().metric_rules()) { + auto method = utils::FindOrNull(method_map_, rule.selector()); + if (method) { + for (auto &metric_cost : rule.metric_costs()) { + (*method)->add_metric_cost(metric_cost.first, metric_cost.second); + } + } else { + env->LogError("Metric rule with selector " + rule.selector() + + "is mismatched."); + return false; + } + } + + return true; +} + bool Config::LoadHttpMethods(ApiManagerEnvInterface *env, PathMatcherBuilder *pmb) { std::set all_urls, urls_with_options; @@ -443,6 +460,9 @@ std::unique_ptr Config::Create(ApiManagerEnvInterface *env, if (!config->LoadBackends(env)) { return nullptr; } + if (!config->LoadQuotaRule(env)) { + return nullptr; + } return config; } diff --git a/contrib/endpoints/src/api_manager/config.h b/contrib/endpoints/src/api_manager/config.h index 9a56d16d745..53c6cf39f2c 100644 --- a/contrib/endpoints/src/api_manager/config.h +++ b/contrib/endpoints/src/api_manager/config.h @@ -111,6 +111,8 @@ class Config { // Load SystemParameters info to MethodInfo. bool LoadSystemParameters(ApiManagerEnvInterface *env); + bool LoadQuotaRule(ApiManagerEnvInterface *env); + // Gets the MethodInfoImpl creating it if necessary MethodInfoImpl *GetOrCreateMethodInfoImpl(const std::string &name, const std::string &api_name, diff --git a/contrib/endpoints/src/api_manager/method_impl.h b/contrib/endpoints/src/api_manager/method_impl.h index e5739d639bc..d6eaba33724 100644 --- a/contrib/endpoints/src/api_manager/method_impl.h +++ b/contrib/endpoints/src/api_manager/method_impl.h @@ -18,6 +18,7 @@ #include #include #include +#include #include "contrib/endpoints/include/api_manager/method.h" #include "contrib/endpoints/src/api_manager/utils/stl_util.h" @@ -62,6 +63,10 @@ class MethodInfoImpl : public MethodInfo { const std::string &backend_address() const { return backend_address_; } + const std::vector> &metric_cost_vector() const { + return metric_cost_vector_; + } + const std::string &rpc_method_full_name() const { return rpc_method_full_name_; } @@ -90,6 +95,10 @@ class MethodInfoImpl : public MethodInfo { url_query_parameters_[name].push_back(url_query_parameter); } + void add_metric_cost(const std::string &metric, int64_t cost) { + metric_cost_vector_.push_back(std::make_pair(metric, cost)); + } + // After add all system parameters, lookup some of them to cache // their lookup results. void process_system_parameters(); @@ -139,13 +148,13 @@ class MethodInfoImpl : public MethodInfo { // such as API Key)? bool allow_unregistered_calls_; // Issuers to allowed audiences map. - std::map > issuer_audiences_map_; + std::map> issuer_audiences_map_; // system parameter map of parameter name to http_header name. - std::map > http_header_parameters_; + std::map> http_header_parameters_; // system parameter map of parameter name to url query parameter name. - std::map > url_query_parameters_; + std::map> url_query_parameters_; // all the names of system query parameters std::set system_query_parameter_names_; @@ -175,6 +184,9 @@ class MethodInfoImpl : public MethodInfo { // Whether the response is streaming or not. bool response_streaming_; + + // map of metric and its cost + std::vector> metric_cost_vector_; }; typedef std::unique_ptr MethodInfoImplPtr;