File: rec-eventtrace.cc

package info (click to toggle)
pdns-recursor 5.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 11,116 kB
  • sloc: cpp: 109,672; javascript: 20,651; python: 5,657; sh: 5,114; makefile: 782; ansic: 582; xml: 37
file content (131 lines) | stat: -rw-r--r-- 4,742 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * 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 "rec-eventtrace.hh"

#define NameEntry(M) {M, #M}

const std::unordered_map<RecEventTrace::EventType, std::string> RecEventTrace::s_eventNames = {
  NameEntry(CustomEvent),
  NameEntry(ReqRecv),
  NameEntry(PCacheCheck),
  NameEntry(AnswerSent),
  NameEntry(SyncRes),
  NameEntry(LuaGetTag),
  NameEntry(LuaGetTagFFI),
  NameEntry(LuaIPFilter),
  NameEntry(LuaPreRPZ),
  NameEntry(LuaPreResolve),
  NameEntry(LuaPreOutQuery),
  NameEntry(LuaPostResolve),
  NameEntry(LuaNoData),
  NameEntry(LuaNXDomain),
  NameEntry(LuaPostResolveFFI),
  NameEntry(AuthRequest),
};

using namespace pdns::trace;

static void addValue(const RecEventTrace::Entry& event, Span& work, bool start)
{
  if (std::holds_alternative<std::nullopt_t>(event.d_value)) {
    return;
  }
  string key = start ? "arg" : "result";
  if (std::holds_alternative<bool>(event.d_value)) {
    work.attributes.emplace_back(KeyValue{std::move(key), {std::get<bool>(event.d_value)}});
  }
  else if (std::holds_alternative<int64_t>(event.d_value)) {
    work.attributes.emplace_back(KeyValue{std::move(key), {std::get<int64_t>(event.d_value)}});
  }
  else if (std::holds_alternative<std::string>(event.d_value)) {
    work.attributes.emplace_back(KeyValue{std::move(key), {std::get<std::string>(event.d_value)}});
  }
  else {
    work.attributes.emplace_back(KeyValue{std::move(key), {RecEventTrace::toString(event.d_value)}});
  }
}

// The event trace uses start-stop records which need to be mapped to OpenTelemetry Spans, which is a
// list of spans. Spans can refer to other spans as their parent.
std::vector<pdns::trace::Span> RecEventTrace::convertToOT(const InitialSpanInfo& span) const
{
  timespec realtime{};
  clock_gettime(CLOCK_REALTIME, &realtime);
  timespec monotime{};
  clock_gettime(CLOCK_MONOTONIC, &monotime);
  auto diff = (1000000000ULL * realtime.tv_sec) + realtime.tv_nsec - ((1000000000ULL * monotime.tv_sec) + monotime.tv_nsec);
  diff += d_base;

  std::vector<pdns::trace::Span> ret;
  ret.reserve((d_events.size() / 2) + 1);

  // The parent of all Spans
  ret.emplace_back(Span{
    .trace_id = span.trace_id,
    .span_id = span.span_id,
    .parent_span_id = span.parent_span_id,
    .name = "RecRequest",
    .start_time_unix_nano = span.start_time_unix_nano,
    .end_time_unix_nano = timestamp(),
  });

  std::vector<SpanID> spanIDs; // mapping of span index in ret vector to SpanID
  std::map<size_t, size_t> ids; // mapping from event record index to index in ret vector (Spans)

  size_t index = 0;
  for (const auto& event : d_events) {
    if (event.d_start) {
      // It's an open event
      Span work{
        .trace_id = span.trace_id,
        .name = RecEventTrace::toString(event.d_event),
        .start_time_unix_nano = static_cast<uint64_t>(event.d_ts + diff),
        .end_time_unix_nano = static_cast<uint64_t>(event.d_ts + diff), // will be updated when we process the close event
      };
      if (event.d_parent == 0 || event.d_parent >= spanIDs.size()) {
        // Use the given parent
        work.parent_span_id = span.span_id;
      }
      else {
        // The parent is coming from the events we already processed
        work.parent_span_id = spanIDs.at(event.d_parent);
      }
      // Assign a span id.
      random(work.span_id);
      addValue(event, work, true);
      spanIDs.emplace_back(work.span_id);
      ret.emplace_back(work);
      ids[index] = ret.size() - 1;
    }
    else {
      // It's a close event
      if (const auto match = ids.find(event.d_matching); match != ids.end()) {
        auto& work = ret.at(match->second);
        addValue(event, work, false);
        work.end_time_unix_nano = static_cast<uint64_t>(event.d_ts + diff);
        spanIDs.emplace_back(work.span_id);
      }
    }
    ++index;
  }
  return ret;
}