11'use strict'
22
33const telemetryMetrics = require ( '../../telemetry/metrics' )
4- const { DD_TELEMETRY_REQUEST_METRICS } = require ( './common' )
4+ const { DD_TELEMETRY_REQUEST_METRICS , getVersionsTags } = require ( './common' )
55
66const appsecMetrics = telemetryMetrics . manager . namespace ( 'appsec' )
77
8+ const BLOCKING_STATUS = {
9+ FAILURE : 'failure' ,
10+ IRRELEVANT : 'irrelevant' ,
11+ SUCCESS : 'success'
12+ }
13+
814function addRaspRequestMetrics ( store , { duration, durationExt, wafTimeout, errorCode } ) {
915 store [ DD_TELEMETRY_REQUEST_METRICS ] . raspDuration += duration || 0
1016 store [ DD_TELEMETRY_REQUEST_METRICS ] . raspDurationExt += durationExt || 0
@@ -26,25 +32,95 @@ function addRaspRequestMetrics (store, { duration, durationExt, wafTimeout, erro
2632 }
2733}
2834
29- function trackRaspMetrics ( metrics , raspRule ) {
30- const tags = { rule_type : raspRule . type , waf_version : metrics . wafVersion }
35+ function trackRaspMetrics ( store , metrics , raspRule ) {
36+ const versionsTags = getVersionsTags ( metrics . wafVersion , metrics . rulesVersion )
37+ const tags = { rule_type : raspRule . type , ...versionsTags }
38+ const telemetryMetrics = store [ DD_TELEMETRY_REQUEST_METRICS ]
3139
3240 if ( raspRule . variant ) {
3341 tags . rule_variant = raspRule . variant
3442 }
3543
44+ if ( metrics . wafVersion ) {
45+ telemetryMetrics . wafVersion = metrics . wafVersion
46+ }
47+
48+ if ( metrics . rulesVersion ) {
49+ telemetryMetrics . rulesVersion = metrics . rulesVersion
50+ }
51+
52+ if ( metrics . ruleTriggered ) {
53+ telemetryMetrics . ruleTriggered = true
54+ }
55+
3656 appsecMetrics . count ( 'rasp.rule.eval' , tags ) . inc ( 1 )
3757
58+ if ( metrics . duration ) {
59+ appsecMetrics . distribution ( 'rasp.rule.duration' , tags ) . track ( metrics . duration )
60+
61+ const raspDuration = telemetryMetrics . raspDuration
62+ appsecMetrics . distribution ( 'rasp.duration' , versionsTags ) . track ( raspDuration )
63+ }
64+
65+ if ( metrics . durationExt ) {
66+ const raspDurationExt = telemetryMetrics . raspDurationExt
67+ appsecMetrics . distribution ( 'rasp.duration_ext' , versionsTags ) . track ( raspDurationExt )
68+ }
69+
70+ if ( metrics . errorCode ) {
71+ const errorTags = { ...tags , waf_error : metrics . errorCode }
72+
73+ appsecMetrics . count ( 'rasp.error' , errorTags ) . inc ( 1 )
74+ }
75+
3876 if ( metrics . wafTimeout ) {
3977 appsecMetrics . count ( 'rasp.timeout' , tags ) . inc ( 1 )
4078 }
79+ }
4180
42- if ( metrics . ruleTriggered ) {
43- appsecMetrics . count ( 'rasp.rule.match' , tags ) . inc ( 1 )
81+ function trackRaspRuleMatch ( store , raspRule , blockTriggered , blocked ) {
82+ const telemetryMetrics = store [ DD_TELEMETRY_REQUEST_METRICS ]
83+ if ( ! telemetryMetrics . ruleTriggered ) return
84+
85+ const tags = {
86+ waf_version : telemetryMetrics . wafVersion ,
87+ event_rules_version : telemetryMetrics . rulesVersion ,
88+ rule_type : raspRule . type ,
89+ block : getRuleMatchBlockingStatus ( blockTriggered , blocked )
90+ }
91+
92+ if ( raspRule . variant ) {
93+ tags . rule_variant = raspRule . variant
94+ }
95+
96+ appsecMetrics . count ( 'rasp.rule.match' , tags ) . inc ( 1 )
97+
98+ // this is needed to not count it twice for the same match
99+ // but it also means it can only be called once per waf call even if there are multiple rasp match
100+ telemetryMetrics . ruleTriggered = null
101+ }
102+
103+ function trackRaspRuleSkipped ( raspRule , reason ) {
104+ const tags = { reason, rule_type : raspRule . type }
105+
106+ if ( raspRule . variant ) {
107+ tags . rule_variant = raspRule . variant
44108 }
109+
110+ appsecMetrics . count ( 'rasp.rule.skipped' , tags ) . inc ( 1 )
111+ }
112+
113+ function getRuleMatchBlockingStatus ( blockTriggered , blocked ) {
114+ if ( ! blockTriggered ) {
115+ return BLOCKING_STATUS . IRRELEVANT
116+ }
117+
118+ return blocked ? BLOCKING_STATUS . SUCCESS : BLOCKING_STATUS . FAILURE
45119}
46120
47121module . exports = {
48122 addRaspRequestMetrics,
49- trackRaspMetrics
123+ trackRaspMetrics,
124+ trackRaspRuleMatch,
125+ trackRaspRuleSkipped
50126}
0 commit comments