loggingManagerTests.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. addStream: sinon.stub(),
  18. debug: sinon.stub(),
  19. error: sinon.stub(),
  20. fatal: sinon.stub(),
  21. info: sinon.stub(),
  22. level: sinon.stub(),
  23. warn: sinon.stub()
  24. }
  25. this.ravenClient = {
  26. captureException: this.captureException,
  27. once: sinon.stub().yields()
  28. }
  29. this.Bunyan = {
  30. createLogger: sinon.stub().returns(this.bunyanLogger),
  31. RingBuffer: bunyan.RingBuffer,
  32. stdSerializers: {
  33. req: sinon.stub(),
  34. res: sinon.stub()
  35. }
  36. }
  37. this.Raven = {
  38. Client: sinon.stub().returns(this.ravenClient)
  39. }
  40. this.Fs = {
  41. readFile: sinon.stub()
  42. }
  43. this.stackdriverStreamConfig = { stream: 'stackdriver' }
  44. this.stackdriverClient = {
  45. stream: sinon.stub().returns(this.stackdriverStreamConfig)
  46. }
  47. this.GCPLogging = {
  48. LoggingBunyan: sinon.stub().returns(this.stackdriverClient)
  49. }
  50. this.LoggingManager = SandboxedModule.require(modulePath, {
  51. globals: { console, process },
  52. requires: {
  53. bunyan: this.Bunyan,
  54. raven: this.Raven,
  55. fs: this.Fs,
  56. '@google-cloud/logging-bunyan': this.GCPLogging
  57. }
  58. })
  59. this.loggerName = 'test'
  60. this.logger = this.LoggingManager.initialize(this.loggerName)
  61. this.logger.initializeErrorReporting('test_dsn')
  62. })
  63. afterEach(function() {
  64. this.clock.restore()
  65. })
  66. describe('initialize', function() {
  67. beforeEach(function() {
  68. this.checkLogLevelStub = sinon.stub(this.LoggingManager, 'checkLogLevel')
  69. this.Bunyan.createLogger.reset()
  70. })
  71. afterEach(function() {
  72. this.checkLogLevelStub.restore()
  73. })
  74. describe('not in production', function() {
  75. beforeEach(function() {
  76. this.logger = this.LoggingManager.initialize(this.loggerName)
  77. })
  78. it('should default to log level debug', function() {
  79. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  80. 'debug'
  81. )
  82. })
  83. it('should not run checkLogLevel', function() {
  84. this.checkLogLevelStub.should.not.have.been.called
  85. })
  86. })
  87. describe('in production', function() {
  88. beforeEach(function() {
  89. process.env.NODE_ENV = 'production'
  90. this.logger = this.LoggingManager.initialize(this.loggerName)
  91. })
  92. afterEach(() => delete process.env.NODE_ENV)
  93. it('should default to log level warn', function() {
  94. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  95. 'warn'
  96. )
  97. })
  98. it('should run checkLogLevel', function() {
  99. this.checkLogLevelStub.should.have.been.calledOnce
  100. })
  101. describe('after 1 minute', () =>
  102. it('should run checkLogLevel again', function() {
  103. this.clock.tick(61 * 1000)
  104. this.checkLogLevelStub.should.have.been.calledTwice
  105. }))
  106. describe('after 2 minutes', () =>
  107. it('should run checkLogLevel again', function() {
  108. this.clock.tick(121 * 1000)
  109. this.checkLogLevelStub.should.have.been.calledThrice
  110. }))
  111. })
  112. describe('when LOG_LEVEL set in env', function() {
  113. beforeEach(function() {
  114. process.env.LOG_LEVEL = 'trace'
  115. this.LoggingManager.initialize()
  116. })
  117. afterEach(() => delete process.env.LOG_LEVEL)
  118. it('should use custom log level', function() {
  119. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  120. 'trace'
  121. )
  122. })
  123. })
  124. })
  125. describe('bunyan logging', function() {
  126. beforeEach(function() {
  127. this.logArgs = [{ foo: 'bar' }, 'foo', 'bar']
  128. })
  129. it('should log debug', function() {
  130. this.logger.debug(this.logArgs)
  131. this.bunyanLogger.debug.should.have.been.calledWith(this.logArgs)
  132. })
  133. it('should log error', function() {
  134. this.logger.error(this.logArgs)
  135. this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
  136. })
  137. it('should log fatal', function() {
  138. this.logger.fatal(this.logArgs)
  139. this.bunyanLogger.fatal.should.have.been.calledWith(this.logArgs)
  140. })
  141. it('should log info', function() {
  142. this.logger.info(this.logArgs)
  143. this.bunyanLogger.info.should.have.been.calledWith(this.logArgs)
  144. })
  145. it('should log warn', function() {
  146. this.logger.warn(this.logArgs)
  147. this.bunyanLogger.warn.should.have.been.calledWith(this.logArgs)
  148. })
  149. it('should log err', function() {
  150. this.logger.err(this.logArgs)
  151. this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
  152. })
  153. it('should log log', function() {
  154. this.logger.log(this.logArgs)
  155. this.bunyanLogger.info.should.have.been.calledWith(this.logArgs)
  156. })
  157. })
  158. describe('logger.error', function() {
  159. it('should report a single error to sentry', function() {
  160. this.logger.error({ foo: 'bar' }, 'message')
  161. this.captureException.called.should.equal(true)
  162. })
  163. it('should report the same error to sentry only once', function() {
  164. const error1 = new Error('this is the error')
  165. this.logger.error({ foo: error1 }, 'first message')
  166. this.logger.error({ bar: error1 }, 'second message')
  167. this.captureException.callCount.should.equal(1)
  168. })
  169. it('should report two different errors to sentry individually', function() {
  170. const error1 = new Error('this is the error')
  171. const error2 = new Error('this is the error')
  172. this.logger.error({ foo: error1 }, 'first message')
  173. this.logger.error({ bar: error2 }, 'second message')
  174. this.captureException.callCount.should.equal(2)
  175. })
  176. it('should remove the path from fs errors', function() {
  177. const fsError = new Error(
  178. "Error: ENOENT: no such file or directory, stat '/tmp/3279b8d0-da10-11e8-8255-efd98985942b'"
  179. )
  180. fsError.path = '/tmp/3279b8d0-da10-11e8-8255-efd98985942b'
  181. this.logger.error({ err: fsError }, 'message')
  182. this.captureException
  183. .calledWith(
  184. sinon.match.has(
  185. 'message',
  186. 'Error: ENOENT: no such file or directory, stat'
  187. )
  188. )
  189. .should.equal(true)
  190. })
  191. it('for multiple errors should only report a maximum of 5 errors to sentry', function() {
  192. this.logger.error({ foo: 'bar' }, 'message')
  193. this.logger.error({ foo: 'bar' }, 'message')
  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. this.logger.error({ foo: 'bar' }, 'message')
  200. this.logger.error({ foo: 'bar' }, 'message')
  201. this.captureException.callCount.should.equal(5)
  202. })
  203. it('for multiple errors with a minute delay should report 10 errors to sentry', function() {
  204. // the first five errors should be reported to sentry
  205. this.logger.error({ foo: 'bar' }, 'message')
  206. this.logger.error({ foo: 'bar' }, 'message')
  207. this.logger.error({ foo: 'bar' }, 'message')
  208. this.logger.error({ foo: 'bar' }, 'message')
  209. this.logger.error({ foo: 'bar' }, 'message')
  210. // the following errors should not be reported
  211. this.logger.error({ foo: 'bar' }, 'message')
  212. this.logger.error({ foo: 'bar' }, 'message')
  213. this.logger.error({ foo: 'bar' }, 'message')
  214. this.logger.error({ foo: 'bar' }, 'message')
  215. // allow a minute to pass
  216. this.clock.tick(this.start + 61 * 1000)
  217. // after a minute the next five errors should be reported to sentry
  218. this.logger.error({ foo: 'bar' }, 'message')
  219. this.logger.error({ foo: 'bar' }, 'message')
  220. this.logger.error({ foo: 'bar' }, 'message')
  221. this.logger.error({ foo: 'bar' }, 'message')
  222. this.logger.error({ foo: 'bar' }, 'message')
  223. // the following errors should not be reported to sentry
  224. this.logger.error({ foo: 'bar' }, 'message')
  225. this.logger.error({ foo: 'bar' }, 'message')
  226. this.logger.error({ foo: 'bar' }, 'message')
  227. this.logger.error({ foo: 'bar' }, 'message')
  228. this.captureException.callCount.should.equal(10)
  229. })
  230. describe('reportedToSentry', function() {
  231. it('should mark the error as reported to sentry', function() {
  232. const err = new Error()
  233. this.logger.error({ err }, 'message')
  234. expect(this.captureException.called).to.equal(true)
  235. expect(err.reportedToSentry).to.equal(true)
  236. })
  237. it('should mark two errors as reported to sentry', function() {
  238. const err1 = new Error()
  239. const err2 = new Error()
  240. this.logger.error({ err: err1, err2 }, 'message')
  241. expect(this.captureException.called).to.equal(true)
  242. expect(err1.reportedToSentry).to.equal(true)
  243. expect(err2.reportedToSentry).to.equal(true)
  244. })
  245. it('should not mark arbitrary objects as reported to sentry', function() {
  246. const err = new Error()
  247. const ctx = { foo: 'bar' }
  248. this.logger.error({ err, ctx }, 'message')
  249. expect(this.captureException.called).to.equal(true)
  250. expect(ctx.reportedToSentry).to.equal(undefined)
  251. })
  252. })
  253. })
  254. describe('checkLogLevel', function() {
  255. it('should request log level override from the config map', function() {
  256. this.logger.checkLogLevel()
  257. this.Fs.readFile.should.have.been.calledWithMatch('/logging/tracingEndTime')
  258. })
  259. describe('when read errors', function() {
  260. beforeEach(function() {
  261. this.Fs.readFile.yields(new Error('error'))
  262. this.logger.checkLogLevel()
  263. })
  264. it('should only set default level', function() {
  265. this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
  266. 'debug'
  267. )
  268. })
  269. })
  270. describe('when the file is empty', function() {
  271. beforeEach(function() {
  272. this.Fs.readFile.yields(null, '')
  273. this.logger.checkLogLevel()
  274. })
  275. it('should only set default level', function() {
  276. this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
  277. 'debug'
  278. )
  279. })
  280. })
  281. describe('when time value returned that is less than current time', function() {
  282. beforeEach(function() {
  283. this.Fs.readFile.yields(null, '1')
  284. this.logger.checkLogLevel()
  285. })
  286. it('should only set default level', function() {
  287. this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
  288. 'debug'
  289. )
  290. })
  291. })
  292. describe('when time value returned that is more than current time', function() {
  293. describe('when level is already set', function() {
  294. beforeEach(function() {
  295. this.bunyanLogger.level.returns(10)
  296. this.Fs.readFile.yields(null, (this.start + 1000).toString())
  297. this.logger.checkLogLevel()
  298. })
  299. it('should set trace level', function() {
  300. this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
  301. 'trace'
  302. )
  303. })
  304. })
  305. describe('when level is not already set', function() {
  306. beforeEach(function() {
  307. this.bunyanLogger.level.returns(20)
  308. this.Fs.readFile.yields(null, (this.start + 1000).toString())
  309. this.logger.checkLogLevel()
  310. })
  311. it('should set trace level', function() {
  312. this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
  313. 'trace'
  314. )
  315. })
  316. })
  317. })
  318. })
  319. describe('ringbuffer', function() {
  320. beforeEach(function() {
  321. this.logBufferMock = [
  322. { msg: 'log 1' },
  323. { msg: 'log 2' },
  324. { level: 50, msg: 'error' }
  325. ]
  326. })
  327. describe('when ring buffer size is positive', function() {
  328. beforeEach(function() {
  329. process.env['LOG_RING_BUFFER_SIZE'] = '20'
  330. this.logger = this.LoggingManager.initialize(this.loggerName)
  331. this.logger.ringBuffer.records = this.logBufferMock
  332. this.logger.error({}, 'error')
  333. })
  334. afterEach(function() {
  335. process.env['LOG_RING_BUFFER_SIZE'] = undefined
  336. })
  337. it('should include buffered logs in error log and filter out error logs in buffer', function() {
  338. this.bunyanLogger.error.lastCall.args[0].logBuffer.should.deep.equal([
  339. { msg: 'log 1' },
  340. { msg: 'log 2' }
  341. ])
  342. })
  343. })
  344. describe('when ring buffer size is zero', function() {
  345. beforeEach(function() {
  346. process.env['LOG_RING_BUFFER_SIZE'] = '0'
  347. this.logger = this.LoggingManager.initialize(this.loggerName)
  348. this.logger.error({}, 'error')
  349. })
  350. afterEach(function() {
  351. process.env['LOG_RING_BUFFER_SIZE'] = undefined
  352. })
  353. it('should not include buffered logs in error log', function() {
  354. expect(this.bunyanLogger.error.lastCall.args[0].logBuffer).be.undefined
  355. })
  356. })
  357. })
  358. describe('stackdriver logging', function() {
  359. describe('when STACKDRIVER_LOGGING is unset', function() {
  360. beforeEach(function() {
  361. process.env['STACKDRIVER_LOGGING'] = undefined
  362. this.LoggingManager.initialize(this.loggerName)
  363. })
  364. it('is disabled', function() {
  365. expect(this.bunyanLogger.addStream).not.to.have.been.calledWith(
  366. this.stackdriverStreamConfig
  367. )
  368. })
  369. })
  370. describe('when STACKDRIVER_LOGGING is true', function() {
  371. beforeEach(function() {
  372. process.env['STACKDRIVER_LOGGING'] = 'true'
  373. this.LoggingManager.initialize(this.loggerName)
  374. })
  375. it('is enabled', function() {
  376. expect(this.bunyanLogger.addStream).to.have.been.calledWith(
  377. this.stackdriverStreamConfig
  378. )
  379. })
  380. it('is configured properly', function() {
  381. expect(this.GCPLogging.LoggingBunyan).to.have.been.calledWith({
  382. logName: this.loggerName,
  383. serviceContext: { service: this.loggerName }
  384. })
  385. })
  386. })
  387. })
  388. })