diff --git a/lib/api/api-router.js b/lib/api/api-router.js index 7bf7da88..699fc0a9 100644 --- a/lib/api/api-router.js +++ b/lib/api/api-router.js @@ -187,7 +187,7 @@ module.exports = class ApiRouter { this.mapRouter = new MapRouter({ collaborators }); this.templateRouter = new TemplateRouter({ collaborators }); this.metadataBackend = metadataBackend; - this.pubSubMetricsBackend = PubSubMetricsBackend.build(); + this.pubSubMetricsBackend = new PubSubMetricsBackend(this.serverOptions.pubSubMetrics); } route (app, routes) { @@ -220,7 +220,10 @@ module.exports = class ApiRouter { apiRouter.use(sendResponse()); apiRouter.use(syntaxError()); apiRouter.use(errorMiddleware()); - apiRouter.use(pubSubMetrics(this.pubSubMetricsBackend)); + apiRouter.use(pubSubMetrics({ + enabled: this.serverOptions.pubSubMetrics.enabled, + metricsBackend: this.pubSubMetricsBackend + })); paths.forEach(path => app.use(path, apiRouter)); }); diff --git a/lib/api/middlewares/pubsub-metrics.js b/lib/api/middlewares/pubsub-metrics.js index 4dbdee00..bae6171a 100644 --- a/lib/api/middlewares/pubsub-metrics.js +++ b/lib/api/middlewares/pubsub-metrics.js @@ -3,8 +3,8 @@ const EVENT_VERSION = '1'; const MAX_LENGTH = 100; -function pubSubMetrics (pubSubMetricsBackend) { - if (!pubSubMetricsBackend.isEnabled()) { +function pubSubMetrics ({ enabled, metricsBackend }) { + if (!enabled) { return function pubSubMetricsDisabledMiddleware (req, res, next) { next(); }; @@ -15,7 +15,7 @@ function pubSubMetrics (pubSubMetricsBackend) { const { event, attributes } = getEventData(req, res); if (event) { - pubSubMetricsBackend.sendEvent(event, attributes); + metricsBackend.send(event, attributes); } }); diff --git a/lib/backends/pubsub-metrics.js b/lib/backends/pubsub-metrics.js index f6565a9b..9cf0ee2d 100644 --- a/lib/backends/pubsub-metrics.js +++ b/lib/backends/pubsub-metrics.js @@ -3,37 +3,18 @@ const { PubSub } = require('@google-cloud/pubsub'); module.exports = class PubSubMetricsBackend { - static build () { - if (!global.environment.pubSubMetrics || !global.environment.pubSubMetrics.enabled) { - return new PubSubMetricsBackend(undefined, false); - } - - const { project_id: projectId, credentials: keyFilename, topic } = global.environment.pubSubMetrics; - const pubsub = new PubSub({ projectId, keyFilename }); - - return new PubSubMetricsBackend(pubsub, topic, true); + constructor (options = {}) { + const { project_id: projectId, credentials: keyFilename, topic } = options; + this._pubsub = new PubSub({ projectId, keyFilename }); + this._topicName = topic; } - constructor (pubSub, topic, enabled) { - this.pubsub = pubSub; - this.topic = topic; - this.enabled = enabled; - } - - isEnabled () { - return this.enabled; - } - - sendEvent (event, attributes) { - if (!this.enabled) { - return; - } - + send (event, attributes) { const data = Buffer.from(event); - this.pubsub.topic(this.topic).publish(data, attributes) + this._pubsub.topic(this._topicName).publish(data, attributes) .then(() => { - console.log(`PubSubTracker: event '${event}' published to '${this.topic}'`); + console.log(`PubSubTracker: event '${event}' published to '${this._topicName}'`); }) .catch((error) => { console.error(`ERROR: pubsub middleware failed to publish event '${event}': ${error.message}`); diff --git a/lib/server-options.js b/lib/server-options.js index c607d98b..8b3ced76 100644 --- a/lib/server-options.js +++ b/lib/server-options.js @@ -134,5 +134,6 @@ module.exports = { fastly: global.environment.fastly || {}, cache_enabled: global.environment.cache_enabled, log_format: global.environment.log_format, - useProfiler: global.environment.useProfiler + useProfiler: global.environment.useProfiler, + pubSubMetrics: Object.assign({ enabled: false }, global.environment.pubSubMetrics) }; diff --git a/lib/server.js b/lib/server.js index be030631..671045c9 100644 --- a/lib/server.js +++ b/lib/server.js @@ -21,6 +21,7 @@ module.exports = function createServer (serverOptions) { app.disable('etag'); app.set('json replacer', jsonReplacer()); + // FIXME: do not pass 'global.environment' as 'serverOptions' should keep defaults from 'global.environment' const apiRouter = new ApiRouter({ serverOptions, environmentOptions: global.environment }); apiRouter.route(app, serverOptions.routes.api);