-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support prometheus client (#963)
- Loading branch information
Showing
11 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
logs/ | ||
npm-debug.log | ||
yarn-error.log | ||
node_modules/ | ||
package-lock.json | ||
yarn.lock | ||
coverage/ | ||
dist/ | ||
.idea/ | ||
run/ | ||
.DS_Store | ||
*.sw* | ||
*.un~ | ||
.tsbuildinfo | ||
.tsbuildinfo.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "@midwayjs/prometheus", | ||
"version": "2.9.3", | ||
"description": "", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "midway-bin build -c", | ||
"test": "midway-bin test --ts", | ||
"cov": "midway-bin cov --ts", | ||
"ci": "npm run cov", | ||
"lint:fix": "../../node_modules/.bin/mwts fix" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"files": [ | ||
"dist/**/*.js", | ||
"dist/**/*.d.ts" | ||
], | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@midwayjs/cli": "^1.2.38", | ||
"@midwayjs/core": "^2.3.0", | ||
"@midwayjs/decorator": "^2.3.0", | ||
"@midwayjs/koa": "^2.9.3", | ||
"@types/jest": "^26.0.10", | ||
"@types/node": "14", | ||
"cross-env": "^6.0.0", | ||
"jest": "^26.4.0", | ||
"mwts": "^1.0.5", | ||
"ts-jest": "^26.2.0", | ||
"typescript": "^4.0.0" | ||
}, | ||
"dependencies": { | ||
"prom-client": "^13.1.0", | ||
"@midwayjs/core": "^2.9.2", | ||
"@midwayjs/decorator": "^2.9.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { DefaultMetricsCollectorConfiguration as DefaultConfig } from 'prom-client'; | ||
|
||
export const prometheus: DefaultConfig = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// src/configuration.ts | ||
import { Config, Configuration } from '@midwayjs/decorator'; | ||
import { join } from 'path'; | ||
import * as PromClient from 'prom-client'; | ||
|
||
@Configuration({ | ||
namespace: 'prometheus', | ||
importConfigs: [join(__dirname, 'config')], | ||
}) | ||
export class AutoConfiguration { | ||
@Config('prometheus') | ||
prometheusConfig: any; | ||
|
||
async onReady(contanier) { | ||
PromClient.collectDefaultMetrics(this.prometheusConfig); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/prometheus/src/controller/prometheusController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Get, Inject, Provide, Controller } from '@midwayjs/decorator'; | ||
import * as PromClient from 'prom-client'; | ||
|
||
@Provide() | ||
@Controller('/') | ||
export class PrometheusControlelr { | ||
@Inject() | ||
ctx; | ||
|
||
@Get('/metrics') | ||
async metrics() { | ||
const Register = PromClient.register; | ||
this.ctx.set('Content-Type', Register.contentType); | ||
const res = await Register.metrics(); | ||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { AutoConfiguration as Configuration } from './configuration'; | ||
export * from './controller/prometheusController'; | ||
export { DefaultMetricsCollectorConfiguration as DefaultConfig } from 'prom-client'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
jest.setTimeout(30000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "ali-demo" | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/prometheus/test/fixtures/base-app/src/configuration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Configuration } from '@midwayjs/decorator'; | ||
import * as metrics from '../../../../src'; | ||
|
||
|
||
@Configuration({ | ||
imports: [metrics] | ||
}) | ||
export class AutoConfiguration { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { createApp, close } from '@midwayjs/mock'; | ||
import { Framework } from '@midwayjs/koa' | ||
import { join } from 'path'; | ||
const request = require('supertest'); | ||
|
||
describe('/test/index.test.ts', () => { | ||
|
||
let app = null; | ||
beforeAll(async () => { | ||
app = await createApp(join(__dirname, 'fixtures', 'base-app'), {}, Framework); | ||
}); | ||
|
||
afterAll(async () => { | ||
await close(app); | ||
}) | ||
it('should get metrics', done => { | ||
|
||
request(app.callback()) | ||
.get('/metrics') | ||
.expect(200, done) | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"compileOnSave": true, | ||
"compilerOptions": { | ||
"target": "ES2018", | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true, | ||
"inlineSourceMap":false, | ||
"noImplicitThis": true, | ||
"noUnusedLocals": true, | ||
"stripInternal": true, | ||
"noImplicitReturns": false, | ||
"pretty": true, | ||
"declaration": true, | ||
"outDir": "dist" | ||
}, | ||
"exclude": [ | ||
"dist", | ||
"node_modules", | ||
"test" | ||
] | ||
} |