]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
feat(dnsdist): make withTraceSpan available globally
authorPieter Lexis <pieter.lexis@powerdns.com>
Wed, 18 Mar 2026 16:01:05 +0000 (17:01 +0100)
committerPieter Lexis <pieter.lexis@powerdns.com>
Mon, 1 Jun 2026 10:51:43 +0000 (12:51 +0200)
pdns/dnsdistdist/dnsdist-actions-factory.cc
pdns/dnsdistdist/dnsdist-lua-bindings-dnsquestion.cc
pdns/dnsdistdist/dnsdist-lua-bindings-opentelemetry.cc [new file with mode: 0644]
pdns/dnsdistdist/dnsdist-lua-bindings-opentelemetry.hh [new file with mode: 0644]
pdns/dnsdistdist/meson.build
regression-tests.dnsdist/test_OpenTelemetryTracing.py

index 7b46a2a9a8bd00586eae095ea75aad0000e0aa07..c13b0aecac1571b70aa57ba8c0373d0a1d3ade79 100644 (file)
@@ -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<std::string> rule = std::get<1>(ret)) {
             *ruleresult = *rule;
index 6ee4207b188a68b21685bfaa708c5de822ba9777..599f7247e63381ecc63057424d4ff936f9da237e 100644 (file)
@@ -400,29 +400,6 @@ void setupLuaBindingsDNSQuestion([[maybe_unused]] LuaContext& luaCtx)
 #endif
     });
 
-  luaCtx.registerFunction<void (DNSQuestion::*)(const std::string&, const std::function<void()>&)>(
-    "withTraceSpan",
-    [](const DNSQuestion& dnsQuestion, const std::string& name, const std::function<void()>& func) {
-#ifndef DISABLE_PROTOBUF
-      if (auto tracer = dnsQuestion.ids.getTracer(); tracer != nullptr) {
-        auto closer = tracer->openSpan(name);
-        func();
-        return;
-      }
-#endif
-      func();
-    });
-
-  luaCtx.registerFunction<void (DNSQuestion::*)(const std::string&, const std::string&)>(
-    "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<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;
diff --git a/pdns/dnsdistdist/dnsdist-lua-bindings-opentelemetry.cc b/pdns/dnsdistdist/dnsdist-lua-bindings-opentelemetry.cc
new file mode 100644 (file)
index 0000000..cbff8ed
--- /dev/null
@@ -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<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(RecursiveLockGuardedHolder<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
diff --git a/pdns/dnsdistdist/dnsdist-lua-bindings-opentelemetry.hh b/pdns/dnsdistdist/dnsdist-lua-bindings-opentelemetry.hh
new file mode 100644 (file)
index 0000000..f12ab30
--- /dev/null
@@ -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 <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(RecursiveLockGuardedHolder<LuaContext>&);
+void setupLuaTracing(RecursiveLockGuardedHolder<LuaContext>&, std::shared_ptr<Tracer>&);
+
+template <typename Func, typename... Args>
+auto runWithLuaTracing(std::shared_ptr<Tracer>& 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>(func), std::forward<Args>(args)...);
+}
+} // namespace pdns::trace::dnsdist
index 65b97b8d7ec306d1b8adc81b96d7d3117ee691d2..0db317a45e6762d50dbee8ce356699268eccbef4 100644 (file)
@@ -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',
index 5c1a2449cf8e5c2a324f047395d7ae8045a2469c..e5cb5ad2f61be53e56cf375391e49a8bee0f23e8 100644 (file)
@@ -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
               )