]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
feat(dnsdist): add hostname and version to scopespan
authorPieter Lexis <pieter.lexis@powerdns.com>
Tue, 14 Oct 2025 11:09:39 +0000 (13:09 +0200)
committerPieter Lexis <pieter.lexis@powerdns.com>
Tue, 14 Oct 2025 18:34:58 +0000 (20:34 +0200)
pdns/dnsdistdist/dnsdist-opentelemetry.cc
pdns/dnsdistdist/dnsdist-opentelemetry.hh
pdns/dnsdistdist/test-dnsdist-opentelemetry_cc.cc

index 822f385bea0116f6afebccb2143a7d9904118e59..1cbb4ba1c33bd9add11d5df6329d04972cafb3ce 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include "dnsdist-opentelemetry.hh"
+#include "misc.hh"
 
 #include <vector>
 
 namespace pdns::trace::dnsdist
 {
 
+#ifndef DISABLE_PROTOBUF
+static const KeyValue hostnameAttr{.key = "hostname", .value = {getHostname().value_or("")}};
+#endif
+
 TracesData Tracer::getTracesData()
 {
 #ifdef DISABLE_PROTOBUF
@@ -38,17 +43,18 @@ TracesData Tracer::getTracesData()
 #else
   auto otTrace = pdns::trace::TracesData{
     .resource_spans = {
-      pdns::trace::ResourceSpans{
-        .resource = {
-          .attributes = std::vector<pdns::trace::KeyValue>{
-            pdns::trace::KeyValue{
-              "service.name", pdns::trace::AnyValue{"dnsdist"}},
-          }},
-        .scope_spans = std::vector<pdns::trace::ScopeSpans>{{.scope = {
-                                                               .name = "queryFromFrontend",
-                                                               .attributes = {d_attributes.begin(), d_attributes.end()},
-                                                             },
-                                                             .spans = {}}}}}};
+      {.resource = {
+         .attributes = {
+           {"service.name", {"dnsdist"}},
+         }},
+       .scope_spans = {{.scope = {
+                          .name = "dnsdist/queryFromFrontend",
+                          .version = PACKAGE_VERSION,
+                          .attributes = {d_attributes.begin(), d_attributes.end()},
+                        },
+                        .spans = {}}}}}};
+
+  otTrace.resource_spans.at(0).scope_spans.at(0).scope.attributes.push_back(hostnameAttr);
 
   {
     auto lockedPre = d_preActivationSpans.read_only_lock();
index bdb17a4d3c2e6cedeac1e896bceb1f0ddd1aa66b..e4480f6a3e0b6081c39f80fe95a60875fec779d9 100644 (file)
@@ -303,7 +303,7 @@ private:
    */
   LockGuarded<std::vector<Span>> d_postActivationSpans;
   /**
-   * @brief All attributes related to this Trace
+   * @brief All attributes related to this Trace (added to the ScopeSpan)
    */
   std::vector<pdns::trace::KeyValue> d_attributes;
 
index 49364631be6ad666b75bba925c06c1ec7b458922..32d03a2cb8a8c38d053117bf476795c6d2bfbfd8 100644 (file)
@@ -205,7 +205,9 @@ BOOST_AUTO_TEST_CASE(attributes)
   BOOST_CHECK_EQUAL(trace.resource_spans.at(0).resource.attributes.size(), 1);
   BOOST_CHECK_EQUAL(trace.resource_spans.at(0).resource.attributes.at(0).key, "service.name");
 
-  BOOST_ASSERT(trace.resource_spans.at(0).scope_spans.at(0).scope.attributes.size() == 0);
+  // Check if we have a hostname
+  BOOST_CHECK_EQUAL(trace.resource_spans.at(0).scope_spans.at(0).scope.attributes.size(), 1U);
+  BOOST_CHECK_EQUAL(trace.resource_spans.at(0).scope_spans.at(0).scope.attributes.at(0).key, "hostname");
 
   // Now activate and add 2 attributes
   tracer->activate();
@@ -215,7 +217,8 @@ BOOST_AUTO_TEST_CASE(attributes)
   trace = tracer->getTracesData();
 
   BOOST_ASSERT(trace.resource_spans.at(0).resource.attributes.size() == 1);
-  BOOST_ASSERT(trace.resource_spans.at(0).scope_spans.at(0).scope.attributes.size() == 2);
+  // hostname plus the two we set
+  BOOST_ASSERT(trace.resource_spans.at(0).scope_spans.at(0).scope.attributes.size() == 3);
 
   BOOST_CHECK_EQUAL(trace.resource_spans.at(0).scope_spans.at(0).scope.attributes.at(0).key, "foo");
   BOOST_CHECK_EQUAL(trace.resource_spans.at(0).scope_spans.at(0).scope.attributes.at(0).value, AnyValue{"bar"});
@@ -238,12 +241,12 @@ BOOST_AUTO_TEST_CASE(getOTProtobuf)
 {
   auto tracer = pdns::trace::dnsdist::Tracer::getTracer();
   auto data = tracer->getOTProtobuf();
-  BOOST_TEST(data.size() == 54U);
+  BOOST_TEST(data.size() >= 100U);
 
   tracer->activate();
   tracer->setTraceAttribute("foo", AnyValue{"bar"});
   data = tracer->getOTProtobuf();
-  BOOST_TEST(data.size() == 68U);
+  BOOST_TEST(data.size() >= 110U);
 }
 
 BOOST_AUTO_TEST_SUITE_END()