-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (95 loc) · 3.1 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
const { Logger: SplunkLogger } = require('splunk-logging')
const debug = require('debug')('plugin:splunk')
/**
* A plugin for artillery.io that records stats and reports into HTTP Event Collector in Splunk
*/
class Splunk {
/**
* For using this plugin you need to enable HEC and create token
* {@link https://docs.splunk.com/Documentation/SplunkCloud/7.1.3/Data/UsetheHTTPEventCollector}
* @param {Object} config - raw config
* @param {Object} config.splunk - splunk plugin configuration
* @param {string} config.splunk.token - token for splunk HEC instance
* @param {string} config.splunk.url - url to splunk cloud (in this format https://input-prd-p-XXXXXXX.cloud.splunk.com:8088/services/collector)
* @param {string} [config.splunk.index = 'main'] - splunk index
* @param {EventEmitter} eventEmiter from artillery
* @param eventEmiter
*/
constructor (config, eventEmiter) {
this.eventEmiter = eventEmiter
this.config = (config && config.plugins && config.plugins.splunk) || {}
Splunk.validateConfig(this.config)
this.config.index = this.config.index || 'main'
this.splunkLogger = new SplunkLogger({
token: this.config.token,
url: this.config.url,
index: this.config.index
})
debug('Successfully initialized', this.config)
this.attachListeners()
}
/**
* Mapping stats event to latency event and send it to splunk
* @param {Object} stats - stats from artillery
* @param {Array<Array<[number, string, number, number]>>} stats._entries - Array of entries
* @param {Array<number>} stats._latencies - latencies
* @return {undefined}
*/
logStatsToSplunk (stats = {}) {
const { _entries = [] } = stats
for (let i = 0; i < _entries.length; i++) {
const [ timeStamp, ruid, latency, statusCode ] = _entries[i]
this.splunkLogger.send({
message: {
from: 'artillery-plugin-splunk',
type: 'latency',
timeStamp,
ruid,
latency,
statusCode
},
metadata: {
index: this.config.index
}
})
}
}
/**
* Log final report from done event to splunk
* @param {Object} message - stats from artillery
* @return {undefined}
*/
logDoneToSplunk (message = {}) {
message.from = 'artillery-plugin-splunk'
message.type = 'report'
this.splunkLogger.send({
message,
metadata: {
index: this.config.index
}
})
}
/**
* Attach listeners
* @return {undefined}
*/
attachListeners () {
this.eventEmiter.on('stats', this.logStatsToSplunk.bind(this))
this.eventEmiter.on('done', this.logDoneToSplunk.bind(this))
}
/**
* validate config
* @param {Object} config - splunk plugin configuration
* @param {string} config.token - token for splunk HEC instance
* @param {string} config.url - url to splunk cloud
*/
static validateConfig ({ token, url } = {}) {
if (!token || typeof token !== 'string') {
throw new Error('token is required')
}
if (!url || typeof url !== 'string') {
throw new Error('url is required')
}
}
}
module.exports = Splunk