forked from pzimmermann/serverless-plugin-metric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (152 loc) · 5.58 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
/* jshint ignore:start */
'use strict';
const pascalcase = require('pascalcase');
/**
* 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: CustomMetrics/<serviceName>)
* @property {string} [value] The value to apply to each occurence
*/
/**
* 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
* 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: 'CustomMetrics/<serviceName>')
* ```
*/
class MetricPlugin {
constructor(serverless, options) {
/**
* @type {object}
*/
this.serverless = serverless;
this.options = options;
/**
* @type {string}
*/
this.functions = serverless.service.getAllFunctions();
this.hooks = {
'package:compileEvents': this.handler.bind(this)
}
}
handler() {
this.metricOptions = get(this.serverless.service, 'custom.metrics', [])
this.service = get(this.serverless.service, 'service')
this.stage = get(this.serverless.service, 'provider.stage')
/**
* @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);
})
});
}
/**
* @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, prefix, namespace, pattern, value = '1' } = metricOptions;
const logGroupName = `/aws/lambda/${this.service}-${this.stage}-${functionName}`;
const dynamicNamespace = `${this.service}/${this.stage}`;
const namePrefix = prefix + '-' || ''
/**
* @type {AWSMetricFilterResource}
*/
const resource = {
__metricOption: metricOptions,
Type: 'AWS::Logs::MetricFilter',
DependsOn: `${pascalcase(functionName)}LogGroup`,
Properties: {
FilterPattern: pattern,
LogGroupName: logGroupName,
MetricTransformations: [
{
MetricName: `${namePrefix}${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 = {};
}
this.serverless.service.provider.compiledCloudFormationTemplate.Resources[pascalcase(name)] = resource;
}
}
function get(obj, path, def) {
return path.split('.').filter(Boolean).every(step => !(step && (obj = obj[step]) === undefined)) ? obj : def;
}
module.exports = MetricPlugin;