From: Otto Date: Wed, 15 Sep 2021 09:02:20 +0000 (+0200) Subject: Allow for a custom eventtype, with a user-specified name X-Git-Tag: dnsdist-1.7.0-alpha2~9^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f5b1d3bb7c9257693150c3dc0cc0931f571398f8;p=thirdparty%2Fpdns.git Allow for a custom eventtype, with a user-specified name --- diff --git a/contrib/ProtobufLogger.py b/contrib/ProtobufLogger.py index 0b6a1a52b5..36b361bfda 100644 --- a/contrib/ProtobufLogger.py +++ b/contrib/ProtobufLogger.py @@ -107,7 +107,10 @@ class PDNSPBConnHandler(object): if message.trace: print("- Event Trace:") for event in message.trace: - ev = self.getEventAsString(event.event) + '(' + str(event.ts) + ev = self.getEventAsString(event.event) + if event.event == dnsmessage_pb2.PBDNSMessage.CustomEvent and event.HasField('custom'): + ev += ":" + event.custom + ev += '(' + str(event.ts) valstr = '' if event.HasField('boolVal'): valstr = str(event.boolVal) diff --git a/pdns/dnsmessage.proto b/pdns/dnsmessage.proto index a9fd1f0730..f37a8442bf 100644 --- a/pdns/dnsmessage.proto +++ b/pdns/dnsmessage.proto @@ -129,6 +129,7 @@ message PBDNSMessage { // The well known EventTrace event numbers enum EventType { + CustomEvent = 0; // A custom event RecRecv = 1; // A request was received by the recursor process PCacheCheck = 2; // A packet cache check was initiated or completed; value: bool cacheHit SyncRes = 3; // Syncres main function has started or completed; value: int rcode @@ -152,6 +153,7 @@ message PBDNSMessage { optional int64 intVal = 5; optional string stringVal = 6; optional bytes bytesVal = 7; + optional string custom = 8; // The name of the event for custom events } repeated Event trace = 23; } diff --git a/pdns/protozero.hh b/pdns/protozero.hh index 665565322f..c8536dc6d6 100644 --- a/pdns/protozero.hh +++ b/pdns/protozero.hh @@ -35,7 +35,7 @@ namespace pdns { enum class MetaValueField : protozero::pbf_tag_type { stringVal = 1, intVal = 2 }; enum class MetaField : protozero::pbf_tag_type { key = 1, value = 2 }; - enum class Event : protozero::pbf_tag_type { ts = 1, event = 2, start = 3, boolVal = 4, intVal = 5, stringVal = 6, bytesVal = 7 }; + enum class Event : protozero::pbf_tag_type { ts = 1, event = 2, start = 3, boolVal = 4, intVal = 5, stringVal = 6, bytesVal = 7, custom = 8 }; enum class MessageType : int32_t { DNSQueryType = 1, DNSResponseType = 2, DNSOutgoingQueryType = 3, DNSIncomingResponseType = 4 }; enum class Field : protozero::pbf_tag_type { type = 1, messageId = 2, serverIdentity = 3, socketFamily = 4, socketProtocol = 5, from = 6, to = 7, inBytes = 8, timeSec = 9, timeUsec = 10, id = 11, question = 12, response = 13, originalRequestorSubnet = 14, requestorId = 15, initialRequestId = 16, deviceId = 17, newlyObservedDomain = 18, deviceName = 19, fromPort = 20, toPort = 21, meta = 22, trace = 23 }; enum class QuestionField : protozero::pbf_tag_type { qName = 1, qType = 2, qClass = 3 }; diff --git a/pdns/recursordist/rec-eventtrace.cc b/pdns/recursordist/rec-eventtrace.cc index c2d04c46ab..040e1629b3 100644 --- a/pdns/recursordist/rec-eventtrace.cc +++ b/pdns/recursordist/rec-eventtrace.cc @@ -27,6 +27,7 @@ } const std::unordered_map RecEventTrace::s_eventNames = { + NameEntry(CustomEvent), NameEntry(RecRecv), NameEntry(PCacheCheck), NameEntry(SyncRes), diff --git a/pdns/recursordist/rec-eventtrace.hh b/pdns/recursordist/rec-eventtrace.hh index fed756dc66..701cad494f 100644 --- a/pdns/recursordist/rec-eventtrace.hh +++ b/pdns/recursordist/rec-eventtrace.hh @@ -37,6 +37,7 @@ public: enum EventType : uint8_t { // Don't forget to add a new entry to the table in the .cc file! + CustomEvent = 0, RecRecv = 1, PCacheCheck = 2, SyncRes = 3, @@ -120,7 +121,12 @@ public: d_value(v), d_ts(ts), d_event(e), d_start(start) { } + Entry(Value_t& v, const std::string& custom, bool start, int64_t ts) : + d_value(v), d_custom(custom), d_ts(ts), d_event(CustomEvent), d_start(start) + { + } Value_t d_value; + std::string d_custom; int64_t d_ts; EventType d_event; bool d_start; @@ -131,7 +137,12 @@ public: if (!v.empty()) { v = "," + v; } - return RecEventTrace::toString(d_event) + "(" + std::to_string(d_ts) + v + (d_start ? ")" : ",done)"); + std::string name = RecEventTrace::toString(d_event); + if (d_event == EventType::CustomEvent) { + name += ":" + d_custom; + } + + return name + "(" + std::to_string(d_ts) + v + (d_start ? ")" : ",done)"); } }; @@ -146,7 +157,8 @@ public: return d_status == Enabled; } - void add(EventType e, Value_t v, bool start) + template + void add(E e, Value_t v, bool start) { assert(d_status != Invalid); if (d_status == Disabled) { @@ -159,24 +171,27 @@ public: d_events.emplace_back(v, e, start, stamp); } - void add(EventType e) + template + void add(E e) { add(e, Value_t(std::nullopt), true); } // We store uint32 in an int64_t - void add(EventType e, uint32_t v, bool start) + template + void add(E e, uint32_t v, bool start) { add(e, static_cast(v), start); } // We store int32 in an int64_t - void add(EventType e, int32_t v, bool start) + template + void add(E e, int32_t v, bool start) { add(e, static_cast(v), start); } - template - void add(EventType e, T v, bool start) + template + void add(E e, T v, bool start) { add(e, Value_t(v), start); } diff --git a/pdns/recursordist/rec-protozero.cc b/pdns/recursordist/rec-protozero.cc index 7a153ea74c..375d80b203 100644 --- a/pdns/recursordist/rec-protozero.cc +++ b/pdns/recursordist/rec-protozero.cc @@ -135,5 +135,8 @@ void pdns::ProtoZero::RecMessage::addEvents(const RecEventTrace& trace) const PacketBuffer& p = std::get(v); pbf_trace.add_bytes(static_cast(Event::bytesVal), reinterpret_cast(p.data()), p.size()); } + if (!t.d_custom.empty()) { + pbf_trace.add_string(static_cast(Event::custom), t.d_custom); + } } }