#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"
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;
#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:
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;
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
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',
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
)