This commit is contained in:
Christopher Hoskin 2020-07-30 12:49:54 +01:00
parent 1ca6269aa5
commit 74e2a5eaa1
2 changed files with 77 additions and 73 deletions

View file

@ -43,41 +43,33 @@ const Logger = (module.exports = {
return this return this
}, },
async checkLogLevelFile() { async checkLogLevel() {
try { try {
const end = await fs.promises.readFile('/logging/tracingEndTime') const end = await this.getTracingEndTime()
if (!end) throw new Error("No end time found") if (parseInt(end, 10) > Date.now()) {
if (parseInt(end,10) > Date.now()) {
this.logger.level('trace') this.logger.level('trace')
} else { } else {
this.logger.level(this.defaultLevel) this.logger.level(this.defaultLevel)
} }
} catch (err) { } catch (err) {
this.logger.level(this.defaultLevel) this.logger.level(this.defaultLevel)
return
} }
}, },
async checkLogLevelMetadata() { async getTracingEndTimeFile() {
return fs.promises.readFile('/logging/tracingEndTime')
},
async getTracingEndTimeMetadata() {
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`
try {
const res = await fetch(uri,options) const res = await fetch(uri,options)
if (!res.ok) throw new Error("Metadata not okay") if (!res.ok) throw new Error("Metadata not okay")
const body = await res.text() return res.text()
if (parseInt(body) > Date.now()) {
this.logger.level('trace')
} else {
this.logger.level(this.defaultLevel)
}
} catch (err) {
this.logger.level(this.defaultLevel)
return
}
}, },
initializeErrorReporting(sentryDsn, options) { initializeErrorReporting(sentryDsn, options) {
@ -267,18 +259,19 @@ async checkLogLevelMetadata() {
clearInterval(this.checkInterval) clearInterval(this.checkInterval)
} }
if (this.logLevelSource === 'file') { if (this.logLevelSource === 'file') {
// check for log level override on startup this.getTracingEndTime = this.getTracingEndTimeFile
this.checkLogLevelFile()
// re-check log level every minute
this.checkInterval = setInterval(this.checkLogLevelFile.bind(this), 1000 * 60)
} else if (this.logLevelSource === 'gce_metadata') { } else if (this.logLevelSource === 'gce_metadata') {
// check for log level override on startup this.getTracingEndTime = this.getTracingEndTimeMetadata
this.checkLogLevelMetadata() } else if (this.logLevelSource === 'none') {
// re-check log level every minute return
this.checkInterval = setInterval(this.checkLogLevelMetadata.bind(this), 1000 * 60) } else {
} else if (this.logLevelSource !== 'none') {
console.log('Unrecognised log level source') console.log('Unrecognised log level source')
return
} }
// check for log level override on startup
this.checkLogLevel()
// re-check log level every minute
this.checkInterval = setInterval(this.checkLogLevel.bind(this), 1000 * 60)
} }
} }
}) })

View file

@ -81,14 +81,14 @@ describe('LoggingManager', function () {
describe('initialize', function () { describe('initialize', function () {
beforeEach(function () { beforeEach(function () {
this.checkLogLevelFileStub = sinon.stub(this.LoggingManager, 'checkLogLevelFile') this.getTracingEndTimeFileStub = sinon.stub(this.LoggingManager, 'getTracingEndTimeFile')
this.checkLogLevelMetadataStub = sinon.stub(this.LoggingManager, 'checkLogLevelMetadata') this.getTracingEndTimeMetadataStub = sinon.stub(this.LoggingManager, 'getTracingEndTimeMetadata')
this.Bunyan.createLogger.reset() this.Bunyan.createLogger.reset()
}) })
afterEach(function () { afterEach(function () {
this.checkLogLevelFileStub.restore() this.getTracingEndTimeFileStub.restore()
this.checkLogLevelMetadataStub.restore() this.getTracingEndTimeMetadataStub.restore()
}) })
describe('not in production', function () { describe('not in production', function () {
@ -102,9 +102,9 @@ describe('LoggingManager', function () {
) )
}) })
it('should not run checkLogLevel', function () { it('should not run getTracingEndTime', function () {
this.checkLogLevelFileStub.should.not.have.been.called this.getTracingEndTimeFileStub.should.not.have.been.called
this.checkLogLevelMetadataStub.should.not.have.been.called this.getTracingEndTimeMetadataStub.should.not.have.been.called
}) })
}) })
@ -115,12 +115,20 @@ describe('LoggingManager', function () {
afterEach(() => delete process.env.NODE_ENV) afterEach(() => delete process.env.NODE_ENV)
it('should default to log level warn', function () { describe('blah', function () {
beforeEach(function () {
this.logger = this.LoggingManager.initialize(this.loggerName) this.logger = this.LoggingManager.initialize(this.loggerName)
this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal( })
it.only('should default to log level warn', function () {
const level = this.Bunyan.createLogger.firstCall.args[0].streams[0].level
console.log(level)
level.should.equal(
'warn' 'warn'
) )
}) })
})
describe('logLevelSource file', function () { describe('logLevelSource file', function () {
beforeEach(function() { beforeEach(function() {
@ -128,19 +136,19 @@ describe('LoggingManager', function () {
}) })
it('should run checkLogLevel', function () { it('should run checkLogLevel', function () {
this.checkLogLevelFileStub.should.have.been.calledOnce this.getTracingEndTimeFileStub.should.have.been.calledOnce
}) })
describe('after 1 minute', () => describe('after 1 minute', () =>
it('should run checkLogLevel again', function () { it('should run checkLogLevel again', function () {
this.clock.tick(61 * 1000) this.clock.tick(61 * 1000)
this.checkLogLevelFileStub.should.have.been.calledTwice this.getTracingEndTimeFileStub.should.have.been.calledTwice
})) }))
describe('after 2 minutes', () => describe('after 2 minutes', () =>
it('should run checkLogLevel again', function () { it('should run checkLogLevel again', function () {
this.clock.tick(121 * 1000) this.clock.tick(121 * 1000)
this.checkLogLevelFileStub.should.have.been.calledThrice this.getTracingEndTimeFileStub.should.have.been.calledThrice
})) }))
}) })
@ -153,19 +161,19 @@ describe('LoggingManager', function () {
afterEach(() => delete process.env.LOG_LEVEL_SOURCE) afterEach(() => delete process.env.LOG_LEVEL_SOURCE)
it('should run checkLogLevel', function () { it('should run checkLogLevel', function () {
this.checkLogLevelMetadataStub.should.have.been.calledOnce this.getTracingEndTimeMetadataStub.should.have.been.calledOnce
}) })
describe('after 1 minute', () => describe('after 1 minute', () =>
it('should run checkLogLevel again', function () { it('should run checkLogLevel again', function () {
this.clock.tick(61 * 1000) this.clock.tick(61 * 1000)
this.checkLogLevelMetadataStub.should.have.been.calledTwice this.getTracingEndTimeMetadataStub.should.have.been.calledTwice
})) }))
describe('after 2 minutes', () => describe('after 2 minutes', () =>
it('should run checkLogLevel again', function () { it('should run checkLogLevel again', function () {
this.clock.tick(121 * 1000) this.clock.tick(121 * 1000)
this.checkLogLevelMetadataStub.should.have.been.calledThrice this.getTracingEndTimeMetadataStub.should.have.been.calledThrice
})) }))
}) })
@ -336,16 +344,18 @@ describe('LoggingManager', function () {
describe('checkLogLevelFile', function () { describe('checkLogLevelFile', function () {
it('should request log level override from the config map', async function () { it('should request log level override from the config map', async function () {
await this.logger.checkLogLevelFile() this.logger.getTracingEndTime = this.logger.getTracingEndTimeFile
await this.logger.checkLogLevel()
this.Fs.promises.readFile.should.have.been.calledWithMatch( this.Fs.promises.readFile.should.have.been.calledWithMatch(
'/logging/tracingEndTime' '/logging/tracingEndTime'
) )
}) })
describe('when read errors', function () { describe('when read errors', function () {
beforeEach(function () { beforeEach(async function () {
this.Fs.promises.readFile.yields(new Error('error')) this.Fs.promises.readFile.yields(new Error('error'))
this.logger.checkLogLevelFile() this.logger.getTracingEndTime = this.logger.getTracingEndTimeFile
await this.logger.checkLogLevel()
}) })
it('should only set default level', function () { it('should only set default level', function () {
@ -356,9 +366,10 @@ describe('LoggingManager', function () {
}) })
describe('when the file is empty', function () { describe('when the file is empty', function () {
beforeEach(function () { beforeEach(async function () {
this.Fs.promises.readFile.yields(null, '') this.Fs.promises.readFile.yields(null, '')
this.logger.checkLogLevelFile() this.logger.getTracingEndTime = this.logger.getTracingEndTimeFile
await this.logger.checkLogLevel()
}) })
it('should only set default level', function () { it('should only set default level', function () {
@ -369,9 +380,10 @@ describe('LoggingManager', function () {
}) })
describe('when time value returned that is less than current time', function () { describe('when time value returned that is less than current time', function () {
beforeEach(function () { beforeEach(async function () {
this.Fs.promises.readFile.yields(null, '1') this.Fs.promises.readFile.yields(null, '1')
this.logger.checkLogLevelFile() this.logger.getTracingEndTime = this.logger.getTracingEndTimeFile
await this.logger.checkLogLevel()
}) })
it('should only set default level', function () { it('should only set default level', function () {
@ -386,7 +398,8 @@ describe('LoggingManager', function () {
beforeEach(async function () { beforeEach(async function () {
this.bunyanLogger.level.returns(10) this.bunyanLogger.level.returns(10)
this.Fs.promises.readFile.returns((this.start + 1000).toString()) this.Fs.promises.readFile.returns((this.start + 1000).toString())
await this.logger.checkLogLevelFile() this.logger.getTracingEndTime = this.logger.getTracingEndTimeFile
await this.logger.checkLogLevel()
}) })
it('should set trace level', function () { it('should set trace level', function () {
@ -400,7 +413,8 @@ describe('LoggingManager', function () {
beforeEach(async function () { beforeEach(async function () {
this.bunyanLogger.level.returns(20) this.bunyanLogger.level.returns(20)
this.Fs.promises.readFile.returns((this.start + 1000).toString()) this.Fs.promises.readFile.returns((this.start + 1000).toString())
await this.logger.checkLogLevelFile() this.logger.getTracingEndTime = this.logger.getTracingEndTimeFile
await this.logger.checkLogLevel()
}) })
it('should set trace level', function () { it('should set trace level', function () {
@ -418,8 +432,9 @@ describe('LoggingManager', function () {
}) })
describe('checkLogLevel', function() { describe('checkLogLevel', function() {
it('should request log level override from google meta data service', function() { it('should request log level override from google meta data service', async function() {
this.logger.checkLogLevelMetadata() this.logger.getTracingEndTime = this.logger.getTracingEndTimeMetadata
await this.logger.checkLogLevel()
const options = { const options = {
headers: { headers: {
'Metadata-Flavor': 'Google' 'Metadata-Flavor': 'Google'
@ -432,8 +447,8 @@ describe('LoggingManager', function () {
describe('when request has error', function() { describe('when request has error', function() {
beforeEach(async function() { beforeEach(async function() {
this.Fetch = sinon.stub().throws() this.Fetch = sinon.stub().throws()
//this.Request.yields('error') this.logger.getTracingEndTime = this.logger.getTracingEndTimeMetadata
await this.logger.checkLogLevelMetadata() await this.logger.checkLogLevel()
}) })
it('should only set default level', function() { it('should only set default level', function() {
@ -446,8 +461,8 @@ describe('LoggingManager', function () {
describe('when statusCode is not 200', function() { describe('when statusCode is not 200', function() {
beforeEach(async function() { beforeEach(async function() {
this.fetchResponse.status = 404 this.fetchResponse.status = 404
//this.Request.yields(null, { statusCode: 404 }) this.logger.getTracingEndTime = this.logger.getTracingEndTimeMetadata
await this.logger.checkLogLevelMetadata() await this.logger.checkLogLevel()
}) })
it('should only set default level', function() { it('should only set default level', function() {
@ -459,9 +474,9 @@ describe('LoggingManager', function () {
describe('when time value returned that is less than current time', function() { describe('when time value returned that is less than current time', function() {
beforeEach(async function() { beforeEach(async function() {
//this.Request.yields(null, { statusCode: 200 }, '1') this.logger.getTracingEndTime = this.logger.getTracingEndTimeMetadata
this.fetchResponse.text = sinon.stub().resolves('1') this.fetchResponse.text = sinon.stub().resolves('1')
await this.logger.checkLogLevelMetadata() await this.logger.checkLogLevel()
}) })
it('should only set default level', function() { it('should only set default level', function() {
@ -475,11 +490,10 @@ describe('LoggingManager', function () {
describe('when level is already set', function() { describe('when level is already set', function() {
beforeEach(async function() { beforeEach(async function() {
this.bunyanLogger.level.returns(10) this.bunyanLogger.level.returns(10)
//this.Request.yields(null, { statusCode: 200 }, this.start + 1000)
this.fetchResponse.text = sinon.stub().resolves(this.start + 1000) this.fetchResponse.text = sinon.stub().resolves(this.start + 1000)
//this.Fetch = sinon.stub().resolves(this.fetchResponse) this.logger.getTracingEndTime = this.logger.getTracingEndTimeMetadata
await this.logger.checkLogLevelMetadata() await this.logger.checkLogLevel()
}) })
it('should set trace level', function() { it('should set trace level', function() {
@ -494,12 +508,9 @@ describe('LoggingManager', function () {
this.bunyanLogger.level.returns(20) this.bunyanLogger.level.returns(20)
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.fetch = sinon.stub().resolves(this.fetchResponse)
//this.Request.yields(null, { statusCode: 200 }, this.start + 1000) this.logger.getTracingEndTime = this.logger.getTracingEndTimeMetadata
//this.fetchResponse = sinon.stub().resolves
await this.logger.checkLogLevel()
//{data: this.start + 1000, status: 200}
await this.logger.checkLogLevelMetadata()
}) })
it('should set trace level', function() { it('should set trace level', function() {