Simplify getMeteredStream to record metric directly

This commit is contained in:
Simon Detheridge 2020-03-04 16:17:36 +00:00
parent 76243fd75a
commit def383574e
4 changed files with 11 additions and 25 deletions

View file

@ -1,5 +1,4 @@
const settings = require('settings-sharelatex')
const metrics = require('metrics-sharelatex')
const fs = require('fs')
const { promisify } = require('util')
const Stream = require('stream')
@ -79,9 +78,7 @@ async function sendStream(bucketName, key, readStream, sourceMd5) {
const meteredStream = PersistorHelper.getMeteredStream(
readStream,
(_, byteCount) => {
metrics.count('gcs.egress', byteCount)
}
'gcs.egress'
)
const writeOptions = {
@ -129,10 +126,7 @@ async function getFileStream(bucketName, key, opts = {}) {
.file(key)
.createReadStream(opts)
const meteredStream = PersistorHelper.getMeteredStream(stream, (_, bytes) => {
// ignore the error parameter and just log the byte count
metrics.count('gcs.ingress', bytes)
})
const meteredStream = PersistorHelper.getMeteredStream(stream, 'gcs.ingress')
try {
await PersistorHelper.waitForStreamReady(stream)

View file

@ -1,4 +1,5 @@
const crypto = require('crypto')
const metrics = require('metrics-sharelatex')
const meter = require('stream-meter')
const Stream = require('stream')
const logger = require('logger-sharelatex')
@ -54,16 +55,16 @@ async function verifyMd5(persistor, bucket, key, sourceMd5, destMd5 = null) {
// returns the next stream in the pipeline, and calls the callback with the byte count
// when the stream finishes or receives an error
function getMeteredStream(stream, callback) {
function getMeteredStream(stream, metricName) {
const meteredStream = meter()
pipeline(stream, meteredStream)
.then(() => {
callback(null, meteredStream.bytes)
metrics.count(metricName, meteredStream.bytes)
})
.catch(err => {
.catch(() => {
// on error, just send how many bytes we received before the stream stopped
callback(err, meteredStream.bytes)
metrics.count(metricName, meteredStream.bytes)
})
return meteredStream

View file

@ -4,7 +4,6 @@ http.globalAgent.maxSockets = 300
https.globalAgent.maxSockets = 300
const settings = require('settings-sharelatex')
const metrics = require('metrics-sharelatex')
const PersistorHelper = require('./PersistorHelper')
@ -75,10 +74,7 @@ async function sendStream(bucketName, key, readStream, sourceMd5) {
const meteredStream = PersistorHelper.getMeteredStream(
readStream,
(_, byteCount) => {
// ignore the error parameter and just log the byte count
metrics.count('s3.egress', byteCount)
}
's3.egress'
)
// if we have an md5 hash, pass this to S3 to verify the upload
@ -143,13 +139,7 @@ async function getFileStream(bucketName, key, opts) {
.getObject(params)
.createReadStream()
const meteredStream = PersistorHelper.getMeteredStream(
stream,
(_, byteCount) => {
// ignore the error parameter and just log the byte count
metrics.count('s3.ingress', byteCount)
}
)
const meteredStream = PersistorHelper.getMeteredStream(stream, 's3.ingress')
try {
await PersistorHelper.waitForStreamReady(stream)

View file

@ -73,7 +73,8 @@ describe('FSPersistorTests', function() {
crypto,
// imported by PersistorHelper but otherwise unused here
'stream-meter': {},
'logger-sharelatex': {}
'logger-sharelatex': {},
'metrics-sharelatex': {}
},
globals: { console }
})