forked from voicenter/newman-reporter-pm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.js
203 lines (202 loc) · 8.12 KB
/
metrics.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
198
199
200
201
202
203
const { QrynClient } = require('qryn-client');
const stringify = require('json-stringify-safe');
module.exports= class metrics {
constructor(emitter, reporterOptions, collectionRunOptions){
this.emitter =emitter
this.reporterOptions =reporterOptions||{}
this.collectionRunOptions =collectionRunOptions
this.metricsRepository=[]
this.logRepository = []
if(reporterOptions.server.timeout &&reporterOptions.server.timeout.constructor.name==="String"){
try{
reporterOptions.server.timeout= Number.parseInt(reporterOptions.server.timeout)
}catch(e){
reporterOptions.server.timeout= 60000
}
}
this.qrynClient = new QrynClient(
reporterOptions.server
);
}
Init(){
let self = this;
this.emitter.on('request',(err, summary)=> {
self.RequestEventHandler(err, summary)
})
this.emitter.on('assertion',(err, summary)=> {
self.AssertionEventHandler(err, summary)
})
this.emitter.on('done',(err, summary)=> {
self.CollectionEventHandler(err, summary)
})
this.emitter.on('test',(err, summary)=> {
self.TestEventHandler(err, summary)
})
this.emitter.on('script',(err, summary)=> {
self.ScriptEventHandler(err, summary)
})
this.emitter.on('console',(err, summary)=> {
self.ConsoleEventHandler(err, summary)
})
this.emitter.on('exception',(err, summary)=> {
self.ExceptionEventHandler(err, summary)
})
}
LogEvent(eventType,label,value){
let summary
if(eventType === 'RequestSummary' || eventType === 'RequestSummaryF' ){
summary = {}
if( value.response?.stream && value.response.stream.constructor.name === 'Buffer' ){
value.response.body =value.response.stream.toString()
// delete value.response.stream;
}
summary.response = {...value.response}
delete summary.response.stream
if(!this.collectionRunOptions.logResponse){
delete summary.response.body
delete summary.response.headers
}
summary.request ={...value.request}
if(!this.collectionRunOptions.logRequest){
delete summary.request.headers
delete summary.request.body
}
}
label.eventType = eventType
const stream = this.qrynClient.createStream(label);
stream.addEntry(Date.now(), stringify(summary||value, null,2));
this.logRepository.push(stream)
}
LogError(eventType,label,error){
if(!error)return;
label.eventType = eventType + 'Error'
const stream = this.qrynClient.createStream(label);
stream.addEntry(Date.now(), stringify(error,null,2));
this.logRepository.push(stream)
}
Set(metricName,value,labels ={}){
const metric = this.qrynClient.createMetric({name:metricName,labels})
metric.addSample(value);
this.metricsRepository.push(metric)
}
SendMetrics(){
this.qrynClient.prom.push(Object.values(this.metricsRepository)).then((promResponse)=>{
// console.log(promResponse)
}).catch((err)=>{
console.error('qrynClient.prom.push error: ',err)
});
}
SendLogs(){
this.qrynClient.loki.push(Object.values(this.logRepository)).then((lokiResponse)=>{
// console.log(lokiResponse)
}).catch((err)=>{
console.error('qrynClient.loki.push error: ',err)
});
}
RequestEventHandler(err, summary){
const labels = this.GetItemLabels(summary)
labels.eventType = "RequestSummary"
this.LogEvent(labels.eventType,labels,summary)
this.LogError(labels.eventType,labels,err)
if(summary.response?.responseTime)this.Set(`ResponseTime`, summary.response.responseTime,labels)
if(summary.response?.code)this.Set(`ResponseCode`, summary.response.code,labels)
if(summary.response?.responseSize)this.Set(`ResponseSize`, summary.response.responseSize,labels)
}
AssertionEventHandler(err, summary){
const labels = this.GetItemLabels(summary)
labels.eventType = "AssertionSummary"
// console.log('AssertionEventHandler.summary:', summary);
labels.test = summary.assertion
this.LogEvent(labels.eventType,labels,summary)
this.LogError(labels.eventType,labels,err)
let testStatus= 1
if(summary.error){
testStatus= -1
}
this.Set(`AssertionStatus`,testStatus,labels)
}
ExceptionEventHandler(err, summary){
const labels = this.GetItemLabels(summary)
labels.eventType = "Exception"
this.LogEvent(labels.eventType,labels,summary.error)
this.Set(`Exception`,1,labels)
}
ConsoleEventHandler(err, summary){
const labels = this.GetItemLabels(summary)
labels.eventType = "Console"
labels.level = summary.level
this.LogEvent(labels.eventType,labels,summary.messages.join(", "))
this.Set(`Exception`,1,labels)
}
TestEventHandler(err, summary){
const labels = this.GetItemLabels(summary)
labels.eventType = "Test"
// console.log('AssertionEventHandler.summary:', summary);
labels.test = summary.item.name
this.LogEvent(labels.eventType,labels,summary.executions)
this.LogError(labels.eventType,labels,err)
let testStatus= 1
if(err){
testStatus= -1
}
this.Set(`testStatus`,testStatus,labels)
}
ScriptEventHandler(err, summary){
const labels = this.GetItemLabels(summary)
labels.eventType = "Script"
// console.log('AssertionEventHandler.summary:', summary);
labels.test = summary.item.name
this.LogEvent(labels.eventType,labels,summary.item)
this.LogError(labels.eventType,labels,err)
let testStatus= 1
if(err){
testStatus= -1
}
this.Set(`TestStatus`,testStatus,labels)
}
CollectionEventHandler(err, summary){
const labels = this.GetItemLabels(summary)
labels.eventType = "CollectionSummary"
this.LogEvent(labels.eventType,labels,summary)
this.LogError(labels.eventType,labels,err)
this.Set(`ResponseAverage`,summary.run.timings.responseAverage,labels)
this.Set(`ResponseMin`,summary.run.timings.responseMin , labels)
this.Set(`ResponseMax`,summary.run.timings.responseMax , labels)
this.Set(`DnsAverage`,summary.run.timings.dnsAverage , labels)
this.Set(`Itemstotal`,summary.run.stats.items.total , labels)
this.Set(`Itemsfailed`,summary.run.stats.items.failed , labels)
this.Set(`RequestsTotal`,summary.run.stats.requests.total , labels)
this.Set(`RequestsFailed`,summary.run.stats.requests.failed , labels)
this.Set(`TestsTotal`,summary.run.stats.assertions.total , labels)
this.Set(`TestsFailed`,summary.run.stats.assertions.failed , labels)
this.SendMetrics()
this.SendLogs()
}
GetObjectID(objectName){
console.log('GetMetricTag:', objectName);
if(objectName.indexOf('-')>0){
return objectName.split('-')[0]
}else{
return objectName
}
}
GetItemLabels(summary){
if(!this.reporterOptions.labels)this.reporterOptions.labels={}
const labels = {...this.reporterOptions.labels}
labels.collection = this.collectionRunOptions.collection.name ;
labels.collectionID = this.collectionRunOptions.collection.id ;
if(summary?.request){
labels.method=summary.request.method;
labels.path=summary.request.url.path.join('/');
labels.host =summary.request.url.host.join('.')
}
if(summary.item){
labels.itemName=summary.item.name
}
if(summary.error){
labels.errorName=summary.error.name
labels.errorType=summary.error.type||summary.error.message
}
return labels;
}
}