Skip to content

Commit 30067b2

Browse files
committed
Count incoming messages per type and endpoint
1 parent ee0d9d1 commit 30067b2

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/remote/endpoint.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55

66
#include "remote/i2-remote.hpp"
77
#include "remote/endpoint-ti.hpp"
8+
#include "base/atomic.hpp"
89
#include "base/ringbuffer.hpp"
10+
#include <cstdint>
911
#include <set>
12+
#include <shared_mutex>
13+
#include <unordered_map>
1014

1115
namespace icinga
1216
{
1317

18+
class ApiFunction;
1419
class JsonRpcConnection;
1520
class Zone;
1621

@@ -21,6 +26,8 @@ class Zone;
2126
*/
2227
class Endpoint final : public ObjectImpl<Endpoint>
2328
{
29+
friend JsonRpcConnection;
30+
2431
public:
2532
DECLARE_OBJECT(Endpoint);
2633
DECLARE_OBJECTNAME(Endpoint);
@@ -61,6 +68,9 @@ class Endpoint final : public ObjectImpl<Endpoint>
6168
mutable RingBuffer m_MessagesReceived{60};
6269
mutable RingBuffer m_BytesSent{60};
6370
mutable RingBuffer m_BytesReceived{60};
71+
72+
mutable std::shared_mutex m_MessageCountersMutex;
73+
std::unordered_map<intrusive_ptr<ApiFunction>, Atomic<uint_fast64_t>> m_MessageCounters;
6474
};
6575

6676
}

lib/remote/jsonrpcconnection.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,25 @@ void JsonRpcConnection::MessageHandler(const Dictionary::Ptr& message)
351351
Log(LogNotice, "JsonRpcConnection")
352352
<< "Call to non-existent function '" << method << "' from endpoint '" << m_Identity << "'.";
353353
} else {
354+
if (m_Endpoint) {
355+
std::shared_lock sLock (m_Endpoint->m_MessageCountersMutex);
356+
auto& mc (m_Endpoint->m_MessageCounters);
357+
auto it (mc.find(afunc.get())); // Lookup by pointer is faster than by string
358+
359+
if (it == mc.end()) {
360+
sLock.unlock();
361+
std::unique_lock uLock (m_Endpoint->m_MessageCountersMutex);
362+
363+
if (it = mc.find(afunc.get()); it == mc.end()) {
364+
mc.emplace(afunc.get(), 1);
365+
} else {
366+
it->second.fetch_add(1, std::memory_order_relaxed);
367+
}
368+
} else {
369+
it->second.fetch_add(1, std::memory_order_relaxed);
370+
}
371+
}
372+
354373
Dictionary::Ptr params = message->Get("params");
355374
if (params)
356375
resultMessage->Set("result", afunc->Invoke(origin, params));

0 commit comments

Comments
 (0)