loggingManagerTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. })
  229. describe('checkLogLevel', function() {
  230. it('should request log level override from google meta data service', function() {
  231. this.logger.checkLogLevel()
  232. const options = {
  233. headers: {
  234. 'Metadata-Flavor': 'Google'
  235. },
  236. uri: `http://metadata.google.internal/computeMetadata/v1/project/attributes/${this.loggerName}-setLogLevelEndTime`
  237. }
  238. this.Request.should.have.been.calledWithMatch(options)
  239. })
  240. describe('when request has error', function() {
  241. beforeEach(function() {
  242. this.Request.yields('error')
  243. this.logger.checkLogLevel()
  244. })
  245. it('should only set default level', function() {
  246. this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
  247. 'debug'
  248. )
  249. })
  250. })
  251. describe('when statusCode is not 200', function() {
  252. beforeEach(function() {
  253. this.Request.yields(null, { statusCode: 404 })
  254. this.logger.checkLogLevel()
  255. })
  256. it('should only set default level', function() {
  257. this.bunyanLogger.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. beforeEach(function() {
  264. this.Request.yields(null, { statusCode: 200 }, '1')
  265. this.logger.checkLogLevel()
  266. })
  267. it('should only set default level', function() {
  268. this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
  269. 'debug'
  270. )
  271. })
  272. })
  273. describe('when time value returned that is more than current time', function() {
  274. describe('when level is already set', function() {
  275. beforeEach(function() {
  276. this.bunyanLogger.level.returns(10)
  277. this.Request.yields(null, { statusCode: 200 }, this.start + 1000)
  278. this.logger.checkLogLevel()
  279. })
  280. it('should set trace level', function() {
  281. this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
  282. 'trace'
  283. )
  284. })
  285. })
  286. describe('when level is not already set', function() {
  287. beforeEach(function() {
  288. this.bunyanLogger.level.returns(20)
  289. this.Request.yields(null, { statusCode: 200 }, this.start + 1000)
  290. this.logger.checkLogLevel()
  291. })
  292. it('should set trace level', function() {
  293. this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith(
  294. 'trace'
  295. )
  296. })
  297. })
  298. })
  299. })
  300. describe('ringbuffer', function() {
  301. beforeEach(function() {
  302. this.logBufferMock = [
  303. { msg: 'log 1' },
  304. { msg: 'log 2' },
  305. { level: 50, msg: 'error' }
  306. ]
  307. })
  308. describe('when ring buffer size is positive', function() {
  309. beforeEach(function() {
  310. process.env['LOG_RING_BUFFER_SIZE'] = '20'
  311. this.logger = this.LoggingManager.initialize(this.loggerName)
  312. this.logger.ringBuffer.records = this.logBufferMock
  313. this.logger.error({}, 'error')
  314. })
  315. afterEach(function() {
  316. process.env['LOG_RING_BUFFER_SIZE'] = undefined
  317. })
  318. it('should include buffered logs in error log and filter out error logs in buffer', function() {
  319. this.bunyanLogger.error.lastCall.args[0].logBuffer.should.deep.equal([
  320. { msg: 'log 1' },
  321. { msg: 'log 2' }
  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. expect(this.bunyanLogger.error.lastCall.args[0].logBuffer).be.undefined
  336. })
  337. })
  338. })
  339. describe('stackdriver logging', function() {
  340. describe('when STACKDRIVER_LOGGING is unset', function() {
  341. beforeEach(function() {
  342. process.env['STACKDRIVER_LOGGING'] = undefined
  343. this.LoggingManager.initialize(this.loggerName)
  344. })
  345. it('is disabled', function() {
  346. expect(this.bunyanLogger.addStream).not.to.have.been.calledWith(
  347. this.stackdriverStreamConfig
  348. )
  349. })
  350. })
  351. describe('when STACKDRIVER_LOGGING is true', function() {
  352. beforeEach(function() {
  353. process.env['STACKDRIVER_LOGGING'] = 'true'
  354. this.LoggingManager.initialize(this.loggerName)
  355. })
  356. it('is enabled', function() {
  357. expect(this.bunyanLogger.addStream).to.have.been.calledWith(
  358. this.stackdriverStreamConfig
  359. )
  360. })
  361. it('is configured properly', function() {
  362. expect(this.GCPLogging.LoggingBunyan).to.have.been.calledWith({
  363. logName: this.loggerName,
  364. serviceContext: { service: this.loggerName }
  365. })
  366. })
  367. })
  368. })
  369. })