From: Pieter Lexis Date: Wed, 18 Mar 2026 16:01:05 +0000 (+0100) Subject: feat(dnsdist): make withTraceSpan available globally X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9fcc7a6f86f9aa66ddf75b3fc3cdadf072983dc6;p=thirdparty%2Fpdns.git feat(dnsdist): make withTraceSpan available globally --- diff --git a/pdns/dnsdistdist/dnsdist-actions-factory.cc b/pdns/dnsdistdist/dnsdist-actions-factory.cc index 7b46a2a9a8..c13b0aecac 100644 --- a/pdns/dnsdistdist/dnsdist-actions-factory.cc +++ b/pdns/dnsdistdist/dnsdist-actions-factory.cc @@ -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" @@ -532,8 +533,8 @@ public: 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 rule = std::get<1>(ret)) { *ruleresult = *rule; diff --git a/pdns/dnsdistdist/dnsdist-lua-bindings-dnsquestion.cc b/pdns/dnsdistdist/dnsdist-lua-bindings-dnsquestion.cc index 6ee4207b18..599f7247e6 100644 --- a/pdns/dnsdistdist/dnsdist-lua-bindings-dnsquestion.cc +++ b/pdns/dnsdistdist/dnsdist-lua-bindings-dnsquestion.cc @@ -400,29 +400,6 @@ void setupLuaBindingsDNSQuestion([[maybe_unused]] LuaContext& luaCtx) #endif }); - luaCtx.registerFunction&)>( - "withTraceSpan", - [](const DNSQuestion& dnsQuestion, const std::string& name, const std::function& func) { -#ifndef DISABLE_PROTOBUF - if (auto tracer = dnsQuestion.ids.getTracer(); tracer != nullptr) { - auto closer = tracer->openSpan(name); - func(); - return; - } -#endif - func(); - }); - - luaCtx.registerFunction( - "setSpanAttribute", - [](const DNSQuestion& dnsQuestion, const std::string& key, const std::string& value) { -#ifndef DISABLE_PROTOBUF - if (auto tracer = dnsQuestion.ids.getTracer(); tracer != nullptr) { - tracer->setSpanAttribute(tracer->getLastSpanID(), key, AnyValue{value}); - } -#endif - }); - class AsynchronousObject { public: @@ -770,29 +747,6 @@ void setupLuaBindingsDNSQuestion([[maybe_unused]] LuaContext& luaCtx) return dnsdist::suspendResponse(dnsResponse, asyncID, queryID, timeoutMs); }); - luaCtx.registerFunction&)>( - "withTraceSpan", - [](const DNSResponse& dnsResponse, const std::string& name, const std::function& func) { -#ifndef DISABLE_PROTOBUF - if (auto tracer = dnsResponse.ids.getTracer(); tracer != nullptr) { - auto closer = tracer->openSpan(name); - func(); - return; - } -#endif - func(); - }); - - luaCtx.registerFunction( - "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("changeName", [](DNSResponse& dnsResponse, const DNSName& newName) -> bool { if (!dnsdist::changeNameInDNSPacket(dnsResponse.getMutableData(), dnsResponse.ids.qname, newName)) { return false; diff --git a/pdns/dnsdistdist/dnsdist-lua-bindings-opentelemetry.cc b/pdns/dnsdistdist/dnsdist-lua-bindings-opentelemetry.cc new file mode 100644 index 0000000000..cbff8ed8c6 --- /dev/null +++ b/pdns/dnsdistdist/dnsdist-lua-bindings-opentelemetry.cc @@ -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(RecursiveLockGuardedHolder& luaCtx) +{ + luaCtx->writeFunction&)>( + "withTraceSpan", + []([[maybe_unused]] const std::string& name, const std::function& luaFunc) { + luaFunc(); + }); + + luaCtx->writeFunction( + "setSpanAttribute", + []([[maybe_unused]] const std::string& key, [[maybe_unused]] const std::string& value) { + return; + }); +}; + +void setupLuaTracing(RecursiveLockGuardedHolder& luaCtx, std::shared_ptr& tracer) +{ + if (tracer != nullptr) { + luaCtx->writeFunction&)>( + "withTraceSpan", + [&tracer](const std::string& name, const std::function& luaFunc) { +#ifndef DISABLE_PROTOBUF + if (tracer != nullptr) { + auto closer = tracer->openSpan(name); + luaFunc(); + return; + } +#endif + luaFunc(); + }); + + luaCtx->writeFunction( + "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 diff --git a/pdns/dnsdistdist/dnsdist-lua-bindings-opentelemetry.hh b/pdns/dnsdistdist/dnsdist-lua-bindings-opentelemetry.hh new file mode 100644 index 0000000000..f12ab30561 --- /dev/null +++ b/pdns/dnsdistdist/dnsdist-lua-bindings-opentelemetry.hh @@ -0,0 +1,48 @@ +/* + * 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 + +#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(RecursiveLockGuardedHolder&); +void setupLuaTracing(RecursiveLockGuardedHolder&, std::shared_ptr&); + +template +auto runWithLuaTracing(std::shared_ptr& tracer, Func&& func, Args&&... args) +{ + auto luaCtx = g_lua.lock(); + setupLuaTracing(luaCtx, tracer); + auto exitGuard = ::pdns::defer([&luaCtx] { + emptyLuaTracing(luaCtx); + }); + return std::invoke(std::forward(func), std::forward(args)...); +} +} // namespace pdns::trace::dnsdist diff --git a/pdns/dnsdistdist/meson.build b/pdns/dnsdistdist/meson.build index 65b97b8d7e..0db317a45e 100644 --- a/pdns/dnsdistdist/meson.build +++ b/pdns/dnsdistdist/meson.build @@ -155,6 +155,7 @@ common_sources += files( src_dir / 'dnsdist-lua-bindings-dnsquestion.cc', src_dir / 'dnsdist-lua-bindings-kvs.cc', src_dir / 'dnsdist-lua-bindings-network.cc', + src_dir / 'dnsdist-lua-bindings-opentelemetry.cc', src_dir / 'dnsdist-lua-bindings-packetcache.cc', src_dir / 'dnsdist-lua-bindings-protobuf.cc', src_dir / 'dnsdist-lua-bindings-rings.cc', diff --git a/regression-tests.dnsdist/test_OpenTelemetryTracing.py b/regression-tests.dnsdist/test_OpenTelemetryTracing.py index 5c1a2449cf..e5cb5ad2f6 100644 --- a/regression-tests.dnsdist/test_OpenTelemetryTracing.py +++ b/regression-tests.dnsdist/test_OpenTelemetryTracing.py @@ -1016,10 +1016,10 @@ query_rules: type: Lua function_code: | return function (dq) - dq:withTraceSpan("my-span", + withTraceSpan("my-span", function () - dq:setSpanAttribute("my-key-from-lua", "my-value-from-lua") - dq:withTraceSpan("my-second-span", + setSpanAttribute("my-key-from-lua", "my-value-from-lua") + withTraceSpan("my-second-span", function() end )