Promisfy log level checks

This commit is contained in:
Christopher Hoskin 2020-07-20 16:57:43 +01:00
parent 6b8dd86a26
commit 6961f41488
2 changed files with 44 additions and 37 deletions

View file

@ -22,6 +22,7 @@ const errSerializer = function (err) {
const Logger = (module.exports = {
initialize(name) {
this.useMetadata = (process.env.USE_METADATA || '').toLowerCase() === 'true'
this.isProduction =
(process.env.NODE_ENV || '').toLowerCase() === 'production'
this.defaultLevel =
@ -42,52 +43,54 @@ const Logger = (module.exports = {
return this
},
checkLogLevelFile() {
fs.readFile('/logging/tracingEndTime', (error, end) => {
if (error || !end) {
this.logger.level(this.defaultLevel)
return
}
if (parseInt(end) > Date.now()) {
async checkLogLevelFile() {
try {
const end = await fs.promises.readFile('/logging/tracingEndTime')
if (!end) throw new Error("No end time found")
if (parseInt(end,10) > Date.now()) {
this.logger.level('trace')
} else {
this.logger.level(this.defaultLevel)
}
})
} catch (err) {
this.logger.level(this.defaultLevel)
return
}
},
checkLogLevelMetadata() {
async checkLogLevelMetadata() {
const options = {
headers: {
'Metadata-Flavor': 'Google'
}
}
const uri = `http://metadata.google.internal/computeMetadata/v1/project/attributes/${this.loggerName}-setLogLevelEndTime`
fetch.fetch(uri,options).then(res => res.text()).then(body => {
console.log("About to parse Int", body)
if (parseInt(body) > Date.now()) {
console.log("About to set logger level to trace")
console.log(this.logger)
this.logger.level('trace')
} else {
console.log("About to set logger level to default")
this.logger.level(this.defaultLevel)
}
}).catch(err => {
try {
const res = await fetch(uri,options)
if (!res.ok) throw new Error("Metadata not okay")
const body = await res.text()
console.log("About to parse Int", body)
if (parseInt(body) > Date.now()) {
console.log("About to set logger level to trace")
console.log(this.logger)
this.logger.level('trace')
} else {
console.log("About to set logger level to default")
this.logger.level(this.defaultLevel)
}
} catch (err) {
console.log("ERROR: About to set logger level to default")
this.logger.level(this.defaultLevel)
return
})
}
},
checkLogLevel() {
fs.access('/logging', (err) => {
if (err) {
this.checkLogLevelMetadata()
} else {
this.checkLogLevelFile()
}
})
async checkLogLevel() {
if (this.useMetadata) {
await this.checkLogLevelMetadata()
} else {
await this.checkLogLevelFile()
}
},
initializeErrorReporting(sentryDsn, options) {

View file

@ -45,9 +45,7 @@ describe('LoggingManager', function () {
this.Raven = {
Client: sinon.stub().returns(this.ravenClient)
}
this.Fetch = {
fetch: sinon.stub().resolves(this.fetchResponse)
}
this.Fetch = sinon.stub().resolves(this.fetchResponse)
this.Fs = {
readFile: sinon.stub(),
access: sinon.stub()
@ -381,9 +379,14 @@ describe('LoggingManager', function () {
})
})
describe('when /logging does not exist', function () {
describe('when not running in GKE', function () {
beforeEach(function () {
this.Fs.access.yields(new Error)
process.env.USE_METADATA = 'TRUE'
this.logger = this.LoggingManager.initialize(this.loggerName)
})
afterEach(function() {
process.env.USE_METADATA = undefined
})
describe('checkLogLevel', function() {
@ -439,13 +442,14 @@ describe('LoggingManager', function () {
describe('when time value returned that is more than current time', function() {
describe('when level is already set', function() {
beforeEach(function() {
beforeEach(async function() {
this.bunyanLogger.level.returns(10)
//this.Request.yields(null, { statusCode: 200 }, this.start + 1000)
console.log("In test ", this.start + 1000)
this.fetchResponse.text = sinon.stub().resolves(this.start + 1000)
this.Fetch.fetch = sinon.stub().resolves(this.fetchResponse)
this.logger.checkLogLevel()
//this.Fetch = sinon.stub().resolves(this.fetchResponse)
await this.logger.checkLogLevel()
})
it.only('should set trace level', function() {