Skip to content

Commit

Permalink
feat: support prometheus client (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
stone-jin authored Apr 1, 2021
1 parent a85af14 commit b0edd42
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/prometheus/.gitignore
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.*
39 changes: 39 additions & 0 deletions packages/prometheus/package.json
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"
}
}
3 changes: 3 additions & 0 deletions packages/prometheus/src/config/config.default.ts
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 = {};
17 changes: 17 additions & 0 deletions packages/prometheus/src/configuration.ts
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 packages/prometheus/src/controller/prometheusController.ts
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;
}
}
3 changes: 3 additions & 0 deletions packages/prometheus/src/index.ts
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';
1 change: 1 addition & 0 deletions packages/prometheus/test/.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jest.setTimeout(30000)
3 changes: 3 additions & 0 deletions packages/prometheus/test/fixtures/base-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ali-demo"
}
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 {
}
23 changes: 23 additions & 0 deletions packages/prometheus/test/index.test.ts
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)
});
});

23 changes: 23 additions & 0 deletions packages/prometheus/tsconfig.json
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"
]
}

0 comments on commit b0edd42

Please sign in to comment.