loggingManagerTests.js 12 KB

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