uri-monitor v0.5.0-0
:toc: macro :toc-title: :toclevels: 9
URI Monitor
image:https://circleci.com/gh/jasonkuhrt/uri-monitor.svg?style=svg["Circle CI", link="https://circleci.com/gh/jasonkuhrt/uri-monitor"]
toc::[]
Installation
npm install --save uri-monitorExamples
Basis for following examples:
import Monitor from 'uri-monitor'
const monitor = Monitor.create('https://foo.io', 1500)Monitoring outages:
const serviceChanges = monitor.filter(({ type }) => type === 'change')
serviceChanges
.filter({ data: { isResponsive }} => !isResponsive)
.observe(({ data: { result }}) => console.warn('Service Down!', result))
serviceChanges
.filter({ data: { isResponsive }} => isResponsive)
.observe(() => console.warn('Service Up!'))Monitoring outages via <<convenience-streams-on-stream, Stream sugar>>:
monitor
.downs
.observe(() => console.warn('Service Down!'))
serviceChanges
.ups
.observe(() => console.warn('Service Up!'))API
.create
create :: String, Maybe Integer -> Stream MonitorEvent
-- uri intervaluriThe URI that periodic GET requests will be made to.intervalOptional, defaults to1000. The milliseconds between each request. Note this is not the time between a response and the next request because requests are evenly staggered. For example:
+
--- = Time Q = Request R = Response
Q1----R1-Q2-R2----Q3--R3---Q4--
^ ^ ^ ^ <== Evenly staggered+
A response cannot temporally leak into the next request because each request is configured to timeout within the interval.
Definition of request failure
The following will cause the result of a request to be considered a failure:
- Request is not responded to within
interval - Response HTTP Status Code is
>= 400. - Any general HTTP Error (e.g. a client-side CORS error)
In all of these cases data.isResponsive will be false and data.result will be either a <<requesterror, RequestError>> or <<networkerror, NetworkError>>.
Convenience streams on Stream
The <<stream,Stream>> returned by <<create, create>> is monkey patched with filtered variants for common patterns. They are:
.pongs
pongs :: Stream CheckedEvent<<checkedevent, CheckedEvents>> where data.isResponsive is true.
.drops
drops :: Stream CheckedEvent<<checkedevent, CheckedEvents>> where data.isResponsive is false.
.ups
ups :: Stream ChangedEvent<<changedevent, ChangedEvents>> where data.isResponsive is true.
.downs
ups :: Stream ChangedEvent<<changedevent, ChangedEvents>> where data.isResponsive is false.
Types
Stream
An instance of link:https://github.com/cujojs/most/blob/master/docs/api.md[most].
MonitorEvent
Base interface that all emitted events implement. If isResponsive is false then result is either a <<requesterror, RequestError>> or <<networkerror, NetworkError>> otherwise result is a <<response, Response>>.
type : String
data :
isResponsive: Bool
result: CheckResultCheckedEvent
Emitted every time a result of a request is available.
type: "checked"
data:
isResponsive: Bool
result: CheckResultChangedEvent
Emitted every time a result of a request has a different isResponsive value than the previous result.
type: "changed"
data:
isResponsive: Bool
result: CheckResultCheckResult
RequestError | NetworkError | ResponseResponse
A link:https://visionmedia.github.io/superagent/#response-properties[Superagent Response].
RequestError
message: String
status: Integer
body: Object
res: ResponseNetworkError
message: String
originalError: Object