loggingManagerTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. const SandboxedModule = require('sandboxed-module')
  2. const bunyan = require('bunyan')
  3. const chai = require('chai')
  4. const path = require('path')
  5. const sinon = require('sinon')
  6. const sinonChai = require('sinon-chai')
  7. chai.use(sinonChai)
  8. chai.should()
  9. const expect = chai.expect
  10. const modulePath = path.join(__dirname, '../../logging-manager.js')
  11. describe('LoggingManager', function() {
  12. beforeEach(function() {
  13. this.start = Date.now()
  14. this.clock = sinon.useFakeTimers(this.start)
  15. this.captureException = sinon.stub()
  16. this.bunyanLogger = {
  17. debug: sinon.stub(),
  18. error: sinon.stub(),
  19. fatal: sinon.stub(),
  20. info: sinon.stub(),
  21. level: sinon.stub(),
  22. warn: sinon.stub()
  23. }
  24. this.ravenClient = {
  25. captureException: this.captureException,
  26. once: sinon.stub().yields()
  27. }
  28. this.Bunyan = {
  29. createLogger: sinon.stub().returns(this.bunyanLogger),
  30. RingBuffer: bunyan.RingBuffer,
  31. stdSerializers: {
  32. req: sinon.stub(),
  33. res: sinon.stub()
  34. }
  35. }
  36. this.Raven = {
  37. Client: sinon.stub().returns(this.ravenClient)
  38. }
  39. this.Request = sinon.stub()
  40. this.LoggingManager = SandboxedModule.require(modulePath, {
  41. globals: { console, process },
  42. requires: {
  43. bunyan: this.Bunyan,
  44. raven: this.Raven,
  45. request: this.Request
  46. }
  47. })
  48. this.loggerName = 'test'
  49. this.logger = this.LoggingManager.initialize(this.loggerName)
  50. this.logger.initializeErrorReporting('test_dsn')
  51. })
  52. afterEach(function() {
  53. this.clock.restore()
  54. })
  55. describe('initialize', function() {
  56. beforeEach(function() {
  57. this.checkLogLevelStub = sinon.stub(this.LoggingManager, 'checkLogLevel')
  58. this.Bunyan.createLogger.reset()
  59. })
  60. afterEach(function() {
  61. this.checkLogLevelStub.restore()
  62. })
  63. describe('not in production', function() {
  64. beforeEach(function() {
  65. this.logger = this.LoggingManager.initialize(this.loggerName)
  66. })
  67. it('should default to log level debug', function() {
  68. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  69. 'debug'
  70. )
  71. })
  72. it('should not run checkLogLevel', function() {
  73. this.checkLogLevelStub.should.not.have.been.called
  74. })
  75. })
  76. describe('in production', function() {
  77. beforeEach(function() {
  78. process.env.NODE_ENV = 'production'
  79. this.logger = this.LoggingManager.initialize(this.loggerName)
  80. })
  81. afterEach(() => delete process.env.NODE_ENV)
  82. it('should default to log level warn', function() {
  83. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  84. 'warn'
  85. )
  86. })
  87. it('should run checkLogLevel', function() {
  88. this.checkLogLevelStub.should.have.been.calledOnce
  89. })
  90. describe('after 1 minute', () =>
  91. it('should run checkLogLevel again', function() {
  92. this.clock.tick(61 * 1000)
  93. this.checkLogLevelStub.should.have.been.calledTwice
  94. }))
  95. describe('after 2 minutes', () =>
  96. it('should run checkLogLevel again', function() {
  97. this.clock.tick(121 * 1000)
  98. this.checkLogLevelStub.should.have.been.calledThrice
  99. }))
  100. })
  101. describe('when LOG_LEVEL set in env', function() {
  102. beforeEach(function() {
  103. process.env.LOG_LEVEL = 'trace'
  104. this.LoggingManager.initialize()
  105. })
  106. afterEach(() => delete process.env.LOG_LEVEL)
  107. it('should use custom log level', function() {
  108. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  109. 'trace'
  110. )
  111. })
  112. })
  113. })
  114. describe('bunyan logging', function() {
  115. beforeEach(function() {
  116. this.logArgs = [{ foo: 'bar' }, 'foo', 'bar']
  117. })
  118. it('should log debug', function() {
  119. this.logger.debug(this.logArgs)
  120. this.bunyanLogger.debug.should.have.been.calledWith(this.logArgs)
  121. })
  122. it('should log error', function() {
  123. this.logger.error(this.logArgs)
  124. this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
  125. })
  126. it('should log fatal', function() {
  127. this.logger.fatal(this.logArgs)
  128. this.bunyanLogger.fatal.should.have.been.calledWith(this.logArgs)
  129. })
  130. it('should log info', function() {
  131. this.logger.info(this.logArgs)
  132. this.bunyanLogger.info.should.have.been.calledWith(this.logArgs)
  133. })
  134. it('should log warn', function() {
  135. this.logger.warn(this.logArgs)
  136. this.bunyanLogger.warn.should.have.been.calledWith(this.logArgs)
  137. })
  138. it('should log err', function() {
  139. this.logger.err(this.logArgs)
  140. this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
  141. })
  142. it('should log log', function() {
  143. this.logger.log(this.logArgs)
  144. this.bunyanLogger.info.should.have.been.calledWith(this.logArgs)
  145. })
  146. })
  147. describe('logger.error', function() {
  148. it('should report a single error to sentry', function() {
  149. this.logger.error({ foo: 'bar' }, 'message')
  150. this.captureException.called.should.equal(true)
  151. })
  152. it('should report the same error to sentry only once', function() {
  153. const error1 = new Error('this is the error')
  154. this.logger.error({ foo: error1 }, 'first message')
  155. this.logger.error({ bar: error1 }, 'second message')
  156. this.captureException.callCount.should.equal(1)
  157. })
  158. it('should report two different errors to sentry individually', function() {
  159. const error1 = new Error('this is the error')
  160. const error2 = new Error('this is the error')
  161. this.logger.error({ foo: error1 }, 'first message')
  162. this.logger.error({ bar: error2 }, 'second message')
  163. this.captureException.callCount.should.equal(2)
  164. })
  165. it('should remove the path from fs errors', function() {
  166. const fsError = new Error(
  167. "Error: ENOENT: no such file or directory, stat '/tmp/3279b8d0-da10-11e8-8255-efd98985942b'"
  168. )
  169. fsError.path = '/tmp/3279b8d0-da10-11e8-8255-efd98985942b'
  170. this.logger.error({ err: fsError }, 'message')
  171. this.captureException
  172. .calledWith(
  173. sinon.match.has(
  174. 'message',
  175. 'Error: ENOENT: no such file or directory, stat'
  176. )
  177. )
  178. .should.equal(true)
  179. })
  180. it('for multiple errors should only report a maximum of 5 errors to sentry', function() {
  181. this.logger.error({ foo: 'bar' }, 'message')
  182. this.logger.error({ foo: 'bar' }, 'message')
  183. this.logger.error({ foo: 'bar' }, 'message')
  184. this.logger.error({ foo: 'bar' }, 'message')
  185. this.logger.error({ foo: 'bar' }, 'message')
  186. this.logger.error({ foo: 'bar' }, 'message')
  187. this.logger.error({ foo: 'bar' }, 'message')
  188. this.logger.error({ foo: 'bar' }, 'message')
  189. this.logger.error({ foo: 'bar' }, 'message')
  190. this.captureException.callCount.should.equal(5)
  191. })
  192. it('for multiple errors with a minute delay should report 10 errors to sentry', function() {
  193. // the first five errors should be reported to sentry
  194. this.logger.error({ foo: 'bar' }, 'message')
  195. this.logger.error({ foo: 'bar' }, 'message')
  196. this.logger.error({ foo: 'bar' }, 'message')
  197. this.logger.error({ foo: 'bar' }, 'message')
  198. this.logger.error({ foo: 'bar' }, 'message')
  199. // the following errors should not be reported
  200. this.logger.error({ foo: 'bar' }, 'message')
  201. this.logger.error({ foo: 'bar' }, 'message')
  202. this.logger.error({ foo: 'bar' }, 'message')
  203. this.logger.error({ foo: 'bar' }, 'message')
  204. // allow a minute to pass
  205. this.clock.tick(this.start + 61 * 1000)
  206. // after a minute the next five errors should be reported to sentry
  207. this.logger.error({ foo: 'bar' }, 'message')
  208. this.logger.error({ foo: 'bar' }, 'message')
  209. this.logger.error({ foo: 'bar' }, 'message')
  210. this.logger.error({ foo: 'bar' }, 'message')
  211. this.logger.error({ foo: 'bar' }, 'message')
  212. // the following errors should not be reported to sentry
  213. this.logger.error({ foo: 'bar' }, 'message')
  214. this.logger.error({ foo: 'bar' }, 'message')
  215. this.logger.error({ foo: 'bar' }, 'message')
  216. this.logger.error({ foo: 'bar' }, 'message')
  217. this.captureException.callCount.should.equal(10)
  218. })
  219. })
  220. describe('checkLogLevel', function() {
  221. it('should request log level override from google meta data service', function() {
  222. this.logger.checkLogLevel()
  223. const options = {
  224. headers: {
  225. 'Metadata-Flavor': 'Google'
  226. },
  227. uri: `http://metadata.google.internal/computeMetadata/v1/project/attributes/${this.loggerName}-setLogLevelEndTime`
  228. }
  229. this.Request.should.have.been.calledWithMatch(options)
  230. })
  231. describe('when request has error', function() {
  232. beforeEach(function() {
  233. this.Request.yields('error')
  234. this.logger.checkLogLevel()
  235. })
  236. it('should only set default level', function() {
  237. this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
  238. 'debug'
  239. )
  240. })
  241. })
  242. describe('when statusCode is not 200', function() {
  243. beforeEach(function() {
  244. this.Request.yields(null, { statusCode: 404 })
  245. this.logger.checkLogLevel()
  246. })
  247. it('should only set default level', function() {
  248. this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
  249. 'debug'
  250. )
  251. })
  252. })
  253. describe('when time value returned that is less than current time', function() {
  254. beforeEach(function() {
  255. this.Request.yields(null, { statusCode: 200 }, '1')
  256. this.logger.checkLogLevel()
  257. })
  258. it('should only set default level', function() {
  259. this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
  260. 'debug'
  261. )
  262. })
  263. })
  264. describe('when time value returned that is more than current time', function() {
  265. describe('when level is already set', function() {
  266. beforeEach(function() {
  267. this.bunyanLogger.level.returns(10)
  268. this.Request.yields(null, { statusCode: 200 }, this.start + 1000)
  269. this.logger.checkLogLevel()
  270. })
  271. it('should set trace level', function() {
  272. this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
  273. 'trace'
  274. )
  275. })
  276. })
  277. describe('when level is not already set', function() {
  278. beforeEach(function() {
  279. this.bunyanLogger.level.returns(20)
  280. this.Request.yields(null, { statusCode: 200 }, this.start + 1000)
  281. this.logger.checkLogLevel()
  282. })
  283. it('should set trace level', function() {
  284. this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
  285. 'trace'
  286. )
  287. })
  288. })
  289. })
  290. })
  291. describe('ringbuffer', function() {
  292. beforeEach(function() {
  293. this.logBufferMock = [
  294. { msg: 'log 1' },
  295. { msg: 'log 2' },
  296. { level: 50, msg: 'error' }
  297. ]
  298. })
  299. describe('when ring buffer size is positive', function() {
  300. beforeEach(function() {
  301. process.env['LOG_RING_BUFFER_SIZE'] = '20'
  302. this.logger = this.LoggingManager.initialize(this.loggerName)
  303. this.logger.ringBuffer.records = this.logBufferMock
  304. this.logger.error({}, 'error')
  305. })
  306. afterEach(function() {
  307. process.env['LOG_RING_BUFFER_SIZE'] = undefined
  308. })
  309. it('should include buffered logs in error log and filter out error logs in buffer', function() {
  310. this.bunyanLogger.error.lastCall.args[0].logBuffer.should.deep.equal([
  311. { msg: 'log 1' },
  312. { msg: 'log 2' }
  313. ])
  314. })
  315. })
  316. describe('when ring buffer size is zero', function() {
  317. beforeEach(function() {
  318. process.env['LOG_RING_BUFFER_SIZE'] = '0'
  319. this.logger = this.LoggingManager.initialize(this.loggerName)
  320. this.logger.error({}, 'error')
  321. })
  322. afterEach(function() {
  323. process.env['LOG_RING_BUFFER_SIZE'] = undefined
  324. })
  325. it('should not include buffered logs in error log', function() {
  326. expect(this.bunyanLogger.error.lastCall.args[0].logBuffer).be.undefined
  327. })
  328. })
  329. })
  330. })