d_pd.push_back({"now", &g_now});
- d_lw->writeFunction("getMetric", [](const std::string& str) {
- return DynMetric{getDynMetric(str)};
+ d_lw->writeFunction("getMetric", [](const std::string& str, boost::optional<std::string> prometheusName) {
+ return DynMetric{getDynMetric(str, prometheusName ? *prometheusName : "")};
});
d_lw->registerFunction("inc", &DynMetric::inc);
enum class StatComponent { API, Carbon, RecControl, SNMP };
-std::map<std::string, std::string> getAllStatsMap(StatComponent component);
+std::map<std::string, std::string> getAllStatsMap(StatComponent component, bool prometheusName = false);
extern std::mutex g_carbon_config_lock;
std::vector<std::pair<DNSName, uint16_t> >* pleaseGetQueryRing();
std::vector<std::pair<DNSName, uint16_t> >* pleaseGetServfailQueryRing();
std::vector<ComboAddress>* pleaseGetLargeAnswerRemotes();
std::vector<ComboAddress>* pleaseGetTimeouts();
DNSName getRegisteredName(const DNSName& dom);
-std::atomic<unsigned long>* getDynMetric(const std::string& str);
+std::atomic<unsigned long>* getDynMetric(const std::string& str, const std::string& prometheusName);
optional<uint64_t> getStatByName(const std::string& name);
bool isStatBlacklisted(StatComponent component, const std::string& name);
void blacklistStat(StatComponent component, const string& name);
static map<string, const std::atomic<uint64_t>*> d_getatomics;
static map<string, function< uint64_t() > > d_get64bitmembers;
static std::mutex d_dynmetricslock;
-static map<string, std::atomic<unsigned long>* > d_dynmetrics;
+struct dynmetrics {
+ std::atomic<unsigned long> *d_ptr;
+ std::string d_prometheusName;
+};
+
+static map<string, dynmetrics> d_dynmetrics;
static std::map<StatComponent, std::set<std::string>> s_blacklistedStats;
d_get64bitmembers[name]=f;
}
-std::atomic<unsigned long>* getDynMetric(const std::string& str)
+std::atomic<unsigned long>* getDynMetric(const std::string& str, const std::string& prometheusName)
{
std::lock_guard<std::mutex> l(d_dynmetricslock);
auto f = d_dynmetrics.find(str);
if(f != d_dynmetrics.end())
- return f->second;
+ return f->second.d_ptr;
- auto ret = new std::atomic<unsigned long>();
+ std::string name(str);
+ if (!prometheusName.empty()) {
+ name = prometheusName;
+ } else {
+ std::replace_if(name.begin(), name.end(), [](char c){
+ return !isalnum(static_cast<unsigned char>(c));}, '_');
+ }
+
+ auto ret = dynmetrics{new std::atomic<unsigned long>(), name};
d_dynmetrics[str]= ret;
- return ret;
+ return ret.d_ptr;
}
static optional<uint64_t> get(const string& name)
return d_get64bitmembers.find(name)->second();
std::lock_guard<std::mutex> l(d_dynmetricslock);
- auto f =rplookup(d_dynmetrics, name);
- if(f)
- return (*f)->load();
+ auto f = rplookup(d_dynmetrics, name);
+ if (f)
+ return f->d_ptr->load();
return ret;
}
return get(name);
}
-map<string,string> getAllStatsMap(StatComponent component)
+map<string,string> getAllStatsMap(StatComponent component, bool prometheusNames)
{
map<string,string> ret;
const auto& blacklistMap = s_blacklistedStats.at(component);
std::lock_guard<std::mutex> l(d_dynmetricslock);
for(const auto& a : d_dynmetrics) {
if (blacklistMap.count(a.first) == 0) {
- ret.insert({a.first, std::to_string(*a.second)});
+ ret.insert({prometheusNames ? a.second.d_prometheusName : a.first,
+ std::to_string(*a.second.d_ptr)});
}
}
}
myMetric=getMetric("myspecialmetric")
-.. function:: getMetric(name) -> Metric
+.. function:: getMetric(name [, prometheusName]) -> Metric
Returns the :class:`Metric` object with the name ``name``, creating the metric if it does not exist.
:param str name: The metric to retrieve
+ .. versionadded:: 4.5.0
+
+ :param string prometheusName: The optional Prometheus specific name.
+
.. class:: Metric
Represents a custom metric
std::ostringstream output;
typedef map <string, string> varmap_t;
- varmap_t varmap = getAllStatsMap(
- StatComponent::API); // Argument controls blacklisting of any stats. So stats-api-blacklist will be used to block returned stats.
+
+ // Argument controls blacklisting of any stats. So
+ // stats-api-blacklist will be used to block returned stats.
+ // Second arg tells to use the prometheus names.
+ varmap_t varmap = getAllStatsMap(StatComponent::API, true);
for (const auto &tup : varmap) {
std::string metricName = tup.first;
-
- // Prometheus suggest using '_' instead of '-'
- std::string prometheusMetricName = "pdns_recursor_" + boost::replace_all_copy(metricName, "-", "_");
+ std::string prometheusMetricName = "pdns_recursor_" + metricName;
MetricDefinition metricDetails;