Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pdns/dnsdistdist/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ dnsdist_SOURCES = \
dnsdist-lua-bindings-dnsquestion.cc \
dnsdist-lua-bindings-kvs.cc \
dnsdist-lua-bindings-network.cc \
dnsdist-lua-bindings-opentelemetry.cc dnsdist-lua-bindings-opentelemetry.hh \
dnsdist-lua-bindings-packetcache.cc \
dnsdist-lua-bindings-protobuf.cc \
dnsdist-lua-bindings-rings.cc \
Expand Down
5 changes: 3 additions & 2 deletions pdns/dnsdistdist/dnsdist-actions-factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "dnsdist-rule-chains.hh"
#include "dnsdist-self-answers.hh"
#include "dnsdist-snmp.hh"
#include "dnsdist-lua-bindings-opentelemetry.hh"

#include "dnstap.hh"
#include "dnswriter.hh"
Expand Down Expand Up @@ -533,8 +534,8 @@ class LuaAction : public DNSAction
try {
DNSAction::Action result{};
{
auto lock = g_lua.lock();
auto ret = d_func(dnsquestion);
auto tracer = dnsquestion->ids.getTracer();
auto ret = pdns::trace::dnsdist::runWithLuaTracing(tracer, d_func, dnsquestion);
if (ruleresult != nullptr) {
if (std::optional<std::string> rule = std::get<1>(ret)) {
*ruleresult = *rule;
Expand Down
26 changes: 26 additions & 0 deletions pdns/dnsdistdist/dnsdist-configuration-yaml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include <vector>

#include "dnsdist-configuration-yaml.hh"
#include "dnsdist-configuration.hh"
#include "logging.hh"
#include "logr.hh"

#if defined(HAVE_YAML_CONFIGURATION)
#include "base64.hh"
Expand Down Expand Up @@ -1024,6 +1027,29 @@
}
config.d_structuredLoggingUseServerID = settings.structured.set_instance_from_server_id;
});

if (!settings.open_telemetry_tracing.internal_tracing.empty() && !settings.open_telemetry_tracing.enabled) {
VERBOSESLOG(infolog("Internal OpenTelemetry tracing requested, but OpenTelemetry is disabled"),
context.logger->info(Logr::Info, "Internal OpenTelemetry tracing requested, but OpenTelemetry is disabled"));
}
else {
for (const auto& internal_trace_config : settings.open_telemetry_tracing.internal_tracing) {
dnsdist::configuration::updateRuntimeConfiguration([context, internal_trace_config](dnsdist::configuration::RuntimeConfiguration& config) {
if (internal_trace_config.kind == "maintenance") {
std::vector<std::shared_ptr<RemoteLoggerInterface>> loggers;
for (const auto& logger_name : internal_trace_config.remote_loggers) {
auto logger = dnsdist::configuration::yaml::getRegisteredTypeByName<RemoteLoggerInterface>(std::string(logger_name));
if (!logger && !(dnsdist::configuration::yaml::s_inClientMode || dnsdist::configuration::yaml::s_inConfigCheckMode)) {
throw std::runtime_error("Unable to find the protobuf logger named '" + std::string(logger_name) + "'");
}
loggers.push_back(std::move(logger));
}
config.d_maintenanceRemoteLoggers = std::move(loggers);
config.d_opentelemetryMaintenanceInterval = internal_trace_config.sample_rate == 0 ? 60 : internal_trace_config.sample_rate;
}
});
}
}
}

static void handleConsoleConfiguration(const dnsdist::rust::settings::ConsoleConfiguration& consoleConf)
Expand Down
6 changes: 6 additions & 0 deletions pdns/dnsdistdist/dnsdist-configuration.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>

#include "config.h"
#include "credentials.hh"
Expand All @@ -35,6 +36,7 @@
#include "dnsdist-rule-chains.hh"
#include "dnsdist-server-pool.hh"
#include "iputils.hh"
#include "remote_logger.hh"

class ServerPolicy;
struct ServerPool;
Expand Down Expand Up @@ -135,6 +137,9 @@ struct RuntimeConfiguration
std::shared_ptr<const CredentialsHolder> d_webAPIKey;
std::optional<std::unordered_map<std::string, std::string>> d_webCustomHeaders;
std::shared_ptr<ServerPolicy> d_lbPolicy;
#ifndef DISABLE_PROTOBUF
std::vector<std::shared_ptr<RemoteLoggerInterface>> d_maintenanceRemoteLoggers;
#endif
NetmaskGroup d_ACL;
NetmaskGroup d_proxyProtocolACL;
NetmaskGroup d_consoleACL;
Expand All @@ -150,6 +155,7 @@ struct RuntimeConfiguration
size_t d_maxTCPQueriesPerConn{0};
size_t d_maxTCPConnectionDuration{0};
size_t d_proxyProtocolMaximumSize{512};
size_t d_opentelemetryMaintenanceInterval{0}; // Sample interval for the maintenance function, 0 means disabled
uint32_t d_staleCacheEntriesTTL{0};
uint32_t d_secPollInterval{3600};
uint32_t d_consoleOutputMsgMaxSize{10000000};
Expand Down
1 change: 1 addition & 0 deletions pdns/dnsdistdist/dnsdist-idstate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ InternalQueryState::~InternalQueryState()
pdns::ProtoZero::Message minimalMsg{pbBuf};
minimalMsg.setType(pdns::ProtoZero::Message::MessageType::DNSQueryType);
minimalMsg.setOpenTelemetryData(OTData);
minimalMsg.setOpenTelemetryTraceID(d_OTTracer->getTraceID());
for (auto const& msg_logger : ottraceLoggers) {
msg_logger->queueData(pbBuf);
}
Expand Down
1 change: 1 addition & 0 deletions pdns/dnsdistdist/dnsdist-idstate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ struct InternalQueryState
}
// OpenTelemetry tracing is enabled, but we don't have a tracer yet
d_OTTracer = pdns::trace::dnsdist::Tracer::getTracer();
d_OTTracer->setScopeSpanName("dnsdist/queryFromFrontend");
return d_OTTracer;
}
return nullptr;
Expand Down
27 changes: 27 additions & 0 deletions pdns/dnsdistdist/dnsdist-lua-bindings-dnsquestion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "dnsdist-edns.hh"
#include "dnsdist-opentelemetry.hh"
#include "dnsdist.hh"
#include "dnsdist-async.hh"
#include "dnsdist-dnsparser.hh"
Expand All @@ -30,7 +31,10 @@
#include "dnsdist-snmp.hh"
#include "dnsparser.hh"

#include "protozero-trace.hh"
#include "protozero.hh"
#include <functional>
#include <optional>
#include <string>

static void addMetaKeyAndValuesToProtobufContent([[maybe_unused]] DNSQuestion& dnsQuestion, [[maybe_unused]] const std::string& key, [[maybe_unused]] const LuaArray<boost::variant<int64_t, std::string>>& values)
Expand Down Expand Up @@ -716,6 +720,29 @@ void setupLuaBindingsDNSQuestion([[maybe_unused]] LuaContext& luaCtx)
return dnsdist::suspendResponse(dnsResponse, asyncID, queryID, timeoutMs);
});

luaCtx.registerFunction<void (DNSResponse::*)(const std::string&, const std::function<void()>&)>(
"withTraceSpan",
[](const DNSResponse& dnsResponse, const std::string& name, const std::function<void()>& func) {
#ifndef DISABLE_PROTOBUF
if (auto tracer = dnsResponse.ids.getTracer(); tracer != nullptr) {
auto closer = tracer->openSpan(name);
func();
return;
}
#endif
func();
});

luaCtx.registerFunction<void (DNSResponse::*)(const std::string& key, const std::string& value)>(
"setSpanAttribute",
[](const DNSResponse& dnsResponse, const std::string& key, const std::string& value) {
#ifndef DISABLE_PROTOBUF
if (auto tracer = dnsResponse.ids.getTracer(); tracer != nullptr) {
tracer->setSpanAttribute(tracer->getLastSpanID(), key, AnyValue{value});
}
#endif
});

luaCtx.registerFunction<bool (DNSResponse::*)(const DNSName& newName)>("changeName", [](DNSResponse& dnsResponse, const DNSName& newName) -> bool {
if (!dnsdist::changeNameInDNSPacket(dnsResponse.getMutableData(), dnsResponse.ids.qname, newName)) {
return false;
Expand Down
71 changes: 71 additions & 0 deletions pdns/dnsdistdist/dnsdist-lua-bindings-opentelemetry.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "dnsdist-lua-bindings-opentelemetry.hh"

namespace pdns::trace::dnsdist
{
void emptyLuaTracing(LuaContext& luaCtx)
{
luaCtx.writeFunction<void(const std::string&, const std::function<void()>&)>(
"withTraceSpan",
[]([[maybe_unused]] const std::string& name, const std::function<void()>& luaFunc) {
luaFunc();
});

luaCtx.writeFunction<void(const std::string&, const std::string&)>(
"setSpanAttribute",
[]([[maybe_unused]] const std::string& key, [[maybe_unused]] const std::string& value) {
return;
});
};

void setupLuaTracing(LuaContext& luaCtx, std::shared_ptr<Tracer>& tracer)
{
if (tracer != nullptr) {
luaCtx.writeFunction<void(const std::string&, const std::function<void()>&)>(
"withTraceSpan",
[&tracer](const std::string& name, const std::function<void()>& luaFunc) {
#ifndef DISABLE_PROTOBUF
if (tracer != nullptr) {
auto closer = tracer->openSpan(name);
luaFunc();
return;
}
#endif
luaFunc();
});

luaCtx.writeFunction<void(const std::string&, const std::string&)>(
"setSpanAttribute",
[&tracer](const std::string& key, const std::string& value) {
#ifndef DISABLE_PROTOBUF
if (tracer != nullptr) {
tracer->setSpanAttribute(tracer->getLastSpanID(), key, AnyValue{value});
}
#endif
return;
});
}
}

} // namespace pdns::trace::dnsdist
55 changes: 55 additions & 0 deletions pdns/dnsdistdist/dnsdist-lua-bindings-opentelemetry.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once

#include <memory>

#include "ext/luawrapper/include/LuaContext.hpp"

#include "dnsdist-lua.hh"
#include "dnsdist-opentelemetry.hh"
#include "lock.hh"
#include "misc.hh"

namespace pdns::trace::dnsdist
{
void emptyLuaTracing(LuaContext&);
void setupLuaTracing(LuaContext&, std::shared_ptr<Tracer>&);

template <typename Func, typename... Args>
auto runWithLuaTracing(LuaContext& luaCtx, std::shared_ptr<Tracer>& tracer, Func&& func, Args&&... args)
{
setupLuaTracing(luaCtx, tracer);
auto exitGuard = ::pdns::defer([&luaCtx] {
emptyLuaTracing(luaCtx);
});
return std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
}

template <typename Func, typename... Args>
auto runWithLuaTracing(std::shared_ptr<Tracer>& tracer, Func&& func, Args&&... args)
{
auto luaCtx = g_lua.lock();
return runWithLuaTracing(*luaCtx, tracer, func, args...);
}

} // namespace pdns::trace::dnsdist
36 changes: 36 additions & 0 deletions pdns/dnsdistdist/dnsdist-lua-configuration-items.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <optional>
#include <stdexcept>
#include <string>
#include <vector>

#include "config.h"
#include "dnsdist-configuration.hh"
#include "dnsdist-lua.hh"
#include "dolog.hh"
#include "ext/luawrapper/include/LuaContext.hpp"

namespace dnsdist::lua
{
Expand Down Expand Up @@ -175,6 +178,38 @@ static const std::map<std::string, DoubleImmutableConfigurationItems> s_doubleIm
};
// clang-format on

static void setupOpenTelemetryConfigurationItems(LuaContext& luaCtx)
{
luaCtx.writeFunction("setOpenTelemetryInternalTrace", []([[maybe_unused]] const std::string& kind, [[maybe_unused]] LuaAssociativeTable<std::shared_ptr<RemoteLoggerInterface>> remote_loggers, [[maybe_unused]] size_t sample_interval = 60) {
#ifndef DISABLE_PROTOBUF
static const std::vector<std::string> validKinds{
"maintenance",
};
if (auto it = std::find(validKinds.cbegin(), validKinds.cend(), kind); it == validKinds.cend()) {
throw std::runtime_error(kind + " is not a valid Trace kind for setOpenTelemetryInternalTrace");
}

setLuaSideEffect();
dnsdist::configuration::updateRuntimeConfiguration([kind, remote_loggers, sample_interval](dnsdist::configuration::RuntimeConfiguration& config) {
std::vector<std::shared_ptr<RemoteLoggerInterface>> loggers;
for (auto& remote_logger : remote_loggers) {
if (remote_logger.second != nullptr) {
// avoids potentially-evaluated-expression warning with clang.
RemoteLoggerInterface& remoteLoggerRef = *remote_logger.second;
if (typeid(remoteLoggerRef) != typeid(RemoteLogger)) {
// We could let the user do what he wants, but wrapping PowerDNS Protobuf inside a FrameStream tagged as dnstap is logically wrong.
throw std::runtime_error(std::string("setOpenTelemetryInternalTrace only takes RemoteLogger."));
}
loggers.push_back(remote_logger.second);
}
}
config.d_maintenanceRemoteLoggers = std::move(loggers);
config.d_opentelemetryMaintenanceInterval = sample_interval == 0 ? 60 : sample_interval;
});
#endif
});
}

void setupConfigurationItems(LuaContext& luaCtx)
{
for (const auto& item : s_booleanConfigItems) {
Expand Down Expand Up @@ -257,5 +292,6 @@ void setupConfigurationItems(LuaContext& luaCtx)
setLuaSideEffect();
});
}
setupOpenTelemetryConfigurationItems(luaCtx);
}
}
Loading
Loading