Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split appsec telemetry file into rasp, waf and user #5303

Merged
merged 4 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, success) {
if (!enabled) return

incrementWafInit(wafVersion, rulesVersion, success)
}

function incrementWafUpdatesMetric (wafVersion, rulesVersion, success) {
if (!enabled) return

incrementWafUpdates(wafVersion, rulesVersion, success)
}

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
Loading