-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support to api security sampling (#4755)
* add support to api security sampling * fix express plugin schema extraction * use priority simpler to get span priority * use lru cache package * use route path instead of url * use route.path or url to generate the key * use ttlcache * Fix standalone integration test * Increase test timeout * simplify force sample * avoid checking is sampled twice * use route.path or url to generate the key * remove unnecessary tests of sample delay * fix non experimental options test * remove unused isSampled * always sample request if delay is 0 --------- Co-authored-by: Igor Unanua <igor.unanua@datadoghq.com> Co-authored-by: simon-id <simon.id@datadoghq.com>
- Loading branch information
Showing
19 changed files
with
352 additions
and
350 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -115,7 +115,6 @@ tracer.init({ | |
}, | ||
apiSecurity: { | ||
enabled: true, | ||
requestSampling: 1.0 | ||
}, | ||
rasp: { | ||
enabled: true | ||
|
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
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
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
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
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 |
---|---|---|
@@ -1,61 +1,84 @@ | ||
'use strict' | ||
|
||
const TTLCache = require('@isaacs/ttlcache') | ||
const web = require('../plugins/util/web') | ||
const log = require('../log') | ||
const { AUTO_REJECT, USER_REJECT } = require('../../../../ext/priority') | ||
|
||
const MAX_SIZE = 4096 | ||
|
||
let enabled | ||
let requestSampling | ||
let sampledRequests | ||
|
||
const sampledRequests = new WeakSet() | ||
class NoopTTLCache { | ||
clear () { } | ||
set (key) { return undefined } | ||
has (key) { return false } | ||
} | ||
|
||
function configure ({ apiSecurity }) { | ||
enabled = apiSecurity.enabled | ||
setRequestSampling(apiSecurity.requestSampling) | ||
sampledRequests = apiSecurity.sampleDelay === 0 | ||
? new NoopTTLCache() | ||
: new TTLCache({ max: MAX_SIZE, ttl: apiSecurity.sampleDelay * 1000 }) | ||
} | ||
|
||
function disable () { | ||
enabled = false | ||
sampledRequests?.clear() | ||
} | ||
|
||
function setRequestSampling (sampling) { | ||
requestSampling = parseRequestSampling(sampling) | ||
} | ||
function sampleRequest (req, res, force = false) { | ||
if (!enabled) return false | ||
|
||
function parseRequestSampling (requestSampling) { | ||
let parsed = parseFloat(requestSampling) | ||
const key = computeKey(req, res) | ||
if (!key || isSampled(key)) return false | ||
|
||
if (isNaN(parsed)) { | ||
log.warn(`Incorrect API Security request sampling value: ${requestSampling}`) | ||
const rootSpan = web.root(req) | ||
if (!rootSpan) return false | ||
|
||
parsed = 0 | ||
} else { | ||
parsed = Math.min(1, Math.max(0, parsed)) | ||
let priority = getSpanPriority(rootSpan) | ||
if (!priority) { | ||
rootSpan._prioritySampler?.sample(rootSpan) | ||
priority = getSpanPriority(rootSpan) | ||
} | ||
|
||
return parsed | ||
} | ||
|
||
function sampleRequest (req) { | ||
if (!enabled || !requestSampling) { | ||
if (priority === AUTO_REJECT || priority === USER_REJECT) { | ||
return false | ||
} | ||
|
||
const shouldSample = Math.random() <= requestSampling | ||
|
||
if (shouldSample) { | ||
sampledRequests.add(req) | ||
if (force) { | ||
sampledRequests.set(key) | ||
} | ||
|
||
return shouldSample | ||
return true | ||
} | ||
|
||
function isSampled (key) { | ||
return sampledRequests.has(key) | ||
} | ||
|
||
function computeKey (req, res) { | ||
const route = web.getContext(req)?.paths?.join('') || '' | ||
const method = req.method | ||
const status = res.statusCode | ||
|
||
if (!method || !status) { | ||
log.warn('Unsupported groupkey for API security') | ||
return null | ||
} | ||
return method + route + status | ||
} | ||
|
||
function isSampled (req) { | ||
return sampledRequests.has(req) | ||
function getSpanPriority (span) { | ||
const spanContext = span.context?.() | ||
return spanContext._sampling?.priority | ||
} | ||
|
||
module.exports = { | ||
configure, | ||
disable, | ||
setRequestSampling, | ||
sampleRequest, | ||
isSampled | ||
isSampled, | ||
computeKey | ||
} |
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
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
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
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
Oops, something went wrong.