Skip to content

Commit 827f85c

Browse files
authored
Merge pull request #10387 from Icinga/cnt-msg
Introduce Endpoint#messages_received_per_type
2 parents 8d15e7f + 98d0975 commit 827f85c

File tree

6 files changed

+54
-4
lines changed

6 files changed

+54
-4
lines changed

lib/remote/apifunction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
using namespace icinga;
77

8-
ApiFunction::ApiFunction(Callback function)
9-
: m_Callback(std::move(function))
8+
ApiFunction::ApiFunction(const char* name, Callback function)
9+
: m_Name(name), m_Callback(std::move(function))
1010
{ }
1111

1212
Value ApiFunction::Invoke(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& arguments)

lib/remote/apifunction.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ class ApiFunction final : public Object
2525

2626
typedef std::function<Value(const MessageOrigin::Ptr& origin, const Dictionary::Ptr&)> Callback;
2727

28-
ApiFunction(Callback function);
28+
ApiFunction(const char* name, Callback function);
29+
30+
const char* GetName() const noexcept
31+
{
32+
return m_Name;
33+
}
2934

3035
Value Invoke(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& arguments);
3136

3237
static ApiFunction::Ptr GetByName(const String& name);
3338
static void Register(const String& name, const ApiFunction::Ptr& function);
3439

3540
private:
41+
const char* m_Name;
3642
Callback m_Callback;
3743
};
3844

@@ -49,7 +55,7 @@ class ApiFunctionRegistry : public Registry<ApiFunctionRegistry, ApiFunction::Pt
4955

5056
#define REGISTER_APIFUNCTION(name, ns, callback) \
5157
INITIALIZE_ONCE([]() { \
52-
ApiFunction::Ptr func = new ApiFunction(callback); \
58+
ApiFunction::Ptr func = new ApiFunction(#ns "::" #name, callback); \
5359
ApiFunctionRegistry::GetInstance()->Register(#ns "::" #name, func); \
5460
})
5561

lib/remote/endpoint.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "remote/endpoint.hpp"
44
#include "remote/endpoint-ti.cpp"
5+
#include "remote/apifunction.hpp"
56
#include "remote/apilistener.hpp"
67
#include "remote/jsonrpcconnection.hpp"
78
#include "remote/zone.hpp"
@@ -35,6 +36,13 @@ void Endpoint::SetCachedZone(const Zone::Ptr& zone)
3536
m_Zone = zone;
3637
}
3738

39+
Endpoint::Endpoint()
40+
{
41+
for (auto& [name, afunc] : ApiFunctionRegistry::GetInstance()->GetItems()) {
42+
m_MessageCounters.emplace(afunc, 0);
43+
}
44+
}
45+
3846
void Endpoint::AddClient(const JsonRpcConnection::Ptr& client)
3947
{
4048
bool was_master = ApiListener::GetInstance()->IsMaster();
@@ -117,6 +125,11 @@ void Endpoint::AddMessageReceived(int bytes)
117125
SetLastMessageReceived(time);
118126
}
119127

128+
void Endpoint::AddMessageReceived(const intrusive_ptr<ApiFunction>& method)
129+
{
130+
m_MessageCounters.at(method).fetch_add(1, std::memory_order_relaxed);
131+
}
132+
120133
double Endpoint::GetMessagesSentPerSecond() const
121134
{
122135
return m_MessagesSent.CalculateRate(Utility::GetTime(), 60);
@@ -136,3 +149,16 @@ double Endpoint::GetBytesReceivedPerSecond() const
136149
{
137150
return m_BytesReceived.CalculateRate(Utility::GetTime(), 60);
138151
}
152+
153+
Dictionary::Ptr Endpoint::GetMessagesReceivedPerType() const
154+
{
155+
DictionaryData result;
156+
157+
for (auto& [afunc, cnt] : m_MessageCounters) {
158+
if (auto v (cnt.load(std::memory_order_relaxed)); v) {
159+
result.emplace_back(afunc->GetName(), v);
160+
}
161+
}
162+
163+
return new Dictionary(std::move(result));
164+
}

lib/remote/endpoint.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
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 <unordered_map>
1013

1114
namespace icinga
1215
{
1316

17+
class ApiFunction;
1418
class JsonRpcConnection;
1519
class Zone;
1620

@@ -28,6 +32,8 @@ class Endpoint final : public ObjectImpl<Endpoint>
2832
static boost::signals2::signal<void(const Endpoint::Ptr&, const intrusive_ptr<JsonRpcConnection>&)> OnConnected;
2933
static boost::signals2::signal<void(const Endpoint::Ptr&, const intrusive_ptr<JsonRpcConnection>&)> OnDisconnected;
3034

35+
Endpoint();
36+
3137
void AddClient(const intrusive_ptr<JsonRpcConnection>& client);
3238
void RemoveClient(const intrusive_ptr<JsonRpcConnection>& client);
3339
std::set<intrusive_ptr<JsonRpcConnection> > GetClients() const;
@@ -42,20 +48,24 @@ class Endpoint final : public ObjectImpl<Endpoint>
4248

4349
void AddMessageSent(int bytes);
4450
void AddMessageReceived(int bytes);
51+
void AddMessageReceived(const intrusive_ptr<ApiFunction>& method);
4552

4653
double GetMessagesSentPerSecond() const override;
4754
double GetMessagesReceivedPerSecond() const override;
4855

4956
double GetBytesSentPerSecond() const override;
5057
double GetBytesReceivedPerSecond() const override;
5158

59+
Dictionary::Ptr GetMessagesReceivedPerType() const override;
60+
5261
protected:
5362
void OnAllConfigLoaded() override;
5463

5564
private:
5665
mutable std::mutex m_ClientsLock;
5766
std::set<intrusive_ptr<JsonRpcConnection> > m_Clients;
5867
intrusive_ptr<Zone> m_Zone;
68+
std::unordered_map<intrusive_ptr<ApiFunction>, Atomic<uint_fast64_t>> m_MessageCounters;
5969

6070
mutable RingBuffer m_MessagesSent{60};
6171
mutable RingBuffer m_MessagesReceived{60};

lib/remote/endpoint.ti

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class Endpoint : ConfigObject
5454
[no_user_modify, no_storage] double bytes_received_per_second {
5555
get;
5656
};
57+
58+
[no_user_modify, no_storage] Dictionary::Ptr messages_received_per_type {
59+
get;
60+
};
5761
};
5862

5963
}

lib/remote/jsonrpcconnection.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ void JsonRpcConnection::MessageHandler(const Dictionary::Ptr& message)
356356
Log(LogNotice, "JsonRpcConnection")
357357
<< "Call to non-existent function '" << method << "' from endpoint '" << m_Identity << "'.";
358358
} else {
359+
if (m_Endpoint) {
360+
m_Endpoint->AddMessageReceived(afunc);
361+
}
362+
359363
Dictionary::Ptr params = message->Get("params");
360364
if (params)
361365
resultMessage->Set("result", afunc->Invoke(origin, params));

0 commit comments

Comments
 (0)