const string pbasename = getPrometheusName(name);
StatsMap entries;
+ uint64_t total = 0;
for (const auto& entry: map) {
auto &key = entry.first;
auto count = entry.second.load();
pname = pbasename + "{type=\"filter\"}";
} else {
sname = name + "-rpz-" + key;
- pname = pbasename + "{type=\"rpz\",zone=\"" + entry.first + "\"}";
+ pname = pbasename + "{type=\"rpz\",policyname=\"" + entry.first + "\"}";
}
- entries.emplace(make_pair(sname, StatsMapEntry{pname, std::to_string(count)}));
+ entries.emplace(sname, StatsMapEntry{pname, std::to_string(count)});
+ total += count;
}
+ entries.emplace(name, StatsMapEntry{pbasename, std::to_string(total)});
return entries;
}
policyName
^^^^^^^^^^
The name logged as 'appliedPolicy' in :doc:`protobuf <protobuf>` messages when this policy is applied.
+Defaults to ``rpzFile`` for RPZs loaded by :func:`rpzFile` or the name of the zone for RPZs loaded by :func:`rpzPrimary`.
tags
^^^^
^^^^^^^^^^^^
packets dropped because of (Lua) policy decision
+policy-hits
+^^^^^^^^^^^
+Number of policy decisions based on Lua (``type = "filter"``), or RPZ (``type = "rpz"``). RPZ hits include the :ref:`rpz-policyName`.
+These metrics are useful for Prometheus and not listed other outputs by default.
+
policy-result-noaction
^^^^^^^^^^^^^^^^^^^^^^
packets that were not acted upon by the RPZ/filter engine
#pragma once
#include <string>
-#include <unordered_map>
-#include <vector>
-#include <inttypes.h>
-#include <unistd.h>
-#include <atomic>
+#include <map>
// Metric types for Prometheus
-enum class PrometheusMetricType : int
+enum class PrometheusMetricType
{
- counter = 1,
- gauge = 2,
- histogram = 3,
- multicounter = 4
+ counter,
+ gauge,
+ histogram,
+ multicounter
};
// Keeps additional information about metrics
struct MetricDefinition
{
- MetricDefinition(const PrometheusMetricType& prometheusType_, const std::string& description_)
+ MetricDefinition(const PrometheusMetricType prometheusType, const std::string& description) :
+ d_description(description), d_prometheusType(prometheusType)
{
- prometheusType = prometheusType_;
- description = description_;
}
MetricDefinition() = default;
// Metric description
- std::string description;
+ std::string d_description;
// Metric type for Prometheus
- PrometheusMetricType prometheusType;
+ PrometheusMetricType d_prometheusType;
};
class MetricDefinitionStorage
// Return metric definition by name
bool getMetricDetails(const std::string& metricName, MetricDefinition& metric)
{
- auto metricDetailsIter = metrics.find(metricName);
+ auto metricDetailsIter = d_metrics.find(metricName);
- if (metricDetailsIter == metrics.end()) {
+ if (metricDetailsIter == d_metrics.end()) {
return false;
}
};
// Return string representation of Prometheus metric type
- std::string getPrometheusStringMetricType(const PrometheusMetricType& metricType)
+ static std::string getPrometheusStringMetricType(const PrometheusMetricType metricType)
{
switch (metricType) {
case PrometheusMetricType::counter:
return "histogram";
break;
case PrometheusMetricType::multicounter:
- return "multicounter";
+ // A multicounter produces multiple values of type "counter"
+ return "counter";
break;
default:
return "";
private:
// Description and types for prometheus output of stats
- static const std::map<std::string, MetricDefinition> metrics;
+ static const std::map<std::string, MetricDefinition> d_metrics;
};
-
-extern MetricDefinitionStorage g_metricDefinitions;
for (const auto& tup : varmap) {
std::string metricName = tup.first;
std::string prometheusMetricName = tup.second.d_prometheusName;
+ std::string helpname = tup.second.d_prometheusName;
MetricDefinition metricDetails;
if (s_metricDefinitions.getMetricDetails(metricName, metricDetails)) {
std::string prometheusTypeName = s_metricDefinitions.getPrometheusStringMetricType(
- metricDetails.prometheusType);
+ metricDetails.d_prometheusType);
if (prometheusTypeName.empty()) {
continue;
}
- if (metricDetails.prometheusType == PrometheusMetricType::multicounter) {
- string shortname = prometheusMetricName.substr(0, prometheusMetricName.find('{'));
- output << "# HELP " << shortname << " " << metricDetails.description << "\n";
- output << "# TYPE " << shortname << " " << "counter" << "\n";
+ if (metricDetails.d_prometheusType == PrometheusMetricType::multicounter) {
+ helpname = prometheusMetricName.substr(0, prometheusMetricName.find('{'));
}
- else if (metricDetails.prometheusType == PrometheusMetricType::histogram) {
+ else if (metricDetails.d_prometheusType == PrometheusMetricType::histogram) {
// name is XXX_count, strip the _count part
- string shortname = prometheusMetricName.substr(0, prometheusMetricName.length() - 6);
- output << "# HELP " << shortname << " " << metricDetails.description << "\n";
- output << "# TYPE " << shortname << " " << prometheusTypeName << "\n";
- } else {
- // for these we have the help and types encoded in the sources:
- output << "# HELP " << prometheusMetricName << " " << metricDetails.description << "\n";
- output << "# TYPE " << prometheusMetricName << " " << prometheusTypeName << "\n";
+ helpname = prometheusMetricName.substr(0, prometheusMetricName.length() - 6);
}
+ output << "# TYPE " << helpname << " " << prometheusTypeName << "\n";
+ output << "# HELP " << helpname << " " << metricDetails.d_description << "\n";
}
output << prometheusMetricName << " " << tup.second.d_value << "\n";
}
}
}
-const std::map<std::string, MetricDefinition> MetricDefinitionStorage::metrics = {
+const std::map<std::string, MetricDefinition> MetricDefinitionStorage::d_metrics = {
{"all-outqueries",
MetricDefinition(PrometheusMetricType::counter,
"Number of outgoing UDP queries since starting")},
{ "almost-expired-exceptions",
MetricDefinition(PrometheusMetricType::counter,
"number of almost-expired tasks that caused an exception")},
+
+ // For multicounters, state the first
+ { "policy-hits",
+ MetricDefinition(PrometheusMetricType::multicounter,
+ "Number of filter or RPZ policy hits")},
};
#define CHECK_PROMETHEUS_METRICS 0