// // Copyright (c) ZeroC, Inc. All rights reserved. // #include #include #include #include #include using namespace std; using namespace Glacier2; using namespace Glacier2::Instrumentation; using namespace IceMX; namespace { class SessionHelper : public MetricsHelperT { public: class Attributes : public AttributeResolverT { public: Attributes() { add("parent", &SessionHelper::getInstanceName); add("id", &SessionHelper::getId); add("connection", &SessionHelper::getConnection); IceInternal::addConnectionAttributes(*this); } }; static Attributes attributes; SessionHelper(const string& instanceName, const string& id, shared_ptr connection, int rtSize) : _instanceName(instanceName), _id(id), _connection(move(connection)), _routingTableSize(rtSize) { } string operator()(const string& attribute) const override { return attributes(this, attribute); } void initMetrics(const shared_ptr& v) const override { v->routingTableSize += _routingTableSize; } const string& getInstanceName() const { return _instanceName; } const string& getId() const { return _id; } shared_ptr getConnectionInfo() const { return _connection->getInfo(); } shared_ptr getEndpoint() const { return _connection->getEndpoint(); } const shared_ptr& getConnection() const { return _connection; } shared_ptr getEndpointInfo() const { if(!_endpointInfo) { _endpointInfo = _connection->getEndpoint()->getInfo(); } return _endpointInfo; } private: const string& _instanceName; const string& _id; const shared_ptr& _connection; const int _routingTableSize; mutable shared_ptr _endpointInfo; }; SessionHelper::Attributes SessionHelper::attributes; namespace { struct ForwardedUpdate { ForwardedUpdate(bool clientP) : client(clientP) { } void operator()(const shared_ptr& v) { if(client) { ++v->forwardedClient; if(v->queuedClient > 0) { --v->queuedClient; } } else { ++v->forwardedServer; if(v->queuedServer > 0) { --v->queuedServer; } } } int client; }; } } void SessionObserverI::forwarded(bool client) { forEach(ForwardedUpdate(client)); } void SessionObserverI::queued(bool client) { if(client) { forEach(inc(&SessionMetrics::queuedClient)); } else { forEach(inc(&SessionMetrics::queuedServer)); } } void SessionObserverI::overridden(bool client) { if(client) { forEach(inc(&SessionMetrics::overriddenClient)); } else { forEach(inc(&SessionMetrics::overriddenServer)); } } void SessionObserverI::routingTableSize(int delta) { forEach(add(&SessionMetrics::routingTableSize, delta)); } RouterObserverI::RouterObserverI(shared_ptr metrics, const string& instanceName) : _metrics(move(metrics)), _instanceName(instanceName), _sessions(_metrics, "Session") { } void RouterObserverI::setObserverUpdater(const shared_ptr& updater) { _sessions.setUpdater(newUpdater(updater, &ObserverUpdater::updateSessionObservers)); } shared_ptr RouterObserverI::getSessionObserver(const string& id, const shared_ptr& connection, int routingTableSize, const shared_ptr& old) { if(_sessions.isEnabled()) { try { return _sessions.getObserver(SessionHelper(_instanceName, id, connection, routingTableSize), old); } catch(const exception& ex) { Ice::Error error(_metrics->getLogger()); error << "unexpected exception trying to obtain observer:\n" << ex; } } return 0; }