Repository: honeycombio/beeline-nodejs
Pull request #61 (2018-07-17 20:15:27)

[eslint] use eslint-env node + jest to clean up our eslint comments
Stop listing every global we use.  it's annoying and error-prone.  Just tell eslint where we expect to run the code and let it do its thing.



Patch:
diff --git a/lib/async_tracker.js b/lib/async_tracker.js
index fc7a02d..ebf468d 100644
--- a/lib/async_tracker.js
+++ b/lib/async_tracker.js
@@ -1,4 +1,4 @@
-/* global require module */
+/* eslint-env node */
 const async_hooks = require("async_hooks");
 
 class AsyncTracker {
diff --git a/lib/deterministic_sampler.js b/lib/deterministic_sampler.js
index a5ad464..af12f64 100644
--- a/lib/deterministic_sampler.js
+++ b/lib/deterministic_sampler.js
@@ -1,4 +1,4 @@
-/* global require module */
+/* eslint-env node */
 const createHash = require("crypto").createHash;
 
 const MAX_UINT32 = Math.pow(2, 32) - 1;
diff --git a/lib/event.js b/lib/event.js
index f586bdf..1a694f7 100644
--- a/lib/event.js
+++ b/lib/event.js
@@ -1,4 +1,4 @@
-/* global require exports __dirname */
+/* eslint-env node */
 const libhoney = require("libhoney").default,
   os = require("os"),
   process = require("process"),
@@ -59,13 +59,14 @@ class MockEventAPI {
     let id = this.traceId++;
     let requestContext = { id: traceId || id };
     tracker.setTracked(requestContext);
-    return this.startEvent(requestContext, source, name);
+    return this.startEvent(source, name);
   }
   finishRequest(ev) {
     this.finishEvent(ev);
     tracker.deleteTracked();
   }
-  startEvent(context, source, name) {
+  startEvent(source, name) {
+    let context = tracker.getTracked();
     const eventPayload = {
       [schema.TRACE_ID]: context.id,
       [schema.TRACE_SPAN_NAME]: name,
@@ -139,7 +140,7 @@ class LibhoneyEventAPI {
       stack: [],
     };
     tracker.setTracked(requestContext);
-    return this.startEvent(requestContext, source, name, id);
+    return this.startEvent(source, name, id);
   }
 
   finishRequest(ev) {
@@ -147,7 +148,8 @@ class LibhoneyEventAPI {
     tracker.deleteTracked();
   }
 
-  startEvent(context, source, name, spanId = uuidv4()) {
+  startEvent(source, name, spanId = uuidv4()) {
+    let context = tracker.getTracked();
     let parentId;
     if (context.stack.length > 0) {
       parentId = context.stack[context.stack.length - 1][schema.TRACE_SPAN_ID];
@@ -281,6 +283,9 @@ const customContext = {
 exports.configure = configure;
 exports.apiForTesting = apiForTesting;
 exports.resetForTesting = resetForTesting;
+exports.traceActive = function traceActive() {
+  return !!tracker.getTracked();
+};
 exports.startRequest = function startRequest(source, name, traceId) {
   return eventAPI.startRequest(source, name, traceId);
 };
@@ -290,8 +295,8 @@ exports.finishRequest = function finishRequest(ev) {
 exports.addContext = function addContext(map) {
   return eventAPI.addContext(map);
 };
-exports.startEvent = function startEvent(context, source, name) {
-  return eventAPI.startEvent(context, source, name);
+exports.startEvent = function startEvent(source, name) {
+  return eventAPI.startEvent(source, name);
 };
 exports.finishEvent = function finishEvent(ev, rollup) {
   return eventAPI.finishEvent(ev, rollup);
diff --git a/lib/index.js b/lib/index.js
index 2c90eac..cb469dc 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -1,4 +1,4 @@
-/* global require module */
+/* eslint-env node */
 const event = require("./event"),
   instrumentation = require("./instrumentation");
 
diff --git a/lib/instrumentation.js b/lib/instrumentation.js
index a976b72..9ce9c22 100644
--- a/lib/instrumentation.js
+++ b/lib/instrumentation.js
@@ -1,4 +1,4 @@
-/* global require exports __dirname global console */
+/* eslint-env node */
 const shimmer = require("shimmer"),
   tracker = require("./async_tracker"),
   Module = require("module"),
diff --git a/lib/instrumentation/bluebird.js b/lib/instrumentation/bluebird.js
index 721f2fd..053b0b0 100644
--- a/lib/instrumentation/bluebird.js
+++ b/lib/instrumentation/bluebird.js
@@ -1,4 +1,4 @@
-/* global require, module */
+/* eslint-env node */
 const shimmer = require("shimmer"),
   tracker = require("../async_tracker");
 
diff --git a/lib/instrumentation/child_process.js b/lib/instrumentation/child_process.js
index 71531c3..a0d791b 100644
--- a/lib/instrumentation/child_process.js
+++ b/lib/instrumentation/child_process.js
@@ -1,14 +1,12 @@
-/* global require, module */
+/* eslint-env node */
 const shimmer = require("shimmer"),
-  tracker = require("../async_tracker"),
   event = require("../event"),
   schema = require("../schema");
 
 function wrapExecLike(name, packageVersion) {
   return function(original) {
     return function(file, args /*, options, callback */) {
-      let context = tracker.getTracked();
-      if (!context) {
+      if (!event.traceActive) {
         return original.apply(this, arguments);
       }
 
@@ -30,7 +28,7 @@ function wrapExecLike(name, packageVersion) {
         };
       });
 
-      ev = event.startEvent(context, "child_process", name);
+      ev = event.startEvent("child_process", name);
       event.addContext({
         [schema.PACKAGE_VERSION]: packageVersion,
       });
diff --git a/lib/instrumentation/express.js b/lib/instrumentation/express.js
index 09d0433..6024eb5 100644
--- a/lib/instrumentation/express.js
+++ b/lib/instrumentation/express.js
@@ -1,4 +1,4 @@
-/* global require, module, __dirname */
+/* eslint-env node */
 const onHeaders = require("on-headers"),
   tracker = require("../async_tracker"),
   schema = require("../schema"),
diff --git a/lib/instrumentation/http.js b/lib/instrumentation/http.js
index 7382400..a0c6cc8 100644
--- a/lib/instrumentation/http.js
+++ b/lib/instrumentation/http.js
@@ -1,7 +1,6 @@
-/* global require, module */
+/* eslint-env node */
 const url = require("url"),
   shimmer = require("shimmer"),
-  tracker = require("../async_tracker"),
   event = require("../event"),
   schema = require("../schema");
 
@@ -18,8 +17,7 @@ let instrumentHTTP = (http, opts = {}) => {
 
   shimmer.wrap(http, "request", function(original) {
     return function(options, cb) {
-      let context = tracker.getTracked();
-      if (!context) {
+      if (!event.traceActive()) {
         return original.apply(this, [options, cb]);
       }
 
@@ -34,7 +32,7 @@ let instrumentHTTP = (http, opts = {}) => {
         }
       };
 
-      ev = event.startEvent(context, "http", "request");
+      ev = event.startEvent("http", "request");
       event.addContext({
         [schema.PACKAGE_VERSION]: opts.packageVersion,
       });
diff --git a/lib/instrumentation/https.js b/lib/instrumentation/https.js
index 2ba564f..309796e 100644
--- a/lib/instrumentation/https.js
+++ b/lib/instrumentation/https.js
@@ -1,7 +1,6 @@
-/* global require, module */
+/* eslint-env node */
 const url = require("url"),
   shimmer = require("shimmer"),
-  tracker = require("../async_tracker"),
   event = require("../event"),
   schema = require("../schema");
 
@@ -18,8 +17,7 @@ let instrumentHTTPS = (https, opts = {}) => {
 
   shimmer.wrap(https, "request", function(original) {
     return function(options, cb) {
-      let context = tracker.getTracked();
-      if (!context) {
+      if (!event.traceActive()) {
         return original.apply(this, [options, cb]);
       }
 
@@ -34,7 +32,7 @@ let instrumentHTTPS = (https, opts = {}) => {
         }
       };
 
-      ev = event.startEvent(context, "https", "request");
+      ev = event.startEvent("https", "request");
       event.addContext({
         [schema.PACKAGE_VERSION]: opts.packageVersion,
       });
diff --git a/lib/instrumentation/mongodb.js b/lib/instrumentation/mongodb.js
index e54cae5..b0a631d 100644
--- a/lib/instrumentation/mongodb.js
+++ b/lib/instrumentation/mongodb.js
@@ -1,11 +1,11 @@
-/* global require, module */
+/* eslint-env node */
 const shimmer = require("shimmer"),
   tracker = require("../async_tracker"),
   event = require("../event"),
   schema = require("../schema");
 
-function startCollectionEvent(context, packageVersion, name) {
-  let ev = event.startEvent(context, "mongodb", `collection.${name}`);
+function startCollectionEvent(packageVersion, name) {
+  let ev = event.startEvent("mongodb", `collection.${name}`);
   event.addContext({
     [schema.PACKAGE_VERSION]: packageVersion,
   });
@@ -43,8 +43,7 @@ function instrumentMongodb(mongodb, opts = {}) {
   function wrap(name, populateArgs, additionalContext) {
     shimmer.wrap(mongodb.Collection.prototype, name, function(original) {
       return function(...args) {
-        let context = tracker.getTracked();
-        if (!context) {
+        if (!event.traceActive()) {
           return original.apply(this, args);
         }
 
@@ -63,7 +62,7 @@ function instrumentMongodb(mongodb, opts = {}) {
           }
         });
 
-        ev = startCollectionEvent(context, opts.packageVersion, name);
+        ev = startCollectionEvent(opts.packageVersion, name);
         event.addContext(prefixKeys("db.options", options));
         if (additionalContext) {
           event.addContext(prefixKeys("db", additionalContext(...populatedArgs)));
diff --git a/lib/instrumentation/mongoose.js b/lib/instrumentation/mongoose.js
index 5e55198..c1b160d 100644
--- a/lib/instrumentation/mongoose.js
+++ b/lib/instrumentation/mongoose.js
@@ -1,4 +1,4 @@
-/* global require, module */
+/* eslint-env node */
 const shimmer = require("shimmer"),
   tracker = require("../async_tracker");
 
diff --git a/lib/instrumentation/mpromise.js b/lib/instrumentation/mpromise.js
index 50f9baf..5f328f2 100644
--- a/lib/instrumentation/mpromise.js
+++ b/lib/instrumentation/mpromise.js
@@ -1,4 +1,4 @@
-/* global require, module */
+/* eslint-env node */
 const shimmer = require("shimmer"),
   tracker = require("../async_tracker");
 
diff --git a/lib/instrumentation/mysql2.js b/lib/instrumentation/mysql2.js
index a00915d..03f5ae1 100644
--- a/lib/instrumentation/mysql2.js
+++ b/lib/instrumentation/mysql2.js
@@ -1,4 +1,4 @@
-/* global require, module */
+/* eslint-env node */
 const shimmer = require("shimmer"),
   tracker = require("../async_tracker"),
   event = require("../event"),
@@ -10,8 +10,7 @@ let instrumentConnection = function(conn, packageVersion) {
       if (args.length < 1) {
         return original.apply(this, args);
       }
-      let context = tracker.getTracked();
-      if (!context) {
+      if (!event.traceActive()) {
         return original.apply(this, args);
       }
 
@@ -28,7 +27,7 @@ let instrumentConnection = function(conn, packageVersion) {
       };
       args = args.slice(0, -1).concat(wrapped_cb);
 
-      ev = event.startEvent(context, "mysql2", "query");
+      ev = event.startEvent("mysql2", "query");
       event.addContext({
         [schema.PACKAGE_VERSION]: packageVersion,
       });
@@ -42,8 +41,7 @@ let instrumentConnection = function(conn, packageVersion) {
       if (args.length < 1) {
         return original.apply(this, args);
       }
-      let context = tracker.getTracked();
-      if (!context) {
+      if (!event.traceActive()) {
         return original.apply(this, args);
       }
 
@@ -62,7 +60,7 @@ let instrumentConnection = function(conn, packageVersion) {
       });
       args = args.slice(0, -1).concat(wrapped_cb);
 
-      ev = event.startEvent(context, "mysql2", "query");
+      ev = event.startEvent("mysql2", "query");
       event.addContext({
         [schema.PACKAGE_VERSION]: packageVersion,
       });
diff --git a/lib/instrumentation/react-dom-server.js b/lib/instrumentation/react-dom-server.js
index 8773f32..7ba8fcb 100644
--- a/lib/instrumentation/react-dom-server.js
+++ b/lib/instrumentation/react-dom-server.js
@@ -1,18 +1,16 @@
-/* global require, module */
+/* eslint-env node */
 const shimmer = require("shimmer"),
-  tracker = require("../async_tracker"),
   event = require("../event"),
   schema = require("../schema");
 
 function shimRenderMethod(reactDOMServer, name, packageVersion) {
   shimmer.wrap(reactDOMServer, name, function(original) {
     return function(...args) {
-      let context = tracker.getTracked();
-      if (!context) {
+      if (!event.traceActive()) {
         return original.apply(this, args);
       }
 
-      let ev = event.startEvent(context, "react", name);
+      let ev = event.startEvent("react", name);
       event.addContext({
         [schema.PACKAGE_VERSION]: packageVersion,
       });
diff --git a/lib/instrumentation/sequelize.js b/lib/instrumentation/sequelize.js
index 33db793..9c99418 100644
--- a/lib/instrumentation/sequelize.js
+++ b/lib/instrumentation/sequelize.js
@@ -1,4 +1,4 @@
-/* global require, module */
+/* eslint-env node */
 const instrumentBluebird = require("./bluebird");
 
 let instrumentSequelize = sequelize => {
diff --git a/lib/schema.js b/lib/schema.js
index 5095b1a..07a4e2a 100644
--- a/lib/schema.js
+++ b/lib/schema.js
@@ -1,4 +1,4 @@
-/* global exports */
+/* eslint-env node */
 
 exports.EVENT_TYPE = "meta.type"; // XXX(toshok) rename "type" to "source"?
 exports.NODE_VERSION = "meta.node_version";


Test patch:
diff --git a/lib/async_tracker.test.js b/lib/async_tracker.test.js
index 399e799..6af9b4f 100644
--- a/lib/async_tracker.test.js
+++ b/lib/async_tracker.test.js
@@ -1,4 +1,4 @@
-/* global require test expect setTimeout */
+/* eslint-env node, jest */
 const tracker = require("./async_tracker");
 
 test("local (non-continuation-based) tracked access", () => {
diff --git a/lib/deterministic_sampler.test.js b/lib/deterministic_sampler.test.js
index 6f436a5..0ab0bbe 100644
--- a/lib/deterministic_sampler.test.js
+++ b/lib/deterministic_sampler.test.js
@@ -1,4 +1,4 @@
-/* global require test expect */
+/* eslint-env node, jest */
 const DeterministicSampler = require("./deterministic_sampler");
 
 test("it works (sample datapoints)", () => {
diff --git a/lib/event.test.js b/lib/event.test.js
index 3acb3d7..06fd2a2 100644
--- a/lib/event.test.js
+++ b/lib/event.test.js
@@ -1,4 +1,4 @@
-/* global require beforeEach afterEach describe test expect __dirname */
+/* eslint-env node, jest */
 const path = require("path"),
   event = require("./event"),
   schema = require("./schema"),
@@ -63,8 +63,8 @@ test("ending events out of order isn't allowed", () => {
 
   expect(context.stack).toEqual([eventPayload]);
 
-  let event2Payload = event.startEvent(context, "source2", "name2");
-  let event3Payload = event.startEvent(context, "source3", "name3");
+  let event2Payload = event.startEvent("source2", "name2");
+  let event3Payload = event.startEvent("source3", "name3");
   expect(context.stack).toEqual([eventPayload, event2Payload, event3Payload]);
 
   // if we end event2Payload from the stack, we should also remove event3Payload.
@@ -78,9 +78,8 @@ test("ending events out of order isn't allowed", () => {
 
 test("sub-events can opt to rollup count/duration into request events", () => {
   let requestPayload = event.startRequest("source", "name");
-  let context = tracker.getTracked();
 
-  let eventPayload = event.startEvent(context, "source2", "name2");
+  let eventPayload = event.startEvent("source2", "name2");
   event.finishEvent(eventPayload, "rollup");
 
   let durationMS = eventPayload[schema.DURATION_MS];
@@ -88,7 +87,7 @@ test("sub-events can opt to rollup count/duration into request events", () => {
   expect(requestPayload["totals.source2.rollup.duration_ms"]).toBe(durationMS);
 
   // another event from the same source
-  let event2Payload = event.startEvent(context, "source2", "name2");
+  let event2Payload = event.startEvent("source2", "name2");
   event.finishEvent(event2Payload, "rollup");
 
   let duration2MS = event2Payload[schema.DURATION_MS];
@@ -100,7 +99,7 @@ test("sub-events can opt to rollup count/duration into request events", () => {
   expect(requestPayload["totals.source2.duration_ms"]).toBe(durationMS + duration2MS);
 
   // one more event from the same source but a different name
-  let event3Payload = event.startEvent(context, "source2");
+  let event3Payload = event.startEvent("source2");
   event.finishEvent(event3Payload, "rollup2");
 
   let duration3MS = event3Payload[schema.DURATION_MS];
@@ -120,9 +119,8 @@ test("sub-events will get manual tracing fields", () => {
   const honey = event.apiForTesting().honey;
 
   let requestPayload = event.startRequest("source", "name");
-  let context = tracker.getTracked();
 
-  let eventPayload = event.startEvent(context, "source2", "name2");
+  let eventPayload = event.startEvent("source2", "name2");
   event.finishEvent(eventPayload);
 
   // finish the request
@@ -174,14 +172,13 @@ describe("custom context", () => {
     const honey = event.apiForTesting().honey;
 
     let requestPayload = event.startRequest("source", "name");
-    let context = tracker.getTracked();
 
-    let eventPayload = event.startEvent(context, "source2", "name2");
+    let eventPayload = event.startEvent("source2", "name2");
     event.finishEvent(eventPayload);
 
     event.customContext.add("testKey", "testVal");
 
-    let eventPayload2 = event.startEvent(context, "source3", "name3");
+    let eventPayload2 = event.startEvent("source3", "name3");
     event.finishEvent(eventPayload2);
 
     // finish the request
diff --git a/lib/index.test.js b/lib/index.test.js
index 3f27874..777b548 100644
--- a/lib/index.test.js
+++ b/lib/index.test.js
@@ -1,4 +1,4 @@
-/* global require test expect */
+/* eslint-env node, jest */
 test("the default export from the package has asyncTracker and customContext properties", () => {
   const beeline = require("./");
   expect(beeline.asyncTracker).toBeDefined();
diff --git a/lib/instrumentation.test.js b/lib/instrumentation.test.js
index 5f0b9d0..1bd9c13 100644
--- a/lib/instrumentation.test.js
+++ b/lib/instrumentation.test.js
@@ -1,4 +1,4 @@
-/* global require test expect */
+/* eslint-env node, jest */
 const instrumentation = require("./instrumentation");
 
 test("preload shims Promise.then", () => {
diff --git a/lib/instrumentation/child_process.test.js b/lib/instrumentation/child_process.test.js
index 993d708..776aa92 100644
--- a/lib/instrumentation/child_process.test.js
+++ b/lib/instrumentation/child_process.test.js
@@ -1,4 +1,4 @@
-/* global require expect test beforeAll */
+/* eslint-env node, jest */
 const child_process = require("child_process"),
   instrumentChildProcess = require("./child_process"),
   tracker = require("../async_tracker"),
diff --git a/lib/instrumentation/express.test.js b/lib/instrumentation/express.test.js
index a3f5354..9d4c21b 100644
--- a/lib/instrumentation/express.test.js
+++ b/lib/instrumentation/express.test.js
@@ -1,4 +1,4 @@
-/* global __dirname require expect describe test beforeEach afterEach */
+/* eslint-env node, jest */
 const request = require("supertest"),
   path = require("path"),
   instrumentExpress = require("./express"),
diff --git a/lib/instrumentation/http.test.js b/lib/instrumentation/http.test.js
index db96cb3..74952b1 100644
--- a/lib/instrumentation/http.test.js
+++ b/lib/instrumentation/http.test.js
@@ -1,4 +1,4 @@
-/* global require expect test beforeAll afterAll */
+/* eslint-env node, jest */
 const http = require("http"),
   instrumentHttp = require("./http"),
   schema = require("../schema"),
diff --git a/lib/instrumentation/https.test.js b/lib/instrumentation/https.test.js
index e28322a..8727a25 100644
--- a/lib/instrumentation/https.test.js
+++ b/lib/instrumentation/https.test.js
@@ -1,4 +1,4 @@
-/* global require expect beforeAll test */
+/* eslint-env node, jest */
 const https = require("https"),
   instrumentHttps = require("./https"),
   schema = require("../schema"),
diff --git a/lib/instrumentation/react-dom-server.test.js b/lib/instrumentation/react-dom-server.test.js
index 4aa30fa..83c85c8 100644
--- a/lib/instrumentation/react-dom-server.test.js
+++ b/lib/instrumentation/react-dom-server.test.js
@@ -1,4 +1,4 @@
-/* global require expect test beforeAll describe */
+/* eslint-env node, jest */
 const ReactDOMServer = require("react-dom/server"),
   React = require("react"),
   instrumentReactDOMServer = require("./react-dom-server"),