-
Notifications
You must be signed in to change notification settings - Fork 821
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
feat: make prometheus config preventServerStart optional #1375
Changes from 6 commits
2c62be3
66dd10b
cb03cd2
cc77819
6387935
e75b4b6
0bdea70
c565286
9e3d50d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,6 @@ import { ExporterConfig } from './export/types'; | |
export class PrometheusExporter implements MetricExporter { | ||
static readonly DEFAULT_OPTIONS = { | ||
port: 9464, | ||
startServer: false, | ||
endpoint: '/metrics', | ||
prefix: '', | ||
}; | ||
|
@@ -59,7 +58,7 @@ export class PrometheusExporter implements MetricExporter { | |
* @param config Exporter configuration | ||
* @param callback Callback to be called after a server was started | ||
*/ | ||
constructor(config: ExporterConfig = {}, callback?: () => void) { | ||
constructor(config: ExporterConfig, callback?: () => void) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should not be changed, if in js user will not define any config we don't want the error "object is undefined" to be thrown. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @legendecas any update here? |
||
this._logger = config.logger || new NoopLogger(); | ||
this._port = config.port || PrometheusExporter.DEFAULT_OPTIONS.port; | ||
this._prefix = config.prefix || PrometheusExporter.DEFAULT_OPTIONS.prefix; | ||
|
@@ -69,10 +68,10 @@ export class PrometheusExporter implements MetricExporter { | |
config.endpoint || PrometheusExporter.DEFAULT_OPTIONS.endpoint | ||
).replace(/^([^/])/, '/$1'); | ||
|
||
if (config.startServer || PrometheusExporter.DEFAULT_OPTIONS.startServer) { | ||
if (config.startServer) { | ||
this.startServer(callback); | ||
} else if (callback) { | ||
callback(); | ||
process.nextTick(callback); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ describe('PrometheusExporter', () => { | |
}); | ||
describe('constructor', () => { | ||
it('should construct an exporter', () => { | ||
const exporter = new PrometheusExporter(); | ||
const exporter = new PrometheusExporter({ startServer: false }); | ||
assert.ok(typeof exporter.startServer === 'function'); | ||
assert.ok(typeof exporter.shutdown === 'function'); | ||
}); | ||
|
@@ -68,17 +68,13 @@ describe('PrometheusExporter', () => { | |
} | ||
); | ||
}); | ||
|
||
it('should not start the server by default', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this test is still relevant , if user doesn't provide anything (when using in pure js for example) the server should not start right ? |
||
const exporter = new PrometheusExporter(); | ||
assert.ok(exporter['_server']!.listening === false); | ||
}); | ||
}); | ||
|
||
describe('server', () => { | ||
it('it should start on startServer() and call the callback', done => { | ||
const exporter = new PrometheusExporter({ | ||
port: 9722, | ||
startServer: false, | ||
}); | ||
exporter.startServer(() => { | ||
exporter.shutdown(() => { | ||
|
@@ -90,7 +86,7 @@ describe('PrometheusExporter', () => { | |
it('it should listen on the default port and default endpoint', done => { | ||
const port = PrometheusExporter.DEFAULT_OPTIONS.port; | ||
const endpoint = PrometheusExporter.DEFAULT_OPTIONS.endpoint; | ||
const exporter = new PrometheusExporter(); | ||
const exporter = new PrometheusExporter({ startServer: false }); | ||
|
||
exporter.startServer(() => { | ||
const url = `http://localhost:${port}${endpoint}`; | ||
|
@@ -110,6 +106,7 @@ describe('PrometheusExporter', () => { | |
const exporter = new PrometheusExporter({ | ||
port, | ||
endpoint, | ||
startServer: false, | ||
}); | ||
|
||
exporter.startServer(() => { | ||
|
@@ -130,6 +127,7 @@ describe('PrometheusExporter', () => { | |
const exporter = new PrometheusExporter({ | ||
port, | ||
endpoint, | ||
startServer: false, | ||
}); | ||
|
||
exporter.startServer(() => { | ||
|
@@ -140,6 +138,7 @@ describe('PrometheusExporter', () => { | |
const exporter2 = new PrometheusExporter({ | ||
port, | ||
endpoint: `/${endpoint}`, | ||
startServer: false, | ||
}); | ||
|
||
exporter2.startServer(() => { | ||
|
@@ -162,6 +161,7 @@ describe('PrometheusExporter', () => { | |
const exporter = new PrometheusExporter({ | ||
port, | ||
endpoint, | ||
startServer: false, | ||
}); | ||
exporter.startServer(() => { | ||
const url = `http://localhost:${port}/invalid`; | ||
|
@@ -176,7 +176,7 @@ describe('PrometheusExporter', () => { | |
}); | ||
|
||
it('should call a provided callback regardless of if the server is running', done => { | ||
const exporter = new PrometheusExporter(); | ||
const exporter = new PrometheusExporter({ startServer: false }); | ||
exporter.shutdown(() => { | ||
return done(); | ||
}); | ||
|
@@ -188,7 +188,7 @@ describe('PrometheusExporter', () => { | |
let meter: Meter; | ||
|
||
beforeEach(done => { | ||
exporter = new PrometheusExporter(); | ||
exporter = new PrometheusExporter({ startServer: false }); | ||
meter = new MeterProvider().getMeter('test-prometheus'); | ||
exporter.startServer(done); | ||
}); | ||
|
@@ -442,6 +442,7 @@ describe('PrometheusExporter', () => { | |
it('should use a configured name prefix', done => { | ||
exporter = new PrometheusExporter({ | ||
prefix: 'test_prefix', | ||
startServer: false, | ||
}); | ||
|
||
exporter.startServer(async () => { | ||
|
@@ -471,6 +472,7 @@ describe('PrometheusExporter', () => { | |
it('should use a configured port', done => { | ||
exporter = new PrometheusExporter({ | ||
port: 8080, | ||
startServer: false, | ||
}); | ||
|
||
exporter.startServer(async () => { | ||
|
@@ -500,6 +502,7 @@ describe('PrometheusExporter', () => { | |
it('should use a configured endpoint', done => { | ||
exporter = new PrometheusExporter({ | ||
endpoint: '/test', | ||
startServer: false, | ||
}); | ||
|
||
exporter.startServer(async () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you make this configuration option required? Should it not just default to true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason
startServer
defaults to false have been stated at the description of the PR "it grabs a port on the host computer. This type of thing is best left explicit so the user is not surprised" -- which is copy-pasted from previous Gitter chat history I considered as a fair reason (unfortunately Gitter doesn't provide chat history links).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am leaning towards default to true, prometheus is meant to be scraped in most cases. The default port of
9464
is meant for this as per https://github.com/prometheus/prometheus/wiki/Default-port-allocationsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the gitter chat @legendecas referenced was actually me :)
Think I see this one both ways. It is unfortunate that we have to grab a port, but the exporter really is useless without it so I would say it's probably ok to just do it.