Skip to content

Introduce Endpoint#messages_received_per_type #10387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/remote/apifunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

using namespace icinga;

ApiFunction::ApiFunction(Callback function)
: m_Callback(std::move(function))
ApiFunction::ApiFunction(const char* name, Callback function)
: m_Name(name), m_Callback(std::move(function))
{ }

Value ApiFunction::Invoke(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& arguments)
Expand Down
10 changes: 8 additions & 2 deletions lib/remote/apifunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@ class ApiFunction final : public Object

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

ApiFunction(Callback function);
ApiFunction(const char* name, Callback function);

const char* GetName() const noexcept
{
return m_Name;
}

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

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

private:
const char* m_Name;
Callback m_Callback;
};

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

#define REGISTER_APIFUNCTION(name, ns, callback) \
INITIALIZE_ONCE([]() { \
ApiFunction::Ptr func = new ApiFunction(callback); \
ApiFunction::Ptr func = new ApiFunction(#ns "::" #name, callback); \
ApiFunctionRegistry::GetInstance()->Register(#ns "::" #name, func); \
})

Expand Down
26 changes: 26 additions & 0 deletions lib/remote/endpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "remote/endpoint.hpp"
#include "remote/endpoint-ti.cpp"
#include "remote/apifunction.hpp"
#include "remote/apilistener.hpp"
#include "remote/jsonrpcconnection.hpp"
#include "remote/zone.hpp"
Expand Down Expand Up @@ -35,6 +36,13 @@ void Endpoint::SetCachedZone(const Zone::Ptr& zone)
m_Zone = zone;
}

Endpoint::Endpoint()
{
for (auto& [name, afunc] : ApiFunctionRegistry::GetInstance()->GetItems()) {
m_MessageCounters.emplace(afunc, 0);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea

  1. std::unordered_map#reserve() twice as many as needed
  2. std::hash collisions will be even less likely at lookup
  3. Lookup gets even faster 😎

}
}

void Endpoint::AddClient(const JsonRpcConnection::Ptr& client)
{
bool was_master = ApiListener::GetInstance()->IsMaster();
Expand Down Expand Up @@ -117,6 +125,11 @@ void Endpoint::AddMessageReceived(int bytes)
SetLastMessageReceived(time);
}

void Endpoint::AddMessageReceived(const intrusive_ptr<ApiFunction>& method)
{
m_MessageCounters.at(method).fetch_add(1, std::memory_order_relaxed);
}

double Endpoint::GetMessagesSentPerSecond() const
{
return m_MessagesSent.CalculateRate(Utility::GetTime(), 60);
Expand All @@ -136,3 +149,16 @@ double Endpoint::GetBytesReceivedPerSecond() const
{
return m_BytesReceived.CalculateRate(Utility::GetTime(), 60);
}

Dictionary::Ptr Endpoint::GetMessagesReceivedPerType() const
{
DictionaryData result;

for (auto& [afunc, cnt] : m_MessageCounters) {
if (auto v (cnt.load(std::memory_order_relaxed)); v) {
result.emplace_back(afunc->GetName(), v);
}
}

return new Dictionary(std::move(result));
}
10 changes: 10 additions & 0 deletions lib/remote/endpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

#include "remote/i2-remote.hpp"
#include "remote/endpoint-ti.hpp"
#include "base/atomic.hpp"
#include "base/ringbuffer.hpp"
#include <cstdint>
#include <set>
#include <unordered_map>

namespace icinga
{

class ApiFunction;
class JsonRpcConnection;
class Zone;

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

Endpoint();

void AddClient(const intrusive_ptr<JsonRpcConnection>& client);
void RemoveClient(const intrusive_ptr<JsonRpcConnection>& client);
std::set<intrusive_ptr<JsonRpcConnection> > GetClients() const;
Expand All @@ -42,20 +48,24 @@ class Endpoint final : public ObjectImpl<Endpoint>

void AddMessageSent(int bytes);
void AddMessageReceived(int bytes);
void AddMessageReceived(const intrusive_ptr<ApiFunction>& method);

double GetMessagesSentPerSecond() const override;
double GetMessagesReceivedPerSecond() const override;

double GetBytesSentPerSecond() const override;
double GetBytesReceivedPerSecond() const override;

Dictionary::Ptr GetMessagesReceivedPerType() const override;

protected:
void OnAllConfigLoaded() override;

private:
mutable std::mutex m_ClientsLock;
std::set<intrusive_ptr<JsonRpcConnection> > m_Clients;
intrusive_ptr<Zone> m_Zone;
std::unordered_map<intrusive_ptr<ApiFunction>, Atomic<uint_fast64_t>> m_MessageCounters;

mutable RingBuffer m_MessagesSent{60};
mutable RingBuffer m_MessagesReceived{60};
Expand Down
4 changes: 4 additions & 0 deletions lib/remote/endpoint.ti
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class Endpoint : ConfigObject
[no_user_modify, no_storage] double bytes_received_per_second {
get;
};

[no_user_modify, no_storage] Dictionary::Ptr messages_received_per_type {
get;
};
};

}
4 changes: 4 additions & 0 deletions lib/remote/jsonrpcconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ void JsonRpcConnection::MessageHandler(const Dictionary::Ptr& message)
Log(LogNotice, "JsonRpcConnection")
<< "Call to non-existent function '" << method << "' from endpoint '" << m_Identity << "'.";
} else {
if (m_Endpoint) {
m_Endpoint->AddMessageReceived(afunc);
}

Dictionary::Ptr params = message->Get("params");
if (params)
resultMessage->Set("result", afunc->Invoke(origin, params));
Expand Down
Loading