@autofleet/outbreak 0.1.0-beta-2 → 0.2.0-beta-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dist/src/{http-wrapper/config.d.ts → config.d.ts} +0 -0
  2. package/dist/src/{http-wrapper/config.js → config.js} +0 -0
  3. package/dist/src/config.js.map +1 -0
  4. package/dist/src/example.d.ts +1 -0
  5. package/dist/src/example.js +46 -0
  6. package/dist/src/example.js.map +1 -0
  7. package/dist/src/{http-wrapper/wrapper.d.ts → http_wrapper.d.ts} +0 -0
  8. package/dist/src/{http-wrapper/wrapper.js → http_wrapper.js} +16 -9
  9. package/dist/src/http_wrapper.js.map +1 -0
  10. package/dist/src/index.d.ts +17 -4
  11. package/dist/src/index.js +36 -11
  12. package/dist/src/index.js.map +1 -1
  13. package/dist/src/tracer.d.ts +10 -0
  14. package/dist/src/tracer.js +55 -0
  15. package/dist/src/tracer.js.map +1 -0
  16. package/dist/tests/http-wrapper.test.js +7 -9
  17. package/dist/tests/http-wrapper.test.js.map +1 -1
  18. package/package.json +1 -1
  19. package/src/{http-wrapper/config.ts → config.ts} +1 -1
  20. package/src/example.ts +39 -0
  21. package/src/{http-wrapper/wrapper.ts → http_wrapper.ts} +8 -8
  22. package/src/index.ts +45 -6
  23. package/src/tracer.ts +67 -0
  24. package/tests/http-wrapper.test.ts +21 -22
  25. package/dist/src/http-wrapper/config.js.map +0 -1
  26. package/dist/src/http-wrapper/index.d.ts +0 -14
  27. package/dist/src/http-wrapper/index.js +0 -39
  28. package/dist/src/http-wrapper/index.js.map +0 -1
  29. package/dist/src/http-wrapper/wrapper.js.map +0 -1
  30. package/dist/src/identity/ApiUser.d.ts +0 -25
  31. package/dist/src/identity/ApiUser.js +0 -73
  32. package/dist/src/identity/ApiUser.js.map +0 -1
  33. package/dist/src/identity/index.d.ts +0 -3
  34. package/dist/src/identity/index.js +0 -13
  35. package/dist/src/identity/index.js.map +0 -1
  36. package/dist/src/identity/middlewares.d.ts +0 -12
  37. package/dist/src/identity/middlewares.js +0 -69
  38. package/dist/src/identity/middlewares.js.map +0 -1
  39. package/dist/src/identity/utils.d.ts +0 -5
  40. package/dist/src/identity/utils.js +0 -79
  41. package/dist/src/identity/utils.js.map +0 -1
  42. package/dist/src/network/index.d.ts +0 -1
  43. package/dist/src/network/index.js +0 -17
  44. package/dist/src/network/index.js.map +0 -1
  45. package/dist/src/tracer/index.d.ts +0 -16
  46. package/dist/src/tracer/index.js +0 -48
  47. package/dist/src/tracer/index.js.map +0 -1
  48. package/src/http-wrapper/index.ts +0 -41
  49. package/src/tracer/index.ts +0 -60
File without changes
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":";;AAAA,MAAM,mBAAmB,GAAG,YAAY,CAAC;AAEzC,MAAM,kBAAkB,GAAG,CAAC,OAAO,EAAQ,EAAE;IAC3C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;QAC3B,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;KAChD;IAED,iEAAiE;IACjE,MAAM,eAAe,GAAG,iBAAiB,CAAC;IAE1C,MAAM,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAEhG,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE;QAC7B,MAAM,IAAI,KAAK,CAAC,yCAAyC,cAAc,EAAE,CAAC,CAAC;KAC5E;AACH,CAAC,CAAC;AASF,MAAM,IAAI,GAAG,CAAC,YAAuB,EAAE,EAAE,EAAE;IACzC,MAAM,EACJ,4BAA4B,GAAG,IAAI,EACnC,oBAAoB,GAAG,KAAK,EAC5B,kBAAkB,GAAG;QACnB,cAAc;QACd,cAAc;KACf,EACD,aAAa,GAAG,OAAO,GACxB,GAAG,SAAS,CAAC;IAEd,kBAAkB,CAAC,kBAAkB,CAAC,CAAC;IAEvC,MAAM,gBAAgB,GAAG,kBAAkB,CAAC;IAE5C,IAAI,eAAe,GAAG,gBAAgB,CAAC;IACvC,IAAI,4BAA4B,KAAK,IAAI,EAAE;QACzC,eAAe,GAAG,CAAC,mBAAmB,CAAC,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;KAClE;IAED,OAAO;QACL,4BAA4B;QAC5B,oBAAoB;QACpB,mBAAmB;QACnB,gBAAgB;QAChB,eAAe;QACf,aAAa;KACd,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe;IACb,IAAI;IACJ,mBAAmB;CACpB,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const express_1 = __importDefault(require("express"));
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const winston_1 = __importDefault(require("winston"));
18
+ const index_1 = __importDefault(require("./index"));
19
+ axios_1.default.defaults.adapter = require('axios/lib/adapters/http');
20
+ const logger = winston_1.default.createLogger({
21
+ transports: [
22
+ new winston_1.default.transports.Console(),
23
+ ],
24
+ });
25
+ index_1.default({
26
+ setAndPropagateCorrelationId: true,
27
+ headersToPropagate: [
28
+ 'x-text',
29
+ ],
30
+ headersPrefix: 'x-af-',
31
+ winstonLogger: logger,
32
+ });
33
+ const app = express_1.default();
34
+ app.all('/', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
35
+ const network = axios_1.default.create({ baseURL: 'http://localhost:8080/' });
36
+ const { data } = yield network.get('/header-parrot');
37
+ res.json(data);
38
+ }));
39
+ app.all('/header-parrot', (req, res) => {
40
+ logger.info('Test message', { t: 1 });
41
+ res.json(req.headers);
42
+ });
43
+ app.listen(8080, () => {
44
+ logger.info('App listen');
45
+ });
46
+ //# sourceMappingURL=example.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"example.js","sourceRoot":"","sources":["../../src/example.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,sDAA8B;AAC9B,kDAA0B;AAC1B,sDAA8B;AAE9B,oDAAoC;AAEpC,eAAK,CAAC,QAAQ,CAAC,OAAO,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAAC;AAE5D,MAAM,MAAM,GAAG,iBAAO,CAAC,YAAY,CAAC;IAClC,UAAU,EAAE;QACV,IAAI,iBAAO,CAAC,UAAU,CAAC,OAAO,EAAE;KACjC;CACF,CAAC,CAAC;AAEH,eAAa,CAAC;IACZ,4BAA4B,EAAE,IAAI;IAClC,kBAAkB,EAAE;QAClB,QAAQ;KACT;IACD,aAAa,EAAE,OAAO;IACtB,aAAa,EAAE,MAAM;CACtB,CAAC,CAAC;AAEH,MAAM,GAAG,GAAG,iBAAO,EAAE,CAAC;AAEtB,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAO,GAAG,EAAE,GAAG,EAAE,EAAE;IAC9B,MAAM,OAAO,GAAG,eAAK,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,wBAAwB,EAAE,CAAC,CAAC;IACpE,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;IACrD,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACjB,CAAC,CAAA,CAAC,CAAC;AAEH,GAAG,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;IACrC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACtC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACxB,CAAC,CAAC,CAAC;AAEH,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;IACpB,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;AAC5B,CAAC,CAAC,CAAC"}
@@ -2,11 +2,18 @@
2
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
+ var __importStar = (this && this.__importStar) || function (mod) {
6
+ if (mod && mod.__esModule) return mod;
7
+ var result = {};
8
+ if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
9
+ result["default"] = mod;
10
+ return result;
11
+ };
5
12
  Object.defineProperty(exports, "__esModule", { value: true });
6
13
  /* eslint-disable max-len */
7
14
  const http_1 = __importDefault(require("http"));
8
15
  const shortid_1 = __importDefault(require("shortid"));
9
- const tracer_1 = require("../tracer");
16
+ const tracer_1 = __importStar(require("./tracer"));
10
17
  const originalHttpCreateServer = http_1.default.createServer;
11
18
  const originalRequest = http_1.default.request;
12
19
  const originalGet = http_1.default.get;
@@ -15,7 +22,7 @@ function setAndCollectCorrelationId(config, req, res) {
15
22
  if (typeof correlationId === 'undefined') {
16
23
  correlationId = shortid_1.default.generate();
17
24
  }
18
- tracer_1.getCurrentTrace().context.set(config.correlationIdHeader, correlationId);
25
+ tracer_1.default.currentTrace.context.set(config.correlationIdHeader, correlationId);
19
26
  res.setHeader(config.correlationIdHeader, correlationId);
20
27
  }
21
28
  function collect(req, headers, headersPrefix) {
@@ -24,14 +31,14 @@ function collect(req, headers, headersPrefix) {
24
31
  : [];
25
32
  [...headers, ...prefixHeaders].forEach((header) => {
26
33
  if (typeof req.headers[header] !== 'undefined') {
27
- tracer_1.getCurrentTrace().context.set(header, req.headers[header]);
34
+ tracer_1.default.currentTrace.context.set(header, req.headers[header]);
28
35
  }
29
36
  });
30
37
  }
31
38
  function injectInResponse(response, headers) {
32
- if (tracer_1.getCurrentTrace()) {
39
+ if (tracer_1.default.currentTrace) {
33
40
  headers.forEach((header) => {
34
- response.setHeader(header, tracer_1.getCurrentTrace().context.get(header));
41
+ response.setHeader(header, tracer_1.default.currentTrace.context.get(header));
35
42
  });
36
43
  }
37
44
  }
@@ -55,14 +62,14 @@ function wrapHttpCreateServer(config) {
55
62
  };
56
63
  }
57
64
  function inject(options) {
58
- if (tracer_1.getCurrentTrace()) {
65
+ if (tracer_1.default.currentTrace) {
59
66
  if (!options.headers) {
60
67
  // eslint-disable-next-line no-param-reassign
61
68
  options.headers = {};
62
69
  }
63
- [...(tracer_1.getCurrentTrace().context.keys())].forEach((header) => {
70
+ [...(tracer_1.default.currentTrace.context.keys())].forEach((header) => {
64
71
  // eslint-disable-next-line no-param-reassign
65
- options.headers[header] = tracer_1.getCurrentTrace().context.get(header);
72
+ options.headers[header] = tracer_1.default.currentTrace.context.get(header);
66
73
  });
67
74
  }
68
75
  }
@@ -95,4 +102,4 @@ function wrapHttp(config) {
95
102
  http_1.default.get = wrapHttpRequest(originalGet);
96
103
  }
97
104
  exports.default = wrapHttp;
98
- //# sourceMappingURL=wrapper.js.map
105
+ //# sourceMappingURL=http_wrapper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http_wrapper.js","sourceRoot":"","sources":["../../src/http_wrapper.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,4BAA4B;AAC5B,gDAAwB;AACxB,sDAA8B;AAG9B,mDAA4C;AAE5C,MAAM,wBAAwB,GAAG,cAAI,CAAC,YAAY,CAA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CAAC,CAAC;AAC1C,CAAC;AAJD,2BAIC"}
@@ -1,4 +1,17 @@
1
- import headersTracer from './http-wrapper';
2
- import { newTrace, getCurrentTrace } from './tracer';
3
- import { ApiUser, middleware, middlewareWithDecode, eagerLoadPermissionsMiddleware, getDecodedBearer } from './identity';
4
- export { headersTracer, getCurrentTrace, newTrace, ApiUser, middleware, middlewareWithDecode, eagerLoadPermissionsMiddleware, getDecodedBearer, };
1
+ import winston from 'winston';
2
+ interface Options {
3
+ winstonLogger?: winston.Logger;
4
+ loggerTraceKey?: string;
5
+ headersPrefix?: string;
6
+ setAndPropagateCorrelationId?: boolean;
7
+ headersToPropagate?: string[];
8
+ }
9
+ export default function (options: Options): void;
10
+ export declare const getCurrentContext: () => any;
11
+ export declare const newTrace: (type: any) => void;
12
+ export declare const traceTypes: {
13
+ HTTP_REQUEST: string;
14
+ WEB_SOCKET: string;
15
+ RABBIT: string;
16
+ };
17
+ export {};
package/dist/src/index.js CHANGED
@@ -2,16 +2,41 @@
2
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
+ var __importStar = (this && this.__importStar) || function (mod) {
6
+ if (mod && mod.__esModule) return mod;
7
+ var result = {};
8
+ if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
9
+ result["default"] = mod;
10
+ return result;
11
+ };
5
12
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const http_wrapper_1 = __importDefault(require("./http-wrapper"));
7
- exports.headersTracer = http_wrapper_1.default;
8
- const tracer_1 = require("./tracer");
9
- exports.newTrace = tracer_1.newTrace;
10
- exports.getCurrentTrace = tracer_1.getCurrentTrace;
11
- const identity_1 = require("./identity");
12
- exports.ApiUser = identity_1.ApiUser;
13
- exports.middleware = identity_1.middleware;
14
- exports.middlewareWithDecode = identity_1.middlewareWithDecode;
15
- exports.eagerLoadPermissionsMiddleware = identity_1.eagerLoadPermissionsMiddleware;
16
- exports.getDecodedBearer = identity_1.getDecodedBearer;
13
+ const winston_1 = require("winston");
14
+ const http_wrapper_1 = __importDefault(require("./http_wrapper"));
15
+ const config_1 = __importDefault(require("./config"));
16
+ const tracer = __importStar(require("./tracer"));
17
+ const addMetadataToLog = (loggerTraceKey) => winston_1.format((info) => {
18
+ const currentTrace = tracer.getCurrentTrace();
19
+ if (currentTrace && currentTrace.context && currentTrace.context.get) {
20
+ // eslint-disable-next-line no-param-reassign
21
+ info[loggerTraceKey] = currentTrace.context.get(config_1.default.correlationIdHeader);
22
+ }
23
+ return info;
24
+ });
25
+ function default_1(options) {
26
+ http_wrapper_1.default(config_1.default.load(options));
27
+ tracer.enable();
28
+ if (options.winstonLogger) {
29
+ const traceKey = options.loggerTraceKey || 'traceId';
30
+ // eslint-disable-next-line no-param-reassign
31
+ options.winstonLogger.format = winston_1.format.combine(addMetadataToLog(traceKey)(), options.winstonLogger.format);
32
+ }
33
+ }
34
+ exports.default = default_1;
35
+ exports.getCurrentContext = () => tracer.getCurrentTrace();
36
+ exports.newTrace = tracer.newTrace;
37
+ exports.traceTypes = {
38
+ HTTP_REQUEST: 'httpRequest',
39
+ WEB_SOCKET: 'webSocket',
40
+ RABBIT: 'rabbit',
41
+ };
17
42
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;AAAA,kEAA2C;AAWzC,wBAXK,sBAAa,CAWL;AAVf,qCAAqD;AAYnD,mBAZO,iBAAQ,CAYP;AADR,0BAXiB,wBAAe,CAWjB;AAVjB,yCAMoB;AAMlB,kBAXA,kBAAO,CAWA;AACP,qBAXA,qBAAU,CAWA;AACV,+BAXA,+BAAoB,CAWA;AACpB,yCAXA,yCAA8B,CAWA;AAC9B,2BAXA,2BAAgB,CAWA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qCAA0C;AAE1C,kEAAyC;AACzC,sDAA8B;AAC9B,iDAAmC;AAUnC,MAAM,gBAAgB,GAAG,CAAC,cAAc,EAAE,EAAE,CAAC,gBAAM,CAAC,CAAC,IAAI,EAAE,EAAE;IAC3D,MAAM,YAAY,GAAG,MAAM,CAAC,eAAe,EAAE,CAAC;IAC9C,IAAI,YAAY,IAAI,YAAY,CAAC,OAAO,IAAI,YAAY,CAAC,OAAO,CAAC,GAAG,EAAE;QACpE,6CAA6C;QAC7C,IAAI,CAAC,cAAc,CAAC,GAAG,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,gBAAM,CAAC,mBAAmB,CAAC,CAAC;KAC7E;IAED,OAAO,IAAI,CAAC;AACd,CAAC,CAAC,CAAC;AAEH,mBAAyB,OAAgB;IACvC,sBAAW,CAAC,gBAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IAClC,MAAM,CAAC,MAAM,EAAE,CAAC;IAEhB,IAAI,OAAO,CAAC,aAAa,EAAE;QACzB,MAAM,QAAQ,GAAG,OAAO,CAAC,cAAc,IAAI,SAAS,CAAC;QACrD,6CAA6C;QAC7C,OAAO,CAAC,aAAa,CAAC,MAAM,GAAG,gBAAM,CAAC,OAAO,CAC3C,gBAAgB,CAAC,QAAQ,CAAC,EAAE,EAC5B,OAAO,CAAC,aAAa,CAAC,MAAM,CAC7B,CAAC;KACH;AACH,CAAC;AAZD,4BAYC;AAEY,QAAA,iBAAiB,GAAG,GAAQ,EAAE,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC;AAErE,mCAAmC;AAEtB,QAAA,UAAU,GAAG;IACxB,YAAY,EAAE,aAAa;IAC3B,UAAU,EAAE,WAAW;IACvB,MAAM,EAAE,QAAQ;CACjB,CAAC"}
@@ -0,0 +1,10 @@
1
+ /// <reference types="node" />
2
+ import asyncHooks from 'async_hooks';
3
+ declare const tracer: {
4
+ currentTrace: any;
5
+ traces: {};
6
+ };
7
+ export declare const newTrace: (type: any) => void;
8
+ export declare const enable: () => asyncHooks.AsyncHook;
9
+ export declare const getCurrentTrace: () => any;
10
+ export default tracer;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const async_hooks_1 = __importDefault(require("async_hooks"));
7
+ const uuid_1 = __importDefault(require("uuid"));
8
+ const prevStates = {};
9
+ const tracer = {
10
+ currentTrace: null,
11
+ traces: {},
12
+ };
13
+ function init(asyncId, type, triggerAsyncId) {
14
+ if (tracer.traces[triggerAsyncId]) {
15
+ tracer.traces[asyncId] = tracer.traces[triggerAsyncId];
16
+ }
17
+ }
18
+ function before(asyncId) {
19
+ if (!tracer.traces[asyncId]) {
20
+ return;
21
+ }
22
+ prevStates[asyncId] = tracer.currentTrace;
23
+ tracer.currentTrace = tracer.traces[asyncId];
24
+ }
25
+ function after(asyncId) {
26
+ if (!tracer.traces[asyncId]) {
27
+ return;
28
+ }
29
+ tracer.currentTrace = prevStates[asyncId];
30
+ }
31
+ function destroy(asyncId) {
32
+ if (tracer.traces[asyncId]) {
33
+ delete tracer.traces[asyncId];
34
+ delete prevStates[asyncId];
35
+ }
36
+ }
37
+ const hook = async_hooks_1.default.createHook({
38
+ init, before, after, destroy,
39
+ });
40
+ class Trace {
41
+ constructor(type) {
42
+ this.id = uuid_1.default.v1();
43
+ this.type = type;
44
+ this.context = new Map();
45
+ }
46
+ }
47
+ exports.newTrace = (type) => {
48
+ tracer.currentTrace = new Trace(type);
49
+ tracer.traces[async_hooks_1.default.executionAsyncId()] = tracer.currentTrace;
50
+ return tracer.currentTrace;
51
+ };
52
+ exports.enable = () => hook.enable();
53
+ exports.getCurrentTrace = () => tracer.currentTrace || {};
54
+ exports.default = tracer;
55
+ //# sourceMappingURL=tracer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracer.js","sourceRoot":"","sources":["../../src/tracer.ts"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AAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;IAC3B,CAAC;CACF;AAEY,QAAA,QAAQ,GAAG,CAAC,IAAI,EAAQ,EAAE;IACrC,MAAM,CAAC,YAAY,GAAG,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC;IACtC,MAAM,CAAC,MAAM,CAAC,qBAAU,CAAC,gBAAgB,EAAE,CAAC,GAAG,MAAM,CAAC,YAAY,CAAC;IACnE,OAAO,MAAM,CAAC,YAAY,CAAC;AAC7B,CAAC,CAAC;AAEW,QAAA,MAAM,GAAG,GAAyB,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;AAEnD,QAAA,eAAe,GAAG,GAAQ,EAAE,CAAC,MAAM,CAAC,YAAY,IAAI,EAAE,CAAC;AAEpE,kBAAe,MAAM,CAAC"}
@@ -15,9 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const express_1 = __importDefault(require("express"));
16
16
  const axios_1 = __importDefault(require("axios"));
17
17
  const winston_1 = __importDefault(require("winston"));
18
- const http_wrapper_1 = __importDefault(require("../src/http-wrapper"));
19
- const index_1 = require("../src/index");
20
- console.log(index_1.headersTracer.toString(), index_1.middlewareWithDecode.toString());
18
+ const index_1 = __importDefault(require("../src/index"));
21
19
  const logger = winston_1.default.createLogger({
22
20
  transports: [
23
21
  new winston_1.default.transports.Console(),
@@ -37,7 +35,7 @@ const generateApp = (addEndpoints, port) => __awaiter(void 0, void 0, void 0, fu
37
35
  describe('E2E', () => {
38
36
  it('Basic functionality', () => __awaiter(void 0, void 0, void 0, function* () {
39
37
  let server2TraceId = null;
40
- http_wrapper_1.default({
38
+ index_1.default({
41
39
  setAndPropagateCorrelationId: true,
42
40
  headersToPropagate: [
43
41
  'x-test-header',
@@ -47,7 +45,7 @@ describe('E2E', () => {
47
45
  });
48
46
  const closeServer1 = yield generateApp((app) => {
49
47
  app.get('/', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
50
- const { data: res1 } = yield axios_1.default.post(`http://localhost:8082`);
48
+ const { data: res1 } = yield axios_1.default.post('http://localhost:8082');
51
49
  res.json(res1);
52
50
  }));
53
51
  }, 8081);
@@ -56,15 +54,15 @@ describe('E2E', () => {
56
54
  server2TraceId = req.headers['x-trace-id'];
57
55
  res.json({
58
56
  value: req.headers['x-test-header'],
59
- wkanda: req.headers['x-wakanda-id']
57
+ wkanda: req.headers['x-wakanda-id'],
60
58
  });
61
59
  });
62
60
  }, 8082);
63
- const { data: res1, headers } = yield axios_1.default.get(`http://localhost:8081`, {
61
+ const { data: res1, headers } = yield axios_1.default.get('http://localhost:8081', {
64
62
  headers: {
65
63
  'x-test-header': 'testHeader',
66
- 'x-wakanda-id': 'my-wakanda-id'
67
- }
64
+ 'x-wakanda-id': 'my-wakanda-id',
65
+ },
68
66
  });
69
67
  closeServer1();
70
68
  closeServer2();
@@ -1 +1 @@
1
- {"version":3,"file":"http-wrapper.test.js","sourceRoot":"","sources":["../../tests/http-wrapper.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,sDAA8B;AAE9B,kDAA0B;AAC1B,sDAA8B;AAE9B,uEAAgD;AAChD,wCAA6E;AAE7E,OAAO,CAAC,GAAG,CAAC,qBAAC,CAAC,QAAQ,EAAE,EAAE,4BAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;AAExC,MAAM,MAAM,GAAG,iBAAO,CAAC,YAAY,CAAC;IAClC,UAAU,EAAE;QACV,IAAI,iBAAO,CAAC,UAAU,CAAC,OAAO,EAAE;KACjC;CACF,CAAC,CAAC;AAEH,MAAM,WAAW,GAAG,CAAO,YAAY,EAAE,IAAI,EAAE,EAAE;IAC/C,MAAM,GAAG,GAAG,iBAAO,EAAE,CAAC;IAEtB,YAAY,CAAC,GAAG,CAAC,CAAC;IAElB,MAAM,MAAM,GAAiB,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QACzD,MAAM,CAA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,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC,EAAE,IAAI,CAAC,CAAC;QAET,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,eAAK,CAAC,GAAG,CAAC,uBAAuB,EAAE;YACvE,OAAO,EAAE;gBACP,eAAe,EAAE,YAAY;gBAC7B,cAAc,EAAE,eAAe;aAChC;SACF,CAAC,CAAC;QACH,YAAY,EAAE,CAAC;QACf,YAAY,EAAE,CAAC;QAGf,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;QACtD,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;QACzC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC;IAC/C,CAAC,CAAA,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/outbreak",
3
- "version": "0.1.0-beta-2",
3
+ "version": "0.2.0-beta-2",
4
4
  "description": "",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",
@@ -30,7 +30,7 @@ const load = (overrides: Overrides = {}) => {
30
30
  'x-request-id',
31
31
  'x-variant-id',
32
32
  ],
33
- headersPrefix = 'x-internal-',
33
+ headersPrefix = 'x-af-',
34
34
  } = overrides;
35
35
 
36
36
  validateHeaderList(headersToPropagate);
package/src/example.ts ADDED
@@ -0,0 +1,39 @@
1
+ import express from 'express';
2
+ import axios from 'axios';
3
+ import winston from 'winston';
4
+
5
+ import headersTracer from './index';
6
+
7
+ axios.defaults.adapter = require('axios/lib/adapters/http');
8
+
9
+ const logger = winston.createLogger({
10
+ transports: [
11
+ new winston.transports.Console(),
12
+ ],
13
+ });
14
+
15
+ headersTracer({
16
+ setAndPropagateCorrelationId: true,
17
+ headersToPropagate: [
18
+ 'x-text',
19
+ ],
20
+ headersPrefix: 'x-af-',
21
+ winstonLogger: logger,
22
+ });
23
+
24
+ const app = express();
25
+
26
+ app.all('/', async (req, res) => {
27
+ const network = axios.create({ baseURL: 'http://localhost:8080/' });
28
+ const { data } = await network.get('/header-parrot');
29
+ res.json(data);
30
+ });
31
+
32
+ app.all('/header-parrot', (req, res) => {
33
+ logger.info('Test message', { t: 1 });
34
+ res.json(req.headers);
35
+ });
36
+
37
+ app.listen(8080, () => {
38
+ logger.info('App listen');
39
+ });
@@ -3,7 +3,7 @@ import http from 'http';
3
3
  import shortid from 'shortid';
4
4
  import { URL } from 'url';
5
5
  import express from 'express';
6
- import { newTrace, getCurrentTrace } from '../tracer';
6
+ import tracer, { newTrace } from './tracer';
7
7
 
8
8
  const originalHttpCreateServer = http.createServer;
9
9
  const originalRequest = http.request;
@@ -14,7 +14,7 @@ function setAndCollectCorrelationId(config, req, res): void {
14
14
  if (typeof correlationId === 'undefined') {
15
15
  correlationId = shortid.generate();
16
16
  }
17
- getCurrentTrace().context.set(config.correlationIdHeader, correlationId);
17
+ tracer.currentTrace.context.set(config.correlationIdHeader, correlationId);
18
18
  res.setHeader(config.correlationIdHeader, correlationId);
19
19
  }
20
20
 
@@ -25,15 +25,15 @@ function collect(req, headers, headersPrefix): void {
25
25
 
26
26
  [...headers, ...prefixHeaders].forEach((header) => {
27
27
  if (typeof req.headers[header] !== 'undefined') {
28
- getCurrentTrace().context.set(header, req.headers[header]);
28
+ tracer.currentTrace.context.set(header, req.headers[header]);
29
29
  }
30
30
  });
31
31
  }
32
32
 
33
33
  function injectInResponse(response, headers): void {
34
- if (getCurrentTrace()) {
34
+ if (tracer.currentTrace) {
35
35
  headers.forEach((header) => {
36
- response.setHeader(header, getCurrentTrace().context.get(header));
36
+ response.setHeader(header, tracer.currentTrace.context.get(header));
37
37
  });
38
38
  }
39
39
  }
@@ -64,14 +64,14 @@ function wrapHttpCreateServer(config) {
64
64
  }
65
65
 
66
66
  function inject(options): void {
67
- if (getCurrentTrace()) {
67
+ if (tracer.currentTrace) {
68
68
  if (!options.headers) {
69
69
  // eslint-disable-next-line no-param-reassign
70
70
  options.headers = {};
71
71
  }
72
- [...(getCurrentTrace().context.keys())].forEach((header) => {
72
+ [...(tracer.currentTrace.context.keys())].forEach((header) => {
73
73
  // eslint-disable-next-line no-param-reassign
74
- options.headers[header] = getCurrentTrace().context.get(header);
74
+ options.headers[header] = tracer.currentTrace.context.get(header);
75
75
  });
76
76
  }
77
77
  }
package/src/index.ts CHANGED
@@ -1,8 +1,47 @@
1
- import headersTracer from './http-wrapper';
2
- import { newTrace, getCurrentTrace } from './tracer';
1
+ import winston, { format } from 'winston';
3
2
 
4
- export {
5
- headersTracer,
6
- getCurrentTrace,
7
- newTrace,
3
+ import httpWrapper from './http_wrapper';
4
+ import config from './config';
5
+ import * as tracer from './tracer';
6
+
7
+ interface Options {
8
+ winstonLogger?: winston.Logger;
9
+ loggerTraceKey? : string;
10
+ headersPrefix?: string;
11
+ setAndPropagateCorrelationId?: boolean;
12
+ headersToPropagate?: string[];
13
+ }
14
+
15
+ const addMetadataToLog = (loggerTraceKey) => format((info) => {
16
+ const currentTrace = tracer.getCurrentTrace();
17
+ if (currentTrace && currentTrace.context && currentTrace.context.get) {
18
+ // eslint-disable-next-line no-param-reassign
19
+ info[loggerTraceKey] = currentTrace.context.get(config.correlationIdHeader);
20
+ }
21
+
22
+ return info;
23
+ });
24
+
25
+ export default function (options: Options): void {
26
+ httpWrapper(config.load(options));
27
+ tracer.enable();
28
+
29
+ if (options.winstonLogger) {
30
+ const traceKey = options.loggerTraceKey || 'traceId';
31
+ // eslint-disable-next-line no-param-reassign
32
+ options.winstonLogger.format = format.combine(
33
+ addMetadataToLog(traceKey)(),
34
+ options.winstonLogger.format,
35
+ );
36
+ }
37
+ }
38
+
39
+ export const getCurrentContext = (): any => tracer.getCurrentTrace();
40
+
41
+ export const { newTrace } = tracer;
42
+
43
+ export const traceTypes = {
44
+ HTTP_REQUEST: 'httpRequest',
45
+ WEB_SOCKET: 'webSocket',
46
+ RABBIT: 'rabbit',
8
47
  };
package/src/tracer.ts ADDED
@@ -0,0 +1,67 @@
1
+ import asyncHooks from 'async_hooks';
2
+ import uuid from 'uuid';
3
+
4
+ const prevStates = {};
5
+
6
+ const tracer = {
7
+ currentTrace: null,
8
+ traces: {},
9
+ };
10
+
11
+ function init(asyncId, type, triggerAsyncId): void {
12
+ if (tracer.traces[triggerAsyncId]) {
13
+ tracer.traces[asyncId] = tracer.traces[triggerAsyncId];
14
+ }
15
+ }
16
+
17
+ function before(asyncId): void {
18
+ if (!tracer.traces[asyncId]) {
19
+ return;
20
+ }
21
+ prevStates[asyncId] = tracer.currentTrace;
22
+ tracer.currentTrace = tracer.traces[asyncId];
23
+ }
24
+
25
+ function after(asyncId): void {
26
+ if (!tracer.traces[asyncId]) {
27
+ return;
28
+ }
29
+ tracer.currentTrace = prevStates[asyncId];
30
+ }
31
+
32
+ function destroy(asyncId): void {
33
+ if (tracer.traces[asyncId]) {
34
+ delete tracer.traces[asyncId];
35
+ delete prevStates[asyncId];
36
+ }
37
+ }
38
+
39
+ const hook = asyncHooks.createHook({
40
+ init, before, after, destroy,
41
+ });
42
+
43
+ class Trace {
44
+ id: string;
45
+
46
+ type: string;
47
+
48
+ context: Map<string, string>;
49
+
50
+ constructor(type) {
51
+ this.id = uuid.v1();
52
+ this.type = type;
53
+ this.context = new Map();
54
+ }
55
+ }
56
+
57
+ export const newTrace = (type): void => {
58
+ tracer.currentTrace = new Trace(type);
59
+ tracer.traces[asyncHooks.executionAsyncId()] = tracer.currentTrace;
60
+ return tracer.currentTrace;
61
+ };
62
+
63
+ export const enable = (): asyncHooks.AsyncHook => hook.enable();
64
+
65
+ export const getCurrentTrace = (): any => tracer.currentTrace || {};
66
+
67
+ export default tracer;