implemented amplitude metrics (#1141)
This commit is contained in:
parent
1a483cad55
commit
9b37e92a81
26 changed files with 774 additions and 528 deletions
|
@ -1,9 +1,20 @@
|
|||
const storage = require('../storage');
|
||||
const { statDeleteEvent } = require('../amplitude');
|
||||
|
||||
module.exports = async function(req, res) {
|
||||
try {
|
||||
await storage.del(req.params.id);
|
||||
const id = req.params.id;
|
||||
const meta = req.meta;
|
||||
const ttl = await storage.ttl(id);
|
||||
await storage.del(id);
|
||||
res.sendStatus(200);
|
||||
statDeleteEvent({
|
||||
id,
|
||||
ip: req.ip,
|
||||
owner: meta.owner,
|
||||
download_count: meta.dl,
|
||||
ttl
|
||||
});
|
||||
} catch (e) {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
const storage = require('../storage');
|
||||
const mozlog = require('../log');
|
||||
const log = mozlog('send.download');
|
||||
const { statDownloadEvent } = require('../amplitude');
|
||||
|
||||
module.exports = async function(req, res) {
|
||||
const id = req.params.id;
|
||||
|
@ -21,6 +22,14 @@ module.exports = async function(req, res) {
|
|||
|
||||
const dl = meta.dl + 1;
|
||||
const dlimit = meta.dlimit;
|
||||
const ttl = await storage.ttl(id);
|
||||
statDownloadEvent({
|
||||
id,
|
||||
ip: req.ip,
|
||||
owner: meta.owner,
|
||||
download_count: dl,
|
||||
ttl
|
||||
});
|
||||
try {
|
||||
if (dl >= dlimit) {
|
||||
await storage.del(id);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
const crypto = require('crypto');
|
||||
const express = require('express');
|
||||
const helmet = require('helmet');
|
||||
const uaparser = require('ua-parser-js');
|
||||
const storage = require('../storage');
|
||||
const config = require('../config');
|
||||
const auth = require('../middleware/auth');
|
||||
|
@ -12,6 +13,7 @@ const IS_DEV = config.env === 'development';
|
|||
const ID_REGEX = '([0-9a-fA-F]{10})';
|
||||
|
||||
module.exports = function(app) {
|
||||
app.set('trust proxy', true);
|
||||
app.use(helmet());
|
||||
app.use(
|
||||
helmet.hsts({
|
||||
|
@ -19,6 +21,10 @@ module.exports = function(app) {
|
|||
force: !IS_DEV
|
||||
})
|
||||
);
|
||||
app.use(function(req, res, next) {
|
||||
req.ua = uaparser(req.header('user-agent'));
|
||||
next();
|
||||
});
|
||||
app.use(function(req, res, next) {
|
||||
req.cspNonce = crypto.randomBytes(16).toString('hex');
|
||||
next();
|
||||
|
@ -35,12 +41,10 @@ module.exports = function(app) {
|
|||
'wss://send.firefox.com',
|
||||
'https://*.dev.lcip.org',
|
||||
'https://*.accounts.firefox.com',
|
||||
'https://sentry.prod.mozaws.net',
|
||||
'https://www.google-analytics.com'
|
||||
'https://sentry.prod.mozaws.net'
|
||||
],
|
||||
imgSrc: [
|
||||
"'self'",
|
||||
'https://www.google-analytics.com',
|
||||
'https://*.dev.lcip.org',
|
||||
'https://firefoxusercontent.com'
|
||||
],
|
||||
|
@ -92,7 +96,7 @@ module.exports = function(app) {
|
|||
require('./params')
|
||||
);
|
||||
app.post(`/api/info/:id${ID_REGEX}`, auth.owner, require('./info'));
|
||||
|
||||
app.post('/api/metrics', require('./metrics'));
|
||||
app.get('/__version__', function(req, res) {
|
||||
res.sendFile(require.resolve('../../dist/version.json'));
|
||||
});
|
||||
|
|
23
server/routes/metrics.js
Normal file
23
server/routes/metrics.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
const { sendBatch, clientEvent } = require('../amplitude');
|
||||
|
||||
module.exports = async function(req, res) {
|
||||
try {
|
||||
const data = req.body;
|
||||
const deltaT = Date.now() - data.now;
|
||||
const events = data.events.map(e =>
|
||||
clientEvent(
|
||||
e,
|
||||
req.ua,
|
||||
data.lang,
|
||||
data.session_id + deltaT,
|
||||
deltaT,
|
||||
data.platform,
|
||||
req.ip
|
||||
)
|
||||
);
|
||||
const status = await sendBatch(events);
|
||||
res.sendStatus(status);
|
||||
} catch (e) {
|
||||
res.sendStatus(500);
|
||||
}
|
||||
};
|
|
@ -5,6 +5,7 @@ const mozlog = require('../log');
|
|||
const Limiter = require('../limiter');
|
||||
const wsStream = require('websocket-stream/stream');
|
||||
const fxa = require('../fxa');
|
||||
const { statUploadEvent } = require('../amplitude');
|
||||
|
||||
const { Duplex } = require('stream');
|
||||
|
||||
|
@ -105,6 +106,15 @@ module.exports = function(ws, req) {
|
|||
// in order to avoid having to check socket state and clean
|
||||
// up storage, possibly with an exception that we can catch.
|
||||
ws.send(JSON.stringify({ ok: true }));
|
||||
statUploadEvent({
|
||||
id: newId,
|
||||
ip: req.ip,
|
||||
owner,
|
||||
dlimit,
|
||||
timeLimit,
|
||||
anonymous: !user,
|
||||
size: limiter.length
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
log.error('upload', e);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue