diff --git a/lib/api/api-router.js b/lib/api/api-router.js index 479d25ed..ab2bf332 100644 --- a/lib/api/api-router.js +++ b/lib/api/api-router.js @@ -21,7 +21,7 @@ const OverviewsMetadataBackend = require('../backends/overviews-metadata'); const FilterStatsApi = require('../backends/filter-stats'); const TablesExtentBackend = require('../backends/tables-extent'); const ClusterBackend = require('../backends/cluster'); -const PubSubMetricsBackend = require('../backends/pubsub-metrics'); +const PubSubMetricsBackend = require('../backends/metrics'); const LayergroupAffectedTablesCache = require('../cache/layergroup-affected-tables'); const SurrogateKeysCache = require('../cache/surrogate-keys-cache'); diff --git a/lib/api/map/anonymous-map-controller.js b/lib/api/map/anonymous-map-controller.js index 356ef338..135da65f 100644 --- a/lib/api/map/anonymous-map-controller.js +++ b/lib/api/map/anonymous-map-controller.js @@ -23,7 +23,7 @@ const mapError = require('../middlewares/map-error'); const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider'); const rateLimit = require('../middlewares/rate-limit'); const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit; -const metrics = require('../middlewares/pubsub-metrics'); +const metrics = require('../middlewares/metrics'); module.exports = class AnonymousMapController { /** diff --git a/lib/api/map/preview-template-controller.js b/lib/api/map/preview-template-controller.js index 832dfacb..d9a64707 100644 --- a/lib/api/map/preview-template-controller.js +++ b/lib/api/map/preview-template-controller.js @@ -12,7 +12,7 @@ const lastModifiedHeader = require('../middlewares/last-modified-header'); const checkStaticImageFormat = require('../middlewares/check-static-image-format'); const rateLimit = require('../middlewares/rate-limit'); const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit; -const metrics = require('../middlewares/pubsub-metrics'); +const metrics = require('../middlewares/metrics'); const DEFAULT_ZOOM_CENTER = { zoom: 1, diff --git a/lib/api/middlewares/pubsub-metrics.js b/lib/api/middlewares/metrics.js similarity index 87% rename from lib/api/middlewares/pubsub-metrics.js rename to lib/api/middlewares/metrics.js index 5e9b1df2..82924f47 100644 --- a/lib/api/middlewares/pubsub-metrics.js +++ b/lib/api/middlewares/metrics.js @@ -3,9 +3,9 @@ const EVENT_VERSION = '1'; const MAX_LENGTH = 100; -module.exports = function pubSubMetrics ({ enabled, metricsBackend, logger, tags }) { +module.exports = function metrics ({ enabled, metricsBackend, logger, tags }) { if (!enabled) { - return function pubSubMetricsDisabledMiddleware (req, res, next) { + return function metricsDisabledMiddleware (req, res, next) { next(); }; } @@ -14,13 +14,13 @@ module.exports = function pubSubMetrics ({ enabled, metricsBackend, logger, tags throw new Error('Missing required "event" parameter to report metrics'); } - return function pubSubMetricsMiddleware (req, res, next) { + return function metricsMiddleware (req, res, next) { res.on('finish', () => { const { event, attributes } = getEventData(req, res, tags); 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}`)); + .then(() => logger.debug(`Event "${event}" published succesfully`)) + .catch((error) => logger.error(`Failed to publish event "${event}": ${error.message}`)); }); return next(); diff --git a/lib/api/template/named-template-controller.js b/lib/api/template/named-template-controller.js index e8891455..b1fd7348 100644 --- a/lib/api/template/named-template-controller.js +++ b/lib/api/template/named-template-controller.js @@ -21,7 +21,7 @@ const NamedMapMapConfigProvider = require('../../models/mapconfig/provider/named const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider'); const rateLimit = require('../middlewares/rate-limit'); const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit; -const metrics = require('../middlewares/pubsub-metrics'); +const metrics = require('../middlewares/metrics'); module.exports = class NamedMapController { /** diff --git a/lib/backends/pubsub-metrics.js b/lib/backends/metrics.js similarity index 61% rename from lib/backends/pubsub-metrics.js rename to lib/backends/metrics.js index e4b37643..ba91b4e6 100644 --- a/lib/backends/pubsub-metrics.js +++ b/lib/backends/metrics.js @@ -2,16 +2,16 @@ const { PubSub } = require('@google-cloud/pubsub'); -module.exports = class PubSubMetricsBackend { +module.exports = class MetricsBackend { constructor (options = {}) { const { project_id: projectId, credentials: keyFilename, topic } = options; - this._pubsub = new PubSub({ projectId, keyFilename }); + this._metricsClient = new PubSub({ projectId, keyFilename }); this._topicName = topic; } send (event, attributes) { const data = Buffer.from(event); - return this._pubsub.topic(this._topicName).publish(data, attributes); + return this._metricsClient.topic(this._topicName).publish(data, attributes); } }; diff --git a/test/integration/pubsub-metrics-test.js b/test/integration/metrics-test.js similarity index 98% rename from test/integration/pubsub-metrics-test.js rename to test/integration/metrics-test.js index 4a4c7272..147224aa 100644 --- a/test/integration/pubsub-metrics-test.js +++ b/test/integration/metrics-test.js @@ -2,7 +2,7 @@ const assert = require('assert'); const TestClient = require('../support/test-client'); -const PubSubMetricsBackend = require('../../lib/backends/pubsub-metrics'); +const MetricsBackend = require('../../lib/backends/metrics'); const apikey = 1234; const mapConfig = { version: '1.8.0', @@ -37,11 +37,11 @@ function templateBuilder ({ name }) { }; }; -describe('pubsub metrics middleware', function () { +describe('metrics middleware', function () { beforeEach(function () { - this.originalPubSubMetricsBackendSendMethod = PubSubMetricsBackend.prototype.send; + this.originalMetricsBackendSendMethod = MetricsBackend.prototype.send; this.pubSubMetricsBackendSendMethodCalled = false; - PubSubMetricsBackend.prototype.send = (event, attributes) => { + MetricsBackend.prototype.send = (event, attributes) => { this.pubSubMetricsBackendSendMethodCalled = true; this.pubSubMetricsBackendSendMethodCalledWith = { event, attributes }; return Promise.resolve(); @@ -49,7 +49,7 @@ describe('pubsub metrics middleware', function () { }); afterEach(function () { - PubSubMetricsBackend.prototype.send = this.originalPubSubMetricsBackendSendMethod; + MetricsBackend.prototype.send = this.originalMetricsBackendSendMethod; }); it('should not send event if not enabled', function (done) {