Merge pull request #13781 from overleaf/em-fetch-utils-logger

Use fetch-utils in logger library

GitOrigin-RevId: f12067a774856469979475a3be63bebbabcd25a6
This commit is contained in:
Jakob Ackermann 2023-09-07 13:57:11 +02:00 committed by Copybot
parent 3bc7407ba9
commit 6781f7d0ac
5 changed files with 18 additions and 35 deletions

View file

@ -1,4 +1,4 @@
const fetch = require('node-fetch')
const { fetchString } = require('@overleaf/fetch-utils')
const fs = require('fs')
class LogLevelChecker {
@ -51,11 +51,7 @@ class GCEMetadataLogLevelChecker extends LogLevelChecker {
},
}
const uri = `http://metadata.google.internal/computeMetadata/v1/project/attributes/${this.logger.fields.name}-setLogLevelEndTime`
const res = await fetch(uri, options)
if (!res.ok) {
throw new Error('Metadata not okay')
}
const strEndTime = await res.text()
const strEndTime = await fetchString(uri, options)
return parseInt(strEndTime, 10)
}
}

View file

@ -18,10 +18,10 @@
},
"dependencies": {
"@google-cloud/logging-bunyan": "^5.0.0",
"@overleaf/fetch-utils": "*",
"@overleaf/o-error": "*",
"@sentry/node": "^6.13.2",
"bunyan": "^1.8.14",
"node-fetch": "^2.6.7"
"bunyan": "^1.8.14"
},
"devDependencies": {
"chai": "^4.3.6",

View file

@ -20,18 +20,14 @@ describe('LogLevelChecker', function () {
level: sinon.stub(),
fields: { name: 'myapp' },
}
this.fetchResponse = {
text: sinon.stub().resolves(''),
status: 200,
ok: true,
this.FetchUtils = {
fetchString: sinon.stub(),
}
this.fetch = sinon
.stub()
.withArgs(
'http://metadata.google.internal/computeMetadata/v1/project/attributes/myapp-setLogLevelEndTime',
{ headers: { 'Metadata-Flavor': 'Google' } }
)
.resolves(this.fetchResponse)
this.fetchLogLevelEndTimeStub = this.FetchUtils.fetchString.withArgs(
'http://metadata.google.internal/computeMetadata/v1/project/attributes/myapp-setLogLevelEndTime',
{ headers: { 'Metadata-Flavor': 'Google' } }
)
this.fetchLogLevelEndTimeStub.resolves('')
this.fs = {
promises: {
@ -43,7 +39,7 @@ describe('LogLevelChecker', function () {
this.module = SandboxedModule.require(MODULE_PATH, {
requires: {
'node-fetch': this.fetch,
'@overleaf/fetch-utils': this.FetchUtils,
fs: this.fs,
},
})
@ -142,16 +138,7 @@ describe('LogLevelChecker', function () {
describe('when the request errors', function () {
beforeEach(async function () {
this.fetchResponse.text.rejects(new Error('Read error!'))
})
checkLogLevel()
expectLevelSetTo(DEFAULT_LEVEL)
})
describe('when the request returns a failure status code', function () {
beforeEach(async function () {
this.fetchResponse.status = 500
this.fetchResponse.ok = false
this.FetchUtils.fetchString.rejects(new Error('Read error!'))
})
checkLogLevel()
expectLevelSetTo(DEFAULT_LEVEL)
@ -181,7 +168,7 @@ function setupTracingEndTimeGCE(contents) {
beforeEach(
`set tracing end time in GCE metadata to ${contents}`,
function () {
this.fetchResponse.text.resolves(contents)
this.fetchLogLevelEndTimeStub.resolves(contents)
}
)
}

View file

@ -5,5 +5,5 @@ chai.use(require('sinon-chai'))
chai.use(require('chai-as-promised'))
SandboxedModule.configure({
globals: { Buffer, console, process, URL },
globals: { Buffer, Math, console, process, URL },
})

6
package-lock.json generated
View file

@ -264,10 +264,10 @@
"license": "AGPL-3.0-only",
"dependencies": {
"@google-cloud/logging-bunyan": "^5.0.0",
"@overleaf/fetch-utils": "*",
"@overleaf/o-error": "*",
"@sentry/node": "^6.13.2",
"bunyan": "^1.8.14",
"node-fetch": "^2.6.7"
"bunyan": "^1.8.14"
},
"devDependencies": {
"chai": "^4.3.6",
@ -53718,12 +53718,12 @@
"version": "file:libraries/logger",
"requires": {
"@google-cloud/logging-bunyan": "^5.0.0",
"@overleaf/fetch-utils": "*",
"@overleaf/o-error": "*",
"@sentry/node": "^6.13.2",
"bunyan": "^1.8.14",
"chai": "^4.3.6",
"mocha": "^10.2.0",
"node-fetch": "^2.6.7",
"sandboxed-module": "^2.0.4",
"sinon": "^9.2.4",
"sinon-chai": "^3.7.0"