Skip to content
Merged
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
80 changes: 44 additions & 36 deletions src/core/common/monitoring/alertprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,55 @@ namespace {
* Static
**********************************************************************************************************************/

class CreateAlertVisitor : public StaticVisitor<AlertVariant> {
class CreateAlertVisitor : public StaticVisitor<void> {
public:
CreateAlertVisitor(uint64_t currentValue, const Time& currentTime, const QuotaAlertState& state)
: mCurrentVal(currentValue)
CreateAlertVisitor(
const ResourceIdentifier& id, uint64_t currentValue, const Time& currentTime, const QuotaAlertState& state)
: mID(id)
, mCurrentVal(currentValue)
, mCurrentTime(currentTime)
, mState(state)
{
}

Res Visit(const SystemQuotaAlert& val) const
Res Visit(SystemQuotaAlert& val) const
{
auto systemQuotaAlert = val;

systemQuotaAlert.mTimestamp = mCurrentTime;
systemQuotaAlert.mValue = mCurrentVal;
systemQuotaAlert.mState = mState;

Res result;
result.SetValue<SystemQuotaAlert>(systemQuotaAlert);

return result;
val.mNodeID = mID.mNodeID;
val.mParameter = GetParameterName(mID);
val.mTimestamp = mCurrentTime;
val.mValue = mCurrentVal;
val.mState = mState;
}

Res Visit(const InstanceQuotaAlert& val) const
Res Visit(InstanceQuotaAlert& val) const
{
auto instanceQuotaAlert = val;

instanceQuotaAlert.mTimestamp = mCurrentTime;
instanceQuotaAlert.mValue = mCurrentVal;
instanceQuotaAlert.mState = mState;

Res result;
result.SetValue<InstanceQuotaAlert>(instanceQuotaAlert);

return result;
val.mParameter = GetParameterName(mID);
static_cast<InstanceIdent&>(val) = mID.mInstanceIdent.GetValue();
val.mTimestamp = mCurrentTime;
val.mValue = mCurrentVal;
val.mState = mState;
}

template <typename T>
Res Visit(const T&) const
{
assert(false);

return {};
}

private:
uint64_t mCurrentVal {};
Time mCurrentTime;
QuotaAlertState mState;
String GetParameterName(const ResourceIdentifier& id) const
{
if (id.mPartitionName.HasValue()) {
return id.mPartitionName.GetValue(); // NOSONAR cpp:S5912 - String is used as a string view.
}

return id.mType.ToString(); // NOSONAR cpp:S5912 - String is used as a string view.
}

const ResourceIdentifier& mID;
uint64_t mCurrentVal {};
Time mCurrentTime;
QuotaAlertState mState;
};

} // namespace
Expand All @@ -73,8 +73,7 @@ class CreateAlertVisitor : public StaticVisitor<AlertVariant> {
* Public
**********************************************************************************************************************/

Error AlertProcessor::Init(const ResourceIdentifier& id, const AlertRulePoints& rule, alerts::SenderItf& sender,
const AlertVariant& alertTemplate)
Error AlertProcessor::Init(const ResourceIdentifier& id, const AlertRulePoints& rule, alerts::SenderItf& sender)
{
mID = id;
mMinTimeout = rule.mMinTimeout;
Expand All @@ -84,8 +83,7 @@ Error AlertProcessor::Init(const ResourceIdentifier& id, const AlertRulePoints&
LOG_DBG() << "Create alert processor" << Log::Field("id", mID) << Log::Field("minThreshold", mMinThreshold)
<< Log::Field("maxThreshold", mMaxThreshold) << Log::Field("minTimeout", mMinTimeout);

mAlertSender = &sender;
mAlertTemplate = alertTemplate;
mAlertSender = &sender;

return ErrorEnum::eNone;
}
Expand Down Expand Up @@ -187,9 +185,19 @@ Error AlertProcessor::HandleMinThreshold(uint64_t currentValue, const Time& curr

Error AlertProcessor::SendAlert(uint64_t currentValue, const Time& currentTime, const QuotaAlertState& state)
{
CreateAlertVisitor visitor(currentValue, currentTime, state);
AlertVariant alert;

if (mID.mLevel == ResourceLevelEnum::eSystem) {
alert.SetValue<SystemQuotaAlert>();
} else if (mID.mLevel == ResourceLevelEnum::eInstance) {
alert.SetValue<InstanceQuotaAlert>();
} else {
return Error(ErrorEnum::eInvalidArgument);
}

const CreateAlertVisitor visitor(mID, currentValue, currentTime, state);

auto alert = mAlertTemplate.ApplyVisitor(visitor);
alert.ApplyVisitor(visitor);

if (auto err = mAlertSender->SendAlert(alert); !err.IsNone()) {
LOG_ERR() << "Failed to send alert" << Log::Field(err);
Expand Down
12 changes: 6 additions & 6 deletions src/core/common/monitoring/alertprocessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,18 @@ struct ResourceIdentifier {
* @param partitionName partition name.
* @param instanceIdent instance identifier.
*/
ResourceIdentifier(ResourceLevel level, ResourceType type,
ResourceIdentifier(const String& nodeId, ResourceLevel level, ResourceType type,
const Optional<StaticString<cPartitionNameLen>>& partitionName = {},
const Optional<InstanceIdent>& instanceIdent = {})
: mLevel(level)
: mNodeID(nodeId)
, mLevel(level)
, mType(type)
, mPartitionName(partitionName)
, mInstanceIdent(instanceIdent)
{
}

StaticString<cIDLen> mNodeID;
ResourceLevel mLevel;
ResourceType mType;
Optional<StaticString<cPartitionNameLen>> mPartitionName;
Expand All @@ -108,7 +110,7 @@ struct ResourceIdentifier {
*/
friend Log& operator<<(Log& log, const ResourceIdentifier& identifier)
{
log << "{" << identifier.mLevel << ":" << identifier.mType;
log << "{" << identifier.mNodeID << ":" << identifier.mLevel << ":" << identifier.mType;

if (identifier.mPartitionName.HasValue()) {
log << ":" << identifier.mPartitionName.GetValue();
Expand Down Expand Up @@ -138,8 +140,7 @@ class AlertProcessor {
* @param alertTemplate alert template.
* @return Error.
*/
Error Init(const ResourceIdentifier& id, const AlertRulePoints& rule, alerts::SenderItf& sender,
const AlertVariant& alertTemplate);
Error Init(const ResourceIdentifier& id, const AlertRulePoints& rule, alerts::SenderItf& sender);

/**
* Checks alert detection. If alert condition is true, sends alert.
Expand All @@ -164,7 +165,6 @@ class AlertProcessor {

ResourceIdentifier mID {};
alerts::SenderItf* mAlertSender {};
AlertVariant mAlertTemplate;

Duration mMinTimeout {};
uint64_t mMinThreshold {};
Expand Down
61 changes: 7 additions & 54 deletions src/core/common/monitoring/monitoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ Optional<AlertRulePoints> ToPoints(const Optional<AlertRulePercents>& percents,
return ToPoints(*percents, totalValue);
}

String GetParameterName(const ResourceIdentifier& id)
{
if (id.mPartitionName.HasValue()) {
return id.mPartitionName.GetValue();
}

return id.mType.ToString();
}

RetWithError<uint64_t> GetCurrentUsage(const ResourceIdentifier& id, const MonitoringData& monitoringData)
{
switch (id.mType.GetValue()) {
Expand Down Expand Up @@ -432,39 +423,6 @@ void Monitoring::ProcessMonitoring()
}
}

Error Monitoring::CreateAlertTemplate(const ResourceIdentifier& resourceIdentifier, AlertVariant& alert) const
{
switch (resourceIdentifier.mLevel.GetValue()) {
case ResourceLevelEnum::eSystem: {
SystemQuotaAlert quotaAlert {};

quotaAlert.mNodeID = mNodeInfo.mNodeID;
quotaAlert.mParameter = GetParameterName(resourceIdentifier);

alert.SetValue<SystemQuotaAlert>(quotaAlert);

return ErrorEnum::eNone;
}

case ResourceLevelEnum::eInstance: {
if (!resourceIdentifier.mInstanceIdent.HasValue()) {
return AOS_ERROR_WRAP(ErrorEnum::eInvalidArgument);
}

InstanceQuotaAlert quotaAlert {};

static_cast<InstanceIdent&>(quotaAlert) = *resourceIdentifier.mInstanceIdent;
quotaAlert.mParameter = GetParameterName(resourceIdentifier);

alert.SetValue<InstanceQuotaAlert>(quotaAlert);

return ErrorEnum::eNone;
}
}

return AOS_ERROR_WRAP(ErrorEnum::eNotSupported);
}

Error Monitoring::AddAlertProcessor(
const AlertRulePoints& rule, const ResourceIdentifier& identifier, Array<AlertProcessor>& processors)
{
Expand All @@ -474,13 +432,7 @@ Error Monitoring::AddAlertProcessor(

auto& alertProcessor = processors.Back();

AlertVariant alertTemplate;

if (auto err = CreateAlertTemplate(identifier, alertTemplate); !err.IsNone()) {
return AOS_ERROR_WRAP(err);
}

if (auto err = alertProcessor.Init(identifier, rule, *mAlertSender, alertTemplate); !err.IsNone()) {
if (auto err = alertProcessor.Init(identifier, rule, *mAlertSender); !err.IsNone()) {
return AOS_ERROR_WRAP(err);
}

Expand Down Expand Up @@ -522,31 +474,31 @@ Error Monitoring::SetAlertProcessors(const AlertRules& alertRules, const Resourc
const Optional<InstanceIdent>& instanceIdent, Array<AlertProcessor>& processors)
{
if (auto cpu = ToPoints(alertRules.mCPU, mNodeInfo.mMaxDMIPS); cpu.HasValue()) {
auto id = ResourceIdentifier(level, ResourceTypeEnum::eCPU, {}, instanceIdent);
auto id = ResourceIdentifier(mNodeInfo.mNodeID, level, ResourceTypeEnum::eCPU, {}, instanceIdent);

if (auto err = AddAlertProcessor(*cpu, id, processors); !err.IsNone()) {
return AOS_ERROR_WRAP(err);
}
}

if (auto ram = ToPoints(alertRules.mRAM, mNodeInfo.mTotalRAM); ram.HasValue()) {
auto id = ResourceIdentifier(level, ResourceTypeEnum::eRAM, {}, instanceIdent);
auto id = ResourceIdentifier(mNodeInfo.mNodeID, level, ResourceTypeEnum::eRAM, {}, instanceIdent);

if (auto err = AddAlertProcessor(*ram, id, processors); !err.IsNone()) {
return AOS_ERROR_WRAP(err);
}
}

if (alertRules.mDownload.HasValue()) {
auto id = ResourceIdentifier(level, ResourceTypeEnum::eDownload, {}, instanceIdent);
auto id = ResourceIdentifier(mNodeInfo.mNodeID, level, ResourceTypeEnum::eDownload, {}, instanceIdent);

if (auto err = AddAlertProcessor(*alertRules.mDownload, id, processors); !err.IsNone()) {
return AOS_ERROR_WRAP(err);
}
}

if (alertRules.mUpload.HasValue()) {
auto id = ResourceIdentifier(level, ResourceTypeEnum::eUpload, {}, instanceIdent);
auto id = ResourceIdentifier(mNodeInfo.mNodeID, level, ResourceTypeEnum::eUpload, {}, instanceIdent);

if (auto err = AddAlertProcessor(*alertRules.mUpload, id, processors); !err.IsNone()) {
return AOS_ERROR_WRAP(err);
Expand All @@ -560,7 +512,8 @@ Error Monitoring::SetAlertProcessors(const AlertRules& alertRules, const Resourc
continue;
}

auto id = ResourceIdentifier(level, ResourceTypeEnum::ePartition, partition.mName, instanceIdent);
auto id = ResourceIdentifier(
mNodeInfo.mNodeID, level, ResourceTypeEnum::ePartition, partition.mName, instanceIdent);

if (auto err = AddAlertProcessor(ToPoints(partition, it->mTotalSize), id, processors); !err.IsNone()) {
return AOS_ERROR_WRAP(err);
Expand Down
1 change: 0 additions & 1 deletion src/core/common/monitoring/monitoring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class Monitoring : public MonitoringItf,
void GetInstanceMonitoringData(Array<InstanceMonitoringData>& instanceMonitoringData);
void ProcessAlerts(NodeMonitoringData& monitoringData);
void ProcessAlerts(MonitoringData& monitoringData, AlertProcessorArray& alertProcessors);
Error CreateAlertTemplate(const ResourceIdentifier& resourceIdentifier, AlertVariant& alert) const;
Error AddAlertProcessor(
const AlertRulePoints& rule, const ResourceIdentifier& identifier, Array<AlertProcessor>& processors);
Error SetNodeAlertProcessors(const Optional<AlertRules>& alertRules);
Expand Down
9 changes: 3 additions & 6 deletions src/core/common/monitoring/tests/alertprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,12 @@ TEST_F(AlertProcessorTest, CheckRulePointAlertDetection)
{
const ResourceType resourceType = ResourceTypeEnum::eDownload;
const AlertRulePoints rulePoints = {Time::cSeconds, 90, 95};
const ResourceIdentifier id = {ResourceLevelEnum::eSystem, resourceType.GetValue(), {}, {}};
const String nodeID = "node-id";
const ResourceIdentifier id = {nodeID, ResourceLevelEnum::eSystem, resourceType.GetValue(), {}};

AlertProcessor alertProcessor;

{
AlertVariant alertTemplate;
alertTemplate.SetValue<SystemQuotaAlert>(CreateSystemQuotaAlert("node-id", resourceType.ToString(), 0));
ASSERT_TRUE(alertProcessor.Init(id, rulePoints, mAlertSender, alertTemplate).IsNone());
}
ASSERT_TRUE(alertProcessor.Init(id, rulePoints, mAlertSender).IsNone());

Time currentTime = Time::Now();

Expand Down
Loading