Coverage Report

Created: 2024-04-25 06:25

/src/pdns/pdns/statbag.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
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.
8
 *
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.
12
 *
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.
17
 *
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.
21
 */
22
#ifdef HAVE_CONFIG_H
23
#include "config.h"
24
#endif
25
#include "utility.hh"
26
#include "statbag.hh"
27
#include "pdnsexception.hh"
28
#include <iostream>
29
#include <sstream>
30
#include <algorithm>
31
#include <utility>
32
#include "arguments.hh"
33
#include "lock.hh"
34
#include "iputils.hh"
35
36
37
#include "namespaces.hh"
38
39
StatBag::StatBag()
40
2
{
41
2
  d_doRings=false;
42
2
  d_allowRedeclare=false;
43
2
}
44
45
void StatBag::exists(const string &key)
46
0
{
47
0
  if (!d_keyDescriptions.count(key)) {
48
0
    throw PDNSException("Trying to deposit into unknown StatBag key '"+key+"'");
49
0
  }
50
0
}
51
52
string StatBag::directory(const string &prefix)
53
0
{
54
0
  string dir;
55
0
  ostringstream o;
56
57
0
  for(const auto& val : d_stats) {
58
0
    if (d_blacklist.find(val.first) != d_blacklist.end())
59
0
      continue;
60
0
    if (val.first.find(prefix) != 0)
61
0
      continue;
62
0
    o << val.first<<"="<<*(val.second)<<",";
63
0
  }
64
65
66
0
  for(const funcstats_t::value_type& val :  d_funcstats) {
67
0
    if (d_blacklist.find(val.first) != d_blacklist.end())
68
0
      continue;
69
0
    if (val.first.find(prefix) != 0)
70
0
      continue;
71
0
    o << val.first<<"="<<val.second(val.first)<<",";
72
0
  }
73
0
  dir=o.str();
74
0
  return dir;
75
0
}
76
77
vector<string>StatBag::getEntries()
78
0
{
79
0
  vector<string> ret;
80
81
0
  for(const auto& i: d_stats) {
82
0
    if (d_blacklist.find(i.first) != d_blacklist.end())
83
0
      continue;
84
0
    ret.push_back(i.first);
85
0
  }
86
87
0
  for(const funcstats_t::value_type& val :  d_funcstats) {
88
0
    if (d_blacklist.find(val.first) != d_blacklist.end())
89
0
      continue;
90
0
    ret.push_back(val.first);
91
0
  }
92
93
94
0
  return ret;
95
96
0
}
97
98
string StatBag::getDescrip(const string &item)
99
0
{
100
0
  exists(item);
101
0
  return d_keyDescriptions[item];
102
0
}
103
104
StatType StatBag::getStatType(const string &item)
105
0
{
106
0
  exists(item);
107
0
  return d_statTypes[item];
108
0
}
109
110
void StatBag::declare(const string &key, const string &descrip, StatType statType)
111
0
{
112
0
  if(d_stats.count(key)) {
113
0
    if (d_allowRedeclare) {
114
0
      *d_stats[key] = 0;
115
0
      return;
116
0
    }
117
0
    else {
118
0
      throw PDNSException("Attempt to re-declare statbag '"+key+"'");
119
0
    }
120
0
  }
121
122
0
  auto i=make_unique<AtomicCounter>(0);
123
0
  d_stats[key]=std::move(i);
124
0
  d_keyDescriptions[key]=descrip;
125
0
  d_statTypes[key]=statType;
126
0
}
127
128
void StatBag::declare(const string &key, const string &descrip, StatBag::func_t func, StatType statType)
129
0
{
130
0
  if(d_funcstats.count(key) && !d_allowRedeclare) {
131
0
    throw PDNSException("Attempt to re-declare func statbag '"+key+"'");
132
0
  }
133
134
0
  d_funcstats[key]=std::move(func);
135
0
  d_keyDescriptions[key]=descrip;
136
0
  d_statTypes[key]=statType;
137
0
}
138
139
          
140
void StatBag::set(const string &key, unsigned long value)
141
0
{
142
0
  exists(key);
143
0
  d_stats[key]->store(value);
144
0
}
145
146
unsigned long StatBag::read(const string &key)
147
0
{
148
0
  exists(key);
149
0
  funcstats_t::const_iterator iter = d_funcstats.find(key);
150
0
  if (iter != d_funcstats.end()) {
151
0
    return iter->second(iter->first);
152
0
  }
153
0
  return *d_stats[key];
154
0
}
155
156
string StatBag::getValueStr(const string &key)
157
0
{
158
0
  ostringstream o;
159
0
  o<<read(key);
160
0
  return o.str();
161
0
}
162
163
AtomicCounter *StatBag::getPointer(const string &key)
164
0
{
165
0
  exists(key);
166
0
  return d_stats[key].get();
167
0
}
168
169
0
StatBag::~StatBag() = default;
170
171
template<typename T, typename Comp>
172
StatRing<T,Comp>::StatRing(unsigned int size)
173
0
{
174
0
  d_items.set_capacity(size);
175
0
}
Unexecuted instantiation: StatRing<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, CIStringCompare>::StatRing(unsigned int)
Unexecuted instantiation: StatRing<SComboAddress, std::__1::less<SComboAddress> >::StatRing(unsigned int)
Unexecuted instantiation: StatRing<std::__1::tuple<DNSName, QType>, std::__1::less<std::__1::tuple<DNSName, QType> > >::StatRing(unsigned int)
176
177
template<typename T, typename Comp>
178
void StatRing<T,Comp>::account(const T& t)
179
0
{
180
0
  d_items.push_back(t);
181
0
}
Unexecuted instantiation: StatRing<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, CIStringCompare>::account(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: StatRing<SComboAddress, std::__1::less<SComboAddress> >::account(SComboAddress const&)
Unexecuted instantiation: StatRing<std::__1::tuple<DNSName, QType>, std::__1::less<std::__1::tuple<DNSName, QType> > >::account(std::__1::tuple<DNSName, QType> const&)
182
183
template<typename T, typename Comp>
184
uint64_t StatRing<T,Comp>::getSize() const
185
0
{
186
0
  return d_items.capacity();
187
0
}
Unexecuted instantiation: StatRing<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, CIStringCompare>::getSize() const
Unexecuted instantiation: StatRing<SComboAddress, std::__1::less<SComboAddress> >::getSize() const
Unexecuted instantiation: StatRing<std::__1::tuple<DNSName, QType>, std::__1::less<std::__1::tuple<DNSName, QType> > >::getSize() const
188
189
template<typename T, typename Comp>
190
uint64_t StatRing<T,Comp>::getEntriesCount() const
191
0
{
192
0
  return d_items.size();
193
0
}
Unexecuted instantiation: StatRing<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, CIStringCompare>::getEntriesCount() const
Unexecuted instantiation: StatRing<SComboAddress, std::__1::less<SComboAddress> >::getEntriesCount() const
Unexecuted instantiation: StatRing<std::__1::tuple<DNSName, QType>, std::__1::less<std::__1::tuple<DNSName, QType> > >::getEntriesCount() const
194
195
template<typename T, typename Comp>
196
void StatRing<T,Comp>::resize(unsigned int newsize)
197
0
{
198
0
  d_items.set_capacity(newsize);
199
0
}
Unexecuted instantiation: StatRing<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, CIStringCompare>::resize(unsigned int)
Unexecuted instantiation: StatRing<SComboAddress, std::__1::less<SComboAddress> >::resize(unsigned int)
Unexecuted instantiation: StatRing<std::__1::tuple<DNSName, QType>, std::__1::less<std::__1::tuple<DNSName, QType> > >::resize(unsigned int)
200
201
template<typename T, typename Comp>
202
void StatRing<T,Comp>::setHelp(const string &str)
203
0
{
204
0
  d_help = str;
205
0
}
Unexecuted instantiation: StatRing<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, CIStringCompare>::setHelp(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: StatRing<SComboAddress, std::__1::less<SComboAddress> >::setHelp(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: StatRing<std::__1::tuple<DNSName, QType>, std::__1::less<std::__1::tuple<DNSName, QType> > >::setHelp(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
206
207
template<typename T, typename Comp>
208
string StatRing<T,Comp>::getHelp() const
209
0
{
210
0
  return d_help;
211
0
}
Unexecuted instantiation: StatRing<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, CIStringCompare>::getHelp() const
Unexecuted instantiation: StatRing<SComboAddress, std::__1::less<SComboAddress> >::getHelp() const
Unexecuted instantiation: StatRing<std::__1::tuple<DNSName, QType>, std::__1::less<std::__1::tuple<DNSName, QType> > >::getHelp() const
212
213
214
template<typename T, typename Comp>
215
vector<pair<T, unsigned int> >StatRing<T,Comp>::get() const
216
0
{
217
0
  map<T,unsigned int, Comp> res;
218
0
  for (typename boost::circular_buffer<T>::const_iterator i = d_items.begin(); i != d_items.end(); ++i) {
219
0
    res[*i]++;
220
0
  }
221
  
222
0
  vector<pair<T ,unsigned int> > tmp;
223
0
  for(typename map<T, unsigned int>::const_iterator i=res.begin();i!=res.end();++i) 
224
0
    tmp.push_back(*i);
225
226
0
  sort(tmp.begin(),tmp.end(),popisort);
227
228
0
  return tmp;
229
0
}
Unexecuted instantiation: StatRing<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, CIStringCompare>::get() const
Unexecuted instantiation: StatRing<SComboAddress, std::__1::less<SComboAddress> >::get() const
Unexecuted instantiation: StatRing<std::__1::tuple<DNSName, QType>, std::__1::less<std::__1::tuple<DNSName, QType> > >::get() const
230
231
void StatBag::registerRingStats(const string& name)
232
0
{
233
0
  declare("ring-" + name + "-size", "Number of entries in the " + name + " ring", [this,name](const std::string&) { return static_cast<uint64_t>(getRingEntriesCount(name)); }, StatType::gauge);
234
0
  declare("ring-" + name + "-capacity", "Maximum number of entries in the " + name + " ring", [this,name](const std::string&) { return static_cast<uint64_t>(getRingSize(name)); }, StatType::gauge);
235
0
}
236
237
void StatBag::declareRing(const string &name, const string &help, unsigned int size)
238
0
{
239
0
  d_rings.emplace(name, size);
240
0
  d_rings[name].lock()->setHelp(help);
241
0
  registerRingStats(name);
242
0
}
243
244
void StatBag::declareComboRing(const string &name, const string &help, unsigned int size)
245
0
{
246
0
  d_comboRings.emplace(name, size);
247
0
  d_comboRings[name].lock()->setHelp(help);
248
0
  registerRingStats(name);
249
0
}
250
251
void StatBag::declareDNSNameQTypeRing(const string &name, const string &help, unsigned int size)
252
0
{
253
0
  d_dnsnameqtyperings.emplace(name, size);
254
0
  d_dnsnameqtyperings[name].lock()->setHelp(help);
255
0
  registerRingStats(name);
256
0
}
257
258
vector<pair<string, unsigned int> > StatBag::getRing(const string &name)
259
0
{
260
0
  if (d_rings.count(name)) {
261
0
    return d_rings[name].lock()->get();
262
0
  }
263
0
  vector<pair<string, unsigned int> > ret;
264
265
0
  if (d_comboRings.count(name)) {
266
0
    for (const auto& [addr, num] : d_comboRings[name].lock()->get()) {
267
0
      ret.emplace_back(addr.ca.toString(), num);
268
0
    }
269
0
  } else if (d_dnsnameqtyperings.count(name)) {
270
0
    for (auto const& [d, t] : d_dnsnameqtyperings[name].lock()->get()) {
271
0
      ret.emplace_back(std::get<0>(d).toLogString() + "/" + std::get<1>(d).toString(), t);
272
0
    }
273
0
  }
274
0
  return ret;
275
0
}
276
277
template<typename T, typename Comp>
278
void StatRing<T,Comp>::reset()
279
0
{
280
0
  d_items.clear();
281
0
}
Unexecuted instantiation: StatRing<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, CIStringCompare>::reset()
Unexecuted instantiation: StatRing<SComboAddress, std::__1::less<SComboAddress> >::reset()
Unexecuted instantiation: StatRing<std::__1::tuple<DNSName, QType>, std::__1::less<std::__1::tuple<DNSName, QType> > >::reset()
282
283
void StatBag::resetRing(const string &name)
284
0
{
285
0
  if(d_rings.count(name))
286
0
    d_rings[name].lock()->reset();
287
0
  if(d_comboRings.count(name))
288
0
    d_comboRings[name].lock()->reset();
289
0
  if(d_dnsnameqtyperings.count(name))
290
0
    d_dnsnameqtyperings[name].lock()->reset();
291
0
}
292
293
void StatBag::resizeRing(const string &name, unsigned int newsize)
294
0
{
295
0
  if(d_rings.count(name))
296
0
    d_rings[name].lock()->resize(newsize);
297
0
  if(d_comboRings.count(name))
298
0
    d_comboRings[name].lock()->resize(newsize);
299
0
  if(d_dnsnameqtyperings.count(name))
300
0
    return d_dnsnameqtyperings[name].lock()->resize(newsize);
301
0
}
302
303
304
uint64_t StatBag::getRingSize(const string &name)
305
0
{
306
0
  if(d_rings.count(name))
307
0
    return d_rings[name].lock()->getSize();
308
0
  if(d_comboRings.count(name))
309
0
    return d_comboRings[name].lock()->getSize();
310
0
  if(d_dnsnameqtyperings.count(name))
311
0
    return d_dnsnameqtyperings[name].lock()->getSize();
312
0
  return 0;
313
0
}
314
315
uint64_t StatBag::getRingEntriesCount(const string &name)
316
0
{
317
0
  if(d_rings.count(name))
318
0
    return d_rings[name].lock()->getEntriesCount();
319
0
  if(d_comboRings.count(name))
320
0
    return d_comboRings[name].lock()->getEntriesCount();
321
0
  if(d_dnsnameqtyperings.count(name))
322
0
    return d_dnsnameqtyperings[name].lock()->getEntriesCount();
323
0
  return 0;
324
0
}
325
326
string StatBag::getRingTitle(const string &name)
327
0
{
328
0
  if(d_rings.count(name))
329
0
    return d_rings[name].lock()->getHelp();
330
0
  if(d_comboRings.count(name))
331
0
    return d_comboRings[name].lock()->getHelp();
332
0
  if(d_dnsnameqtyperings.count(name))
333
0
    return d_dnsnameqtyperings[name].lock()->getHelp();
334
0
  return "";
335
0
}
336
337
vector<string>StatBag::listRings() const
338
0
{
339
0
  vector<string> ret;
340
0
  for(auto & d_ring : d_rings)
341
0
    ret.push_back(d_ring.first);
342
0
  for(auto & d_comboRing : d_comboRings)
343
0
    ret.push_back(d_comboRing.first);
344
0
  for(const auto &i : d_dnsnameqtyperings)
345
0
    ret.push_back(i.first);
346
347
0
  return ret;
348
0
}
349
350
bool StatBag::ringExists(const string &name) const
351
0
{
352
0
  return d_rings.count(name) || d_comboRings.count(name) || d_dnsnameqtyperings.count(name);
353
0
}
354
355
0
void StatBag::blacklist(const string& str) {
356
0
  d_blacklist.insert(str);
357
0
}
358
359
template class StatRing<std::string, CIStringCompare>;
360
template class StatRing<SComboAddress>;
361
template class StatRing<std::tuple<DNSName, QType> >;