diff --git a/lib/api/middlewares/pubsub-metrics.js b/lib/api/middlewares/pubsub-metrics.js index bae6171a..ae0195d4 100644 --- a/lib/api/middlewares/pubsub-metrics.js +++ b/lib/api/middlewares/pubsub-metrics.js @@ -3,7 +3,7 @@ const EVENT_VERSION = '1'; const MAX_LENGTH = 100; -function pubSubMetrics ({ enabled, metricsBackend }) { +function pubSubMetrics ({ enabled, metricsBackend, logger }) { if (!enabled) { return function pubSubMetricsDisabledMiddleware (req, res, next) { next(); @@ -15,7 +15,9 @@ function pubSubMetrics ({ enabled, metricsBackend }) { const { event, attributes } = getEventData(req, res); if (event) { - metricsBackend.send(event, attributes); + metricsBackend.send(event, attributes) + .then(() => logger.debug(`PubSubTracker: event '${event}' published succesfully`)) + .catch((error) => logger.error(`ERROR: pubsub middleware failed to publish event '${event}': ${error.message}`)); } }); diff --git a/lib/backends/pubsub-metrics.js b/lib/backends/pubsub-metrics.js index 9cf0ee2d..e4b37643 100644 --- a/lib/backends/pubsub-metrics.js +++ b/lib/backends/pubsub-metrics.js @@ -5,19 +5,13 @@ const { PubSub } = require('@google-cloud/pubsub'); module.exports = class PubSubMetricsBackend { constructor (options = {}) { const { project_id: projectId, credentials: keyFilename, topic } = options; + this._pubsub = new PubSub({ projectId, keyFilename }); this._topicName = topic; } send (event, attributes) { const data = Buffer.from(event); - - this._pubsub.topic(this._topicName).publish(data, attributes) - .then(() => { - console.log(`PubSubTracker: event '${event}' published to '${this._topicName}'`); - }) - .catch((error) => { - console.error(`ERROR: pubsub middleware failed to publish event '${event}': ${error.message}`); - }); + return this._pubsub.topic(this._topicName).publish(data, attributes); } };