Skip to content

Commit dca7e56

Browse files
authored
More hydro error info (#22533)
* Add more info to hydro failures * Clean up * Update hydro.js * Move hydro names to event schema file * Update hydro.js
1 parent ad0af33 commit dca7e56

File tree

4 files changed

+33
-63
lines changed

4 files changed

+33
-63
lines changed

lib/hydro.js

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,10 @@ import fetch from 'node-fetch'
33
import statsd from '../lib/statsd.js'
44
import FailBot from '../lib/failbot.js'
55

6-
const SCHEMAS = {
7-
page: 'docs.v0.PageEvent',
8-
exit: 'docs.v0.ExitEvent',
9-
link: 'docs.v0.LinkEvent',
10-
search: 'docs.v0.SearchEvent',
11-
searchResult: 'docs.v0.SearchResultEvent',
12-
navigate: 'docs.v0.NavigateEvent',
13-
survey: 'docs.v0.SurveyEvent',
14-
experiment: 'docs.v0.ExperimentEvent',
15-
redirect: 'docs.v0.RedirectEvent',
16-
clipboard: 'docs.v0.ClipboardEvent',
17-
print: 'docs.v0.PrintEvent',
18-
preference: 'docs.v0.PreferenceEvent',
19-
}
20-
216
export default class Hydro {
227
constructor({ secret, endpoint } = {}) {
238
this.secret = secret || process.env.HYDRO_SECRET
249
this.endpoint = endpoint || process.env.HYDRO_ENDPOINT
25-
this.schemas = SCHEMAS
2610
}
2711

2812
/**
@@ -47,20 +31,14 @@ export default class Hydro {
4731
* @param {any} value
4832
*/
4933
async publish(schema, value) {
50-
return this.publishMany([{ schema, value }])
51-
}
52-
53-
/**
54-
* Publish multiple events to Hydro
55-
* @param {[{ schema: string, value: any }]} events
56-
*/
57-
async publishMany(events) {
5834
const body = JSON.stringify({
59-
events: events.map(({ schema, value }) => ({
60-
schema,
61-
value: JSON.stringify(value), // We must double-encode the value property
62-
cluster: 'potomac', // We only have ability to publish externally to potomac cluster
63-
})),
35+
events: [
36+
{
37+
schema,
38+
value: JSON.stringify(value), // We must double-encode the value property
39+
cluster: 'potomac', // We only have ability to publish externally to potomac cluster
40+
},
41+
],
6442
})
6543
const token = this.generatePayloadHmac(body)
6644

@@ -81,23 +59,24 @@ export default class Hydro {
8159
statsd.increment(`hydro.response_code.${res.status}`, 1, statTags)
8260
statsd.increment('hydro.response_code.all', 1, statTags)
8361

84-
// Track hydro exceptions in Sentry, but don't track 503s because we can't do anything about service availability
85-
if (!res.ok && res.status !== 503) {
62+
// Track hydro exceptions in Sentry,
63+
// but don't track 5xx because we can't do anything about service availability
64+
if (!res.ok && res.status < 500) {
8665
const err = new Error(`Hydro request failed: ${res.statusText}`)
8766
err.status = res.status
8867

68+
const failures = await res.text()
69+
8970
FailBot.report(err, {
9071
hydroStatus: res.status,
9172
hydroText: res.statusText,
73+
hydroFailures: failures,
9274
})
9375

9476
// If the Hydro request failed as an "Unprocessable Entity", log it for diagnostics
9577
if (res.status === 422) {
96-
const failures = await res.json()
9778
console.error(
98-
`Hydro schema validation failed:\n - Request: ${body}\n - Failures: ${JSON.stringify(
99-
failures
100-
)}`
79+
`Hydro schema validation failed:\n - Request: ${body}\n - Failures: ${failures}`
10180
)
10281
}
10382

lib/schema-event.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ const preferenceSchema = {
461461
},
462462
}
463463

464-
export default {
464+
export const eventSchema = {
465465
oneOf: [
466466
pageSchema,
467467
exitSchema,
@@ -477,3 +477,18 @@ export default {
477477
preferenceSchema,
478478
],
479479
}
480+
481+
export const hydroNames = {
482+
page: 'docs.v0.PageEvent',
483+
exit: 'docs.v0.ExitEvent',
484+
link: 'docs.v0.LinkEvent',
485+
search: 'docs.v0.SearchEvent',
486+
searchResult: 'docs.v0.SearchResultEvent',
487+
navigate: 'docs.v0.NavigateEvent',
488+
survey: 'docs.v0.SurveyEvent',
489+
experiment: 'docs.v0.ExperimentEvent',
490+
redirect: 'docs.v0.RedirectEvent',
491+
clipboard: 'docs.v0.ClipboardEvent',
492+
print: 'docs.v0.PrintEvent',
493+
preference: 'docs.v0.PreferenceEvent',
494+
}

middleware/events.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express from 'express'
22
import { omit } from 'lodash-es'
33
import Ajv from 'ajv'
44
import addFormats from 'ajv-formats'
5-
import schema from '../lib/schema-event.js'
5+
import { eventSchema, hydroNames } from '../lib/schema-event.js'
66

77
const OMIT_FIELDS = ['type']
88

@@ -15,14 +15,14 @@ router.post('/', async function postEvents(req, res, next) {
1515
const isDev = process.env.NODE_ENV === 'development'
1616
const fields = omit(req.body, '_csrf')
1717

18-
if (!ajv.validate(schema, fields)) {
18+
if (!ajv.validate(eventSchema, fields)) {
1919
return res.status(400).json(isDev ? ajv.errorsText() : {})
2020
}
2121

2222
if (req.hydro.maySend()) {
2323
// intentionally don't await this async request
2424
// so that the http response afterwards is sent immediately
25-
req.hydro.publish(req.hydro.schemas[fields.type], omit(fields, OMIT_FIELDS)).catch((e) => {
25+
req.hydro.publish(hydroNames[fields.type], omit(fields, OMIT_FIELDS)).catch((e) => {
2626
if (isDev) console.error(e)
2727
})
2828
}

tests/unit/hydro.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,6 @@ describe('hydro', () => {
3636
})
3737
})
3838

39-
describe('#publishMany', () => {
40-
it('publishes multiple events to Hydro', async () => {
41-
await hydro.publishMany([
42-
{ schema: 'event-name', value: { pizza: true } },
43-
{ schema: 'other-name', value: { salad: false } },
44-
])
45-
46-
expect(params).toEqual({
47-
events: [
48-
{
49-
schema: 'event-name',
50-
value: JSON.stringify({ pizza: true }),
51-
cluster: 'potomac',
52-
},
53-
{
54-
schema: 'other-name',
55-
value: JSON.stringify({ salad: false }),
56-
cluster: 'potomac',
57-
},
58-
],
59-
})
60-
})
61-
})
62-
6339
describe('#generatePayloadHmac', () => {
6440
it('returns a SHA256 HMAC string', () => {
6541
const body = JSON.stringify({ pizza: true })

0 commit comments

Comments
 (0)