forked from siimon/prom-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from roylines/express-support
Express support
- Loading branch information
Showing
7 changed files
with
120 additions
and
7 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,8 @@ | ||
version: '2' | ||
services: | ||
prometheus: | ||
image: prom/prometheus | ||
volumes: | ||
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml | ||
ports: | ||
- "9090:9090" |
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 @@ | ||
const express = require('express'); | ||
const epithemeus = require('../../index.js'); | ||
|
||
const app = express(); | ||
epithemeus.instrument(app); | ||
|
||
app.get('/', function (req, res) { | ||
var high = 500, low = 150; | ||
|
||
setTimeout(() => { | ||
res.send(); | ||
}, Math.floor(Math.random() * (high - low) + low)); | ||
}); | ||
|
||
app.listen(8000, () => { | ||
console.log('express listening on 8000'); | ||
}); |
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,8 @@ | ||
global: | ||
scrape_interval: 5s | ||
evaluation_interval: 5s | ||
|
||
scrape_configs: | ||
- job_name: 'express' | ||
target_groups: | ||
- targets: ['192.168.1.3:8000'] |
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 |
---|---|---|
@@ -1 +1,46 @@ | ||
module.exports = {} | ||
const client = require('prom-client'); | ||
const metric = { | ||
http: { | ||
requests: { | ||
total: new client.Counter('http_requests_total', 'total number of http requests', ['method', 'path', 'status']), | ||
duration: new client.Summary('http_requests_duration', 'request duration', ['method', 'path', 'status']) | ||
} | ||
} | ||
} | ||
|
||
function ignore(request) { | ||
return request.path === '/metrics' || request.path === '/metrics/'; | ||
} | ||
|
||
function ms(start) { | ||
var diff = process.hrtime(start); | ||
return Math.round((diff[0] * 1e9 + diff[1]) / 1000000); | ||
} | ||
|
||
function middleware(request, response, done) { | ||
if(ignore(request)) { | ||
return done(); | ||
} | ||
|
||
var start = process.hrtime(); | ||
|
||
response.on('finish', function() { | ||
metric.http.requests.total.labels(request.method, request.path, response.statusCode).inc(); | ||
metric.http.requests.duration.labels(request.method, request.path, response.statusCode).observe(ms(start)); | ||
}); | ||
|
||
return done(); | ||
}; | ||
|
||
function metrics(req, res) { | ||
return res.send(client.register.metrics()); | ||
} | ||
|
||
function instrument(app) { | ||
app.use(middleware); | ||
app.get('/metrics', metrics); | ||
} | ||
|
||
module.exports = { | ||
instrument: instrument | ||
} |
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
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,34 @@ | ||
const express = require('express'); | ||
const epithemeus = require('../index'); | ||
const request = require('request'); | ||
const should = require('chai').should(); | ||
|
||
describe('express', () => { | ||
before((done) => { | ||
const app = express(); | ||
epithemeus.instrument(app); | ||
app.get('/', (req, res) => { | ||
res.send(); | ||
}); | ||
this.server = app.listen(3000, done); | ||
}); | ||
|
||
after((done) => { | ||
return this.server.close(done) | ||
}); | ||
|
||
it('should return 200 for /', (done) => { | ||
request('http://localhost:3000/', (e, r, b) => { | ||
r.statusCode.should.equal(200); | ||
return done(e); | ||
}); | ||
}); | ||
|
||
it('should return 200 for /metrics', (done) => { | ||
request('http://localhost:3000/metrics', (e, r, b) => { | ||
console.log('BODY', b); | ||
r.statusCode.should.equal(200); | ||
return done(e); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.