Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(prometheus): call Client.register.clear() before init #2892

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/prometheus/src/service/dataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class DataService {
bInit = false;

async init() {
Client.register.clear();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

调用这个的原因是?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我单测里面会出现这种异常

 2023-04-11 15:39:52,166 ERROR 3250 Error [unhandledRejectionError]: A metric with the name http_request_latency_milliseconds has already been registered.
    at Registry.registerMetric (/__w/midway-components/midway-components/node_modules/prom-client/lib/registry.js:67:10)
    at new Metric (/__w/midway-components/midway-components/node_modules/prom-client/lib/metric.js:48:13)
    at new Summary (/__w/midway-components/midway-components/node_modules/prom-client/lib/summary.js:17:3)
    at DataService.init (/__w/midway-components/midway-components/node_modules/@midwayjs/prometheus/dist/service/dataService.js:23:32)
    at DataService.getUser (/__w/midway-components/midway-components/node_modules/@midwayjs/prometheus/dist/service/dataService.js:47:18)
    at /__w/midway-components/midway-components/node_modules/@midwayjs/prometheus/dist/configuration.js:77:25
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /__w/midway-components/midway-components/node_modules/@midwayjs/i18n/dist/middleware.js:150:17
    at async handleAppExceptionAndNext (/__w/midway-components/midway-components/packages/otel/dist/middleware/helper.middleware.js:28:9)
    at async bodyParser (/__w/midway-components/midway-components/node_modules/koa-bodyparser/index.js:91:5)
2023-04-11 15:39:52,169 ERROR 3250 TypeError: Cannot read properties of undefined (reading 'labels')
    at DataService.getUser (/__w/midway-components/midway-components/node_modules/@midwayjs/prometheus/dist/service/dataService.js:51:14)
    at /__w/midway-components/midway-components/node_modules/@midwayjs/prometheus/dist/configuration.js:77:25
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /__w/midway-components/midway-components/node_modules/@midwayjs/i18n/dist/middleware.js:150:17
    at async handleAppExceptionAndNext (/__w/midway-components/midway-components/packages/otel/dist/middleware/helper.middleware.js:28:9)
    at async bodyParser (/__w/midway-components/midway-components/node_modules/koa-bodyparser/index.js:91:5)
    at async session (/__w/midway-components/midway-components/node_modules/@midwayjs/session/dist/middleware/session.js:137:17)
    at async middleware (/__w/midway-components/midway-components/packages/boot/dist/middleware/error-handler.middleware.js:30:9)
    at async handleTopExceptionAndNext (/__w/midway-components/midway-components/packages/otel/dist/middleware/helper.middleware.js:12:9)
    at async middleware (/__w/midway-components/midway-components/packages/otel/dist/middleware/trace-outer.middleware.js:58:5)

导致后续频繁出现 undefined 异常:

2023-04-11 15:39:52,177 ERROR 3250 TypeError: Cannot read properties of undefined (reading 'labels')
    at DataService.getUser (/__w/midway-components/midway-components/node_modules/@midwayjs/prometheus/dist/service/dataService.js:51:14)
    at /__w/midway-components/midway-components/node_modules/@midwayjs/prometheus/dist/configuration.js:77:25
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /__w/midway-components/midway-components/node_modules/@midwayjs/i18n/dist/middleware.js:150:17
    at async handleAppExceptionAndNext (/__w/midway-components/midway-components/packages/otel/dist/middleware/helper.middleware.js:28:9)
    at async bodyParser (/__w/midway-components/midway-components/node_modules/koa-bodyparser/index.js:91:5)
    at async session (/__w/midway-components/midway-components/node_modules/@midwayjs/session/dist/middleware/session.js:137:17)
    at async middleware (/__w/midway-components/midway-components/packages/boot/dist/middleware/error-handler.middleware.js:30:9)
    at async handleTopExceptionAndNext (/__w/midway-components/midway-components/packages/otel/dist/middleware/helper.middleware.js:12:9)
    at async middleware (/__w/midway-components/midway-components/packages/otel/dist/middleware/trace-outer.middleware.js:58:5)

搜索了下,问题似乎应该是 prom 重复注册导致的,解决办法是注册之前先清理一次。

this.responseSummary = new Client.Summary({
name: 'http_request_latency_milliseconds',
help: 'response time in millis',
Expand Down