Skip to content
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

Add rule to limit excessive use of sequential loggers #56

Merged
merged 17 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.avioconsulting.mule.linter.rule.configuration

import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.configuration.FlowComponent
import com.avioconsulting.mule.linter.model.rule.Rule
import com.avioconsulting.mule.linter.model.rule.RuleViolation

class ExcessiveLoggersRule extends Rule {
static final String RULE_ID = 'EXCESSIVE_LOGGERS_RULE'
static final String RULE_NAME = 'Excessive use of Sequential Loggers'
static final String RULE_VIOLATION_MESSAGE = 'Too many sequential loggers of same level in flow '

Map<String, Integer> excessiveLoggers = ["TRACE": 2, "DEBUG": 2, "INFO": 2, "WARN": 2, "ERROR": 2]
Copy link
Contributor

Choose a reason for hiding this comment

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

We should move these strings to either be an enum or static constants, probably not on this class but maybe on the LoggerComponent model class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We'll try an Enum for now.


ExcessiveLoggersRule() {}

ExcessiveLoggersRule(Integer excessiveLoggers) {
this(["TRACE": excessiveLoggers, "DEBUG": excessiveLoggers, "INFO": excessiveLoggers,
"WARN": excessiveLoggers, "ERROR": excessiveLoggers])
}

ExcessiveLoggersRule(Map<String, Integer> excessiveLoggers) {
this.ruleId = RULE_ID
this.ruleName = RULE_NAME
this.excessiveLoggers.putAll excessiveLoggers
}

@Override
List<RuleViolation> execute(Application application) {
List<RuleViolation> violations = []
application.flows.each { FlowComponent flow ->
RuleViolation violation = searchFlowForLoggers(flow)
if (violation != null) {
violations.add(violation)
}
}
application.subFlows.each { FlowComponent flow ->
RuleViolation violation = searchFlowForLoggers(flow)
if (violation != null) {
violations.add(violation)
}
}
return violations
}

private RuleViolation searchFlowForLoggers(FlowComponent flow) {
String logLevel = null
Integer count = 0
RuleViolation violation = null
flow.children.each {
if (it.componentName == "logger") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use COMPONENT_NAME from the LoggerComponent model class

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Easy enough

if (it.getAttributeValue("level") == logLevel) {
count++
if (count >= excessiveLoggers.get(it.getAttributeValue("level"))) {
violation = new RuleViolation(this, flow.file.path, flow.lineNumber, RULE_VIOLATION_MESSAGE
+ flow.getAttributeValue("name"))
}
} else {
logLevel = it.getAttributeValue("level")
count = 1
}
}
}
return violation
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.avioconsulting.mule.linter.rule.configuration

import com.avioconsulting.mule.linter.TestApplication
import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.rule.Rule
import com.avioconsulting.mule.linter.model.rule.RuleViolation
import spock.lang.Specification

class ExcessiveLoggersRuleTest extends Specification{
private final TestApplication testApp = new TestApplication()

def setup() {
testApp.initialize()
testApp.addPom()
testApp.buildConfigContent('business-logic.xml', LOGGERS)
}

def cleanup() {
testApp.remove()
}

def 'Default excessive loggers rule fails two flows'() {
given:
Rule rule = new ExcessiveLoggersRule()

when:
Application app = new Application(testApp.appDir)
List<RuleViolation> violations = rule.execute(app)

then:
violations.size() == 2
violations[0].message == ExcessiveLoggersRule.RULE_VIOLATION_MESSAGE +
"get:\\user\\(id)\\roles:application\\json:my-api-config"
violations[1].message == ExcessiveLoggersRule.RULE_VIOLATION_MESSAGE + "business-subflow-three"
}

def '3 Count excessive loggers rule fails one flow'() {
given:
Rule rule = new ExcessiveLoggersRule(3)

when:
Application app = new Application(testApp.appDir)
List<RuleViolation> violations = rule.execute(app)

then:
violations.size() == 1
violations[0].message == ExcessiveLoggersRule.RULE_VIOLATION_MESSAGE + "business-subflow-three"
}

def 'Custom excessive loggers rule fails no flows'() {
given:
Rule rule = new ExcessiveLoggersRule(["TRACE": 2, "DEBUG": 3, "INFO": 3, "WARN": 2, "ERROR": 4])

when:
Application app = new Application(testApp.appDir)
List<RuleViolation> violations = rule.execute(app)

then:
violations.size() == 0
}

private static final String LOGGERS = '''
\t<flow name="get:\\user\\(id)\\roles:application\\json:my-api-config" doc:id="bcadf69e-4504-4654-9c7a-38971a01ed11" >
\t\t<logger level="INFO" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c298a" />
\t\t<logger level="INFO" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c298b" />
\t\t<logger level="DEBUG" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c298c" />
\t\t<logger level="DEBUG" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c298d" />
\t\t<flow-ref doc:name="Flow Reference" doc:id="4bc8fa65-90c4-454f-b5b7-c1c318f3a1cb" name="business-subflow-two"/>
\t</flow>

\t<sub-flow name="business-subflow-two">
\t\t<logger level="INFO" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297a" />
\t\t<logger level="DEBUG" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297b" />
\t\t<logger level="WARN" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297c" />
\t\t<logger level="INFO" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297d" />
\t</sub-flow>

\t<sub-flow name="business-subflow-three">
\t\t<logger level="INFO" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297a" />
\t\t<logger level="ERROR" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297b" />
\t\t<logger level="ERROR" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297c" />
\t\t<logger level="ERROR" doc:name="Logger" doc:id="6a676a90-4f3a-48c5-90af-358e8c1c297d" />
\t</sub-flow>'''
}