Implement a metrics daemon to expose Prometheus metrics
[quassel.git] / src / core / coresession.cpp
index b5de50e..f4196b0 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2018 by the Quassel Project                        *
+ *   Copyright (C) 2005-2019 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -20,6 +20,8 @@
 
 #include "coresession.h"
 
+#include <utility>
+
 #include <QtScript>
 
 #include "core.h"
@@ -44,7 +46,6 @@
 #include "ircchannel.h"
 #include "ircparser.h"
 #include "ircuser.h"
-#include "logmessage.h"
 #include "messageevent.h"
 #include "remotepeer.h"
 #include "storage.h"
@@ -81,6 +82,7 @@ CoreSession::CoreSession(UserId uid, bool restoreState, bool strictIdentEnabled,
     , _processMessages(false)
     , _ignoreListManager(this)
     , _highlightRuleManager(this)
+    , _metricsServer(Core::instance()->metricsServer())
 {
     SignalProxy* p = signalProxy();
     p->setHeartBeatInterval(30);
@@ -91,34 +93,30 @@ CoreSession::CoreSession(UserId uid, bool restoreState, bool strictIdentEnabled,
     connect(p, &SignalProxy::connected, this, &CoreSession::clientsConnected);
     connect(p, &SignalProxy::disconnected, this, &CoreSession::clientsDisconnected);
 
-    p->attachSlot(SIGNAL(sendInput(BufferInfo, QString)), this, SLOT(msgFromClient(BufferInfo, QString)));
-    p->attachSignal(this, SIGNAL(displayMsg(Message)));
-    p->attachSignal(this, SIGNAL(displayStatusMsg(QString, QString)));
+    p->attachSlot(SIGNAL(sendInput(BufferInfo,QString)), this, &CoreSession::msgFromClient);
+    p->attachSignal(this, &CoreSession::displayMsg);
+    p->attachSignal(this, &CoreSession::displayStatusMsg);
 
-    p->attachSignal(this, SIGNAL(identityCreated(const Identity&)));
-    p->attachSignal(this, SIGNAL(identityRemoved(IdentityId)));
-    p->attachSlot(SIGNAL(createIdentity(const Identity&, const QVariantMap&)),
-                  this,
-                  SLOT(createIdentity(const Identity&, const QVariantMap&)));
-    p->attachSlot(SIGNAL(removeIdentity(IdentityId)), this, SLOT(removeIdentity(IdentityId)));
+    p->attachSignal(this, &CoreSession::identityCreated);
+    p->attachSignal(this, &CoreSession::identityRemoved);
+    p->attachSlot(SIGNAL(createIdentity(Identity,QVariantMap)), this, selectOverload<const Identity&, const QVariantMap&>(&CoreSession::createIdentity));
+    p->attachSlot(SIGNAL(removeIdentity(IdentityId)), this, &CoreSession::removeIdentity);
 
-    p->attachSignal(this, SIGNAL(networkCreated(NetworkId)));
-    p->attachSignal(this, SIGNAL(networkRemoved(NetworkId)));
-    p->attachSlot(SIGNAL(createNetwork(const NetworkInfo&, const QStringList&)),
-                  this,
-                  SLOT(createNetwork(const NetworkInfo&, const QStringList&)));
-    p->attachSlot(SIGNAL(removeNetwork(NetworkId)), this, SLOT(removeNetwork(NetworkId)));
+    p->attachSignal(this, &CoreSession::networkCreated);
+    p->attachSignal(this, &CoreSession::networkRemoved);
+    p->attachSlot(SIGNAL(createNetwork(NetworkInfo,QStringList)), this,&CoreSession::createNetwork);
+    p->attachSlot(SIGNAL(removeNetwork(NetworkId)), this, &CoreSession::removeNetwork);
 
-    p->attachSlot(SIGNAL(changePassword(PeerPtr, QString, QString, QString)), this, SLOT(changePassword(PeerPtr, QString, QString, QString)));
-    p->attachSignal(this, SIGNAL(passwordChanged(PeerPtr, bool)));
+    p->attachSlot(SIGNAL(changePassword(PeerPtr,QString,QString,QString)), this, &CoreSession::changePassword);
+    p->attachSignal(this, &CoreSession::passwordChanged);
 
-    p->attachSlot(SIGNAL(kickClient(int)), this, SLOT(kickClient(int)));
-    p->attachSignal(this, SIGNAL(disconnectFromCore()));
+    p->attachSlot(SIGNAL(kickClient(int)), this, &CoreSession::kickClient);
+    p->attachSignal(this, &CoreSession::disconnectFromCore);
 
     QVariantMap data;
     data["quasselVersion"] = Quassel::buildInfo().fancyVersionString;
     data["quasselBuildDate"] = Quassel::buildInfo().commitDate;  // "BuildDate" for compatibility
-    data["startTime"] = Core::instance()->startTime();
+    data["startTime"] = Core::startTime();
     data["sessionConnectedClients"] = 0;
     _coreInfo->setCoreData(data);
 
@@ -135,7 +133,7 @@ CoreSession::CoreSession(UserId uid, bool restoreState, bool strictIdentEnabled,
     eventManager()->registerObject(ctcpParser(), EventManager::LowPriority, "send");
 
     // periodically save our session state
-    connect(Core::instance()->syncTimer(), &QTimer::timeout, this, &CoreSession::saveSessionState);
+    connect(Core::syncTimer(), &QTimer::timeout, this, &CoreSession::saveSessionState);
 
     p->synchronize(_bufferSyncer);
     p->synchronize(&aliasManager());
@@ -154,6 +152,10 @@ CoreSession::CoreSession(UserId uid, bool restoreState, bool strictIdentEnabled,
         restoreSessionState();
 
     emit initialized();
+
+    if (_metricsServer) {
+        _metricsServer->addSession(user(), Core::instance()->strictSysIdent(_user));
+    }
 }
 
 void CoreSession::shutdown()
@@ -174,6 +176,10 @@ void CoreSession::shutdown()
         // Nothing to do, suicide so the core can shut down
         deleteLater();
     }
+
+    if (_metricsServer) {
+        _metricsServer->removeSession(user());
+    }
 }
 
 void CoreSession::onNetworkDisconnected(NetworkId networkId)
@@ -206,7 +212,7 @@ void CoreSession::loadSettings()
     // migrate to db
     QList<IdentityId> ids = s.identityIds();
     QList<NetworkInfo> networkInfos = Core::networks(user());
-    foreach (IdentityId id, ids) {
+    for (IdentityId id : ids) {
         CoreIdentity identity(s.identity(id));
         IdentityId newId = Core::createIdentity(user(), identity);
         QList<NetworkInfo>::iterator networkIter = networkInfos.begin();
@@ -224,11 +230,11 @@ void CoreSession::loadSettings()
     }
     // end of migration
 
-    foreach (CoreIdentity identity, Core::identities(user())) {
+    for (const CoreIdentity& identity : Core::identities(user())) {
         createIdentity(identity);
     }
 
-    foreach (NetworkInfo info, Core::networks(user())) {
+    for (const NetworkInfo& info : Core::networks(user())) {
         createNetwork(info);
     }
 }
@@ -244,7 +250,7 @@ void CoreSession::restoreSessionState()
 {
     QList<NetworkId> nets = Core::connectedNetworks(user());
     CoreNetwork* net = nullptr;
-    foreach (NetworkId id, nets) {
+    for (NetworkId id :  nets) {
         net = network(id);
         Q_ASSERT(net);
         net->connectToIrc();
@@ -260,6 +266,10 @@ void CoreSession::addClient(RemotePeer* peer)
     _coreInfo->setConnectedClientData(signalProxy()->peerCount(), signalProxy()->peerData());
 
     signalProxy()->setTargetPeer(nullptr);
+
+    if (_metricsServer) {
+        _metricsServer->addClient(user());
+    }
 }
 
 void CoreSession::addClient(InternalPeer* peer)
@@ -272,8 +282,12 @@ void CoreSession::removeClient(Peer* peer)
 {
     auto* p = qobject_cast<RemotePeer*>(peer);
     if (p)
-        quInfo() << qPrintable(tr("Client")) << p->description() << qPrintable(tr("disconnected (UserId: %1).").arg(user().toInt()));
+        qInfo() << qPrintable(tr("Client")) << p->description() << qPrintable(tr("disconnected (UserId: %1).").arg(user().toInt()));
     _coreInfo->setConnectedClientData(signalProxy()->peerCount(), signalProxy()->peerData());
+
+    if (_metricsServer) {
+        _metricsServer->removeClient(user());
+    }
 }
 
 QHash<QString, QString> CoreSession::persistentChannels(NetworkId id) const
@@ -305,31 +319,23 @@ void CoreSession::msgFromClient(BufferInfo bufinfo, QString msg)
 
 // ALL messages coming pass through these functions before going to the GUI.
 // So this is the perfect place for storing the backlog and log stuff.
-void CoreSession::recvMessageFromServer(NetworkId networkId,
-                                        Message::Type type,
-                                        BufferInfo::Type bufferType,
-                                        const QString& target,
-                                        const QString& text_,
-                                        const QString& sender,
-                                        Message::Flags flags)
+void CoreSession::recvMessageFromServer(RawMessage msg)
 {
     // U+FDD0 and U+FDD1 are special characters for Qt's text engine, specifically they mark the boundaries of
     // text frames in a QTextDocument. This might lead to problems in widgets displaying QTextDocuments (such as
     // KDE's notifications), hence we remove those just to be safe.
-    QString text = text_;
-    text.remove(QChar(0xfdd0)).remove(QChar(0xfdd1));
-    RawMessage rawMsg(networkId, type, bufferType, target, text, sender, flags);
+    msg.text.remove(QChar(0xfdd0)).remove(QChar(0xfdd1));
 
     // check for HardStrictness ignore
-    CoreNetwork* currentNetwork = network(networkId);
+    CoreNetwork* currentNetwork = network(msg.networkId);
     QString networkName = currentNetwork ? currentNetwork->networkName() : QString("");
-    if (_ignoreListManager.match(rawMsg, networkName) == IgnoreListManager::HardStrictness)
+    if (_ignoreListManager.match(msg, networkName) == IgnoreListManager::HardStrictness)
         return;
 
-    if (currentNetwork && _highlightRuleManager.match(rawMsg, currentNetwork->myNick(), currentNetwork->identityPtr()->nicks()))
-        rawMsg.flags |= Message::Flag::Highlight;
+    if (currentNetwork && _highlightRuleManager.match(msg, currentNetwork->myNick(), currentNetwork->identityPtr()->nicks()))
+        msg.flags |= Message::Flag::Highlight;
 
-    _messageQueue << rawMsg;
+    _messageQueue << std::move(msg);
     if (!_processMessages) {
         _processMessages = true;
         QCoreApplication::postEvent(this, new ProcessMessagesEvent());
@@ -340,18 +346,21 @@ void CoreSession::recvStatusMsgFromServer(QString msg)
 {
     auto* net = qobject_cast<CoreNetwork*>(sender());
     Q_ASSERT(net);
-    emit displayStatusMsg(net->networkName(), msg);
+    emit displayStatusMsg(net->networkName(), std::move(msg));
 }
 
 void CoreSession::processMessageEvent(MessageEvent* event)
 {
-    recvMessageFromServer(event->networkId(),
-                          event->msgType(),
-                          event->bufferType(),
-                          event->target().isNull() ? "" : event->target(),
-                          event->text().isNull() ? "" : event->text(),
-                          event->sender().isNull() ? "" : event->sender(),
-                          event->msgFlags());
+    recvMessageFromServer(RawMessage{
+        event->timestamp(),
+        event->networkId(),
+        event->msgType(),
+        event->bufferType(),
+        event->target().isNull() ? "" : event->target(),
+        event->text().isNull() ? "" : event->text(),
+        event->sender().isNull() ? "" : event->sender(),
+        event->msgFlags()
+    });
 }
 
 QList<BufferInfo> CoreSession::buffers() const
@@ -378,7 +387,8 @@ void CoreSession::processMessages()
             Q_ASSERT(!createBuffer);
             bufferInfo = Core::bufferInfo(user(), rawMsg.networkId, BufferInfo::StatusBuffer, "");
         }
-        Message msg(bufferInfo,
+        Message msg(rawMsg.timestamp,
+                    bufferInfo,
                     rawMsg.type,
                     rawMsg.text,
                     rawMsg.sender,
@@ -409,7 +419,8 @@ void CoreSession::processMessages()
                 }
                 bufferInfoCache[rawMsg.networkId][rawMsg.target] = bufferInfo;
             }
-            Message msg(bufferInfo,
+            Message msg(rawMsg.timestamp,
+                        bufferInfo,
                         rawMsg.type,
                         rawMsg.text,
                         rawMsg.sender,
@@ -432,7 +443,8 @@ void CoreSession::processMessages()
                 // add the StatusBuffer to the Cache in case there are more Messages for the original target
                 bufferInfoCache[rawMsg.networkId][rawMsg.target] = bufferInfo;
             }
-            Message msg(bufferInfo,
+            Message msg(rawMsg.timestamp,
+                        bufferInfo,
                         rawMsg.type,
                         rawMsg.text,
                         rawMsg.sender,
@@ -507,20 +519,23 @@ Protocol::SessionState CoreSession::sessionState() const
     QVariantList networkIds;
     QVariantList identities;
 
-    foreach (const BufferInfo& id, buffers())
+    for (const BufferInfo& id : buffers()) {
         bufferInfos << QVariant::fromValue(id);
-    foreach (const NetworkId& id, _networks.keys())
+    }
+    for (const NetworkId& id : _networks.keys()) {
         networkIds << QVariant::fromValue(id);
-    foreach (const Identity* i, _identities.values())
+    }
+    for (const Identity* i : _identities.values()) {
         identities << QVariant::fromValue(*i);
+    }
 
     return Protocol::SessionState(identities, bufferInfos, networkIds);
 }
 
 void CoreSession::initScriptEngine()
 {
-    signalProxy()->attachSlot(SIGNAL(scriptRequest(QString)), this, SLOT(scriptRequest(QString)));
-    signalProxy()->attachSignal(this, SIGNAL(scriptResult(QString)));
+    signalProxy()->attachSlot(SIGNAL(scriptRequest(QString)), this, &CoreSession::scriptRequest);
+    signalProxy()->attachSignal(this, &CoreSession::scriptResult);
 
     // FIXME
     // QScriptValue storage_ = scriptEngine->newQObject(storage);
@@ -614,7 +629,7 @@ void CoreSession::createNetwork(const NetworkInfo& info_, const QStringList& per
     if (!_networks.contains(id)) {
         // create persistent chans
         QRegExp rx(R"(\s*(\S+)(?:\s*(\S+))?\s*)");
-        foreach (QString channel, persistentChans) {
+        for (const QString& channel : persistentChans) {
             if (!rx.exactMatch(channel)) {
                 qWarning() << QString("Invalid persistent channel declaration: %1").arg(channel);
                 continue;
@@ -677,7 +692,7 @@ void CoreSession::destroyNetwork(NetworkId id)
             }
         }
         // remove buffers from syncer
-        foreach (BufferId bufferId, removedBuffers) {
+        for (BufferId bufferId : removedBuffers) {
             _bufferSyncer->removeBuffer(bufferId);
         }
         emit networkRemoved(id);