This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
197 lines (163 loc) · 4.48 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
189
190
191
192
193
194
195
196
197
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
const AWS = require('aws-sdk');
const debug = require('debug')('plugin:sqsReporter');
const { randomUUID } = require('crypto');
module.exports = {
Plugin: ArtillerySQSPlugin,
LEGACY_METRICS_FORMAT: false,
};
function ArtillerySQSPlugin(script, events) {
this.script = script;
this.events = events;
this.unsent = 0;
const self = this;
// List of objects: [{key: 'SomeKey', value: 'SomeValue'}, ...]
this.tags = process.env.SQS_TAGS ? JSON.parse(process.env.SQS_TAGS) : [];
this.testId = null;
let messageAttributes = {};
this.tags.forEach(function(tag) {
if(tag.key === 'testId') {
self.testId = tag.value;
}
messageAttributes[tag.key] = {
DataType: 'String',
StringValue: tag.value
};
});
this.messageAttributes = messageAttributes;
this.sqs = new AWS.SQS({
region: process.env.SQS_REGION || script.config.plugins['sqs-reporter'].region
});
this.queueUrl =
process.env.SQS_QUEUE_URL || script.config.plugins['sqs-reporter'].queueUrl;
events.on('phaseStarted', (phase) => {
const body = JSON.stringify({
event: 'phaseStarted',
phase,
});
debug('Prepared messsage body');
debug(body);
this.unsent++;
// TODO: Check that body is not longer than 255kb
const params = {
MessageBody: body,
QueueUrl: this.queueUrl,
MessageAttributes: this.messageAttributes,
MessageDeduplicationId: randomUUID(),
MessageGroupId: this.testId
};
this.sqs.sendMessage(params, (err, data) => {
if (err) {
console.error(err);
}
this.unsent--;
});
});
events.on('phaseCompleted', (phase) => {
const body = JSON.stringify({
event: 'phaseCompleted',
phase,
});
debug('Prepared messsage body');
debug(body);
this.unsent++;
// TODO: Check that body is not longer than 255kb
const params = {
MessageBody: body,
QueueUrl: this.queueUrl,
MessageAttributes: this.messageAttributes,
MessageDeduplicationId: randomUUID(),
MessageGroupId: this.testId
};
this.sqs.sendMessage(params, (err, data) => {
if (err) {
console.error(err);
}
this.unsent--;
});
});
global.artillery.globalEvents.on('log', (opts, ...args) => {
if (process.env.SHIP_LOGS) {
const body = JSON.stringify({
event: 'artillery.log',
log: {
opts,
args: [...args],
},
});
const params = {
MessageBody: body,
QueueUrl: this.queueUrl,
MessageAttributes: this.messageAttributes,
MessageDeduplicationId: randomUUID(),
MessageGroupId: this.testId
};
this.sqs.sendMessage(params, (err, data) => {
if (err) {
console.error(err);
}
this.unsent--;
});
}
debug('global log', opts, [...args]);
});
events.on('stats', statsOriginal => {
let body;
const serialized = global.artillery.__SSMS.serializeMetrics(statsOriginal);
body = {
event: 'workerStats',
stats: serialized
}
body = JSON.stringify(body);
debug('Prepared messsage body');
debug(body);
this.unsent++;
// TODO: Check that body is not longer than 255kb
const params = {
MessageBody: body,
QueueUrl: this.queueUrl,
MessageAttributes: this.messageAttributes,
MessageDeduplicationId: randomUUID(),
MessageGroupId: this.testId
};
this.sqs.sendMessage(params, (err, data) => {
if (err) {
console.error(err);
}
this.unsent--;
});
});
events.on('done', stats => {
this.unsent++;
const body = JSON.stringify({
event: 'done',
stats: global.artillery.__SSMS.serializeMetrics(stats),
});
const params = {
MessageBody: body,
QueueUrl: this.queueUrl,
MessageAttributes: this.messageAttributes,
MessageDeduplicationId: randomUUID(),
MessageGroupId: this.testId
};
this.sqs.sendMessage(params, (err, data) => {
if (err) {
console.error(err);
}
this.unsent--;
});
});
return this;
}
;
ArtillerySQSPlugin.prototype.cleanup = function(done) {
const interval = setInterval(() => {
if (this.unsent <= 0) {
clearInterval(interval);
done(null);
}
}, 200).unref();
};