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

View file

@ -45,9 +45,7 @@ describe('LoggingManager', function () {
this.Raven = { this.Raven = {
Client: sinon.stub().returns(this.ravenClient) Client: sinon.stub().returns(this.ravenClient)
} }
this.Fetch = { this.Fetch = sinon.stub().resolves(this.fetchResponse)
fetch: sinon.stub().resolves(this.fetchResponse)
}
this.Fs = { this.Fs = {
readFile: sinon.stub(), readFile: sinon.stub(),
access: 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 () { 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() { describe('checkLogLevel', function() {
@ -439,13 +442,14 @@ describe('LoggingManager', function () {
describe('when time value returned that is more than current time', function() { describe('when time value returned that is more than current time', function() {
describe('when level is already set', function() { describe('when level is already set', function() {
beforeEach(function() { beforeEach(async function() {
this.bunyanLogger.level.returns(10) this.bunyanLogger.level.returns(10)
//this.Request.yields(null, { statusCode: 200 }, this.start + 1000) //this.Request.yields(null, { statusCode: 200 }, this.start + 1000)
console.log("In test ", this.start + 1000) console.log("In test ", this.start + 1000)
this.fetchResponse.text = sinon.stub().resolves(this.start + 1000) this.fetchResponse.text = sinon.stub().resolves(this.start + 1000)
this.Fetch.fetch = sinon.stub().resolves(this.fetchResponse) //this.Fetch = sinon.stub().resolves(this.fetchResponse)
this.logger.checkLogLevel()
await this.logger.checkLogLevel()
}) })
it.only('should set trace level', function() { it.only('should set trace level', function() {