()
| 34 | http_server: any; |
| 35 | |
| 36 | async onReady() { |
| 37 | PromClient.collectDefaultMetrics(this.prometheusConfig); |
| 38 | const modules = DecoratorManager.listModule('prometheus:master'); |
| 39 | const handlers = {}; |
| 40 | let sockFile = path.join(os.tmpdir(), 'midway-master.sock'); |
| 41 | if (process.platform === 'win32') { |
| 42 | sockFile = |
| 43 | '\\\\.\\pipe\\' + sockFile.replace(/^\//, '').replace(/\//g, '-'); |
| 44 | } |
| 45 | if (modules.length > 0 && process.platform !== 'win32') { |
| 46 | if (isMaster()) { |
| 47 | if (fs.existsSync(sockFile)) { |
| 48 | fs.unlinkSync(sockFile); |
| 49 | } |
| 50 | this.http_server = http |
| 51 | .createServer((req, res) => { |
| 52 | const query = qs.parse(req.url.slice('/?'.length)); |
| 53 | const params = JSON.parse(query.params as string); |
| 54 | handlers[`${query.path}`](...params).then(result => { |
| 55 | res.end(result); |
| 56 | }); |
| 57 | }) |
| 58 | .listen(sockFile); |
| 59 | } |
| 60 | } |
| 61 | for (const module of modules) { |
| 62 | const rules = MetadataManager.getOwnMetadata( |
| 63 | 'prometheus:master:options', |
| 64 | module |
| 65 | ); |
| 66 | for (const rule of rules) { |
| 67 | if (isMaster()) { |
| 68 | handlers[rule.name] = async (...args) => { |
| 69 | const service = await this.app |
| 70 | .createAnonymousContext() |
| 71 | .requestContext.getAsync(module); |
| 72 | return rule.value.call(service, ...args); |
| 73 | }; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | this.app.use(async (ctx, next) => { |
| 78 | const service = await ctx.requestContext.getAsync(DataService); |
| 79 | const startAt = process.hrtime(); |
| 80 | try { |
| 81 | if (ctx.path === '/metrics') { |
| 82 | const result = await service.getData(); |
| 83 | const Register = PromClient.register; |
| 84 | ctx.set('Content-Type', Register.contentType); |
| 85 | ctx.body = result; |
| 86 | } else { |
| 87 | await next(); |
| 88 | } |
| 89 | const diff = process.hrtime(startAt); |
| 90 | const time = diff[0] * 1e3 + diff[1] * 1e-6; |
| 91 | service.getUser(ctx.method, '200', ctx.path, time); |
| 92 | } catch (e) { |
| 93 | const diff = process.hrtime(startAt); |
nothing calls this directly
no test coverage detected