Skip to content

Commit

Permalink
Split appsec telemetry file into rasp, waf and user (#5303)
Browse files Browse the repository at this point in the history
* 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
IlyasShabi authored and juan-fernandez committed Feb 28, 2025
1 parent f0c5551 commit cdd962c
Show file tree
Hide file tree
Showing 9 changed files with 579 additions and 401 deletions.
218 changes: 0 additions & 218 deletions packages/dd-trace/src/appsec/telemetry.js

This file was deleted.

24 changes: 24 additions & 0 deletions packages/dd-trace/src/appsec/telemetry/common.js
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
}
126 changes: 126 additions & 0 deletions packages/dd-trace/src/appsec/telemetry/index.js
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
}
Loading

0 comments on commit cdd962c

Please sign in to comment.