-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmonitor.ts
More file actions
157 lines (149 loc) · 4.92 KB
/
monitor.ts
File metadata and controls
157 lines (149 loc) · 4.92 KB
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
import { Region } from '..'
import { AlertChannel, AlertChannelRef } from './alert-channel'
import { AlertEscalation } from './alert-escalation-policy'
import { CheckGroupRef } from './check-group-ref'
import { CheckGroupV1 } from './check-group-v1'
import { CheckGroupV2 } from './check-group-v2'
import { Frequency } from './frequency'
import { IncidentTrigger } from './incident'
import { PrivateLocation, PrivateLocationRef } from './private-location'
import { NoRetriesRetryStrategy, RetryStrategyType, SingleRetryStrategy } from './retry-strategy'
import { Check, CheckProps } from './check'
import { Diagnostics } from './diagnostics'
import { validateRemovedDoubleCheck } from './internal/common-diagnostics'
import { InvalidPropertyValueDiagnostic } from './construct-diagnostics'
import { ConfigDefaultsGetter, makeConfigDefaultsGetter } from './check-config'
import { Session } from './project'
/**
* Retry strategies supported by monitors.
*/
export type MonitorRetryStrategy =
| SingleRetryStrategy
| NoRetriesRetryStrategy
export interface MonitorProps extends Omit<CheckProps, 'doubleCheck'> {
/**
* The name of the monitor.
*/
name: string
/**
* Determines whether the monitor will run periodically or not after being deployed.
*/
activated?: boolean
/**
* Determines if any notifications will be sent out when a check fails and/or recovers.
*/
muted?: boolean
/**
* An array of one or more data center locations where to run this monitor.
*/
locations?: (keyof Region)[]
/**
* An array of one or more private locations where to run the check.
* PrivateLocation instances or slug name strings are allowed.
*
* `string` slug names are **only** allowed for private locations that do
* **not** belong to the project. Use PrivateLocation instances for private
* locations created within the project.
*/
privateLocations?: (string | PrivateLocation | PrivateLocationRef)[]
/**
* Tags for organizing and filtering checks.
*/
tags?: string[]
/**
* How often the check should run in minutes.
*/
frequency?: number | Frequency
/**
* The CheckGroup that this monitor is part of.
*
* Note that despite their name, CheckGroups can also contain monitors.
*/
group?: CheckGroupV1 | CheckGroupV2 | CheckGroupRef
/**
* List of alert channels to notify when the monitor fails or recovers.
* If you don't set at least one, we won't be able to alert you.
*
* See https://www.checklyhq.com/docs/alerting-and-retries/alert-channels/#alert-channels
* to learn more about alert channels.
*/
alertChannels?: (AlertChannel | AlertChannelRef)[],
/**
* Determines the alert escalation policy for the monitor.
*/
alertEscalationPolicy?: AlertEscalation
/**
* Determines whether the monitor is deployable.
*
* When `true`, the monitor will not be deployed, otherwise, the monitor
* will be deployed.
*/
testOnly?: boolean
/**
* Sets a retry policy for the monitor. Use RetryStrategyBuilder to create a
* suitable retry strategy.
*
* Note that monitors only support a single retry.
*
* @example
* ```typescript
* // Single retry
* RetryStrategyBuilder.singleRetry()
*
* // No retries
* RetryStrategyBuilder.noRetries()
* ```
*/
retryStrategy?: MonitorRetryStrategy
/**
* Determines whether the monitor should create and resolve an incident
* based on its alert configuration.
*
* See https://www.checklyhq.com/docs/status-pages/incidents/#incident-automation
* to learn more about automated incidents.
*/
triggerIncident?: IncidentTrigger
}
export abstract class Monitor extends Check {
constructor (logicalId: string, props: MonitorProps) {
super(logicalId, props)
}
protected async validateDoubleCheck (diagnostics: Diagnostics): Promise<void> {
await validateRemovedDoubleCheck(diagnostics, this)
}
async validate (diagnostics: Diagnostics): Promise<void> {
await super.validate(diagnostics)
if (this.retryStrategy) {
const supported: RetryStrategyType[] = ['SINGLE', 'NO_RETRIES']
if (!supported.includes(this.retryStrategy.type)) {
diagnostics.add(new InvalidPropertyValueDiagnostic(
'retryStrategy',
new Error(
`Monitors only support a single retry. The following options ` +
`are available:` +
`\n\n` +
` // Single retry\n` +
` RetryStrategyBuilder.singleRetry()` +
`\n\n` +
` // No retries\n` +
` RetryStrategyBuilder.noRetries()`,
),
))
}
}
}
protected configDefaultsGetter (props: MonitorProps): ConfigDefaultsGetter {
return makeConfigDefaultsGetter(
props.group?.getMonitorDefaults(),
Session.monitorDefaults,
props.group?.getCheckDefaults(),
Session.checkDefaults,
)
}
synthesize() {
return {
...super.synthesize(),
doubleCheck: false,
}
}
}