]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
feat(dnsdist): add getSpanID to DNSQuestion
authorPieter Lexis <pieter.lexis@powerdns.com>
Tue, 30 Sep 2025 16:07:14 +0000 (18:07 +0200)
committerPieter Lexis <pieter.lexis@powerdns.com>
Tue, 14 Oct 2025 18:34:58 +0000 (20:34 +0200)
pdns/dnsdistdist/dnsdist-lua-bindings-dnsquestion.cc
pdns/dnsdistdist/docs/reference/dq.rst
pdns/dnsdistdist/docs/reference/ottrace.rst

index 42501d8fc795a884895bc776ab99a295f0e4650c..c56b0c5256def7dff8efd778f532c1496d339204 100644 (file)
@@ -351,6 +351,20 @@ void setupLuaBindingsDNSQuestion([[maybe_unused]] LuaContext& luaCtx)
 #endif
     });
 
+  luaCtx.registerFunction<std::optional<std::string> (DNSQuestion::*)()>(
+    "getSpanID",
+    []([[maybe_unused]] const DNSQuestion& dnsQuestion) -> std::optional<std::string> {
+#ifdef DISABLE_PROTOBUF
+      return std::nullopt;
+#else
+      if (dnsQuestion.ids.tracingEnabled) {
+        auto spanID = dnsQuestion.ids.d_OTTracer->getLastSpanID();
+        return std::string(spanID.begin(), spanID.end());
+      }
+      return std::nullopt;
+#endif
+    });
+
   class AsynchronousObject
   {
   public:
index 10338e0ee901aab658c8eddde271d5e5bf163f31..b7a35c2301b2130e14e57c6e732788c729aac69f 100644 (file)
@@ -255,6 +255,14 @@ This state can be modified from the various hooks.
 
     :returns: A binary string containing the OpenTelemetry trace identifier
 
+  .. method:: DNSQuestion:getSpanID() -> string
+
+    .. versionadded:: 2.1.0
+
+    When :doc:`OpenTelemetry tracing <ottrace>` is enabled for this query, get the ID of the current Span. Otherwise, ``nil``. This is an opaque binary string.
+
+    :returns: A binary string containing the OpenTelemetry Span identifier
+
   .. method:: DNSQuestion:getTrailingData() -> string
 
     .. versionadded:: 1.4.0
index b2229acb7467194a04dd7dda5d2b12481f9fb48d..075c8d5fad58e45189194677c446b3c00890644e 100644 (file)
@@ -43,8 +43,8 @@ Example configuration
          type: RemoteLog
          logger_name: pblog
 
-Passing Trace ID to downstream servers
-======================================
+Passing Trace ID and Span ID to downstream servers
+==================================================
 
 When storing traces, it is beneficial to correlate traces of the same query through different applications.
 The `PowerDNS Recursor <https://doc.powerdns.com/recursor>`__ (since 5.3.0) supports the experimental `draft-edns-otel-trace-ids <https://github.com/PowerDNS/draft-edns-otel-trace-ids>`__ EDNS option to pass the trace identifier.
@@ -67,3 +67,24 @@ Combining all this, a :func:`LuaAction` can be used to add this EDNS option to t
            end
            return DNSAction.None
          end
+
+Optionally, the Span ID can also be added to the query.
+This value is retrieved with the :func:`getSpanID <DNSQuestion:getSpanID>` function and can be added to the query as follows:
+
+.. code-block:: yaml
+
+   - name: Add TraceID and SpanID to EDNS for backend
+     selector:
+       type: All
+     action:
+       type: Lua
+       function_code: |
+         return function (dq)
+           tid = dq:getTraceID()
+           sid = dq:getSpanID()
+           if (tid ~= nil and sid ~= nil) then
+             -- PowerDNS Recursor uses EDNS Option Code 65500.
+             dq:setEDNSOption(65500, "\000\000" .. tid .. sid)
+           end
+           return DNSAction.None
+         end