Skip to content

Commit

Permalink
Fix/1176 asymmetric recovery (#1210)
Browse files Browse the repository at this point in the history
* initial fix

* add test

* add write permission to pipeline (#1209)

* fix test expectation
  • Loading branch information
sapiderman authored Dec 18, 2023
1 parent bdb4855 commit 63ccdf8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 21 deletions.
59 changes: 50 additions & 9 deletions src/components/downtime-counter/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@
**********************************************************************************/

import { expect } from '@oclif/test'
import {
getDowntimeDuration,
startDowntimeCounter,
stopDowntimeCounter,
} from '.'
import { getDowntimeDuration, addIncident, removeIncident } from '.'
import { getContext } from '../../context'

describe('Downtime counter', () => {
it('should start counter', () => {
Expand All @@ -39,7 +36,7 @@ describe('Downtime counter', () => {
}

// act
startDowntimeCounter(probeConfig)
addIncident(probeConfig)

// assert
expect(getDowntimeDuration(probeConfig)).not.eq('0 seconds')
Expand All @@ -54,8 +51,8 @@ describe('Downtime counter', () => {
}

// act
startDowntimeCounter(probeConfig)
stopDowntimeCounter(probeConfig)
addIncident(probeConfig)
removeIncident(probeConfig)

// assert
expect(getDowntimeDuration(probeConfig)).eq('0 seconds')
Expand All @@ -70,7 +67,7 @@ describe('Downtime counter', () => {
}

// act
stopDowntimeCounter(probeConfig)
removeIncident(probeConfig)

// assert
expect(getDowntimeDuration(probeConfig)).eq('0 seconds')
Expand All @@ -87,4 +84,48 @@ describe('Downtime counter', () => {
// assert
expect(getDowntimeDuration(probeConfig)).eq('0 seconds')
})

it('allow identical urls but different probeID', () => {
// arrange
const probeConfig = {
alert: { id: 'VyYwG', assertion: '', message: '' },
probeID: 'Pn9x',
url: 'https://example.com',
}
const probeConfig2 = {
alert: { id: 'VyYwG', assertion: '', message: '' },
probeID: 'Pn9x-2',
url: 'https://example.com',
}

// act
addIncident(probeConfig)
addIncident(probeConfig2)
removeIncident(probeConfig)

// assert
expect(getContext().incidents[0].probeID).eq(probeConfig2.probeID)
})

it('allow identical probe-ids but different urls', () => {
// arrange
const probeConfig = {
alert: { id: 'VyYwG', assertion: '', message: '' },
probeID: 'Pn9x',
url: 'https://example.com',
}
const probeConfig2 = {
alert: { id: 'VyYwG', assertion: '', message: '' },
probeID: 'Pn9x',
url: 'https://sub.example.com',
}

// act
addIncident(probeConfig)
addIncident(probeConfig2)
removeIncident(probeConfig)

// assert
expect(getContext().incidents[0].probeRequestURL).eq(probeConfig2.url)
})
})
7 changes: 4 additions & 3 deletions src/components/downtime-counter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type DowntimeCounter = {
createdAt?: Date
}

export function startDowntimeCounter({
export function addIncident({
alert,
createdAt,
probeID,
Expand Down Expand Up @@ -67,10 +67,11 @@ export function getDowntimeDuration({
})
}

export function stopDowntimeCounter({ probeID, url }: DowntimeCounter): void {
export function removeIncident({ probeID, url }: DowntimeCounter): void {
const newIncidents = getContext().incidents.filter(
(incident) =>
incident.probeID !== probeID && incident.probeRequestURL !== url
incident.probeID !== probeID || incident.probeRequestURL !== url
// remove incidents with exact mach of probeID and url
)

setContext({ incidents: newIncidents })
Expand Down
4 changes: 2 additions & 2 deletions src/components/probe/prober/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import responseChecker from '../../../../plugins/validate-response/checkers'
import { getAlertID } from '../../../../utils/alert-id'
import { getEventEmitter } from '../../../../utils/events'
import { isSymonModeFrom } from '../../../config'
import { startDowntimeCounter } from '../../../downtime-counter'
import { addIncident } from '../../../downtime-counter'
import { saveProbeRequestLog } from '../../../logger/history'
import { logResponseTime } from '../../../logger/response-time-log'
import { httpRequest } from './request'
Expand Down Expand Up @@ -200,7 +200,7 @@ export class HTTPProber extends BaseProber {
alertQuery: '',
})

startDowntimeCounter({
addIncident({
alert: triggeredAlert,
probeID,
url,
Expand Down
11 changes: 4 additions & 7 deletions src/components/probe/prober/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ import {
DEFAULT_INCIDENT_THRESHOLD,
DEFAULT_RECOVERY_THRESHOLD,
} from '../../config/validation/validator/default-values'
import {
startDowntimeCounter,
stopDowntimeCounter,
} from '../../downtime-counter'
import { addIncident, removeIncident } from '../../downtime-counter'
import { saveNotificationLog, saveProbeRequestLog } from '../../logger/history'
import { logResponseTime } from '../../logger/response-time-log'
import { sendAlerts } from '../../notification'
Expand Down Expand Up @@ -330,7 +327,7 @@ export class BaseProber implements Prober {
alertQuery: failedRequestAssertion,
})

startDowntimeCounter({
addIncident({
alert: failedRequestAssertion,
probeID: this.probeConfig.id,
url: this.probeConfig?.requests?.[requestIndex].url || '',
Expand Down Expand Up @@ -374,7 +371,7 @@ export class BaseProber implements Prober {
) || 0

if (recoveredIncident) {
stopDowntimeCounter({
removeIncident({
alert: recoveredIncident.alert,
probeID: this.probeConfig.id,
url: this.probeConfig?.requests?.[requestIndex].url || '',
Expand Down Expand Up @@ -448,7 +445,7 @@ export class BaseProber implements Prober {
return
}

startDowntimeCounter({
addIncident({
alert,
probeID: this.probeConfig.id,
url: request.url,
Expand Down

0 comments on commit 63ccdf8

Please sign in to comment.