1
- const fs = require ( 'fs' ) ;
2
1
var fasquest = require ( 'fasquest' ) ;
3
2
const client = {
4
3
https : require ( 'https' ) ,
@@ -104,11 +103,28 @@ class Alerts {
104
103
return ;
105
104
}
106
105
// check if status has changed
106
+
107
107
if ( ! this . alerts [ alert . type ] [ service . name ] ) {
108
108
this . alerts [ alert . type ] [ service . name ] = {
109
109
status : { ...service . status , count : { ...service . status . count } }
110
110
} ;
111
+
111
112
this . alerts [ alert . type ] [ service . name ] . status . count [ alert . type ] -- ;
113
+
114
+ // If sky puppy just started and the first status is healthy then don't alert.
115
+ var count = this . alerts [ alert . type ] [ service . name ] . status . count ;
116
+ var total =
117
+ count . healthy +
118
+ count . down +
119
+ count . unhealthy +
120
+ count . unhealthy_response_time +
121
+ count . unhealthy_status ;
122
+
123
+ if ( service . status . up === 1 && total === 0 ) {
124
+ this . alerts [ alert . type ] [ service . name ] . alerted = true ;
125
+ console . log ( service . name , 'is healthy!' ) ;
126
+ return ;
127
+ }
112
128
}
113
129
114
130
if (
@@ -118,7 +134,7 @@ class Alerts {
118
134
! this . alerts [ alert . type ] [ service . name ] . alerted
119
135
) {
120
136
this . alerts [ alert . type ] [ service . name ] . alerted = true ;
121
- await this . _send_alert ( alert , service ) ;
137
+ await this . _sendAlert ( alert , service ) ;
122
138
return ;
123
139
}
124
140
} else {
@@ -146,39 +162,40 @@ class Alerts {
146
162
}
147
163
}
148
164
149
- async _send_alert ( alert , service ) {
165
+ async _sendAlert ( alert , service ) {
150
166
if ( this . alerters [ alert . alerter ] ) {
151
- var overrides = alert . overrides || { } ;
152
- var request = { ...this . alerters [ alert . alerter ] . request , ...overrides } ;
153
-
154
- if ( request . body ) {
155
- request . body = this . nbars . transform (
156
- request . json ? JSON . stringify ( request . body ) : request . body ,
157
- {
158
- alert_type : alert . type ,
159
- service_name : service . name ,
160
- message : service . status . message ,
161
- timestamp : new Date ( ) . toISOString ( ) ,
162
- last_unhealthy_total_duration :
163
- service . status . last_unhealthy_total_duration || 'Unknown' ,
164
- last_healthy_total_duration : service . status . last_healthy ?
165
- (
166
- Number (
167
- process . hrtime . bigint ( ) - service . status . last_healthy
168
- ) / 1000000000
169
- ) . toFixed ( 2 ) :
170
- 'Unknown'
167
+ try {
168
+ var overrides = alert . overrides || { } ;
169
+ var request = { ...this . alerters [ alert . alerter ] . request , ...overrides } ;
170
+
171
+ if ( request . body ) {
172
+ request . body = this . nbars . transform (
173
+ request . json ? JSON . stringify ( request . body ) : request . body ,
174
+ {
175
+ alert_type : alert . type ,
176
+ service_name : service . name ,
177
+ message : service . status . message || '' ,
178
+ timestamp : new Date ( ) . toISOString ( ) ,
179
+ last_unhealthy_total_duration :
180
+ service . status . last_unhealthy_total_duration || 'Unknown' ,
181
+ last_healthy_total_duration : service . status . last_healthy ?
182
+ (
183
+ Number (
184
+ process . hrtime . bigint ( ) - service . status . last_healthy
185
+ ) / 1000000000
186
+ ) . toFixed ( 2 ) :
187
+ 'Unknown'
188
+ }
189
+ ) ;
190
+
191
+ if ( request . json ) {
192
+ //console.log(request.body);
193
+ request . body = JSON . parse ( request . body ) ;
171
194
}
172
- ) ;
173
-
174
- if ( request . json ) {
175
- request . body = JSON . parse ( request . body ) ;
176
195
}
177
- }
178
- //console.log(request.body)
179
- this . alerts_status [ service . name ] = service . status . up ;
196
+ //console.log(request.body)
197
+ this . alerts_status [ service . name ] = service . status . up ;
180
198
181
- try {
182
199
await fasquest . request ( JSON . parse ( JSON . stringify ( request ) ) ) ;
183
200
} catch ( e ) {
184
201
console . log ( e ) ;
0 commit comments