@midwayjs/cos 4.0.0-beta.8 → 4.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.
package/dist/manager.d.ts CHANGED
@@ -1,10 +1,15 @@
1
- import { ServiceFactory } from '@midwayjs/core';
1
+ import { ServiceFactory, MidwayTraceService } from '@midwayjs/core';
2
2
  import * as COS from 'cos-nodejs-sdk-v5';
3
3
  export declare class COSServiceFactory extends ServiceFactory<COS> {
4
4
  cosConfig: any;
5
5
  init(): Promise<void>;
6
6
  logger: any;
7
+ protected traceService: MidwayTraceService;
8
+ protected traceMetaResolver: any;
9
+ protected traceEnabled: any;
10
+ protected traceInjector: any;
7
11
  createClient(config: COS.COSOptions): Promise<COS>;
12
+ protected bindTraceContext(client: COS): void;
8
13
  getName(): string;
9
14
  }
10
15
  export declare class COSService implements COS {
package/dist/manager.js CHANGED
@@ -21,10 +21,59 @@ let COSServiceFactory = class COSServiceFactory extends core_1.ServiceFactory {
21
21
  });
22
22
  }
23
23
  logger;
24
+ traceService;
25
+ traceMetaResolver;
26
+ traceEnabled;
27
+ traceInjector;
24
28
  async createClient(config) {
25
29
  assert.ok(config.SecretKey && config.SecretId, '[@midwayjs/cos] secretId secretKey is required on config');
26
30
  this.logger.info('[midway:cos] init %s', config.SecretKey);
27
- return new COS(config);
31
+ const client = new COS(config);
32
+ this.bindTraceContext(client);
33
+ return client;
34
+ }
35
+ bindTraceContext(client) {
36
+ if (!client || !this.traceService) {
37
+ return;
38
+ }
39
+ const rawRequest = client.request?.bind(client);
40
+ if (!rawRequest) {
41
+ return;
42
+ }
43
+ client.request = async (...args) => {
44
+ if (typeof args[1] === 'function') {
45
+ return rawRequest(...args);
46
+ }
47
+ const params = args?.[0] || {};
48
+ const apiName = params.Action || params.action || 'request';
49
+ const rawCarrier = typeof this.traceInjector === 'function'
50
+ ? this.traceInjector({
51
+ request: params,
52
+ custom: {
53
+ action: String(apiName),
54
+ },
55
+ })
56
+ : {};
57
+ const carrier = rawCarrier && typeof rawCarrier === 'object' ? rawCarrier : {};
58
+ return await this.traceService.runWithExitSpan(`cos.${String(apiName).toLowerCase()}`, {
59
+ enable: this.traceEnabled !== false,
60
+ carrier,
61
+ attributes: {
62
+ 'midway.protocol': 'cos',
63
+ 'midway.cos.action': String(apiName),
64
+ },
65
+ meta: this.traceMetaResolver,
66
+ metaArgs: {
67
+ carrier,
68
+ request: params,
69
+ custom: {
70
+ action: String(apiName),
71
+ },
72
+ },
73
+ }, async () => {
74
+ return await rawRequest(...args);
75
+ });
76
+ };
28
77
  }
29
78
  getName() {
30
79
  return 'cos';
@@ -45,6 +94,22 @@ __decorate([
45
94
  (0, core_1.Logger)('coreLogger'),
46
95
  __metadata("design:type", Object)
47
96
  ], COSServiceFactory.prototype, "logger", void 0);
97
+ __decorate([
98
+ (0, core_1.Inject)(),
99
+ __metadata("design:type", core_1.MidwayTraceService)
100
+ ], COSServiceFactory.prototype, "traceService", void 0);
101
+ __decorate([
102
+ (0, core_1.Config)('cos.tracing.meta'),
103
+ __metadata("design:type", Object)
104
+ ], COSServiceFactory.prototype, "traceMetaResolver", void 0);
105
+ __decorate([
106
+ (0, core_1.Config)('cos.tracing.enable'),
107
+ __metadata("design:type", Object)
108
+ ], COSServiceFactory.prototype, "traceEnabled", void 0);
109
+ __decorate([
110
+ (0, core_1.Config)('cos.tracing.injector'),
111
+ __metadata("design:type", Object)
112
+ ], COSServiceFactory.prototype, "traceInjector", void 0);
48
113
  exports.COSServiceFactory = COSServiceFactory = __decorate([
49
114
  (0, core_1.Provide)(),
50
115
  (0, core_1.Scope)(core_1.ScopeEnum.Singleton)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@midwayjs/cos",
3
3
  "description": "midway cos component",
4
- "version": "4.0.0-beta.8",
4
+ "version": "4.0.0",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
7
7
  "files": [
@@ -10,8 +10,8 @@
10
10
  "index.d.ts"
11
11
  ],
12
12
  "devDependencies": {
13
- "@midwayjs/core": "^4.0.0-beta.8",
14
- "@midwayjs/mock": "^4.0.0-beta.8",
13
+ "@midwayjs/core": "^4.0.0",
14
+ "@midwayjs/mock": "^4.0.0",
15
15
  "dotenv": "16.4.7"
16
16
  },
17
17
  "dependencies": {
@@ -37,5 +37,5 @@
37
37
  "type": "git",
38
38
  "url": "https://github.com/midwayjs/midway.git"
39
39
  },
40
- "gitHead": "355e55949fdd132b0bdcb4830222a0a027e92ded"
40
+ "gitHead": "014f32c23ebc1d5ac21777c76be2fd373ce992d8"
41
41
  }