2021-07-14 23:49:18 +03:00
|
|
|
import crypto from 'crypto'
|
2021-12-02 07:30:44 +03:00
|
|
|
import got from 'got'
|
2021-07-14 23:49:18 +03:00
|
|
|
import statsd from '../lib/statsd.js'
|
|
|
|
import FailBot from '../lib/failbot.js'
|
2021-04-07 02:08:51 +03:00
|
|
|
|
2021-11-03 19:50:35 +03:00
|
|
|
const TIME_OUT_TEXT = 'ms has passed since batch creation'
|
|
|
|
|
2021-12-02 07:30:44 +03:00
|
|
|
// If the request to Hydro took an age, it could be either our
|
|
|
|
// network or that Hydro is just slow to respond. (Event possibly
|
|
|
|
// too slow to respond with a 422 code)
|
|
|
|
// If this happens, we'd rather kill it now rather than let it
|
|
|
|
// linger within the thread.
|
|
|
|
const POST_TIMEOUT_MS = 3000
|
|
|
|
|
2021-07-14 23:49:18 +03:00
|
|
|
export default class Hydro {
|
2021-07-15 00:35:01 +03:00
|
|
|
constructor({ secret, endpoint } = {}) {
|
2020-09-30 19:49:27 +03:00
|
|
|
this.secret = secret || process.env.HYDRO_SECRET
|
|
|
|
this.endpoint = endpoint || process.env.HYDRO_ENDPOINT
|
|
|
|
}
|
|
|
|
|
2020-10-26 20:18:42 +03:00
|
|
|
/**
|
|
|
|
* Can check if it can actually send to Hydro
|
|
|
|
*/
|
2021-07-15 00:35:01 +03:00
|
|
|
maySend() {
|
2021-11-11 00:38:26 +03:00
|
|
|
return Boolean(this.secret && this.endpoint && process.env.NODE_ENV !== 'test')
|
2020-10-26 20:18:42 +03:00
|
|
|
}
|
|
|
|
|
2020-09-30 19:49:27 +03:00
|
|
|
/**
|
|
|
|
* Generate a SHA256 hash of the payload using the secret
|
|
|
|
* to authenticate with Hydro
|
|
|
|
* @param {string} body
|
|
|
|
*/
|
2021-07-15 00:35:01 +03:00
|
|
|
generatePayloadHmac(body) {
|
|
|
|
return crypto.createHmac('sha256', this.secret).update(body).digest('hex')
|
2020-09-30 19:49:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Publish a single event to Hydro
|
|
|
|
* @param {string} schema
|
|
|
|
* @param {any} value
|
|
|
|
*/
|
2021-07-15 00:35:01 +03:00
|
|
|
async publish(schema, value) {
|
2020-10-08 19:24:41 +03:00
|
|
|
const body = JSON.stringify({
|
2021-11-02 19:13:48 +03:00
|
|
|
events: [
|
|
|
|
{
|
|
|
|
schema,
|
|
|
|
value: JSON.stringify(value), // We must double-encode the value property
|
|
|
|
cluster: 'potomac', // We only have ability to publish externally to potomac cluster
|
|
|
|
},
|
|
|
|
],
|
2020-10-08 19:24:41 +03:00
|
|
|
})
|
2020-09-30 19:49:27 +03:00
|
|
|
const token = this.generatePayloadHmac(body)
|
|
|
|
|
2021-12-02 07:30:44 +03:00
|
|
|
const doPost = () =>
|
|
|
|
got(this.endpoint, {
|
2021-07-15 00:35:01 +03:00
|
|
|
method: 'POST',
|
|
|
|
body,
|
|
|
|
headers: {
|
|
|
|
Authorization: `Hydro ${token}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Hydro-App': 'docs-production',
|
|
|
|
},
|
2021-12-02 07:30:44 +03:00
|
|
|
// Because we prefer to handle the status code manually below
|
|
|
|
throwHttpErrors: false,
|
|
|
|
// The default is no timeout.
|
|
|
|
timeout: POST_TIMEOUT_MS,
|
2021-07-15 00:35:01 +03:00
|
|
|
})
|
2021-04-07 02:08:51 +03:00
|
|
|
|
2021-12-02 07:30:44 +03:00
|
|
|
const res = await statsd.asyncTimer(doPost, 'hydro.response_time')()
|
2021-04-07 02:08:51 +03:00
|
|
|
|
2021-12-02 07:30:44 +03:00
|
|
|
const statTags = [`response_code:${res.statusCode}`]
|
|
|
|
statsd.increment(`hydro.response_code.${res.statusCode}`, 1, statTags)
|
2021-04-11 00:06:22 +03:00
|
|
|
statsd.increment('hydro.response_code.all', 1, statTags)
|
2021-04-07 02:08:51 +03:00
|
|
|
|
2021-11-02 19:13:48 +03:00
|
|
|
// Track hydro exceptions in Sentry,
|
|
|
|
// but don't track 5xx because we can't do anything about service availability
|
2021-12-02 07:30:44 +03:00
|
|
|
if (res.statusCode !== 200 && res.statusCode < 500) {
|
2022-01-14 20:09:48 +03:00
|
|
|
const hydroText = await res.text()
|
2021-12-02 07:30:44 +03:00
|
|
|
const err = new Error(
|
2022-01-14 20:09:48 +03:00
|
|
|
`Hydro request failed: (${res.statusCode}) ${res.statusMessage} - ${hydroText}`
|
2021-12-02 07:30:44 +03:00
|
|
|
)
|
|
|
|
err.status = res.statusCode
|
2021-04-07 02:28:41 +03:00
|
|
|
|
2022-01-14 20:09:48 +03:00
|
|
|
// If the Hydro request failed as an "Unprocessable Entity":
|
|
|
|
// - If it was a timeout, don't log it to Sentry
|
|
|
|
// - If not, log it to Sentry for diagnostics
|
|
|
|
const hydroFailures = []
|
|
|
|
if (res.statusCode === 422) {
|
|
|
|
let failureResponse
|
|
|
|
try {
|
|
|
|
failureResponse = JSON.parse(hydroText)
|
|
|
|
} catch (error) {
|
|
|
|
// Not JSON... ignore it
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failureResponse) {
|
|
|
|
const { failures } = failureResponse
|
|
|
|
if (Array.isArray(failures) && failures.length > 0) {
|
|
|
|
// IMPORTANT: Although these timeouts typically contain a `retriable: true` property,
|
|
|
|
// our discussions with the Hydro team left us deciding we did NOT want to retry
|
|
|
|
// sending them. The timeout response does NOT guarantee that the original message
|
|
|
|
// failed to make it through. As such, if we resend, we may create duplicate events;
|
|
|
|
// if we don't, we may drop events.
|
|
|
|
|
|
|
|
// Find the timeouts, if any
|
|
|
|
const timeoutFailures = failures.filter(({ error }) => error.includes(TIME_OUT_TEXT))
|
|
|
|
|
|
|
|
// If there were ONLY timeouts, throw the error to avoid logging to Sentry
|
|
|
|
if (timeoutFailures.length === failures.length) {
|
|
|
|
err.message = `Hydro timed out: ${failures}`
|
|
|
|
err.status = 503 // Give it a more accurate error code
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile list of the other failures for logging
|
|
|
|
hydroFailures.push(...failures.filter(({ error }) => !error.includes(TIME_OUT_TEXT)))
|
|
|
|
}
|
|
|
|
}
|
2021-11-02 19:13:48 +03:00
|
|
|
|
2022-01-14 20:09:48 +03:00
|
|
|
console.error(
|
|
|
|
`Hydro schema validation failed:\n - Request: ${body}\n - Failure: (${res.statusCode}) ${hydroText}`
|
|
|
|
)
|
|
|
|
}
|
2021-11-03 19:50:35 +03:00
|
|
|
|
2021-04-07 02:28:41 +03:00
|
|
|
FailBot.report(err, {
|
2021-12-02 07:30:44 +03:00
|
|
|
hydroStatus: res.statusCode,
|
2022-01-14 20:09:48 +03:00
|
|
|
hydroText,
|
|
|
|
hydroFailures,
|
2021-04-07 02:28:41 +03:00
|
|
|
})
|
|
|
|
|
2021-04-07 02:38:45 +03:00
|
|
|
throw err
|
2021-04-07 02:28:41 +03:00
|
|
|
}
|
|
|
|
|
2021-04-07 02:38:45 +03:00
|
|
|
return res
|
2020-09-30 19:49:27 +03:00
|
|
|
}
|
|
|
|
}
|