Coverage Report

Created: 2026-10-03 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pdns/pdns/dnsdistdist/otlp_logger.hh
Line
Count
Source
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
#pragma once
23
#include "remote_logger.hh"
24
#include "dnsdist-opentelemetry.hh"
25
26
#if !defined(DISABLE_PROTOBUF) && defined(HAVE_LIBCURL)
27
#include <stdexcept>
28
#include <memory>
29
30
#include "logr.hh"
31
#include "minicurl.hh"
32
class OTLPLogger : public RemoteLoggerInterface
33
{
34
public:
35
  enum class LoggerType : uint8_t
36
  {
37
    HTTP,
38
    gRPC,
39
  };
40
41
  OTLPLogger(std::string address, const size_t interval = 5, const size_t queueSize = 500, const size_t batchSize = 100);
42
  OTLPLogger(const OTLPLogger&) = delete;
43
  OTLPLogger(OTLPLogger&&) = delete;
44
  OTLPLogger& operator=(const OTLPLogger&) = delete;
45
  OTLPLogger& operator=(OTLPLogger&&) = delete;
46
  ~OTLPLogger() override
47
  {
48
    d_exiting = true;
49
    d_thread.join();
50
  };
51
52
  [[nodiscard]] RemoteLoggerInterface::Result queueData(const std::string& data) override;
53
  RemoteLoggerInterface::Result queueData(const TracesData& data);
54
55
  [[nodiscard]] std::string address() const override
56
  {
57
    return d_address;
58
  }
59
60
  [[nodiscard]] std::string name() const override
61
  {
62
    return "OTLP";
63
  }
64
65
  [[nodiscard]] std::string toString() override
66
  {
67
    return "OTLPLogger to " + d_address;
68
  }
69
70
  [[nodiscard]] RemoteLoggerInterface::Stats getStats() override
71
  {
72
    return Stats{.d_queued = d_framesSent,
73
                 .d_pipeFull = d_queueFullDrops,
74
                 .d_tooLarge = d_tooLargeCount,
75
                 .d_otherError = d_permanentFailures};
76
  }
77
78
  void setHTTPTimeout(const int timeout)
79
  {
80
    if (d_type != LoggerType::HTTP) {
81
      throw std::invalid_argument("Can not set HTTP timeout on non-HTTP logger");
82
    }
83
    d_http_timeout = timeout;
84
  }
85
86
  void setHTTPFastOpen(const bool value)
87
  {
88
    if (d_type != LoggerType::HTTP) {
89
      throw std::invalid_argument("Can not set fast open on non-HTTP logger");
90
    }
91
    d_http_fastopen = value;
92
  }
93
94
  void setSetHTTPSVerify(const bool value)
95
  {
96
    if (d_type != LoggerType::HTTP) {
97
      throw std::invalid_argument("Can not set HTTPS verigy on non-HTTP logger");
98
    }
99
    d_https_verify = value;
100
  }
101
102
  [[nodiscard]] LoggerType getType() const
103
  {
104
    return d_type;
105
  }
106
107
private:
108
  LoggerType d_type;
109
  std::string d_address;
110
  size_t d_interval;
111
  size_t d_batchSize;
112
113
  std::unique_ptr<MiniCurl> d_miniCurl{nullptr};
114
  MiniCurl::MiniCurlHeaders d_httpHeaders{{"Content-Type", "application/x-protobuf"}};
115
  int d_http_timeout{2};
116
  bool d_http_fastopen{false};
117
  bool d_https_verify{true};
118
119
  [[nodiscard]] RemoteLoggerInterface::Result queueHttpData(const std::string& data);
120
  void senderThread();
121
  int sendBatch();
122
123
  LockGuarded<std::vector<TracesData>> d_traces;
124
  size_t d_failedSends{0};
125
126
  std::thread d_thread;
127
  std::shared_ptr<const Logr::Logger> d_logger{nullptr};
128
129
  std::atomic<uint64_t> d_framesSent{0};
130
  std::atomic<uint64_t> d_queueFullDrops{0};
131
  std::atomic<uint64_t> d_tooLargeCount{0};
132
  std::atomic<uint64_t> d_permanentFailures{0};
133
134
  bool d_exiting{false};
135
};
136
#else
137
class OTLPLogger : public RemoteLoggerInterface
138
{
139
  OTLPLogger(const OTLPLogger&) = delete;
140
  OTLPLogger(OTLPLogger&&) = delete;
141
  OTLPLogger& operator=(const OTLPLogger&) = delete;
142
  OTLPLogger& operator=(OTLPLogger&&) = delete;
143
144
public:
145
  [[nodiscard]] RemoteLoggerInterface::Result queueData([[maybe_unused]] const std::string& data) override
146
0
  {
147
0
    return RemoteLogger::Result::Queued;
148
0
  }
149
  RemoteLoggerInterface::Result queueData([[maybe_unused]] const TracesData& data)
150
0
  {
151
0
    return RemoteLogger::Result::Queued;
152
0
  }
153
};
154
#endif /* !defined(DISABLE_PROTOBUF) && defined(HAVE_LIBCURL) */