-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split appsec telemetry file into rasp, waf and user (#5303)
* Split appsec telemetry file into rasp, waf and user * split appsec telemetry test files * remove unused success param * fix user test file name
- Loading branch information
1 parent
f0c5551
commit cdd962c
Showing
9 changed files
with
579 additions
and
401 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strinct' | ||
|
||
const DD_TELEMETRY_REQUEST_METRICS = Symbol('_dd.appsec.telemetry.request.metrics') | ||
|
||
const tags = { | ||
REQUEST_BLOCKED: 'request_blocked', | ||
RULE_TRIGGERED: 'rule_triggered', | ||
WAF_TIMEOUT: 'waf_timeout', | ||
WAF_VERSION: 'waf_version', | ||
EVENT_RULES_VERSION: 'event_rules_version' | ||
} | ||
|
||
function getVersionsTags (wafVersion, rulesVersion) { | ||
return { | ||
[tags.WAF_VERSION]: wafVersion, | ||
[tags.EVENT_RULES_VERSION]: rulesVersion | ||
} | ||
} | ||
|
||
module.exports = { | ||
tags, | ||
getVersionsTags, | ||
DD_TELEMETRY_REQUEST_METRICS | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
'use strict' | ||
|
||
const { DD_TELEMETRY_REQUEST_METRICS } = require('./common') | ||
const { addRaspRequestMetrics, trackRaspMetrics } = require('./rasp') | ||
const { incrementMissingUserId, incrementMissingUserLogin } = require('./user') | ||
const { | ||
addWafRequestMetrics, | ||
trackWafMetrics, | ||
incrementWafInit, | ||
incrementWafUpdates, | ||
incrementWafRequests | ||
} = require('./waf') | ||
|
||
const metricsStoreMap = new WeakMap() | ||
|
||
let enabled = false | ||
|
||
function enable (telemetryConfig) { | ||
enabled = telemetryConfig?.enabled && telemetryConfig.metrics | ||
} | ||
|
||
function disable () { | ||
enabled = false | ||
} | ||
|
||
function newStore () { | ||
return { | ||
[DD_TELEMETRY_REQUEST_METRICS]: { | ||
duration: 0, | ||
durationExt: 0, | ||
raspDuration: 0, | ||
raspDurationExt: 0, | ||
raspEvalCount: 0 | ||
} | ||
} | ||
} | ||
|
||
function getStore (req) { | ||
let store = metricsStoreMap.get(req) | ||
if (!store) { | ||
store = newStore() | ||
metricsStoreMap.set(req, store) | ||
} | ||
return store | ||
} | ||
|
||
function updateRaspRequestsMetricTags (metrics, req, raspRule) { | ||
if (!req) return | ||
|
||
const store = getStore(req) | ||
|
||
// it does not depend on whether telemetry is enabled or not | ||
addRaspRequestMetrics(store, metrics) | ||
|
||
if (!enabled) return | ||
|
||
trackRaspMetrics(metrics, raspRule) | ||
} | ||
|
||
function updateWafRequestsMetricTags (metrics, req) { | ||
if (!req) return | ||
|
||
const store = getStore(req) | ||
|
||
// it does not depend on whether telemetry is enabled or not | ||
addWafRequestMetrics(store, metrics) | ||
|
||
if (!enabled) return | ||
|
||
return trackWafMetrics(store, metrics) | ||
} | ||
|
||
function incrementWafInitMetric (wafVersion, rulesVersion) { | ||
if (!enabled) return | ||
|
||
incrementWafInit(wafVersion, rulesVersion) | ||
} | ||
|
||
function incrementWafUpdatesMetric (wafVersion, rulesVersion) { | ||
if (!enabled) return | ||
|
||
incrementWafUpdates(wafVersion, rulesVersion) | ||
} | ||
|
||
function incrementWafRequestsMetric (req) { | ||
if (!req || !enabled) return | ||
|
||
const store = getStore(req) | ||
incrementWafRequests(store) | ||
|
||
metricsStoreMap.delete(req) | ||
} | ||
|
||
function incrementMissingUserLoginMetric (framework, eventType) { | ||
if (!enabled) return | ||
|
||
incrementMissingUserLogin(framework, eventType) | ||
} | ||
|
||
function incrementMissingUserIdMetric (framework, eventType) { | ||
if (!enabled) return | ||
|
||
incrementMissingUserId(framework, eventType) | ||
} | ||
|
||
function getRequestMetrics (req) { | ||
if (req) { | ||
const store = getStore(req) | ||
return store?.[DD_TELEMETRY_REQUEST_METRICS] | ||
} | ||
} | ||
|
||
module.exports = { | ||
enable, | ||
disable, | ||
|
||
updateWafRequestsMetricTags, | ||
updateRaspRequestsMetricTags, | ||
incrementWafInitMetric, | ||
incrementWafUpdatesMetric, | ||
incrementWafRequestsMetric, | ||
incrementMissingUserLoginMetric, | ||
incrementMissingUserIdMetric, | ||
|
||
getRequestMetrics | ||
} |
Oops, something went wrong.