loggingManagerTests.js 14 KB

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