-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathindex.js
78 lines (68 loc) · 2.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const middy = require('@middy/core')
const sampleLogging = require('@dazn/lambda-powertools-middleware-sample-logging')
const { obfuscaterMiddleware, FILTERING_MODE: obfuscaterFilteringMode } = require('@dazn/lambda-powertools-middleware-obfuscater')
const captureCorrelationIds = require('@dazn/lambda-powertools-middleware-correlation-ids')
const logTimeout = require('@dazn/lambda-powertools-middleware-log-timeout')
const supplementCsv = require('./supplement-csv')
const AWS_REGION = process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION
const FUNCTION_NAME = process.env.AWS_LAMBDA_FUNCTION_NAME
const FUNCTION_VERSION = process.env.AWS_LAMBDA_FUNCTION_VERSION
const ENV = process.env.ENVIRONMENT || process.env.STAGE
/**
* Filtering modes this library supports.
*/
const FILTERING_MODE = Object.freeze({ 'BLACKLIST': 'BLACKLIST', 'WHITELIST': 'WHITELIST' })
if (process.env.DATADOG_PREFIX === undefined) {
process.env.DATADOG_PREFIX = FUNCTION_NAME + '.'
}
process.env.DATADOG_TAGS = supplementCsv({
existing: process.env.DATADOG_TAGS,
additional: {
awsRegion: AWS_REGION,
functionName: FUNCTION_NAME,
functionVersion: FUNCTION_VERSION,
environment: ENV
}
})
const toObfuscaterFilteringMode = (mode) => {
if (FILTERING_MODE.BLACKLIST === mode) {
return obfuscaterFilteringMode.BLACKLIST
}
if (FILTERING_MODE.WHITELIST === mode) {
return obfuscaterFilteringMode.WHITELIST
}
return null
}
const errorObfuscater = (obfuscationFilters, filteringMode) => obfuscaterMiddleware({
obfuscationFilters,
filteringMode: toObfuscaterFilteringMode(filteringMode),
filterOnBefore: false,
filterOnAfter: false,
filterOnError: true
})
const genericObfuscater = (obfuscationFilters, filteringMode, filterOnAfter) => obfuscaterMiddleware({
obfuscationFilters,
filteringMode: toObfuscaterFilteringMode(filteringMode),
filterOnBefore: false,
filterOnAfter: filterOnAfter,
filterOnError: false
})
const obfuscaterPattern = (obfuscationFilters, f, filterOnAfter = false, filteringMode = FILTERING_MODE.BLACKLIST) => {
return middy(f)
.use(captureCorrelationIds({
sampleDebugLogRate: parseFloat(process.env.SAMPLE_DEBUG_LOG_RATE || '0.01')
}))
// Ensure that the error part of the code is executed last as middy runs before1 > before2 > before3 > after 3 > after 2 > after 1
// but runs errors error1 > error2 > error 3.
.use(errorObfuscater(obfuscationFilters, filteringMode))
.use(sampleLogging({
sampleRate: parseFloat(process.env.SAMPLE_DEBUG_LOG_RATE || '0.01'),
obfuscationFilters
}))
.use(genericObfuscater(obfuscationFilters, filteringMode, filterOnAfter))
.use(logTimeout())
}
module.exports = {
FILTERING_MODE,
obfuscaterPattern
}