• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PowerDNS / pdns / 9418798096

07 Jun 2024 02:38PM UTC coverage: 64.612% (+0.002%) from 64.61%
9418798096

Pull #14057

github

web-flow
Merge 3ef28144e into b1d2ec1c5
Pull Request #14057: Auth: fix a crash and some cleanup in the auth-catalogzone.cc

37074 of 88162 branches covered (42.05%)

Branch coverage included in aggregate %.

23 of 50 new or added lines in 4 files covered. (46.0%)

45 existing lines in 14 files now uncovered.

124373 of 161708 relevant lines covered (76.91%)

5379791.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.82
/pdns/dnsdistdist/dnsdist-carbon.cc
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

26
#include "dnsdist-carbon.hh"
27
#include "dnsdist.hh"
28
#include "dnsdist-backoff.hh"
29
#include "dnsdist-metrics.hh"
30

31
#ifndef DISABLE_CARBON
32
#include "dolog.hh"
33
#include "sstuff.hh"
34
#include "threadname.hh"
35

36
namespace dnsdist
37
{
38

39
LockGuarded<Carbon::Config> Carbon::s_config;
40

41
static bool doOneCarbonExport(const Carbon::Endpoint& endpoint)
42
{
6✔
43
  const auto& server = endpoint.server;
6✔
44
  const std::string& namespace_name = endpoint.namespace_name;
6✔
45
  const std::string& hostname = endpoint.ourname;
6✔
46
  const std::string& instance_name = endpoint.instance_name;
6✔
47

48
  try {
6✔
49
    Socket carbonSock(server.sin4.sin_family, SOCK_STREAM);
6✔
50
    carbonSock.setNonBlocking();
6✔
51
    carbonSock.connect(server); // we do the connect so the attempt happens while we gather stats
6✔
52
    ostringstream str;
6✔
53

54
    const time_t now = time(nullptr);
6✔
55

56
    {
6✔
57
      auto entries = dnsdist::metrics::g_stats.entries.read_lock();
6✔
58
      for (const auto& entry : *entries) {
516✔
59
        str << namespace_name << "." << hostname << "." << instance_name << "." << entry.d_name << ' ';
516✔
60
        if (const auto& val = std::get_if<pdns::stat_t*>(&entry.d_value)) {
516✔
61
          str << (*val)->load();
251✔
62
        }
251✔
63
        else if (const auto& adval = std::get_if<pdns::stat_t_trait<double>*>(&entry.d_value)) {
265!
64
          str << (*adval)->load();
×
65
        }
×
66
        else if (const auto& dval = std::get_if<double*>(&entry.d_value)) {
265✔
67
          str << **dval;
144✔
68
        }
144✔
69
        else if (const auto& func = std::get_if<dnsdist::metrics::Stats::statfunction_t>(&entry.d_value)) {
121✔
70
          str << (*func)(entry.d_name);
120✔
71
        }
120✔
72
        str << ' ' << now << "\r\n";
516✔
73
      }
516✔
74
    }
6✔
75

76
    auto states = g_dstates.getLocal();
6✔
77
    for (const auto& state : *states) {
18✔
78
      string serverName = state->getName().empty() ? state->d_config.remote.toStringWithPort() : state->getName();
18!
79
      boost::replace_all(serverName, ".", "_");
18✔
80
      string base = namespace_name;
18✔
81
      base += ".";
18✔
82
      base += hostname;
18✔
83
      base += ".";
18✔
84
      base += instance_name;
18✔
85
      base += ".servers.";
18✔
86
      base += serverName;
18✔
87
      base += ".";
18✔
88
      str << base << "queries" << ' ' << state->queries.load() << " " << now << "\r\n";
18✔
89
      str << base << "responses" << ' ' << state->responses.load() << " " << now << "\r\n";
18✔
90
      str << base << "drops" << ' ' << state->reuseds.load() << " " << now << "\r\n";
18✔
91
      str << base << "latency" << ' ' << (state->d_config.availability != DownstreamState::Availability::Down ? state->latencyUsec / 1000.0 : 0) << " " << now << "\r\n";
18✔
92
      str << base << "latencytcp" << ' ' << (state->d_config.availability != DownstreamState::Availability::Down ? state->latencyUsecTCP / 1000.0 : 0) << " " << now << "\r\n";
18✔
93
      str << base << "senderrors" << ' ' << state->sendErrors.load() << " " << now << "\r\n";
18✔
94
      str << base << "outstanding" << ' ' << state->outstanding.load() << " " << now << "\r\n";
18✔
95
      str << base << "tcpdiedsendingquery" << ' ' << state->tcpDiedSendingQuery.load() << " " << now << "\r\n";
18✔
96
      str << base << "tcpdiedreaddingresponse" << ' ' << state->tcpDiedReadingResponse.load() << " " << now << "\r\n";
18✔
97
      str << base << "tcpgaveup" << ' ' << state->tcpGaveUp.load() << " " << now << "\r\n";
18✔
98
      str << base << "tcpreadimeouts" << ' ' << state->tcpReadTimeouts.load() << " " << now << "\r\n";
18✔
99
      str << base << "tcpwritetimeouts" << ' ' << state->tcpWriteTimeouts.load() << " " << now << "\r\n";
18✔
100
      str << base << "tcpconnecttimeouts" << ' ' << state->tcpConnectTimeouts.load() << " " << now << "\r\n";
18✔
101
      str << base << "tcpcurrentconnections" << ' ' << state->tcpCurrentConnections.load() << " " << now << "\r\n";
18✔
102
      str << base << "tcpmaxconcurrentconnections" << ' ' << state->tcpMaxConcurrentConnections.load() << " " << now << "\r\n";
18✔
103
      str << base << "tcpnewconnections" << ' ' << state->tcpNewConnections.load() << " " << now << "\r\n";
18✔
104
      str << base << "tcpreusedconnections" << ' ' << state->tcpReusedConnections.load() << " " << now << "\r\n";
18✔
105
      str << base << "tlsresumptions" << ' ' << state->tlsResumptions.load() << " " << now << "\r\n";
18✔
106
      str << base << "tcpavgqueriesperconnection" << ' ' << state->tcpAvgQueriesPerConnection.load() << " " << now << "\r\n";
18✔
107
      str << base << "tcpavgconnectionduration" << ' ' << state->tcpAvgConnectionDuration.load() << " " << now << "\r\n";
18✔
108
      str << base << "tcptoomanyconcurrentconnections" << ' ' << state->tcpTooManyConcurrentConnections.load() << " " << now << "\r\n";
18✔
109
      str << base << "healthcheckfailures" << ' ' << state->d_healthCheckMetrics.d_failures << " " << now << "\r\n";
18✔
110
      str << base << "healthcheckfailuresparsing" << ' ' << state->d_healthCheckMetrics.d_parseErrors << " " << now << "\r\n";
18✔
111
      str << base << "healthcheckfailurestimeout" << ' ' << state->d_healthCheckMetrics.d_timeOuts << " " << now << "\r\n";
18✔
112
      str << base << "healthcheckfailuresnetwork" << ' ' << state->d_healthCheckMetrics.d_networkErrors << " " << now << "\r\n";
18✔
113
      str << base << "healthcheckfailuresmismatch" << ' ' << state->d_healthCheckMetrics.d_mismatchErrors << " " << now << "\r\n";
18✔
114
      str << base << "healthcheckfailuresinvalid" << ' ' << state->d_healthCheckMetrics.d_invalidResponseErrors << " " << now << "\r\n";
18✔
115
    }
18✔
116

117
    std::map<std::string, uint64_t> frontendDuplicates;
6✔
118
    for (const auto& front : g_frontends) {
12✔
119
      if (front->udpFD == -1 && front->tcpFD == -1) {
12!
120
        continue;
×
121
      }
×
122

123
      string frontName = front->local.toStringWithPort() + (front->udpFD >= 0 ? "_udp" : "_tcp");
12✔
124
      boost::replace_all(frontName, ".", "_");
12✔
125
      auto dupPair = frontendDuplicates.insert({frontName, 1});
12✔
126
      if (!dupPair.second) {
12!
127
        frontName += "_" + std::to_string(dupPair.first->second);
×
128
        ++(dupPair.first->second);
×
129
      }
×
130

131
      string base = namespace_name;
12✔
132
      base += ".";
12✔
133
      base += hostname;
12✔
134
      base += ".";
12✔
135
      base += instance_name;
12✔
136
      base += ".frontends.";
12✔
137
      base += frontName;
12✔
138
      base += ".";
12✔
139
      str << base << "queries" << ' ' << front->queries.load() << " " << now << "\r\n";
12✔
140
      str << base << "responses" << ' ' << front->responses.load() << " " << now << "\r\n";
12✔
141
      str << base << "tcpdiedreadingquery" << ' ' << front->tcpDiedReadingQuery.load() << " " << now << "\r\n";
12✔
142
      str << base << "tcpdiedsendingresponse" << ' ' << front->tcpDiedSendingResponse.load() << " " << now << "\r\n";
12✔
143
      str << base << "tcpgaveup" << ' ' << front->tcpGaveUp.load() << " " << now << "\r\n";
12✔
144
      str << base << "tcpclienttimeouts" << ' ' << front->tcpClientTimeouts.load() << " " << now << "\r\n";
12✔
145
      str << base << "tcpdownstreamtimeouts" << ' ' << front->tcpDownstreamTimeouts.load() << " " << now << "\r\n";
12✔
146
      str << base << "tcpcurrentconnections" << ' ' << front->tcpCurrentConnections.load() << " " << now << "\r\n";
12✔
147
      str << base << "tcpmaxconcurrentconnections" << ' ' << front->tcpMaxConcurrentConnections.load() << " " << now << "\r\n";
12✔
148
      str << base << "tcpavgqueriesperconnection" << ' ' << front->tcpAvgQueriesPerConnection.load() << " " << now << "\r\n";
12✔
149
      str << base << "tcpavgconnectionduration" << ' ' << front->tcpAvgConnectionDuration.load() << " " << now << "\r\n";
12✔
150
      str << base << "tls10-queries" << ' ' << front->tls10queries.load() << " " << now << "\r\n";
12✔
151
      str << base << "tls11-queries" << ' ' << front->tls11queries.load() << " " << now << "\r\n";
12✔
152
      str << base << "tls12-queries" << ' ' << front->tls12queries.load() << " " << now << "\r\n";
12✔
153
      str << base << "tls13-queries" << ' ' << front->tls13queries.load() << " " << now << "\r\n";
12✔
154
      str << base << "tls-unknown-queries" << ' ' << front->tlsUnknownqueries.load() << " " << now << "\r\n";
12✔
155
      str << base << "tlsnewsessions" << ' ' << front->tlsNewSessions.load() << " " << now << "\r\n";
12✔
156
      str << base << "tlsresumptions" << ' ' << front->tlsResumptions.load() << " " << now << "\r\n";
12✔
157
      str << base << "tlsunknownticketkeys" << ' ' << front->tlsUnknownTicketKey.load() << " " << now << "\r\n";
12✔
158
      str << base << "tlsinactiveticketkeys" << ' ' << front->tlsInactiveTicketKey.load() << " " << now << "\r\n";
12✔
159

160
      const TLSErrorCounters* errorCounters = nullptr;
12✔
161
      if (front->tlsFrontend != nullptr) {
12!
162
        errorCounters = &front->tlsFrontend->d_tlsCounters;
×
163
      }
×
164
      else if (front->dohFrontend != nullptr) {
12!
165
        errorCounters = &front->dohFrontend->d_tlsContext.d_tlsCounters;
×
166
      }
×
167
      if (errorCounters != nullptr) {
12!
168
        str << base << "tlsdhkeytoosmall" << ' ' << errorCounters->d_dhKeyTooSmall << " " << now << "\r\n";
×
169
        str << base << "tlsinappropriatefallback" << ' ' << errorCounters->d_inappropriateFallBack << " " << now << "\r\n";
×
170
        str << base << "tlsnosharedcipher" << ' ' << errorCounters->d_noSharedCipher << " " << now << "\r\n";
×
171
        str << base << "tlsunknownciphertype" << ' ' << errorCounters->d_unknownCipherType << " " << now << "\r\n";
×
172
        str << base << "tlsunknownkeyexchangetype" << ' ' << errorCounters->d_unknownKeyExchangeType << " " << now << "\r\n";
×
173
        str << base << "tlsunknownprotocol" << ' ' << errorCounters->d_unknownProtocol << " " << now << "\r\n";
×
174
        str << base << "tlsunsupportedec" << ' ' << errorCounters->d_unsupportedEC << " " << now << "\r\n";
×
175
        str << base << "tlsunsupportedprotocol" << ' ' << errorCounters->d_unsupportedProtocol << " " << now << "\r\n";
×
176
      }
×
177
    }
12✔
178

179
    auto localPools = g_pools.getLocal();
6✔
180
    for (const auto& entry : *localPools) {
6✔
181
      string poolName = entry.first;
6✔
182
      boost::replace_all(poolName, ".", "_");
6✔
183
      if (poolName.empty()) {
6!
184
        poolName = "_default_";
6✔
185
      }
6✔
186
      string base = namespace_name;
6✔
187
      base += ".";
6✔
188
      base += hostname;
6✔
189
      base += ".";
6✔
190
      base += instance_name;
6✔
191
      base += ".pools.";
6✔
192
      base += poolName;
6✔
193
      base += ".";
6✔
194
      const std::shared_ptr<ServerPool> pool = entry.second;
6✔
195
      str << base << "servers"
6✔
196
          << " " << pool->countServers(false) << " " << now << "\r\n";
6✔
197
      str << base << "servers-up"
6✔
198
          << " " << pool->countServers(true) << " " << now << "\r\n";
6✔
199
      if (pool->packetCache != nullptr) {
6!
200
        const auto& cache = pool->packetCache;
×
201
        str << base << "cache-size"
×
202
            << " " << cache->getMaxEntries() << " " << now << "\r\n";
×
203
        str << base << "cache-entries"
×
204
            << " " << cache->getEntriesCount() << " " << now << "\r\n";
×
205
        str << base << "cache-hits"
×
206
            << " " << cache->getHits() << " " << now << "\r\n";
×
207
        str << base << "cache-misses"
×
208
            << " " << cache->getMisses() << " " << now << "\r\n";
×
209
        str << base << "cache-deferred-inserts"
×
210
            << " " << cache->getDeferredInserts() << " " << now << "\r\n";
×
211
        str << base << "cache-deferred-lookups"
×
212
            << " " << cache->getDeferredLookups() << " " << now << "\r\n";
×
213
        str << base << "cache-lookup-collisions"
×
214
            << " " << cache->getLookupCollisions() << " " << now << "\r\n";
×
215
        str << base << "cache-insert-collisions"
×
216
            << " " << cache->getInsertCollisions() << " " << now << "\r\n";
×
217
        str << base << "cache-ttl-too-shorts"
×
218
            << " " << cache->getTTLTooShorts() << " " << now << "\r\n";
×
219
        str << base << "cache-cleanup-count"
×
220
            << " " << cache->getCleanupCount() << " " << now << "\r\n";
×
221
      }
×
222
    }
6✔
223

224
#ifdef HAVE_DNS_OVER_HTTPS
6✔
225
    {
6✔
226
      std::map<std::string, uint64_t> dohFrontendDuplicates;
6✔
227
      const string base = "dnsdist." + hostname + ".main.doh.";
6✔
228
      for (const auto& doh : g_dohlocals) {
6!
229
        string name = doh->d_tlsContext.d_addr.toStringWithPort();
×
230
        boost::replace_all(name, ".", "_");
×
231
        boost::replace_all(name, ":", "_");
×
232
        boost::replace_all(name, "[", "_");
×
233
        boost::replace_all(name, "]", "_");
×
234

235
        auto dupPair = dohFrontendDuplicates.insert({name, 1});
×
236
        if (!dupPair.second) {
×
237
          name += "_" + std::to_string(dupPair.first->second);
×
238
          ++(dupPair.first->second);
×
239
        }
×
240

241
        const vector<pair<const char*, const pdns::stat_t&>> values{
×
242
          {"http-connects", doh->d_httpconnects},
×
243
          {"http1-queries", doh->d_http1Stats.d_nbQueries},
×
244
          {"http2-queries", doh->d_http2Stats.d_nbQueries},
×
245
          {"http1-200-responses", doh->d_http1Stats.d_nb200Responses},
×
246
          {"http2-200-responses", doh->d_http2Stats.d_nb200Responses},
×
247
          {"http1-400-responses", doh->d_http1Stats.d_nb400Responses},
×
248
          {"http2-400-responses", doh->d_http2Stats.d_nb400Responses},
×
249
          {"http1-403-responses", doh->d_http1Stats.d_nb403Responses},
×
250
          {"http2-403-responses", doh->d_http2Stats.d_nb403Responses},
×
251
          {"http1-500-responses", doh->d_http1Stats.d_nb500Responses},
×
252
          {"http2-500-responses", doh->d_http2Stats.d_nb500Responses},
×
253
          {"http1-502-responses", doh->d_http1Stats.d_nb502Responses},
×
254
          {"http2-502-responses", doh->d_http2Stats.d_nb502Responses},
×
255
          {"http1-other-responses", doh->d_http1Stats.d_nbOtherResponses},
×
256
          {"http2-other-responses", doh->d_http2Stats.d_nbOtherResponses},
×
257
          {"get-queries", doh->d_getqueries},
×
258
          {"post-queries", doh->d_postqueries},
×
259
          {"bad-requests", doh->d_badrequests},
×
260
          {"error-responses", doh->d_errorresponses},
×
261
          {"redirect-responses", doh->d_redirectresponses},
×
262
          {"valid-responses", doh->d_validresponses}};
×
263

264
        for (const auto& item : values) {
×
265
          str << base << name << "." << item.first << " " << item.second << " " << now << "\r\n";
×
266
        }
×
267
      }
×
268
    }
6✔
269
#endif /* HAVE_DNS_OVER_HTTPS */
6✔
270

271
    {
6✔
272
      std::string qname;
6✔
273
      auto records = g_qcount.records.write_lock();
6✔
274
      for (const auto& record : *records) {
6!
275
        qname = record.first;
×
276
        boost::replace_all(qname, ".", "_");
×
277
        str << "dnsdist.querycount." << qname << ".queries " << record.second << " " << now << "\r\n";
×
278
      }
×
279
      records->clear();
6✔
280
    }
6✔
281

282
    const string msg = str.str();
6✔
283

284
    int ret = waitForRWData(carbonSock.getHandle(), false, 1, 0);
6✔
285
    if (ret <= 0) {
6!
286
      vinfolog("Unable to write data to carbon server on %s: %s", server.toStringWithPort(), (ret < 0 ? stringerror() : "Timeout"));
×
287
      return false;
×
288
    }
×
289
    carbonSock.setBlocking();
6✔
290
    writen2(carbonSock.getHandle(), msg.c_str(), msg.size());
6✔
291
  }
6✔
292
  catch (const std::exception& e) {
6✔
293
    warnlog("Problem sending carbon data to %s: %s", server.toStringWithPort(), e.what());
×
294
    return false;
×
295
  }
×
296

297
  return true;
6✔
298
}
6✔
299

300
static void carbonHandler(Carbon::Endpoint&& endpoint)
301
{
2✔
302
  setThreadName("dnsdist/carbon");
2✔
303
  const auto intervalUSec = endpoint.interval * 1000 * 1000;
2✔
304
  /* maximum interval between two attempts is 10 minutes */
305
  const ExponentialBackOffTimer backOffTimer(10 * 60);
2✔
306

307
  try {
2✔
308
    uint8_t consecutiveFailures = 0;
2✔
309
    do {
6✔
310
      DTime dtimer;
6✔
311
      dtimer.set();
6✔
312
      if (doOneCarbonExport(endpoint)) {
6!
313
        const auto elapsedUSec = dtimer.udiff();
6✔
314
        if (elapsedUSec < 0 || static_cast<unsigned int>(elapsedUSec) <= intervalUSec) {
6!
315
          useconds_t toSleepUSec = intervalUSec - elapsedUSec;
6✔
316
          usleep(toSleepUSec);
6✔
317
        }
6✔
UNCOV
318
        else {
×
UNCOV
319
          vinfolog("Carbon export for %s took longer (%s us) than the configured interval (%d us)", endpoint.server.toStringWithPort(), elapsedUSec, intervalUSec);
×
UNCOV
320
        }
×
321
        consecutiveFailures = 0;
6✔
322
      }
6✔
323
      else {
×
324
        const auto backOff = backOffTimer.get(consecutiveFailures);
×
325
        if (consecutiveFailures < std::numeric_limits<decltype(consecutiveFailures)>::max()) {
×
326
          consecutiveFailures++;
×
327
        }
×
328
        vinfolog("Run for %s - %s failed, next attempt in %d", endpoint.server.toStringWithPort(), endpoint.ourname, backOff);
×
329
        std::this_thread::sleep_for(std::chrono::seconds(backOff));
×
330
      }
×
331
    } while (true);
6✔
332
  }
2✔
333
  catch (const PDNSException& e) {
2✔
334
    errlog("Carbon thread for %s died, PDNSException: %s", endpoint.server.toStringWithPort(), e.reason);
×
335
  }
×
336
  catch (...) {
2✔
337
    errlog("Carbon thread for %s died", endpoint.server.toStringWithPort());
×
338
  }
×
339
}
2✔
340

341
bool Carbon::addEndpoint(Carbon::Endpoint&& endpoint)
342
{
4✔
343
  if (endpoint.ourname.empty()) {
4!
344
    try {
×
345
      endpoint.ourname = getCarbonHostName();
×
346
    }
×
347
    catch (const std::exception& e) {
×
348
      throw std::runtime_error(std::string("The 'ourname' setting in 'carbonServer()' has not been set and we are unable to determine the system's hostname: ") + e.what());
×
349
    }
×
350
  }
×
351

352
  auto config = s_config.lock();
4✔
353
  if (config->d_running) {
4!
354
    // we already started the threads, let's just spawn a new one
355
    std::thread newHandler(carbonHandler, std::move(endpoint));
×
356
    newHandler.detach();
×
357
  }
×
358
  else {
4✔
359
    config->d_endpoints.push_back(std::move(endpoint));
4✔
360
  }
4✔
361
  return true;
4✔
362
}
4✔
363

364
void Carbon::run()
365
{
313✔
366
  auto config = s_config.lock();
313✔
367
  if (config->d_running) {
313!
368
    throw std::runtime_error("The carbon threads are already running");
×
369
  }
×
370
  for (auto& endpoint : config->d_endpoints) {
313✔
371
    std::thread newHandler(carbonHandler, std::move(endpoint));
2✔
372
    newHandler.detach();
2✔
373
  }
2✔
374
  config->d_endpoints.clear();
313✔
375
  config->d_running = true;
313✔
376
}
313✔
377

378
}
379
#endif /* DISABLE_CARBON */
380

381
static time_t s_start = time(nullptr);
382

383
uint64_t uptimeOfProcess(const std::string& str)
384
{
215✔
385
  return time(nullptr) - s_start;
215✔
386
}
215✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc