-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a webhook that accepts metrics from Calibre and forwards them to DataDog.
- Loading branch information
Showing
20 changed files
with
2,340 additions
and
1,244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,5 @@ node_modules | |
npm-debug.log | ||
.vscode | ||
.circleci | ||
|
||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
NODE_ENV=test | ||
DATADOG_AGENT_HOSTNAME=test-hostname | ||
DATADOG_AGENT_HOSTNAME=test-hostname |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,5 @@ typings/ | |
# dotenv environment variables file | ||
.env | ||
|
||
# Dist folder | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,44 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
"version": "0.1.0", | ||
"version": "2.0.0", | ||
"command": "yarn", | ||
"isShellCommand": true, | ||
"showOutput": "always", | ||
"suppressTaskName": true, | ||
"tasks": [ | ||
{ | ||
"taskName": "install", | ||
"args": ["install"] | ||
"label": "install", | ||
"type": "shell", | ||
"args": [ | ||
"install" | ||
], | ||
"problemMatcher": [] | ||
}, | ||
{ | ||
"taskName": "update", | ||
"args": ["update"] | ||
"label": "update", | ||
"type": "shell", | ||
"args": [ | ||
"update" | ||
], | ||
"problemMatcher": [] | ||
}, | ||
{ | ||
"taskName": "test", | ||
"args": ["run", "test"] | ||
"label": "test", | ||
"type": "shell", | ||
"args": [ | ||
"run", | ||
"test" | ||
], | ||
"problemMatcher": [], | ||
"group": "test" | ||
}, | ||
{ | ||
"taskName": "build", | ||
"isBuildCommand": true, | ||
"args": ["run", "build"] | ||
"label": "build", | ||
"type": "shell", | ||
"args": [ | ||
"run", | ||
"build" | ||
], | ||
"problemMatcher": [], | ||
"group": "build" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,30 @@ | ||
FROM node:12.18-alpine | ||
|
||
WORKDIR /app | ||
|
||
# Install dumb-init | ||
# Set up deploy user | ||
RUN apk --no-cache --quiet add \ | ||
dumb-init && \ | ||
adduser -D -g '' deploy | ||
|
||
WORKDIR /app | ||
RUN chown deploy:deploy $(pwd) | ||
|
||
# Switch to deploy user | ||
USER deploy | ||
|
||
# Set up node modules | ||
COPY package.json yarn.lock ./ | ||
COPY --chown=deploy:deploy package.json yarn.lock ./ | ||
RUN yarn install --frozen-lockfile && yarn cache clean | ||
|
||
ENV PORT 8080 | ||
EXPOSE 8080 | ||
|
||
# Finally, add the rest of our app's code | ||
# (this is done at the end so that changes to our app's code | ||
# don't bust Docker's cache) | ||
COPY . ./ | ||
RUN chown -R deploy:deploy ./ | ||
COPY --chown=deploy:deploy . ./ | ||
|
||
# Switch to deploy user | ||
USER deploy | ||
|
||
ENV PORT 8080 | ||
EXPOSE 8080 | ||
RUN yarn build | ||
|
||
ENTRYPOINT ["/usr/bin/dumb-init", "--"] | ||
CMD ["node", "index.js"] | ||
CMD ["node", "dist/index.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { CalibrePage } from '../common/calibre' | ||
import { VolleyApi } from '../common/volley' | ||
import Koa from 'koa' | ||
import Router from '@koa/router' | ||
import type { postMetric } from './postMetric' | ||
|
||
const CALIBRE_SERVICE_PREFIX = process.env.CALIBRE_SERVICE_PREFIX || 'calibre' | ||
|
||
const CALIBRE_PRODUCTION_SITE = process.env.CALIBRE_PRODUCTION_SITE ? process.env.CALIBRE_PRODUCTION_SITE.split('/') : [ 'artsy_net' ] | ||
|
||
function sanitize(data: string): string { | ||
return data.replace(/[- ]/g, "_") | ||
} | ||
|
||
export const calibreWebhookRoute = (postMetric: postMetric) => { | ||
return (ctx: Koa.ParameterizedContext<any, Router.RouterParamContext<any, {}>, any>) => { | ||
const siteId = sanitize(ctx.request.body.site_id) | ||
const environment = (CALIBRE_PRODUCTION_SITE.indexOf(siteId) !== -1) ? 'production' : 'staging' | ||
|
||
const pages = ctx.request.body.pages as CalibrePage[] | ||
for (let page of pages) { | ||
const pageId = sanitize(page.id) | ||
const pageDevice = sanitize(page.profile).toLowerCase() | ||
const deviceType = (pageDevice === 'chrome_desktop') ? 'desktop' : 'mobile' | ||
|
||
for (let metric of page.metrics) { | ||
const prefix = `${CALIBRE_SERVICE_PREFIX}.${siteId}` | ||
|
||
const volley = VolleyApi.fromCalibreApi(metric, [ | ||
`env:${environment}`, `form:${deviceType}`, `mode:${pageDevice}`, `page:${pageId}` | ||
]) | ||
|
||
postMetric(prefix, volley) | ||
} | ||
} | ||
|
||
ctx.status = 202 | ||
ctx.body = 'OK' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* The shape of a calibre page report | ||
*/ | ||
export type CalibrePage = { | ||
id: string | ||
uuid: string | ||
name: string | ||
status: string | ||
endpoint: string | ||
canonical: string | ||
profile: string | ||
profile_uuid: string | ||
metrics: CalibreMetric[] | ||
} | ||
|
||
export type CalibreMetric = { | ||
name: string | ||
value: number | ||
/** | ||
* Accepted values | ||
* | ||
* fileSize | ||
* gradeScore | ||
* humanDuration | ||
* milliunit | ||
* trust | ||
*/ | ||
formatter: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { CalibreMetric } from './calibre' | ||
|
||
interface VolleyMetric { | ||
type: 'timing' | 'increment' | 'incrementBy' | 'decrement' | 'decrementBy' | 'gauge' | 'histogram' | 'set' | ||
name: string, | ||
value: number | ||
} | ||
|
||
interface VolleySampledMetric extends VolleyMetric { | ||
sampleRate: number | ||
} | ||
|
||
interface VolleyTaggedMetric extends VolleyMetric { | ||
tags: string[] | ||
} | ||
|
||
interface VolleyTaggedSampledMetric extends VolleySampledMetric { | ||
tags: string[] | ||
} | ||
|
||
export type VolleyMetrics = VolleyMetric | VolleySampledMetric | VolleyTaggedMetric | VolleyTaggedSampledMetric | ||
|
||
export class VolleyApi { | ||
public static fromCalibreApi(calibreMetric: CalibreMetric, tags: string[]): VolleyMetrics { | ||
return { | ||
type: 'gauge', | ||
name: calibreMetric.name.replace(/[- ]/g, "_"), | ||
value: calibreMetric.value, | ||
tags: tags, | ||
} | ||
} | ||
} |
Oops, something went wrong.