2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "namespaces.hh"
30 #include "circular_buffer.hh"
32 template<typename T, typename Comp=std::less<T> >
36 StatRing(unsigned int size=10000);
37 StatRing(const StatRing&) = delete;
38 StatRing& operator=(const StatRing&) = delete;
39 StatRing& operator=(StatRing&&) = delete;
40 StatRing(StatRing&&) = default;
42 void account(const T &item);
44 uint64_t getSize() const;
45 uint64_t getEntriesCount() const;
46 void resize(unsigned int newsize);
48 void setHelp(const string &str);
49 string getHelp() const;
51 vector<pair<T, unsigned int> > get() const;
53 static bool popisort(const pair<T,int> &a, const pair<T,int> &b)
55 return (a.second > b.second);
58 boost::circular_buffer<T> d_items;
62 enum class StatType : uint8_t {
67 //! use this to gather and query statistics
70 map<string, std::unique_ptr<AtomicCounter>> d_stats;
71 map<string, string> d_keyDescriptions;
72 map<string, StatType> d_statTypes;
73 map<string, LockGuarded<StatRing<string, CIStringCompare> > > d_rings;
74 map<string, LockGuarded<StatRing<SComboAddress> > > d_comboRings;
75 map<string, LockGuarded<StatRing<std::tuple<DNSName, QType> > > > d_dnsnameqtyperings;
76 typedef std::function<uint64_t(const std::string&)> func_t;
77 typedef map<string, func_t> funcstats_t;
78 funcstats_t d_funcstats;
81 std::set<string> d_blacklist;
83 void registerRingStats(const string& name);
86 StatBag(); //!< Naked constructor. You need to declare keys before this class becomes useful
88 void declare(const string &key, const string &descrip="", StatType statType=StatType::counter); //!< Before you can store or access a key, you need to declare it
89 void declare(const string &key, const string &descrip, func_t func, StatType statType); //!< Before you can store or access a key, you need to declare it
91 void declareRing(const string &name, const string &title, unsigned int size=10000);
92 void declareComboRing(const string &name, const string &help, unsigned int size=10000);
93 void declareDNSNameQTypeRing(const string &name, const string &help, unsigned int size=10000);
94 vector<pair<string, unsigned int> >getRing(const string &name);
95 string getRingTitle(const string &name);
96 void ringAccount(const char* name, const string &item)
99 auto it = d_rings.find(name);
100 if (it == d_rings.end()) {
101 throw runtime_error("Attempting to account to nonexistent ring '"+std::string(name)+"'");
104 it->second.lock()->account(item);
107 void ringAccount(const char* name, const ComboAddress &item)
110 auto it = d_comboRings.find(name);
111 if (it == d_comboRings.end()) {
112 throw runtime_error("Attempting to account to nonexistent comboRing '"+std::string(name)+"'");
114 it->second.lock()->account(item);
117 void ringAccount(const char* name, const DNSName &dnsname, const QType &qtype)
120 auto it = d_dnsnameqtyperings.find(name);
121 if (it == d_dnsnameqtyperings.end()) {
122 throw runtime_error("Attempting to account to nonexistent dnsname+qtype ring '"+std::string(name)+"'");
124 it->second.lock()->account(std::make_tuple(dnsname, qtype));
133 vector<string>listRings() const;
134 bool ringExists(const string &name) const;
135 void resetRing(const string &name);
136 void resizeRing(const string &name, unsigned int newsize);
137 uint64_t getRingSize(const string &name);
138 uint64_t getRingEntriesCount(const string &name);
140 string directory(const string &prefix = ""); //!< Returns a list of all data stored. If prefix is given, only stats named with this prefix are returned.
141 vector<string> getEntries(); //!< returns a vector with datums (items)
142 string getDescrip(const string &item); //!< Returns the description of this datum/item
143 StatType getStatType(const string &item); //!< Returns the stats type for the metrics endpoint
144 void exists(const string &key); //!< call this function to throw an exception in case a key does not exist
145 inline void deposit(const string &key, int value); //!< increment the statistics behind this key by value amount
146 inline void inc(const string &key); //!< increase this key's value by one
147 void set(const string &key, unsigned long value); //!< set this key's value
148 unsigned long read(const string &key); //!< read the value behind this key
149 AtomicCounter *getPointer(const string &key); //!< get a direct pointer to the value behind a key. Use this for high performance increments
150 string getValueStr(const string &key); //!< read a value behind a key, and return it as a string
151 void blacklist(const string &str);
153 bool d_allowRedeclare; // only set this true during tests, never in production code
156 inline void StatBag::deposit(const string &key, int value)
160 *d_stats[key]+=value;
163 inline void StatBag::inc(const string &key)