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

Enable gc metric collection in Prometheus #256

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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: 0 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@ services:
port: 12345
interface: localhost
# more per-service config settings

21 changes: 21 additions & 0 deletions lib/heapwatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ const cluster = require( 'cluster' );

class HeapWatch {
constructor( conf, logger, metrics ) {
if ( metrics !== null ) {
const prometheusOptions = metrics.options.find(
( option ) => option.type === 'prometheus' && option.collect_default === true
);
// When true, a noop dummy metric will be instantiated
// and passed down to a PrometheusClient instance.
// This metric won't be registered directly by service-runner,
// but will be used to trigger a call to prom-client.collectDefaultMetrics().
this.collect_default = !!prometheusOptions;
}
this.conf = conf = conf || {};
this.limit = ( conf.limitMB || 1500 ) * 1024 * 1024;
this.logger = logger;
Expand All @@ -15,6 +25,17 @@ class HeapWatch {

watch() {
const usage = process.memoryUsage();
if ( this.collect_default ) {
// collect some default metrics recommended by Prometheus itself.
// https://prometheus.io/docs/instrumenting/writing_clientlibs/#standard-and-runtime-collectors
this.metrics.makeMetric( {
type: 'noop',
name: 'prometheus.default',
prometheus: {
collect_default: true
}
} );
}
this.metrics.makeMetric( {
type: 'Gauge',
name: 'heap.rss',
Expand Down
15 changes: 10 additions & 5 deletions lib/metrics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,17 @@ class Metrics {
// }
// }
makeMetric( options ) {
let metric = this.cache.get( options.name );
if ( metric === undefined ) {
metric = new Metric( this.clients, this.logger, options );
this.cache.set( options.name, metric );
// noop is a kind of metric not registered by service-runner Metric interface.
// This is used to delegate collection of default prometheus
// metrics to prom-client.collectDefaultMetrics.
if ( options.collect_default === undefined || options.type !== 'noop' ) {
let metric = this.cache.get( options.name );
if ( metric === undefined ) {
metric = new Metric( this.clients, this.logger, options );
this.cache.set( options.name, metric );
}
return metric;
}
return metric;
}

close() {
Expand Down
1 change: 1 addition & 0 deletions lib/metrics/metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Metric {
this.logger.log( 'error/metrics', `endTiming() unsupported for metric type ${ this.type }` );
}
}

}

module.exports = Metric;
25 changes: 18 additions & 7 deletions lib/metrics/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,24 @@ class PrometheusMetric {
this.options.labels.names = this.options.labels.names.map( this._normalize );

this.options.prometheus.name = this._normalize( this.options.prometheus.name );
this.metric = new this.client[ this.options.type ]( {
name: this.options.prometheus.name,
help: this.options.prometheus.help,
labelNames: this.options.labels.names,
buckets: this.options.prometheus.buckets,
percentiles: this.options.prometheus.percentiles
} );
if ( this.options.type !== 'noop' ) {
this.metric = new this.client[ this.options.type ]( {
name: this.options.prometheus.name,
help: this.options.prometheus.help,
labelNames: this.options.labels.names,
buckets: this.options.prometheus.buckets,
percentiles: this.options.prometheus.percentiles
} );
} else if ( this.options.prometheus && this.options.prometheus.collect_default === true ) {
// TODO: update eslint rules to allow optional chaining operator `?`
// A no op metric.
// Invoke collectDefaultMetrics() but don't register any new metric
// via the service-runner Metric interface.
// https://prometheus.io/docs/instrumenting/writing_clientlibs/#standard-and-runtime-collectors
// TODO: collectDefaultMetrics() does not support providing labels.
// Default labels should be set for the whole registry.
this.client.collectDefaultMetrics();
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/metrics/servers/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class PrometheusServer {
if ( num_workers === 0 ) {
res.end( Prometheus.register.metrics() );
} else {
// config should contain the first `metrics` block in service-runner config,
// that matched `type === 'prometheus'`.
if ( config !== null && config.collect_default === true ) {
throw new Error( 'metrics.prometheus.collect_default cannot be enabled in cluster mode' );
}
AggregatorRegistry.clusterMetrics( ( err, metrics ) => {
if ( err ) {
this._logger.log( 'error/service-runner', err );
Expand Down
26 changes: 24 additions & 2 deletions test/features/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,30 @@ describe( 'service-runner tests', () => {
.finally( () => process.removeListener( 'warning', warningListener ) );
} );

// preq prevents the AssertionErrors from surfacing and failing the test
// performing the test this way presents them correctly
it( 'Must produce prometheus default metrics when hit ', () => {
const server = new TestServer( `${ __dirname }/../utils/simple_config_no_workers_collect_default.yaml` );
const response = { status: null, body: null };
return server.start()
.then( async () => {
// eslint-disable-next-line n/no-unsupported-features/node-builtins
await fetch( 'http://127.0.0.1:9000' )
.then( async ( res ) => {
response.status = res.status;
// This is a ReadableStream of a Uint8Array, but we just want the string
response.body = new TextDecoder().decode( await res.arrayBuffer() );
} );
} )
.delay( 1000 )
.then( () => {
// nodejs_version_info is reported by calls to prom-client.collectDefaultMetrics()
assert.ok(
response.body.includes( 'nodejs_version_info ' ),
'Must collect default metrics prometheus output.'
);
} )
.finally( () => server.stop() );
} );

it( 'Must increment hitcount metrics when hit, no workers', () => {
const server = new TestServer( `${ __dirname }/../utils/simple_config_no_workers.yaml` );
const response = { status: null, body: null };
Expand Down
15 changes: 15 additions & 0 deletions test/utils/simple_config_no_workers_collect_default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
num_workers: 0
logging:
level: fatal
metrics:
- type: prometheus
port: 9000
collect_default: true
- type: statsd
host: localhost
port: 8125
services:
- name: test
module: test/utils/simple_server.js
conf:
port: 12345
Loading