newThread.detach();
});
- luaCtx.writeFunction("declareMetric", [](const std::string& name, const std::string& type) {
+ luaCtx.writeFunction("declareMetric", [](const std::string& name, const std::string& type, const std::string& description) {
if (g_configurationDone) {
g_outputBuffer = "declareMetric cannot be used at runtime!\n";
return false;
auto itp = g_stats.customCounters.emplace(name, 0);
if (itp.second) {
g_stats.entries.emplace_back(name, &g_stats.customCounters[name]);
+ addMetricDefinition(name, "counter", description);
}
} else if (type == "gauge") {
auto itp = g_stats.customGauges.emplace(name, 0.);
if (itp.second) {
g_stats.entries.emplace_back(name, &g_stats.customGauges[name]);
+ addMetricDefinition(name, "gauge", description);
}
} else {
g_outputBuffer = "declareMetric unknown type '" + type + "'\n";
};
#ifndef DISABLE_PROMETHEUS
-static const MetricDefinitionStorage s_metricDefinitions;
+static MetricDefinitionStorage s_metricDefinitions;
-const std::map<std::string, MetricDefinition> MetricDefinitionStorage::metrics{
+std::map<std::string, MetricDefinition> MetricDefinitionStorage::metrics{
{ "responses", MetricDefinition(PrometheusMetricType::counter, "Number of responses received from backends") },
{ "servfail-responses", MetricDefinition(PrometheusMetricType::counter, "Number of SERVFAIL answers received from backends") },
{ "queries", MetricDefinition(PrometheusMetricType::counter, "Number of received queries")},
};
#endif /* DISABLE_PROMETHEUS */
+bool addMetricDefinition(const std::string& name, const std::string type, const std::string& description) {
+#ifndef DISABLE_PROMETHEUS
+ return MetricDefinitionStorage::addMetricDefinition(name, type, description);
+#else
+ return true;
+#endif /* DISABLE_PROMETHEUS */
+}
+
#ifndef DISABLE_WEB_CONFIG
static bool apiWriteConfigFile(const string& filebasename, const string& content)
{
return true;
};
+ static bool addMetricDefinition(const std::string& name, const std::string type, const std::string& description) {
+ static const std::map<std::string, PrometheusMetricType> namesToTypes = {
+ {"counter", PrometheusMetricType::counter},
+ {"gauge", PrometheusMetricType::gauge},
+ };
+ auto realtype = namesToTypes.find(type);
+ if (realtype == namesToTypes.end()) {
+ return false;
+ }
+ metrics.emplace(name, MetricDefinition{realtype->second, description});
+ return true;
+ }
+
// Return string representation of Prometheus metric type
std::string getPrometheusStringMetricType(PrometheusMetricType metricType) const {
switch (metricType) {
}
};
- static const std::map<std::string, MetricDefinition> metrics;
+ static std::map<std::string, MetricDefinition> metrics;
};
#endif /* DISABLE_PROMETHEUS */
void registerBuiltInWebHandlers();
void clearWebHandlers();
+bool addMetricDefinition(const std::string& name, const std::string type, const std::string& description);
+
std::string getWebserverConfig();
* manipulate counters using :func:`incMetric` and :func:`decMetric`
* update a gauge using :func:`setMetric`
-.. function:: declareMetric(name, type) -> bool
+.. function:: declareMetric(name, type, description) -> bool
.. versionadded:: 1.8.0
:param str name: The name of the metric, lowercase alphanumerical characters and dashes (-) only
:param str type: The desired type in ``gauge`` or ``counter``
+ :param str name: The description of the metric
.. function:: incMetric(name) -> int
_config_template = """
newServer{address="127.0.0.1:%s"}
webserver("127.0.0.1:%s")
- declareMetric("my-custom-metric", "counter")
- declareMetric("my-other-metric", "counter")
- declareMetric("my-gauge", "gauge")
+ declareMetric("my-custom-metric", "counter", "Number of statistics")
+ declareMetric("my-other-metric", "counter", "Another number of statistics")
+ declareMetric("my-gauge", "gauge", "Current memory usage")
setWebserverConfig({password="%s", apiKey="%s"})
"""
end
function declareNewMetric(dq)
- if declareMetric("new-runtime-metric", "counter") then
+ if declareMetric("new-runtime-metric", "counter", "Metric declaration at runtime should fail") then
return DNSAction.Spoof, '1.2.3.4'
end
return DNSAction.None
end
- declareMetric("my-custom-counter", "counter")
- declareMetric("my-custom-gauge", "gauge")
+ declareMetric("my-custom-counter", "counter", "Number of tests run")
+ declareMetric("my-custom-gauge", "gauge", "Temperature of the tests")
addAction("declare.metric.advanced.tests.powerdns.com.", LuaAction(declareNewMetric))
addAction("operations.metric.advanced.tests.powerdns.com.", LuaAction(custommetrics))
newServer{address="127.0.0.1:%s"}