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') const fs = require('fs')
class LogLevelChecker { 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 uri = `http://metadata.google.internal/computeMetadata/v1/project/attributes/${this.logger.fields.name}-setLogLevelEndTime`
const res = await fetch(uri, options) const strEndTime = await fetchString(uri, options)
if (!res.ok) {
throw new Error('Metadata not okay')
}
const strEndTime = await res.text()
return parseInt(strEndTime, 10) return parseInt(strEndTime, 10)
} }
} }

View file

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

View file

@ -20,18 +20,14 @@ describe('LogLevelChecker', function () {
level: sinon.stub(), level: sinon.stub(),
fields: { name: 'myapp' }, fields: { name: 'myapp' },
} }
this.fetchResponse = { this.FetchUtils = {
text: sinon.stub().resolves(''), fetchString: sinon.stub(),
status: 200,
ok: true,
} }
this.fetch = sinon this.fetchLogLevelEndTimeStub = this.FetchUtils.fetchString.withArgs(
.stub() 'http://metadata.google.internal/computeMetadata/v1/project/attributes/myapp-setLogLevelEndTime',
.withArgs( { headers: { 'Metadata-Flavor': 'Google' } }
'http://metadata.google.internal/computeMetadata/v1/project/attributes/myapp-setLogLevelEndTime', )
{ headers: { 'Metadata-Flavor': 'Google' } } this.fetchLogLevelEndTimeStub.resolves('')
)
.resolves(this.fetchResponse)
this.fs = { this.fs = {
promises: { promises: {
@ -43,7 +39,7 @@ describe('LogLevelChecker', function () {
this.module = SandboxedModule.require(MODULE_PATH, { this.module = SandboxedModule.require(MODULE_PATH, {
requires: { requires: {
'node-fetch': this.fetch, '@overleaf/fetch-utils': this.FetchUtils,
fs: this.fs, fs: this.fs,
}, },
}) })
@ -142,16 +138,7 @@ describe('LogLevelChecker', function () {
describe('when the request errors', function () { describe('when the request errors', function () {
beforeEach(async function () { beforeEach(async function () {
this.fetchResponse.text.rejects(new Error('Read error!')) this.FetchUtils.fetchString.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
}) })
checkLogLevel() checkLogLevel()
expectLevelSetTo(DEFAULT_LEVEL) expectLevelSetTo(DEFAULT_LEVEL)
@ -181,7 +168,7 @@ function setupTracingEndTimeGCE(contents) {
beforeEach( beforeEach(
`set tracing end time in GCE metadata to ${contents}`, `set tracing end time in GCE metadata to ${contents}`,
function () { 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')) chai.use(require('chai-as-promised'))
SandboxedModule.configure({ 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", "license": "AGPL-3.0-only",
"dependencies": { "dependencies": {
"@google-cloud/logging-bunyan": "^5.0.0", "@google-cloud/logging-bunyan": "^5.0.0",
"@overleaf/fetch-utils": "*",
"@overleaf/o-error": "*", "@overleaf/o-error": "*",
"@sentry/node": "^6.13.2", "@sentry/node": "^6.13.2",
"bunyan": "^1.8.14", "bunyan": "^1.8.14"
"node-fetch": "^2.6.7"
}, },
"devDependencies": { "devDependencies": {
"chai": "^4.3.6", "chai": "^4.3.6",
@ -53718,12 +53718,12 @@
"version": "file:libraries/logger", "version": "file:libraries/logger",
"requires": { "requires": {
"@google-cloud/logging-bunyan": "^5.0.0", "@google-cloud/logging-bunyan": "^5.0.0",
"@overleaf/fetch-utils": "*",
"@overleaf/o-error": "*", "@overleaf/o-error": "*",
"@sentry/node": "^6.13.2", "@sentry/node": "^6.13.2",
"bunyan": "^1.8.14", "bunyan": "^1.8.14",
"chai": "^4.3.6", "chai": "^4.3.6",
"mocha": "^10.2.0", "mocha": "^10.2.0",
"node-fetch": "^2.6.7",
"sandboxed-module": "^2.0.4", "sandboxed-module": "^2.0.4",
"sinon": "^9.2.4", "sinon": "^9.2.4",
"sinon-chai": "^3.7.0" "sinon-chai": "^3.7.0"