diff --git a/ghost/core/core/shared/prometheus-client.js b/ghost/core/core/shared/prometheus-client.js index 6db7ed6f3d..2ce4af4726 100644 --- a/ghost/core/core/shared/prometheus-client.js +++ b/ghost/core/core/shared/prometheus-client.js @@ -3,7 +3,7 @@ const config = require('./config'); let prometheusClient; -if (!prometheusClient) { +if (!prometheusClient && config.get('prometheus:enabled')) { const pushgatewayConfig = config.get('prometheus:pushgateway'); const prometheusConfig = {pushgateway: pushgatewayConfig}; prometheusClient = new PrometheusClient(prometheusConfig); diff --git a/ghost/core/test/integration/prometheus-client.test.js b/ghost/core/test/integration/prometheus-client.test.js new file mode 100644 index 0000000000..eb78775df4 --- /dev/null +++ b/ghost/core/test/integration/prometheus-client.test.js @@ -0,0 +1,40 @@ +const assert = require('assert').strict; +const configUtils = require('../utils/configUtils'); + +function clearModuleCache() { + delete require.cache[require.resolve('../../core/shared/prometheus-client')]; +} + +function getFreshPrometheusClient() { + clearModuleCache(); + return require('../../core/shared/prometheus-client'); +} + +describe('Integration: prometheus-client', function () { + let prometheusClient; + + beforeEach(function () { + if (prometheusClient) { + prometheusClient.client.register.clear(); + } + }); + + it('should export an instance of the prometheus client if it is enabled', async function () { + configUtils.set('prometheus:enabled', true); + prometheusClient = getFreshPrometheusClient(); + assert.ok(prometheusClient); + }); + + it('should not create a new instance if one already exists', async function () { + configUtils.set('prometheus:enabled', true); + prometheusClient = getFreshPrometheusClient(); + const prometheusClient2 = require('../../core/shared/prometheus-client'); + assert.equal(prometheusClient, prometheusClient2); + }); + + it('should not export an instance of the prometheus client if it is disabled', async function () { + configUtils.set('prometheus:enabled', false); + prometheusClient = getFreshPrometheusClient(); + assert.equal(prometheusClient, undefined); + }); +});