|
6 | 6 | import java.util.List; |
7 | 7 | import java.util.concurrent.ArrayBlockingQueue; |
8 | 8 | import java.util.concurrent.BlockingQueue; |
| 9 | +import java.util.concurrent.ConcurrentMap; |
| 10 | +import java.util.concurrent.ConcurrentSkipListMap; |
9 | 11 | import java.util.concurrent.atomic.AtomicInteger; |
10 | 12 | import java.util.concurrent.atomic.AtomicLong; |
11 | 13 | import java.util.concurrent.atomic.AtomicLongArray; |
12 | 14 |
|
13 | 15 | public class WafMetricCollector implements MetricCollector<WafMetricCollector.WafMetric> { |
14 | 16 |
|
15 | 17 | public static WafMetricCollector INSTANCE = new WafMetricCollector(); |
| 18 | + private static final int ABSTRACT_POWERWAF_EXCEPTION_NUMBER = |
| 19 | + 3; // only 3 error codes are possible for now in AbstractPowerwafException |
16 | 20 |
|
17 | 21 | public static WafMetricCollector get() { |
18 | 22 | return WafMetricCollector.INSTANCE; |
@@ -40,6 +44,8 @@ private WafMetricCollector() { |
40 | 44 | new AtomicLongArray(RuleType.getNumValues()); |
41 | 45 | private static final AtomicLongArray raspTimeoutCounter = |
42 | 46 | new AtomicLongArray(RuleType.getNumValues()); |
| 47 | + private static final ConcurrentMap<Integer, AtomicLongArray> raspErrorCodeCounter = |
| 48 | + new ConcurrentSkipListMap<>(); |
43 | 49 | private static final AtomicLongArray missingUserLoginQueue = |
44 | 50 | new AtomicLongArray(LoginFramework.getNumValues() * LoginEvent.getNumValues()); |
45 | 51 | private static final AtomicLongArray missingUserIdQueue = |
@@ -104,6 +110,10 @@ public void raspTimeout(final RuleType ruleType) { |
104 | 110 | raspTimeoutCounter.incrementAndGet(ruleType.ordinal()); |
105 | 111 | } |
106 | 112 |
|
| 113 | + public void raspErrorCode(final RuleType ruleType, final int ddwafRunErrorCode) { |
| 114 | + raspErrorCodeCounter.get(ddwafRunErrorCode).incrementAndGet(ruleType.ordinal()); |
| 115 | + } |
| 116 | + |
107 | 117 | public void missingUserLogin(final LoginFramework framework, final LoginEvent eventType) { |
108 | 118 | missingUserLoginQueue.incrementAndGet( |
109 | 119 | framework.ordinal() * LoginEvent.getNumValues() + eventType.ordinal()); |
@@ -216,6 +226,21 @@ public void prepareMetrics() { |
216 | 226 | } |
217 | 227 | } |
218 | 228 |
|
| 229 | + // RASP rule type for each possible error code |
| 230 | + for (int i = -1 * ABSTRACT_POWERWAF_EXCEPTION_NUMBER; i < 0; i++) { |
| 231 | + raspErrorCodeCounter.put(i, new AtomicLongArray(RuleType.getNumValues())); |
| 232 | + |
| 233 | + for (RuleType ruleType : RuleType.values()) { |
| 234 | + long counter = raspErrorCodeCounter.get(i).getAndSet(ruleType.ordinal(), 0); |
| 235 | + if (counter > 0) { |
| 236 | + if (!rawMetricsQueue.offer( |
| 237 | + new RaspError(counter, ruleType, WafMetricCollector.wafVersion, i))) { |
| 238 | + return; |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + |
219 | 244 | // Missing user login |
220 | 245 | for (LoginFramework framework : LoginFramework.values()) { |
221 | 246 | for (LoginEvent event : LoginEvent.values()) { |
@@ -367,6 +392,27 @@ public RaspTimeout(final long counter, final RuleType ruleType, final String waf |
367 | 392 | } |
368 | 393 | } |
369 | 394 |
|
| 395 | + public static class RaspError extends WafMetric { |
| 396 | + public RaspError( |
| 397 | + final long counter, |
| 398 | + final RuleType ruleType, |
| 399 | + final String wafVersion, |
| 400 | + final Integer ddwafRunError) { |
| 401 | + super( |
| 402 | + "rasp.error", |
| 403 | + counter, |
| 404 | + ruleType.variant != null |
| 405 | + ? new String[] { |
| 406 | + "rule_type:" + ruleType.type, |
| 407 | + "rule_variant:" + ruleType.variant, |
| 408 | + "waf_version:" + wafVersion, |
| 409 | + "event_rules_version:" + rulesVersion, |
| 410 | + "waf_error:" + ddwafRunError |
| 411 | + } |
| 412 | + : new String[] {"rule_type:" + ruleType.type, "waf_version:" + wafVersion}); |
| 413 | + } |
| 414 | + } |
| 415 | + |
370 | 416 | public static class AtomicRequestCounter { |
371 | 417 |
|
372 | 418 | private final AtomicLong atomicLong = new AtomicLong(); |
|
0 commit comments