@eggjs/tracer 3.0.0

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 (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +77 -0
  3. package/dist/commonjs/agent.d.ts +2 -0
  4. package/dist/commonjs/agent.js +5 -0
  5. package/dist/commonjs/app/extend/agent.d.ts +2 -0
  6. package/dist/commonjs/app/extend/agent.js +8 -0
  7. package/dist/commonjs/app/extend/application.d.ts +14 -0
  8. package/dist/commonjs/app/extend/application.js +20 -0
  9. package/dist/commonjs/app/extend/context.d.ts +15 -0
  10. package/dist/commonjs/app/extend/context.js +18 -0
  11. package/dist/commonjs/app.d.ts +2 -0
  12. package/dist/commonjs/app.js +5 -0
  13. package/dist/commonjs/boot.d.ts +6 -0
  14. package/dist/commonjs/boot.js +15 -0
  15. package/dist/commonjs/config/config.default.d.ts +18 -0
  16. package/dist/commonjs/config/config.default.js +9 -0
  17. package/dist/commonjs/index.d.ts +6 -0
  18. package/dist/commonjs/index.js +10 -0
  19. package/dist/commonjs/lib/tracer.d.ts +7 -0
  20. package/dist/commonjs/lib/tracer.js +19 -0
  21. package/dist/commonjs/package.json +3 -0
  22. package/dist/esm/agent.d.ts +2 -0
  23. package/dist/esm/agent.js +3 -0
  24. package/dist/esm/app/extend/agent.d.ts +2 -0
  25. package/dist/esm/app/extend/agent.js +3 -0
  26. package/dist/esm/app/extend/application.d.ts +14 -0
  27. package/dist/esm/app/extend/application.js +16 -0
  28. package/dist/esm/app/extend/context.d.ts +15 -0
  29. package/dist/esm/app/extend/context.js +15 -0
  30. package/dist/esm/app.d.ts +2 -0
  31. package/dist/esm/app.js +3 -0
  32. package/dist/esm/boot.d.ts +6 -0
  33. package/dist/esm/boot.js +11 -0
  34. package/dist/esm/config/config.default.d.ts +18 -0
  35. package/dist/esm/config/config.default.js +7 -0
  36. package/dist/esm/index.d.ts +6 -0
  37. package/dist/esm/index.js +7 -0
  38. package/dist/esm/lib/tracer.d.ts +7 -0
  39. package/dist/esm/lib/tracer.js +15 -0
  40. package/dist/esm/package.json +3 -0
  41. package/dist/package.json +4 -0
  42. package/package.json +98 -0
  43. package/src/agent.ts +3 -0
  44. package/src/app/extend/agent.ts +3 -0
  45. package/src/app/extend/application.ts +27 -0
  46. package/src/app/extend/context.ts +25 -0
  47. package/src/app.ts +3 -0
  48. package/src/boot.ts +10 -0
  49. package/src/config/config.default.ts +22 -0
  50. package/src/index.ts +8 -0
  51. package/src/lib/tracer.ts +18 -0
  52. package/src/typings/index.d.ts +4 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) Alibaba Group Holding Limited and other contributors.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # @eggjs/tracer
2
+
3
+ [![NPM version][npm-image]][npm-url]
4
+ [![Test coverage][codecov-image]][codecov-url]
5
+ [![Known Vulnerabilities][snyk-image]][snyk-url]
6
+ [![npm download][download-image]][download-url]
7
+ [![Node.js Version](https://img.shields.io/node/v/@eggjs/tracer.svg?style=flat)](https://nodejs.org/en/download/)
8
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)
9
+ ![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/eggjs/security)
10
+
11
+ [npm-image]: https://img.shields.io/npm/v/@eggjs/tracer.svg?style=flat-square
12
+ [npm-url]: https://npmjs.org/package/@eggjs/tracer
13
+ [codecov-image]: https://img.shields.io/codecov/c/github/eggjs/tracer.svg?style=flat-square
14
+ [codecov-url]: https://codecov.io/github/eggjs/tracer?branch=master
15
+ [snyk-image]: https://snyk.io/test/npm/@eggjs/tracer/badge.svg?style=flat-square
16
+ [snyk-url]: https://snyk.io/test/npm/@eggjs/tracer
17
+ [download-image]: https://img.shields.io/npm/dm/@eggjs/tracer.svg?style=flat-square
18
+ [download-url]: https://npmjs.org/package/@eggjs/tracer
19
+
20
+ tracer plugin for egg.
21
+
22
+ ## Install
23
+
24
+ ```bash
25
+ npm i @eggjs/tracer
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ Enable tracer plugin:
31
+
32
+ ```js
33
+ // config/plugin.js
34
+ exports.tracer = {
35
+ enable: true,
36
+ package: '@eggjs/tracer',
37
+ };
38
+ ```
39
+
40
+ ### Build my own tracer
41
+
42
+ ```js
43
+ // my_tracer.js
44
+ const { Tracer } = require('@eggjs/tracer');
45
+
46
+ const counter = 0;
47
+
48
+ class MyTracer extends Tracer {
49
+ get traceId() {
50
+ return `${counter++}-${Date.now()}-${process.pid}`;
51
+ }
52
+ }
53
+ module.exports = MyTracer;
54
+ ```
55
+
56
+ Change the config to use `MyTracer`:
57
+
58
+ ```js
59
+ // config/config.default.js
60
+ exports.tracer = {
61
+ Class: require('path/to/my_tracer.js'),
62
+ };
63
+ ```
64
+
65
+ ## Questions & Suggestions
66
+
67
+ Please open an issue [here](https://github.com/eggjs/egg/issues).
68
+
69
+ ## License
70
+
71
+ [MIT](LICENSE)
72
+
73
+ ## Contributors
74
+
75
+ [![Contributors](https://contrib.rocks/image?repo=eggjs/tracer)](https://github.com/eggjs/tracer/graphs/contributors)
76
+
77
+ Made with [contributors-img](https://contrib.rocks).
@@ -0,0 +1,2 @@
1
+ import { TracerBoot } from './boot.js';
2
+ export default TracerBoot;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const boot_js_1 = require("./boot.js");
4
+ exports.default = boot_js_1.TracerBoot;
5
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYWdlbnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvYWdlbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSx1Q0FBdUM7QUFFdkMsa0JBQWUsb0JBQVUsQ0FBQyJ9
@@ -0,0 +1,2 @@
1
+ import TracerApplication from './application.js';
2
+ export default TracerApplication;
@@ -0,0 +1,8 @@
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 application_js_1 = __importDefault(require("./application.js"));
7
+ exports.default = application_js_1.default;
8
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYWdlbnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvYXBwL2V4dGVuZC9hZ2VudC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLHNFQUFpRDtBQUVqRCxrQkFBZSx3QkFBaUIsQ0FBQyJ9
@@ -0,0 +1,14 @@
1
+ import { EggCore } from '@eggjs/core';
2
+ import type { Tracer } from '../../lib/tracer.js';
3
+ declare const cacheTracer: unique symbol;
4
+ export declare const isReady: unique symbol;
5
+ export default class TracerApplication extends EggCore {
6
+ [cacheTracer]: Tracer | undefined;
7
+ get tracer(): Tracer;
8
+ }
9
+ declare module '@eggjs/core' {
10
+ interface EggCore {
11
+ tracer: Tracer;
12
+ }
13
+ }
14
+ export {};
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isReady = void 0;
4
+ const core_1 = require("@eggjs/core");
5
+ const cacheTracer = Symbol('before_ready_tracer');
6
+ exports.isReady = Symbol('egg_tracer_is_ready');
7
+ class TracerApplication extends core_1.EggCore {
8
+ [cacheTracer];
9
+ get tracer() {
10
+ if (this[exports.isReady]) {
11
+ return new this.config.tracer.Class(this.createAnonymousContext());
12
+ }
13
+ if (!this[cacheTracer]) {
14
+ this[cacheTracer] = new this.config.tracer.Class(this.createAnonymousContext());
15
+ }
16
+ return this[cacheTracer];
17
+ }
18
+ }
19
+ exports.default = TracerApplication;
20
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBwbGljYXRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvYXBwL2V4dGVuZC9hcHBsaWNhdGlvbi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFBQSxzQ0FBc0M7QUFHdEMsTUFBTSxXQUFXLEdBQUcsTUFBTSxDQUFDLHFCQUFxQixDQUFDLENBQUM7QUFDckMsUUFBQSxPQUFPLEdBQUcsTUFBTSxDQUFDLHFCQUFxQixDQUFDLENBQUM7QUFFckQsTUFBcUIsaUJBQWtCLFNBQVEsY0FBTztJQUNwRCxDQUFDLFdBQVcsQ0FBQyxDQUFxQjtJQUVsQyxJQUFJLE1BQU07UUFDUixJQUFJLElBQUksQ0FBQyxlQUFPLENBQUMsRUFBRSxDQUFDO1lBQ2xCLE9BQU8sSUFBSSxJQUFJLENBQUMsTUFBTSxDQUFDLE1BQU0sQ0FBQyxLQUFLLENBQUMsSUFBSSxDQUFDLHNCQUFzQixFQUFFLENBQUMsQ0FBQztRQUNyRSxDQUFDO1FBRUQsSUFBSSxDQUFDLElBQUksQ0FBQyxXQUFXLENBQUMsRUFBRSxDQUFDO1lBQ3ZCLElBQUksQ0FBQyxXQUFXLENBQUMsR0FBRyxJQUFJLElBQUksQ0FBQyxNQUFNLENBQUMsTUFBTSxDQUFDLEtBQUssQ0FBQyxJQUFJLENBQUMsc0JBQXNCLEVBQUUsQ0FBQyxDQUFDO1FBQ2xGLENBQUM7UUFFRCxPQUFPLElBQUksQ0FBQyxXQUFXLENBQUMsQ0FBQztJQUMzQixDQUFDO0NBQ0Y7QUFkRCxvQ0FjQyJ9
@@ -0,0 +1,15 @@
1
+ import { Context } from '@eggjs/core';
2
+ import type { Tracer } from '../../lib/tracer.js';
3
+ declare const TRACER: unique symbol;
4
+ export default class TracerContext extends Context {
5
+ [TRACER]: Tracer | undefined;
6
+ get tracer(): Tracer;
7
+ get traceId(): string;
8
+ }
9
+ declare module '@eggjs/core' {
10
+ interface Context {
11
+ tracer: Tracer;
12
+ traceId: string;
13
+ }
14
+ }
15
+ export {};
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const core_1 = require("@eggjs/core");
4
+ const TRACER = Symbol('context tracer');
5
+ class TracerContext extends core_1.Context {
6
+ [TRACER];
7
+ get tracer() {
8
+ if (!this[TRACER]) {
9
+ this[TRACER] = new this.app.config.tracer.Class(this);
10
+ }
11
+ return this[TRACER];
12
+ }
13
+ get traceId() {
14
+ return this.tracer.traceId;
15
+ }
16
+ }
17
+ exports.default = TracerContext;
18
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udGV4dC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3NyYy9hcHAvZXh0ZW5kL2NvbnRleHQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxzQ0FBc0M7QUFFdEMsTUFBTSxNQUFNLEdBQUcsTUFBTSxDQUFDLGdCQUFnQixDQUFDLENBQUM7QUFFeEMsTUFBcUIsYUFBYyxTQUFRLGNBQU87SUFDaEQsQ0FBQyxNQUFNLENBQUMsQ0FBcUI7SUFFN0IsSUFBSSxNQUFNO1FBQ1IsSUFBSSxDQUFDLElBQUksQ0FBQyxNQUFNLENBQUMsRUFBRSxDQUFDO1lBQ2xCLElBQUksQ0FBQyxNQUFNLENBQUMsR0FBRyxJQUFJLElBQUksQ0FBQyxHQUFHLENBQUMsTUFBTSxDQUFDLE1BQU0sQ0FBQyxLQUFLLENBQUMsSUFBSSxDQUFDLENBQUM7UUFDeEQsQ0FBQztRQUNELE9BQU8sSUFBSSxDQUFDLE1BQU0sQ0FBQyxDQUFDO0lBQ3RCLENBQUM7SUFFRCxJQUFJLE9BQU87UUFDVCxPQUFPLElBQUksQ0FBQyxNQUFNLENBQUMsT0FBTyxDQUFDO0lBQzdCLENBQUM7Q0FDRjtBQWJELGdDQWFDIn0=
@@ -0,0 +1,2 @@
1
+ import { TracerBoot } from './boot.js';
2
+ export default TracerBoot;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const boot_js_1 = require("./boot.js");
4
+ exports.default = boot_js_1.TracerBoot;
5
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBwLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2FwcC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLHVDQUF1QztBQUV2QyxrQkFBZSxvQkFBVSxDQUFDIn0=
@@ -0,0 +1,6 @@
1
+ import type { EggCore, ILifecycleBoot } from '@eggjs/core';
2
+ export declare class TracerBoot implements ILifecycleBoot {
3
+ private app;
4
+ constructor(app: EggCore);
5
+ didLoad(): Promise<void>;
6
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TracerBoot = void 0;
4
+ const application_js_1 = require("./app/extend/application.js");
5
+ class TracerBoot {
6
+ app;
7
+ constructor(app) {
8
+ this.app = app;
9
+ }
10
+ async didLoad() {
11
+ this.app[application_js_1.isReady] = true;
12
+ }
13
+ }
14
+ exports.TracerBoot = TracerBoot;
15
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYm9vdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ib290LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUNBLGdFQUFzRDtBQUV0RCxNQUFhLFVBQVU7SUFDRDtJQUFwQixZQUFvQixHQUFZO1FBQVosUUFBRyxHQUFILEdBQUcsQ0FBUztJQUFHLENBQUM7SUFFcEMsS0FBSyxDQUFDLE9BQU87UUFDWCxJQUFJLENBQUMsR0FBRyxDQUFDLHdCQUFPLENBQUMsR0FBRyxJQUFJLENBQUM7SUFDM0IsQ0FBQztDQUNGO0FBTkQsZ0NBTUMifQ==
@@ -0,0 +1,18 @@
1
+ import { Tracer } from '../lib/tracer.js';
2
+ /**
3
+ * tracer config
4
+ * @member Config#tracer
5
+ * @property {Tracer} Class - tracer class name
6
+ */
7
+ interface TracerConfig {
8
+ Class: typeof Tracer;
9
+ }
10
+ declare const _default: {
11
+ tracer: TracerConfig;
12
+ };
13
+ export default _default;
14
+ declare module '@eggjs/core' {
15
+ interface EggAppConfig {
16
+ tracer: TracerConfig;
17
+ }
18
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tracer_js_1 = require("../lib/tracer.js");
4
+ exports.default = {
5
+ tracer: {
6
+ Class: tracer_js_1.Tracer,
7
+ },
8
+ };
9
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmRlZmF1bHQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvY29uZmlnL2NvbmZpZy5kZWZhdWx0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsZ0RBQTBDO0FBVzFDLGtCQUFlO0lBQ2IsTUFBTSxFQUFFO1FBQ04sS0FBSyxFQUFFLGtCQUFNO0tBQ0U7Q0FDbEIsQ0FBQyJ9
@@ -0,0 +1,6 @@
1
+ import './config/config.default.js';
2
+ import './app/extend/application.js';
3
+ import './app/extend/agent.js';
4
+ import './app/extend/context.js';
5
+ import { Tracer } from './lib/tracer.js';
6
+ export { Tracer };
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Tracer = void 0;
4
+ require("./config/config.default.js");
5
+ require("./app/extend/application.js");
6
+ require("./app/extend/agent.js");
7
+ require("./app/extend/context.js");
8
+ const tracer_js_1 = require("./lib/tracer.js");
9
+ Object.defineProperty(exports, "Tracer", { enumerable: true, get: function () { return tracer_js_1.Tracer; } });
10
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQUEsc0NBQW9DO0FBQ3BDLHVDQUFxQztBQUNyQyxpQ0FBK0I7QUFDL0IsbUNBQWlDO0FBRWpDLCtDQUF5QztBQUVoQyx1RkFGQSxrQkFBTSxPQUVBIn0=
@@ -0,0 +1,7 @@
1
+ import type { Context } from '@eggjs/core';
2
+ export declare class Tracer {
3
+ #private;
4
+ readonly ctx: Context;
5
+ constructor(ctx: Context);
6
+ get traceId(): string;
7
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Tracer = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ class Tracer {
6
+ ctx;
7
+ #traceId;
8
+ constructor(ctx) {
9
+ this.ctx = ctx;
10
+ }
11
+ get traceId() {
12
+ if (!this.#traceId) {
13
+ this.#traceId = (0, node_crypto_1.randomUUID)();
14
+ }
15
+ return this.#traceId;
16
+ }
17
+ }
18
+ exports.Tracer = Tracer;
19
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHJhY2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL2xpYi90cmFjZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQUEsNkNBQXlDO0FBR3pDLE1BQWEsTUFBTTtJQUNSLEdBQUcsQ0FBVTtJQUN0QixRQUFRLENBQXFCO0lBRTdCLFlBQVksR0FBWTtRQUN0QixJQUFJLENBQUMsR0FBRyxHQUFHLEdBQUcsQ0FBQztJQUNqQixDQUFDO0lBRUQsSUFBSSxPQUFPO1FBQ1QsSUFBSSxDQUFDLElBQUksQ0FBQyxRQUFRLEVBQUUsQ0FBQztZQUNuQixJQUFJLENBQUMsUUFBUSxHQUFHLElBQUEsd0JBQVUsR0FBRSxDQUFDO1FBQy9CLENBQUM7UUFDRCxPQUFPLElBQUksQ0FBQyxRQUFRLENBQUM7SUFDdkIsQ0FBQztDQUNGO0FBZEQsd0JBY0MifQ==
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,2 @@
1
+ import { TracerBoot } from './boot.js';
2
+ export default TracerBoot;
@@ -0,0 +1,3 @@
1
+ import { TracerBoot } from './boot.js';
2
+ export default TracerBoot;
3
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYWdlbnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvYWdlbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLFdBQVcsQ0FBQztBQUV2QyxlQUFlLFVBQVUsQ0FBQyJ9
@@ -0,0 +1,2 @@
1
+ import TracerApplication from './application.js';
2
+ export default TracerApplication;
@@ -0,0 +1,3 @@
1
+ import TracerApplication from './application.js';
2
+ export default TracerApplication;
3
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYWdlbnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvYXBwL2V4dGVuZC9hZ2VudC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLGlCQUFpQixNQUFNLGtCQUFrQixDQUFDO0FBRWpELGVBQWUsaUJBQWlCLENBQUMifQ==
@@ -0,0 +1,14 @@
1
+ import { EggCore } from '@eggjs/core';
2
+ import type { Tracer } from '../../lib/tracer.js';
3
+ declare const cacheTracer: unique symbol;
4
+ export declare const isReady: unique symbol;
5
+ export default class TracerApplication extends EggCore {
6
+ [cacheTracer]: Tracer | undefined;
7
+ get tracer(): Tracer;
8
+ }
9
+ declare module '@eggjs/core' {
10
+ interface EggCore {
11
+ tracer: Tracer;
12
+ }
13
+ }
14
+ export {};
@@ -0,0 +1,16 @@
1
+ import { EggCore } from '@eggjs/core';
2
+ const cacheTracer = Symbol('before_ready_tracer');
3
+ export const isReady = Symbol('egg_tracer_is_ready');
4
+ export default class TracerApplication extends EggCore {
5
+ [cacheTracer];
6
+ get tracer() {
7
+ if (this[isReady]) {
8
+ return new this.config.tracer.Class(this.createAnonymousContext());
9
+ }
10
+ if (!this[cacheTracer]) {
11
+ this[cacheTracer] = new this.config.tracer.Class(this.createAnonymousContext());
12
+ }
13
+ return this[cacheTracer];
14
+ }
15
+ }
16
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBwbGljYXRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvYXBwL2V4dGVuZC9hcHBsaWNhdGlvbi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsT0FBTyxFQUFFLE1BQU0sYUFBYSxDQUFDO0FBR3RDLE1BQU0sV0FBVyxHQUFHLE1BQU0sQ0FBQyxxQkFBcUIsQ0FBQyxDQUFDO0FBQ2xELE1BQU0sQ0FBQyxNQUFNLE9BQU8sR0FBRyxNQUFNLENBQUMscUJBQXFCLENBQUMsQ0FBQztBQUVyRCxNQUFNLENBQUMsT0FBTyxPQUFPLGlCQUFrQixTQUFRLE9BQU87SUFDcEQsQ0FBQyxXQUFXLENBQUMsQ0FBcUI7SUFFbEMsSUFBSSxNQUFNO1FBQ1IsSUFBSSxJQUFJLENBQUMsT0FBTyxDQUFDLEVBQUUsQ0FBQztZQUNsQixPQUFPLElBQUksSUFBSSxDQUFDLE1BQU0sQ0FBQyxNQUFNLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxzQkFBc0IsRUFBRSxDQUFDLENBQUM7UUFDckUsQ0FBQztRQUVELElBQUksQ0FBQyxJQUFJLENBQUMsV0FBVyxDQUFDLEVBQUUsQ0FBQztZQUN2QixJQUFJLENBQUMsV0FBVyxDQUFDLEdBQUcsSUFBSSxJQUFJLENBQUMsTUFBTSxDQUFDLE1BQU0sQ0FBQyxLQUFLLENBQUMsSUFBSSxDQUFDLHNCQUFzQixFQUFFLENBQUMsQ0FBQztRQUNsRixDQUFDO1FBRUQsT0FBTyxJQUFJLENBQUMsV0FBVyxDQUFDLENBQUM7SUFDM0IsQ0FBQztDQUNGIn0=
@@ -0,0 +1,15 @@
1
+ import { Context } from '@eggjs/core';
2
+ import type { Tracer } from '../../lib/tracer.js';
3
+ declare const TRACER: unique symbol;
4
+ export default class TracerContext extends Context {
5
+ [TRACER]: Tracer | undefined;
6
+ get tracer(): Tracer;
7
+ get traceId(): string;
8
+ }
9
+ declare module '@eggjs/core' {
10
+ interface Context {
11
+ tracer: Tracer;
12
+ traceId: string;
13
+ }
14
+ }
15
+ export {};
@@ -0,0 +1,15 @@
1
+ import { Context } from '@eggjs/core';
2
+ const TRACER = Symbol('context tracer');
3
+ export default class TracerContext extends Context {
4
+ [TRACER];
5
+ get tracer() {
6
+ if (!this[TRACER]) {
7
+ this[TRACER] = new this.app.config.tracer.Class(this);
8
+ }
9
+ return this[TRACER];
10
+ }
11
+ get traceId() {
12
+ return this.tracer.traceId;
13
+ }
14
+ }
15
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udGV4dC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3NyYy9hcHAvZXh0ZW5kL2NvbnRleHQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLE9BQU8sRUFBRSxNQUFNLGFBQWEsQ0FBQztBQUV0QyxNQUFNLE1BQU0sR0FBRyxNQUFNLENBQUMsZ0JBQWdCLENBQUMsQ0FBQztBQUV4QyxNQUFNLENBQUMsT0FBTyxPQUFPLGFBQWMsU0FBUSxPQUFPO0lBQ2hELENBQUMsTUFBTSxDQUFDLENBQXFCO0lBRTdCLElBQUksTUFBTTtRQUNSLElBQUksQ0FBQyxJQUFJLENBQUMsTUFBTSxDQUFDLEVBQUUsQ0FBQztZQUNsQixJQUFJLENBQUMsTUFBTSxDQUFDLEdBQUcsSUFBSSxJQUFJLENBQUMsR0FBRyxDQUFDLE1BQU0sQ0FBQyxNQUFNLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxDQUFDO1FBQ3hELENBQUM7UUFDRCxPQUFPLElBQUksQ0FBQyxNQUFNLENBQUMsQ0FBQztJQUN0QixDQUFDO0lBRUQsSUFBSSxPQUFPO1FBQ1QsT0FBTyxJQUFJLENBQUMsTUFBTSxDQUFDLE9BQU8sQ0FBQztJQUM3QixDQUFDO0NBQ0YifQ==
@@ -0,0 +1,2 @@
1
+ import { TracerBoot } from './boot.js';
2
+ export default TracerBoot;
@@ -0,0 +1,3 @@
1
+ import { TracerBoot } from './boot.js';
2
+ export default TracerBoot;
3
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBwLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2FwcC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sV0FBVyxDQUFDO0FBRXZDLGVBQWUsVUFBVSxDQUFDIn0=
@@ -0,0 +1,6 @@
1
+ import type { EggCore, ILifecycleBoot } from '@eggjs/core';
2
+ export declare class TracerBoot implements ILifecycleBoot {
3
+ private app;
4
+ constructor(app: EggCore);
5
+ didLoad(): Promise<void>;
6
+ }
@@ -0,0 +1,11 @@
1
+ import { isReady } from './app/extend/application.js';
2
+ export class TracerBoot {
3
+ app;
4
+ constructor(app) {
5
+ this.app = app;
6
+ }
7
+ async didLoad() {
8
+ this.app[isReady] = true;
9
+ }
10
+ }
11
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYm9vdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ib290LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLE9BQU8sRUFBRSxPQUFPLEVBQUUsTUFBTSw2QkFBNkIsQ0FBQztBQUV0RCxNQUFNLE9BQU8sVUFBVTtJQUNEO0lBQXBCLFlBQW9CLEdBQVk7UUFBWixRQUFHLEdBQUgsR0FBRyxDQUFTO0lBQUcsQ0FBQztJQUVwQyxLQUFLLENBQUMsT0FBTztRQUNYLElBQUksQ0FBQyxHQUFHLENBQUMsT0FBTyxDQUFDLEdBQUcsSUFBSSxDQUFDO0lBQzNCLENBQUM7Q0FDRiJ9
@@ -0,0 +1,18 @@
1
+ import { Tracer } from '../lib/tracer.js';
2
+ /**
3
+ * tracer config
4
+ * @member Config#tracer
5
+ * @property {Tracer} Class - tracer class name
6
+ */
7
+ interface TracerConfig {
8
+ Class: typeof Tracer;
9
+ }
10
+ declare const _default: {
11
+ tracer: TracerConfig;
12
+ };
13
+ export default _default;
14
+ declare module '@eggjs/core' {
15
+ interface EggAppConfig {
16
+ tracer: TracerConfig;
17
+ }
18
+ }
@@ -0,0 +1,7 @@
1
+ import { Tracer } from '../lib/tracer.js';
2
+ export default {
3
+ tracer: {
4
+ Class: Tracer,
5
+ },
6
+ };
7
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmRlZmF1bHQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvY29uZmlnL2NvbmZpZy5kZWZhdWx0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxNQUFNLEVBQUUsTUFBTSxrQkFBa0IsQ0FBQztBQVcxQyxlQUFlO0lBQ2IsTUFBTSxFQUFFO1FBQ04sS0FBSyxFQUFFLE1BQU07S0FDRTtDQUNsQixDQUFDIn0=
@@ -0,0 +1,6 @@
1
+ import './config/config.default.js';
2
+ import './app/extend/application.js';
3
+ import './app/extend/agent.js';
4
+ import './app/extend/context.js';
5
+ import { Tracer } from './lib/tracer.js';
6
+ export { Tracer };
@@ -0,0 +1,7 @@
1
+ import './config/config.default.js';
2
+ import './app/extend/application.js';
3
+ import './app/extend/agent.js';
4
+ import './app/extend/context.js';
5
+ import { Tracer } from './lib/tracer.js';
6
+ export { Tracer };
7
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyw0QkFBNEIsQ0FBQztBQUNwQyxPQUFPLDZCQUE2QixDQUFDO0FBQ3JDLE9BQU8sdUJBQXVCLENBQUM7QUFDL0IsT0FBTyx5QkFBeUIsQ0FBQztBQUVqQyxPQUFPLEVBQUUsTUFBTSxFQUFFLE1BQU0saUJBQWlCLENBQUM7QUFFekMsT0FBTyxFQUFFLE1BQU0sRUFBRSxDQUFDIn0=
@@ -0,0 +1,7 @@
1
+ import type { Context } from '@eggjs/core';
2
+ export declare class Tracer {
3
+ #private;
4
+ readonly ctx: Context;
5
+ constructor(ctx: Context);
6
+ get traceId(): string;
7
+ }
@@ -0,0 +1,15 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ export class Tracer {
3
+ ctx;
4
+ #traceId;
5
+ constructor(ctx) {
6
+ this.ctx = ctx;
7
+ }
8
+ get traceId() {
9
+ if (!this.#traceId) {
10
+ this.#traceId = randomUUID();
11
+ }
12
+ return this.#traceId;
13
+ }
14
+ }
15
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHJhY2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL2xpYi90cmFjZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLGFBQWEsQ0FBQztBQUd6QyxNQUFNLE9BQU8sTUFBTTtJQUNSLEdBQUcsQ0FBVTtJQUN0QixRQUFRLENBQXFCO0lBRTdCLFlBQVksR0FBWTtRQUN0QixJQUFJLENBQUMsR0FBRyxHQUFHLEdBQUcsQ0FBQztJQUNqQixDQUFDO0lBRUQsSUFBSSxPQUFPO1FBQ1QsSUFBSSxDQUFDLElBQUksQ0FBQyxRQUFRLEVBQUUsQ0FBQztZQUNuQixJQUFJLENBQUMsUUFBUSxHQUFHLFVBQVUsRUFBRSxDQUFDO1FBQy9CLENBQUM7UUFDRCxPQUFPLElBQUksQ0FBQyxRQUFRLENBQUM7SUFDdkIsQ0FBQztDQUNGIn0=
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "name": "@eggjs/tracer",
3
+ "version": "3.0.0"
4
+ }
package/package.json ADDED
@@ -0,0 +1,98 @@
1
+ {
2
+ "name": "@eggjs/tracer",
3
+ "version": "3.0.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "tracer for egg",
8
+ "eggPlugin": {
9
+ "name": "tracer",
10
+ "exports": {
11
+ "import": "./dist/esm",
12
+ "require": "./dist/commonjs",
13
+ "typescript": "./src"
14
+ }
15
+ },
16
+ "keywords": [
17
+ "egg",
18
+ "egg-plugin",
19
+ "eggPlugin",
20
+ "tracer",
21
+ "traceId",
22
+ "spanId",
23
+ "parentSpanId"
24
+ ],
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/eggjs/tracer.git"
28
+ },
29
+ "bugs": {
30
+ "url": "https://github.com/eggjs/egg/issues"
31
+ },
32
+ "homepage": "https://github.com/eggjs/tracer#readme",
33
+ "author": "fengmk2 <fengmk2@gmail.com> (https://github.com/fengmk2)",
34
+ "license": "MIT",
35
+ "engines": {
36
+ "node": ">= 18.19.0"
37
+ },
38
+ "dependencies": {
39
+ "@eggjs/core": "^6.2.13"
40
+ },
41
+ "devDependencies": {
42
+ "@arethetypeswrong/cli": "^0.17.3",
43
+ "@eggjs/bin": "7",
44
+ "@eggjs/mock": "^6.0.5",
45
+ "@eggjs/tsconfig": "1",
46
+ "@types/mocha": "10",
47
+ "@types/node": "22",
48
+ "egg": "^4.0.5",
49
+ "egg-logger": "^3.6.1",
50
+ "eslint": "8",
51
+ "eslint-config-egg": "14",
52
+ "glob": "^11.0.1",
53
+ "rimraf": "6",
54
+ "snap-shot-it": "^7.9.10",
55
+ "supertest": "^6.3.3",
56
+ "tsd": "^0.31.2",
57
+ "tshy": "3",
58
+ "tshy-after": "1",
59
+ "typescript": "5"
60
+ },
61
+ "scripts": {
62
+ "lint": "eslint --cache src test --ext .ts",
63
+ "pretest": "npm run clean && npm run lint -- --fix",
64
+ "test": "egg-bin test",
65
+ "preci": "npm run clean && npm run lint",
66
+ "ci": "egg-bin cov",
67
+ "postci": "npm run prepublishOnly && npm run clean",
68
+ "clean": "rimraf dist",
69
+ "prepublishOnly": "tshy && tshy-after && attw --pack"
70
+ },
71
+ "type": "module",
72
+ "tshy": {
73
+ "exports": {
74
+ ".": "./src/index.ts",
75
+ "./package.json": "./package.json"
76
+ }
77
+ },
78
+ "exports": {
79
+ ".": {
80
+ "import": {
81
+ "types": "./dist/esm/index.d.ts",
82
+ "default": "./dist/esm/index.js"
83
+ },
84
+ "require": {
85
+ "types": "./dist/commonjs/index.d.ts",
86
+ "default": "./dist/commonjs/index.js"
87
+ }
88
+ },
89
+ "./package.json": "./package.json"
90
+ },
91
+ "files": [
92
+ "dist",
93
+ "src"
94
+ ],
95
+ "types": "./dist/commonjs/index.d.ts",
96
+ "main": "./dist/commonjs/index.js",
97
+ "module": "./dist/esm/index.js"
98
+ }
package/src/agent.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { TracerBoot } from './boot.js';
2
+
3
+ export default TracerBoot;
@@ -0,0 +1,3 @@
1
+ import TracerApplication from './application.js';
2
+
3
+ export default TracerApplication;
@@ -0,0 +1,27 @@
1
+ import { EggCore } from '@eggjs/core';
2
+ import type { Tracer } from '../../lib/tracer.js';
3
+
4
+ const cacheTracer = Symbol('before_ready_tracer');
5
+ export const isReady = Symbol('egg_tracer_is_ready');
6
+
7
+ export default class TracerApplication extends EggCore {
8
+ [cacheTracer]: Tracer | undefined;
9
+
10
+ get tracer(): Tracer {
11
+ if (this[isReady]) {
12
+ return new this.config.tracer.Class(this.createAnonymousContext());
13
+ }
14
+
15
+ if (!this[cacheTracer]) {
16
+ this[cacheTracer] = new this.config.tracer.Class(this.createAnonymousContext());
17
+ }
18
+
19
+ return this[cacheTracer];
20
+ }
21
+ }
22
+
23
+ declare module '@eggjs/core' {
24
+ interface EggCore {
25
+ tracer: Tracer;
26
+ }
27
+ }
@@ -0,0 +1,25 @@
1
+ import { Context } from '@eggjs/core';
2
+ import type { Tracer } from '../../lib/tracer.js';
3
+ const TRACER = Symbol('context tracer');
4
+
5
+ export default class TracerContext extends Context {
6
+ [TRACER]: Tracer | undefined;
7
+
8
+ get tracer(): Tracer {
9
+ if (!this[TRACER]) {
10
+ this[TRACER] = new this.app.config.tracer.Class(this);
11
+ }
12
+ return this[TRACER];
13
+ }
14
+
15
+ get traceId(): string {
16
+ return this.tracer.traceId;
17
+ }
18
+ }
19
+
20
+ declare module '@eggjs/core' {
21
+ interface Context {
22
+ tracer: Tracer;
23
+ traceId: string;
24
+ }
25
+ }
package/src/app.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { TracerBoot } from './boot.js';
2
+
3
+ export default TracerBoot;
package/src/boot.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { EggCore, ILifecycleBoot } from '@eggjs/core';
2
+ import { isReady } from './app/extend/application.js';
3
+
4
+ export class TracerBoot implements ILifecycleBoot {
5
+ constructor(private app: EggCore) {}
6
+
7
+ async didLoad() {
8
+ this.app[isReady] = true;
9
+ }
10
+ }
@@ -0,0 +1,22 @@
1
+ import { Tracer } from '../lib/tracer.js';
2
+
3
+ /**
4
+ * tracer config
5
+ * @member Config#tracer
6
+ * @property {Tracer} Class - tracer class name
7
+ */
8
+ interface TracerConfig {
9
+ Class: typeof Tracer;
10
+ }
11
+
12
+ export default {
13
+ tracer: {
14
+ Class: Tracer,
15
+ } as TracerConfig,
16
+ };
17
+
18
+ declare module '@eggjs/core' {
19
+ interface EggAppConfig {
20
+ tracer: TracerConfig;
21
+ }
22
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ import './config/config.default.js';
2
+ import './app/extend/application.js';
3
+ import './app/extend/agent.js';
4
+ import './app/extend/context.js';
5
+
6
+ import { Tracer } from './lib/tracer.js';
7
+
8
+ export { Tracer };
@@ -0,0 +1,18 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import type { Context } from '@eggjs/core';
3
+
4
+ export class Tracer {
5
+ readonly ctx: Context;
6
+ #traceId: string | undefined;
7
+
8
+ constructor(ctx: Context) {
9
+ this.ctx = ctx;
10
+ }
11
+
12
+ get traceId() {
13
+ if (!this.#traceId) {
14
+ this.#traceId = randomUUID();
15
+ }
16
+ return this.#traceId;
17
+ }
18
+ }
@@ -0,0 +1,4 @@
1
+ // make sure to import egg typings and let typescript know about it
2
+ // @see https://github.com/whxaxes/blog/issues/11
3
+ // and https://www.typescriptlang.org/docs/handbook/declaration-merging.html
4
+ import 'egg';