@midwayjs/consul 4.0.0-beta.11 → 4.0.0-beta.13
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.
- package/dist/manager.d.ts +6 -1
- package/dist/manager.js +74 -1
- package/package.json +5 -5
package/dist/manager.d.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import { ServiceFactory, ServiceFactoryConfigOption, ILogger } from '@midwayjs/core';
|
|
1
|
+
import { ServiceFactory, ServiceFactoryConfigOption, ILogger, MidwayTraceService } from '@midwayjs/core';
|
|
2
2
|
import Consul = require('consul');
|
|
3
3
|
import { ConsulClient, ConsulOptions } from './interface';
|
|
4
4
|
export declare class ConsulServiceFactory extends ServiceFactory<ConsulClient> {
|
|
5
5
|
consulConfig: ServiceFactoryConfigOption<ConsulOptions>;
|
|
6
|
+
traceMetaResolver: any;
|
|
7
|
+
traceEnabled: any;
|
|
8
|
+
traceInjector: any;
|
|
6
9
|
init(): Promise<void>;
|
|
7
10
|
logger: ILogger;
|
|
11
|
+
traceService: MidwayTraceService;
|
|
8
12
|
createClient(config: ConsulOptions, clientName: string): Promise<InstanceType<typeof Consul>>;
|
|
13
|
+
protected bindTraceContext(client: any, clientName: string): void;
|
|
9
14
|
getName(): string;
|
|
10
15
|
destroyClient(client: InstanceType<typeof Consul>, clientName: string): Promise<void>;
|
|
11
16
|
}
|
package/dist/manager.js
CHANGED
|
@@ -14,15 +14,72 @@ const core_1 = require("@midwayjs/core");
|
|
|
14
14
|
const Consul = require("consul");
|
|
15
15
|
let ConsulServiceFactory = class ConsulServiceFactory extends core_1.ServiceFactory {
|
|
16
16
|
consulConfig;
|
|
17
|
+
traceMetaResolver;
|
|
18
|
+
traceEnabled;
|
|
19
|
+
traceInjector;
|
|
17
20
|
async init() {
|
|
18
21
|
await this.initClients(this.consulConfig, {
|
|
19
22
|
concurrent: true,
|
|
20
23
|
});
|
|
21
24
|
}
|
|
22
25
|
logger;
|
|
26
|
+
traceService;
|
|
23
27
|
async createClient(config, clientName) {
|
|
24
28
|
this.logger.info('[midway:consul] init %s at %s:%s', clientName, config.host, config.port);
|
|
25
|
-
|
|
29
|
+
const client = new Consul(config);
|
|
30
|
+
this.bindTraceContext(client, clientName);
|
|
31
|
+
return client;
|
|
32
|
+
}
|
|
33
|
+
bindTraceContext(client, clientName) {
|
|
34
|
+
if (!client || !this.traceService) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const rawRequest = client.request?.bind(client);
|
|
38
|
+
if (!rawRequest) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
client.request = (...args) => {
|
|
42
|
+
const hasCallback = args.some(arg => typeof arg === 'function');
|
|
43
|
+
if (hasCallback) {
|
|
44
|
+
return rawRequest(...args);
|
|
45
|
+
}
|
|
46
|
+
const requestOptions = args?.[0] ?? {};
|
|
47
|
+
const isWatch = Boolean(requestOptions?.watch ??
|
|
48
|
+
requestOptions?.qs?.watch ??
|
|
49
|
+
requestOptions?.query?.watch);
|
|
50
|
+
if (isWatch) {
|
|
51
|
+
return rawRequest(...args);
|
|
52
|
+
}
|
|
53
|
+
const requestMethod = requestOptions?.method ?? 'request';
|
|
54
|
+
const rawCarrier = typeof this.traceInjector === 'function'
|
|
55
|
+
? this.traceInjector({
|
|
56
|
+
request: requestOptions,
|
|
57
|
+
custom: {
|
|
58
|
+
clientName,
|
|
59
|
+
requestMethod: String(requestMethod),
|
|
60
|
+
},
|
|
61
|
+
})
|
|
62
|
+
: {};
|
|
63
|
+
const carrier = rawCarrier && typeof rawCarrier === 'object' ? rawCarrier : {};
|
|
64
|
+
return this.traceService.runWithExitSpan(`consul.${String(requestMethod).toLowerCase()}`, {
|
|
65
|
+
enable: this.traceEnabled !== false,
|
|
66
|
+
carrier,
|
|
67
|
+
attributes: {
|
|
68
|
+
'midway.protocol': 'consul',
|
|
69
|
+
'midway.consul.client': clientName,
|
|
70
|
+
'midway.consul.method': String(requestMethod),
|
|
71
|
+
},
|
|
72
|
+
meta: this.traceMetaResolver,
|
|
73
|
+
metaArgs: {
|
|
74
|
+
carrier,
|
|
75
|
+
request: requestOptions,
|
|
76
|
+
custom: {
|
|
77
|
+
clientName,
|
|
78
|
+
requestMethod: String(requestMethod),
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
}, async () => rawRequest(...args));
|
|
82
|
+
};
|
|
26
83
|
}
|
|
27
84
|
getName() {
|
|
28
85
|
return 'consul';
|
|
@@ -37,6 +94,18 @@ __decorate([
|
|
|
37
94
|
(0, core_1.Config)('consul'),
|
|
38
95
|
__metadata("design:type", Object)
|
|
39
96
|
], ConsulServiceFactory.prototype, "consulConfig", void 0);
|
|
97
|
+
__decorate([
|
|
98
|
+
(0, core_1.Config)('consul.tracing.meta'),
|
|
99
|
+
__metadata("design:type", Object)
|
|
100
|
+
], ConsulServiceFactory.prototype, "traceMetaResolver", void 0);
|
|
101
|
+
__decorate([
|
|
102
|
+
(0, core_1.Config)('consul.tracing.enable'),
|
|
103
|
+
__metadata("design:type", Object)
|
|
104
|
+
], ConsulServiceFactory.prototype, "traceEnabled", void 0);
|
|
105
|
+
__decorate([
|
|
106
|
+
(0, core_1.Config)('consul.tracing.injector'),
|
|
107
|
+
__metadata("design:type", Object)
|
|
108
|
+
], ConsulServiceFactory.prototype, "traceInjector", void 0);
|
|
40
109
|
__decorate([
|
|
41
110
|
(0, core_1.Init)(),
|
|
42
111
|
__metadata("design:type", Function),
|
|
@@ -47,6 +116,10 @@ __decorate([
|
|
|
47
116
|
(0, core_1.Logger)('coreLogger'),
|
|
48
117
|
__metadata("design:type", Object)
|
|
49
118
|
], ConsulServiceFactory.prototype, "logger", void 0);
|
|
119
|
+
__decorate([
|
|
120
|
+
(0, core_1.Inject)(),
|
|
121
|
+
__metadata("design:type", core_1.MidwayTraceService)
|
|
122
|
+
], ConsulServiceFactory.prototype, "traceService", void 0);
|
|
50
123
|
exports.ConsulServiceFactory = ConsulServiceFactory = __decorate([
|
|
51
124
|
(0, core_1.Singleton)()
|
|
52
125
|
], ConsulServiceFactory);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/consul",
|
|
3
3
|
"description": "midway consul",
|
|
4
|
-
"version": "4.0.0-beta.
|
|
4
|
+
"version": "4.0.0-beta.13",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
"index.d.ts"
|
|
11
11
|
],
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"@midwayjs/core": "^4.0.0-beta.
|
|
14
|
-
"@midwayjs/koa": "^4.0.0-beta.
|
|
15
|
-
"@midwayjs/mock": "^4.0.0-beta.
|
|
13
|
+
"@midwayjs/core": "^4.0.0-beta.13",
|
|
14
|
+
"@midwayjs/koa": "^4.0.0-beta.13",
|
|
15
|
+
"@midwayjs/mock": "^4.0.0-beta.13",
|
|
16
16
|
"@types/sinon": "17.0.4",
|
|
17
17
|
"nock": "13.5.6"
|
|
18
18
|
},
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"type": "git",
|
|
40
40
|
"url": "https://github.com/midwayjs/midway.git"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "9a38b66a84a6880370cac90d737f94f9c5f2f256"
|
|
43
43
|
}
|