Skip to content

Commit 9872b5d

Browse files
committed
Abstract log message publish in reporter
1 parent 90544fc commit 9872b5d

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

packages/dd-trace/src/appsec/reporter.js

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -242,48 +242,49 @@ function reportWafInit (wafVersion, rulesVersion, diagnosticsRules = {}, success
242242
incrementWafInitMetric(wafVersion, rulesVersion, success)
243243
}
244244

245+
function logWafDiagnosticMessage (product, rcConfigId, configKey, message, level) {
246+
telemetryLogCh.publish({
247+
message,
248+
level,
249+
tags: {
250+
log_type: `rc::${product.toLowerCase()}::diagnostic`,
251+
appsec_config_key: configKey,
252+
rc_config_id: rcConfigId
253+
}
254+
})
255+
}
256+
245257
function reportSuccessfulWafUpdate (product, rcConfigId, diagnostics) {
246258
for (const configKey of WAF_DIAGNOSTICS_CONFIG_KEYS_TO_REPORT) {
247-
if (!diagnostics[configKey]) continue
248-
249-
if (diagnostics[configKey].error) {
250-
telemetryLogCh.publish({
251-
message: diagnostics[configKey].error,
252-
level: 'ERROR',
253-
tags: {
254-
log_type: `rc::${product.toLowerCase()}::diagnostic`,
255-
appsec_config_key: configKey,
256-
rc_config_id: rcConfigId
257-
}
258-
})
259+
const configDiagnostics = diagnostics[configKey]
260+
if (!configDiagnostics) continue
261+
262+
if (configDiagnostics.error) {
263+
logWafDiagnosticMessage(product, rcConfigId, configKey, configDiagnostics.error, 'ERROR')
259264
continue
260265
}
261266

262-
if (diagnostics[configKey].errors) {
263-
for (const [errorMessage, errorIds] of Object.entries(diagnostics[configKey].errors)) {
264-
telemetryLogCh.publish({
265-
message: `"${errorMessage}": ${JSON.stringify(errorIds)}`,
266-
level: 'ERROR',
267-
tags: {
268-
log_type: `rc::${product.toLowerCase()}::diagnostic`,
269-
appsec_config_key: configKey,
270-
rc_config_id: rcConfigId
271-
}
272-
})
267+
if (configDiagnostics.errors) {
268+
for (const [errorMessage, errorIds] of Object.entries(configDiagnostics.errors)) {
269+
logWafDiagnosticMessage(
270+
product,
271+
rcConfigId,
272+
configKey,
273+
`"${errorMessage}": ${JSON.stringify(errorIds)}`,
274+
'ERROR'
275+
)
273276
}
274277
}
275278

276-
if (diagnostics[configKey].warnings) {
277-
for (const [warningMessage, warningIds] of Object.entries(diagnostics[configKey].warnings)) {
278-
telemetryLogCh.publish({
279-
message: `"${warningMessage}": ${JSON.stringify(warningIds)}`,
280-
level: 'WARN',
281-
tags: {
282-
log_type: `rc::${product.toLowerCase()}::diagnostic`,
283-
appsec_config_key: configKey,
284-
rc_config_id: rcConfigId
285-
}
286-
})
279+
if (configDiagnostics.warnings) {
280+
for (const [warningMessage, warningIds] of Object.entries(configDiagnostics.warnings)) {
281+
logWafDiagnosticMessage(
282+
product,
283+
rcConfigId,
284+
configKey,
285+
`"${warningMessage}": ${JSON.stringify(warningIds)}`,
286+
'WARN'
287+
)
287288
}
288289
}
289290
}

0 commit comments

Comments
 (0)