@midwayjs/prometheus 3.0.3 → 3.0.6
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/prometheus",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.6",
|
|
4
4
|
"description": "midway component for prometheus",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -19,15 +19,14 @@
|
|
|
19
19
|
"files": [
|
|
20
20
|
"dist/**/*.js",
|
|
21
21
|
"dist/**/*.d.ts",
|
|
22
|
-
"index.d.ts"
|
|
23
|
-
"dist/**/*.js.map"
|
|
22
|
+
"index.d.ts"
|
|
24
23
|
],
|
|
25
24
|
"license": "MIT",
|
|
26
25
|
"devDependencies": {
|
|
27
|
-
"@midwayjs/core": "^3.0.
|
|
28
|
-
"@midwayjs/decorator": "^3.0.
|
|
29
|
-
"@midwayjs/koa": "^3.0.
|
|
30
|
-
"@midwayjs/mock": "^3.0.
|
|
26
|
+
"@midwayjs/core": "^3.0.6",
|
|
27
|
+
"@midwayjs/decorator": "^3.0.6",
|
|
28
|
+
"@midwayjs/koa": "^3.0.6",
|
|
29
|
+
"@midwayjs/mock": "^3.0.6",
|
|
31
30
|
"@types/request": "2.48.8"
|
|
32
31
|
},
|
|
33
32
|
"dependencies": {
|
|
@@ -37,5 +36,5 @@
|
|
|
37
36
|
"engines": {
|
|
38
37
|
"node": ">=12"
|
|
39
38
|
},
|
|
40
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "afaa5b59a2be85e915233a9268c0e05965dd5c61"
|
|
41
40
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"config.default.js","sourceRoot":"","sources":["../../src/config/config.default.ts"],"names":[],"mappings":";;;AAEa,QAAA,UAAU,GAAyC;IAC9D,MAAM,EAAE;QACN,QAAQ,EAAE,SAAS;KACpB;CACF,CAAC","sourcesContent":["import { DefaultMetricsCollectorConfiguration } from 'prom-client';\n\nexport const prometheus: DefaultMetricsCollectorConfiguration = {\n labels: {\n APP_NAME: 'default',\n },\n};\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"configuration.js","sourceRoot":"","sources":["../src/configuration.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mDAM6B;AAC7B,0CAA0C;AAC1C,yCAAoD;AACpD,yBAAyB;AACzB,6BAA6B;AAC7B,yBAAyB;AACzB,6BAA6B;AAC7B,kCAAkC;AAClC,uDAAoD;AACpD,yDAAyD;AAUzD,IAAa,uBAAuB,GAApC,MAAa,uBAAuB;IASlC,KAAK,CAAC,OAAO;QACX,UAAU,CAAC,qBAAqB,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;QACxD,MAAM,OAAO,GAAG,IAAA,sBAAU,EAAC,mBAAmB,CAAC,CAAC;QAChD,MAAM,QAAQ,GAAG,EAAE,CAAC;QACpB,IAAI,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,EAAE,oBAAoB,CAAC,CAAC;QAC5D,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE;YAChC,QAAQ;gBACN,eAAe,GAAG,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;SACrE;QACD,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE;YACtD,IAAI,IAAA,gBAAQ,GAAE,EAAE;gBACd,IAAI,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;oBAC3B,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;iBACzB;gBACD,IAAI,CAAC,WAAW,GAAG,IAAI;qBACpB,YAAY,CAAC,CAAC,GA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kBAAM,EAAC,YAAY,CAAC;;iEACC;AAGtB;IADC,IAAA,eAAG,GAAE;;oDACF;AALO,uBAAuB;IARnC,IAAA,yBAAa,EAAC;QACb,SAAS,EAAE,YAAY;QACvB,aAAa,EAAE;YACb;gBACE,OAAO,EAAE,aAAa;aACvB;SACF;KACF,CAAC;GACW,uBAAuB,CA6EnC;AA7EY,0DAAuB","sourcesContent":["import {\n App,\n Config,\n Configuration,\n getClassMetadata,\n listModule,\n} from '@midwayjs/decorator';\nimport * as PromClient from 'prom-client';\nimport { isMaster, closeLock } from './utils/utils';\nimport * as os from 'os';\nimport * as path from 'path';\nimport * as fs from 'fs';\nimport * as http from 'http';\nimport * as qs from 'querystring';\nimport { DataService } from './service/dataService';\nimport * as DefaultConfig from './config/config.default';\n\n@Configuration({\n namespace: 'prometheus',\n importConfigs: [\n {\n default: DefaultConfig,\n },\n ],\n})\nexport class PrometheusConfiguration {\n @Config('prometheus')\n prometheusConfig: any;\n\n @App()\n app;\n\n http_server: any;\n\n async onReady() {\n PromClient.collectDefaultMetrics(this.prometheusConfig);\n const modules = listModule('prometheus:master');\n const handlers = {};\n let sockFile = path.join(os.tmpdir(), 'midway-master.sock');\n if (process.platform === 'win32') {\n sockFile =\n '\\\\\\\\.\\\\pipe\\\\' + sockFile.replace(/^\\//, '').replace(/\\//g, '-');\n }\n if (modules.length > 0 && process.platform !== 'win32') {\n if (isMaster()) {\n if (fs.existsSync(sockFile)) {\n fs.unlinkSync(sockFile);\n }\n this.http_server = http\n .createServer((req, res) => {\n const query = qs.parse(req.url.substr('/?'.length));\n const params = JSON.parse(query.params as string);\n handlers[`${query.path}`](...params).then(result => {\n res.end(result);\n });\n })\n .listen(sockFile);\n }\n }\n for (const module of modules) {\n const rules = getClassMetadata('prometheus:master:options', module);\n for (const rule of rules) {\n if (isMaster()) {\n handlers[rule.name] = async (...args) => {\n const service = await this.app\n .createAnonymousContext()\n .requestContext.getAsync(module);\n return rule.value.call(service, ...args);\n };\n }\n }\n }\n this.app.use(async (ctx, next) => {\n const service = await ctx.requestContext.getAsync(DataService);\n const startAt = process.hrtime();\n try {\n if (ctx.path === '/metrics') {\n const result = await service.getData();\n const Register = PromClient.register;\n ctx.set('Content-Type', Register.contentType);\n ctx.body = result;\n } else {\n await next();\n }\n const diff = process.hrtime(startAt);\n const time = diff[0] * 1e3 + diff[1] * 1e-6;\n service.getUser(ctx.method, '200', ctx.path, time);\n } catch (e) {\n const diff = process.hrtime(startAt);\n const time = diff[0] * 1e3 + diff[1] * 1e-6;\n service.getUser(ctx.method, '500', ctx.path, time);\n throw e;\n }\n });\n }\n\n async onStop() {\n if (isMaster()) {\n closeLock();\n this.http_server && this.http_server.close();\n }\n }\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"master.js","sourceRoot":"","sources":["../../src/decorator/master.ts"],"names":[],"mappings":";;;AAAA,mDAAsE;AACtE,mCAAmC;AACnC,6BAA6B;AAC7B,yBAAyB;AACzB,0CAA0C;AAC1C,kCAAkC;AAElC,SAAgB,MAAM;IACpB,OAAO,UACL,MAAW,EACX,WAAmB,EACnB,UAA8B;QAE9B,IAAA,sBAAU,EAAC,mBAAmB,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;QACpD,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,EAAE,oBAAoB,CAAC,CAAC;QAC9D,IAAI,CAAC,IAAA,gBAAQ,GAAE,EAAE;YACf,UAAU,CAAC,KAAK,GAAG,CAAC,GAAG,IAAI,EAAE,EAAE;gBAC7B,MAAM,MAAM,GAAG;oBACb,IAAI,EAAE,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,IAAI,WAAW,EAAE;oBACjD,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;iBAC7B,CAAC;gBACF,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;oBACrC,OAAO,CACL;wBACE,GAAG,EAAE,eAAe,QAAQ,MAAM,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE;qBACzD,EACD,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;wBACjB,OAAO,CAAC,IAAI,CAAC,CAAC;oBAChB,CAAC,CACF,CAAC;gBACJ,CAAC,CAAC,CAAC;YACL,CAAC,CAAC;SACH;QACD,IAAA,+BAAmB,EACjB,2BAA2B,EAC3B;YACE,IAAI,EAAE,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,IAAI,WAAW,EAAE;YACjD,KAAK,EAAE,UAAU,CAAC,KAAK;SACxB,EACD,MAAM,CAAC,WAAW,CACnB,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAnCD,wBAmCC","sourcesContent":["import { saveModule, attachClassMetadata } from '@midwayjs/decorator';\nimport * as request from 'request';\nimport * as path from 'path';\nimport * as os from 'os';\nimport { isMaster } from '../utils/utils';\nimport * as qs from 'querystring';\n\nexport function Master() {\n return function (\n target: any,\n propertyKey: string,\n descriptor: PropertyDescriptor\n ) {\n saveModule('prometheus:master', target.constructor);\n const sockFile = path.join(os.tmpdir(), 'midway-master.sock');\n if (!isMaster()) {\n descriptor.value = (...args) => {\n const params = {\n path: `${target.constructor.name}_${propertyKey}`,\n params: JSON.stringify(args),\n };\n return new Promise((resolve, reject) => {\n request(\n {\n uri: `http://unix:${sockFile}:/?${qs.stringify(params)}`,\n },\n (err, res, body) => {\n resolve(body);\n }\n );\n });\n };\n }\n attachClassMetadata(\n 'prometheus:master:options',\n {\n name: `${target.constructor.name}_${propertyKey}`,\n value: descriptor.value,\n },\n target.constructor\n );\n };\n}\n"]}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iDAA2E;AAAlE,8GAAA,uBAAuB,OAAiB;AACjD,qDAAoD;AAA3C,0GAAA,WAAW,OAAA","sourcesContent":["export { PrometheusConfiguration as Configuration } from './configuration';\nexport { DataService } from './service/dataService';\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dataService.js","sourceRoot":"","sources":["../../src/service/dataService.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mDAAwE;AACxE,sCAAsC;AACtC,gDAA6C;AAC7C,0CAA0C;AAI1C,IAAa,WAAW,GAAxB,MAAa,WAAW;IAAxB;QAKE,uBAAkB,GAAQ,EAAE,CAAC;QAO7B,UAAK,GAAG,KAAK,CAAC;IA2FhB,CAAC;IAzFC,KAAK,CAAC,IAAI;QACR,IAAI,CAAC,eAAe,GAAG,IAAI,MAAM,CAAC,OAAO,CAAC;YACxC,IAAI,EAAE,mCAAmC;YACzC,IAAI,EAAE,yBAAyB;YAC/B,UAAU,EAAE;gBACV,QAAQ;gBACR,QAAQ;gBACR,KAAK;gBACL,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC;aAC7C;SACF,CAAC,CAAC;QACH,IAAI,CAAC,iBAAiB,GAAG,IAAI,MAAM,CAAC,SAAS,CAAC;YAC5C,IAAI,EAAE,oCAAoC;YAC1C,IAAI,EAAE,8CAA8C;YACpD,UAAU,EAAE;gBACV,QAAQ;gBACR,QAAQ;gBACR,KAAK;gBACL,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC;aAC7C;YACD,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC;SACjE,CAAC,CAAC;IACL,CAAC;IAGD,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI;QACtC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE;YACf,IAAI,C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AC/B,EAAE,GAAG,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,EACnC,KAAK,CACN,CAAC;IACJ,CAAC;IAGD,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI;QACtB,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC;QAC1D,IAAI,KAAK,GAAG,IAAI,CAAC;QACjB,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;YAC/B,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC;SACxC;QACD,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC,GAAG,CAC/B,EAAE,GAAG,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,EACnC,KAAK,CACN,CAAC;IACJ,CAAC;IAGD,KAAK,CAAC,OAAO;QACX,MAAM,QAAQ,GAAG,UAAU,CAAC,QAAQ,CAAC;QACrC,OAAO,QAAQ,CAAC,OAAO,EAAE,CAAC;IAC5B,CAAC;CACF,CAAA;AA7FC;IADC,IAAA,kBAAM,EAAC,YAAY,CAAC;;qDACJ;AA6BjB;IADC,IAAA,eAAM,GAAE;;;;0CAsBR;AAUD;IADC,IAAA,eAAM,GAAE;;;;sCAMR;AAGD;IADC,IAAA,eAAM,GAAE;;;;sCAMR;AAGD;IADC,IAAA,eAAM,GAAE;;;;0CAWR;AAGD;IADC,IAAA,eAAM,GAAE;;;;0CAIR;AAtGU,WAAW;IAFvB,IAAA,mBAAO,GAAE;IACT,IAAA,iBAAK,EAAC,qBAAS,CAAC,SAAS,CAAC;GACd,WAAW,CAuGvB;AAvGY,kCAAW","sourcesContent":["import { Config, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';\nimport * as Client from 'prom-client';\nimport { Master } from '../decorator/master';\nimport * as PromClient from 'prom-client';\n\n@Provide()\n@Scope(ScopeEnum.Singleton)\nexport class DataService {\n responseSummary: Client.Summary<any>;\n\n responseHistogram: Client.Histogram<any>;\n\n userDefinedMetrics: any = {};\n\n metrics: any;\n\n @Config('prometheus')\n prometheusConfig;\n\n bInit = false;\n\n async init() {\n this.responseSummary = new Client.Summary({\n name: 'http_request_latency_milliseconds',\n help: 'response time in millis',\n labelNames: [\n 'method',\n 'status',\n 'uri',\n ...Object.keys(this.prometheusConfig.labels),\n ],\n });\n this.responseHistogram = new Client.Histogram({\n name: 'http_request_duration_milliseconds',\n help: 'http_request_duration_milliseconds histogram',\n labelNames: [\n 'method',\n 'status',\n 'uri',\n ...Object.keys(this.prometheusConfig.labels),\n ],\n buckets: [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000],\n });\n }\n\n @Master()\n async getUser(method, status, path, time) {\n if (!this.bInit) {\n this.init();\n this.bInit = true;\n }\n this.responseSummary\n .labels(\n method,\n status,\n path,\n ...(Object.values(this.prometheusConfig.labels) as string[])\n )\n .observe(time);\n this.responseHistogram\n .labels(\n method,\n status,\n path,\n ...(Object.values(this.prometheusConfig.labels) as string[])\n )\n .observe(time);\n }\n\n define(name, type, options) {\n options.labelNames = options.labelNames\n ? [...options.labelNames, ...Object.keys(this.prometheusConfig.labels)]\n : Object.keys(this.prometheusConfig.labels);\n this.userDefinedMetrics[name] = new Client[type](options);\n }\n\n @Master()\n async inc(name, labels, value = 1) {\n this.userDefinedMetrics[name].inc(\n { ...labels, ...this.prometheusConfig.labels },\n value\n );\n }\n\n @Master()\n async set(name, value) {\n this.userDefinedMetrics[name].set(\n { ...this.prometheusConfig.labels },\n value\n );\n }\n\n @Master()\n async setDiff(name, diff) {\n const current = await this.userDefinedMetrics[name].get();\n let value = diff;\n if (current.values.length !== 0) {\n value = current.values[0].value + diff;\n }\n this.userDefinedMetrics[name].set(\n { ...this.prometheusConfig.labels },\n value\n );\n }\n\n @Master()\n async getData() {\n const Register = PromClient.register;\n return Register.metrics();\n }\n}\n"]}
|
package/dist/utils/utils.js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,EAAE;QACpC,iBAAiB;QACjB,IAAI,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,KAAK,CAAC,EAAE;YACjD,OAAO,IAAI,CAAC;SACb;KACF;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAhCD,4BAgCC;AAED,SAAgB,SAAS;IACvB,IAAI,GAAG,EAAE;QACP,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QAClB,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;QACxB,GAAG,GAAG,IAAI,CAAC;KACZ;AACH,CAAC;AAND,8BAMC","sourcesContent":["import * as cluster from 'cluster';\nimport * as path from 'path';\nimport * as os from 'os';\nimport * as fs from 'fs';\n\nlet gfp = null;\nlet bInit = false;\nconst ppid = process.ppid;\nconst now = new Date();\nconst lockFile = path.join(\n os.tmpdir(),\n `midway-master-${now.getFullYear()}-${\n now.getMonth() + 1\n }-${now.getDate()}-${ppid}.lock`\n);\nexport function isMaster() {\n // fix for node v16\n if (cluster['isPrimary'] || cluster['isMaster']) {\n return true;\n }\n\n if (process.argv[1].indexOf('egg-cluster') >= 0) {\n // Is run with egg-scripts\n if (bInit && gfp) {\n return true;\n } else if (bInit) {\n return false;\n } else {\n bInit = true;\n try {\n const result = fs.openSync(lockFile, 'wx');\n gfp = result;\n return true;\n } catch (e) {\n return false;\n }\n }\n }\n\n if (process.env && process.env.pm_id) {\n //Is run with PM2\n if (parseInt(process.env.NODE_APP_INSTANCE) === 0) {\n return true;\n }\n }\n\n return false;\n}\n\nexport function closeLock() {\n if (gfp) {\n fs.closeSync(gfp);\n fs.unlinkSync(lockFile);\n gfp = null;\n }\n}\n"]}
|