Add metric for s3 egress

This commit is contained in:
Simon Detheridge 2019-12-05 15:25:35 +00:00
parent c01603b1e7
commit 1d1106bc67
4 changed files with 32 additions and 8 deletions

View file

@ -9,6 +9,7 @@ https.globalAgent.maxSockets = 300
settings = require("settings-sharelatex") settings = require("settings-sharelatex")
request = require("request") request = require("request")
logger = require("logger-sharelatex") logger = require("logger-sharelatex")
metrics = require("metrics-sharelatex")
fs = require("fs") fs = require("fs")
knox = require("knox") knox = require("knox")
path = require("path") path = require("path")
@ -73,7 +74,9 @@ module.exports =
sendFile: (bucketName, key, fsPath, callback)-> sendFile: (bucketName, key, fsPath, callback)->
s3Client = getKnoxClient(bucketName) s3Client = getKnoxClient(bucketName)
uploaded = 0
putEventEmiter = s3Client.putFile fsPath, key, (err, res)-> putEventEmiter = s3Client.putFile fsPath, key, (err, res)->
metrics.count 's3.egress', uploaded
if err? if err?
logger.err err:err, bucketName:bucketName, key:key, fsPath:fsPath,"something went wrong uploading file to s3" logger.err err:err, bucketName:bucketName, key:key, fsPath:fsPath,"something went wrong uploading file to s3"
return callback(err) return callback(err)
@ -88,6 +91,8 @@ module.exports =
putEventEmiter.on "error", (err)-> putEventEmiter.on "error", (err)->
logger.err err:err, bucketName:bucketName, key:key, fsPath:fsPath, "error emmited on put of file" logger.err err:err, bucketName:bucketName, key:key, fsPath:fsPath, "error emmited on put of file"
callback err callback err
putEventEmiter.on "progress", (progress)->
uploaded = progress.written
sendStream: (bucketName, key, readStream, callback)-> sendStream: (bucketName, key, readStream, callback)->
logger.log bucketName:bucketName, key:key, "sending file to s3" logger.log bucketName:bucketName, key:key, "sending file to s3"

View file

@ -25,6 +25,7 @@ services:
ENABLE_CONVERSIONS: "true" ENABLE_CONVERSIONS: "true"
MOCHA_GREP: ${MOCHA_GREP} MOCHA_GREP: ${MOCHA_GREP}
NODE_ENV: test NODE_ENV: test
USE_PROM_METRICS: "true"
AWS_KEY: fake AWS_KEY: fake
AWS_SECRET: fake AWS_SECRET: fake
AWS_S3_USER_FILES_BUCKET_NAME: fake_user_files AWS_S3_USER_FILES_BUCKET_NAME: fake_user_files

View file

@ -31,6 +31,7 @@ services:
ENABLE_CONVERSIONS: "true" ENABLE_CONVERSIONS: "true"
LOG_LEVEL: ERROR LOG_LEVEL: ERROR
NODE_ENV: test NODE_ENV: test
USE_PROM_METRICS: "true"
AWS_KEY: fake AWS_KEY: fake
AWS_SECRET: fake AWS_SECRET: fake
AWS_S3_USER_FILES_BUCKET_NAME: fake_user_files AWS_S3_USER_FILES_BUCKET_NAME: fake_user_files

View file

@ -10,8 +10,14 @@ request = require("request")
settings = require("settings-sharelatex") settings = require("settings-sharelatex")
FilestoreApp = require "./FilestoreApp" FilestoreApp = require "./FilestoreApp"
describe "Filestore", ->
getMetric = (filestoreUrl, metric, cb) ->
request.get "#{filestoreUrl}/metrics", (err, res) ->
expect(res.statusCode).to.equal 200
metricRegex = new RegExp("^#{metric}{[^}]+} ([0-9]+)$", "m")
cb(parseInt(metricRegex.exec(res.body)?[1] || '0'))
describe "Filestore", ->
before (done)-> before (done)->
@localFileReadPath = "/tmp/filestore_acceptence_tests_file_read.txt" @localFileReadPath = "/tmp/filestore_acceptence_tests_file_read.txt"
@localFileWritePath = "/tmp/filestore_acceptence_tests_file_write.txt" @localFileWritePath = "/tmp/filestore_acceptence_tests_file_write.txt"
@ -27,11 +33,11 @@ describe "Filestore", ->
beforeEach (done)-> beforeEach (done)->
FilestoreApp.ensureRunning => FilestoreApp.ensureRunning =>
fs.unlink @localFileWritePath, -> fs.unlink @localFileWritePath, =>
getMetric @filestoreUrl, 's3_egress', (metric) =>
@previousEgress = metric
done() done()
it "should send a 200 for status endpoint", (done)-> it "should send a 200 for status endpoint", (done)->
request "#{@filestoreUrl}/status", (err, response, body)-> request "#{@filestoreUrl}/status", (err, response, body)->
response.statusCode.should.equal 200 response.statusCode.should.equal 200
@ -59,6 +65,11 @@ describe "Filestore", ->
response.statusCode.should.equal 404 response.statusCode.should.equal 404
done() done()
it 'should record an egress metric for the upload', (done) ->
getMetric @filestoreUrl, 's3_egress', (metric) =>
expect(metric - @previousEgress).to.equal @constantFileContent.length
done()
it "should return the file size on a HEAD request", (done) -> it "should return the file size on a HEAD request", (done) ->
expectedLength = Buffer.byteLength(@constantFileContent) expectedLength = Buffer.byteLength(@constantFileContent)
request.head @fileUrl, (err, res) => request.head @fileUrl, (err, res) =>
@ -128,12 +139,18 @@ describe "Filestore", ->
@file_id = Math.random() @file_id = Math.random()
@fileUrl = "#{@filestoreUrl}/project/acceptence_tests/file/#{@file_id}" @fileUrl = "#{@filestoreUrl}/project/acceptence_tests/file/#{@file_id}"
@localFileReadPath = __dirname + '/../../fixtures/test.pdf' @localFileReadPath = __dirname + '/../../fixtures/test.pdf'
fs.stat @localFileReadPath, (err, stat) =>
@localFileSize = stat.size
writeStream = request.post(@fileUrl) writeStream = request.post(@fileUrl)
writeStream.on "end", done writeStream.on "end", done
fs.createReadStream(@localFileReadPath).pipe writeStream fs.createReadStream(@localFileReadPath).pipe writeStream
it 'should record an egress metric for the upload', (done) ->
getMetric @filestoreUrl, 's3_egress', (metric) =>
expect(metric - @previousEgress).to.equal @localFileSize
done()
it "should be able get the file back", (done)-> it "should be able get the file back", (done)->
@timeout(1000 * 10) @timeout(1000 * 10)
request.get @fileUrl, (err, response, body)=> request.get @fileUrl, (err, response, body)=>