-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
188 lines (172 loc) · 6.22 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* ABSTRACT PLUGIN TYPE DEFINITIONS
*
* @typedef {object} MetricOption
* @property {string} name The name of the metric
* @property {string} pattern Filter patter doc (https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html)
* @property {string[]} [functions] Default: ALL
* @property {string} [namespace] Override dynamic generated namespace (default: '<serviceName>/<stageName>')
* @property {string} [value] The value to apply to each occurence. (default: 1)
*/
/**
* AWS TYPE DEFINITIONS
*
* @typedef {object} AWSMetricFilterResourceProperty
* @property {string} FilterPattern
* @property {string} LogGroupName
* @property {AWSMetricFilterResourceMetricTransformation[]} MetricTransformations
*
* @typedef {object} AWSMetricFilterResourceMetricTransformation
* @property {string} MetricValue
* @property {string} MetricNamespace
* @property {string} MetricName
*
* @typedef {object} AWSMetricFilterResource
* @property {string} Type
* @property {AWSMetricFilterResourceProperty} Properties
* @property {MetricOption} __metricOption Internal used meta information
*/
/**
* This plugin creates "AWS:Log:MetricFilter" resources using the `MetricOption` definition
* under the serverless.yml location `custom.metrics`.
*
* By default the plugin applies the metric resources to all functions, except
* when specific function-names are provided (`MetricOptions.functions`).
*
* OPTION EXAMPLE:
*
* ```
* custom:
* metrics:
* - name: foo
* pattern: "{ $.statusCode != 200 }"
* functions: (optional, default: ALL)
* - getBar
* namespace: "custom/metric" (optional, default: '<serviceName>/<stageName>')
* value: (optional, default: 1)
* ```
*/
class MetricPlugin {
constructor(serverless, options) {
/**
* @type {string}
*/
this.service = serverless.service.service;
/**
* @type {object}
*/
this.serverless = serverless;
/**
* @type {object}
*/
this.provider = serverless.getProvider('aws');
/**
* @type {MetricOption[]}
*/
this.metricOptions = serverless.service.custom && serverless.service.custom.metrics
? serverless.service.custom.metrics
: [];
/**
* @type {string}
*/
this.functions = this.getAllFunctions();
this.hooks = {
'package:compileEvents': this.handler.bind(this)
}
}
handler() {
/**
* @type {AWSMetricFilterResource[]}
*/
this.functions
.map((functionName) => this.createMetricFilterResources(functionName))
.forEach(({ functionName, resources }) => {
resources.forEach((resource) => {
/**
* @type {MetricOption}
*/
const metricOption = resource.__metricOption;
const resourceName = `${functionName}MetricFilter${metricOption.name}`;
this.registerResource(resourceName, resource);
})
});
}
/**
* Get all the function names including support for an array of function files
* @param {array|object} functions
* @returns {string[]}
*/
getAllFunctions() {
if (Array.isArray(this.serverless.service.functions)) {
return this.serverless.service.functions.reduce((allFunctions, functionObject) => {
return [...allFunctions, ...Object.keys(functionObject)];
}, []);
} else {
return this.serverless.service.getAllFunctions();
}
}
/**
* @param {string} functionName
* @returns {{functionName: string, resources: AWSMetricFilterResource[]}}
*/
createMetricFilterResources(functionName) {
const resources = this.metricOptions
.filter((option) => {
if (option.functions && option.functions.length) {
return option.functions.indexOf(functionName) !== -1;
} else {
return true;
}
})
.map((option) => this.createAWSMetricResource(functionName, option));
return { functionName, resources };
}
/**
* AWS compatible metric resource creation.
*
* @param {string} functionName
* @param {MetricOption} metricOptions
* @returns {AWSMetricFilterResource}
*/
createAWSMetricResource(functionName, metricOptions) {
const { name, namespace, pattern, value = '1' } = metricOptions;
const stage = this.provider.getStage();
const logGroupName = this.provider.naming.getLogGroupName(this.serverless.service.getFunction(functionName).name);
const dynamicNamespace = `${this.service}/${stage}`;
/**
* @type {AWSMetricFilterResource}
*/
const resource = {
__metricOption: metricOptions,
Type: 'AWS::Logs::MetricFilter',
DependsOn: this.provider.naming.getLogGroupLogicalId(functionName),
Properties: {
FilterPattern: pattern,
LogGroupName: logGroupName,
MetricTransformations: [
{
MetricName: `${functionName}-${name}`,
MetricNamespace: namespace || dynamicNamespace,
MetricValue: value
}
]
}
}
return resource;
}
/**
* Register a aws resource OR override.
*
* @param {string} name
* @param {AWSMetricFilterResource} resource
*/
registerResource(name, resource) {
delete resource.__metricOption; // delete associated meta information
if (!this.serverless.service.provider.compiledCloudFormationTemplate.Resources) {
this.serverless.service.provider.compiledCloudFormationTemplate.Resources = {};
}
const normalizedName = this.provider.naming.normalizeNameToAlphaNumericOnly(name);
this.serverless.service.provider.compiledCloudFormationTemplate.Resources[normalizedName] = resource;
}
}
module.exports = MetricPlugin;