Skip to content

Commit

Permalink
Merge pull request #8 from Goodluckhf/self-metrics
Browse files Browse the repository at this point in the history
fix: add '_' if prefix options provided
  • Loading branch information
Goodluckhf authored Oct 8, 2019
2 parents 3200a27 + cdb911a commit 1f01462
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/UMetrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,26 @@ class UMetrics {
*/
start() {
if (this.nodejsMetricsEnabled) {
this.enableExportingDefaultMetrics();
this.enableExportingDefaultMetrics(this.prefix && `${this.prefix}_`);
}
this.transport.start();
return this;
}

/**
* @param {String} prefix
* @private
*/
enableExportingDefaultMetrics() {
promClient.collectDefaultMetrics({
prefix: this.prefix,
enableExportingDefaultMetrics(prefix) {
const options = {
timeout: this.nodejsMetricsInterval,
});
};

if (prefix) {
options.prefix = prefix;
}

promClient.collectDefaultMetrics(options);
}
}

Expand Down
31 changes: 31 additions & 0 deletions src/UMetrics.server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,35 @@ describe('UMetrics facade', () => {
uMetrics.start();
expect(calledCount).to.be.equals(0);
});

it('Should add _ if prefix option provided', () => {
let expectedPrefix = null;
const uMetrics = new UMetrics(new Transport(), {
port: 1111,
nodejsMetricsEnabled: true,
prefix: 'test',
});

uMetrics.enableExportingDefaultMetrics = prefix => {
expectedPrefix = prefix;
};

uMetrics.start();
expect(expectedPrefix).to.be.equals('test_');
});

it('Should call enableExportMetrics without parameters if prefix is not provided', () => {
let expectedPrefix = 0;
const uMetrics = new UMetrics(new Transport(), {
port: 1111,
nodejsMetricsEnabled: true,
});

uMetrics.enableExportingDefaultMetrics = prefix => {
expectedPrefix = prefix;
};

uMetrics.start();
expect(expectedPrefix).to.be.null;
});
});

0 comments on commit 1f01462

Please sign in to comment.