diff --git a/lib/api/middlewares/pubsub-metrics.js b/lib/api/middlewares/pubsub-metrics.js index ecd77644..5e9b1df2 100644 --- a/lib/api/middlewares/pubsub-metrics.js +++ b/lib/api/middlewares/pubsub-metrics.js @@ -78,7 +78,7 @@ function getResponseTime (res) { return undefined; } - return stats.total.toString(); + return stats && stats.total ? stats.total.toString() : undefined; } function getFromReq (req, { query = {}, body = {}, params = {}, headers = {} } = {}) { diff --git a/test/integration/pubsub-metrics-test.js b/test/integration/pubsub-metrics-test.js index 1ded5193..4a4c7272 100644 --- a/test/integration/pubsub-metrics-test.js +++ b/test/integration/pubsub-metrics-test.js @@ -418,4 +418,45 @@ describe('pubsub metrics middleware', function () { return testClient.drain(done); }); }); + + it('should send event for errored static named map requests', function (done) { + const expectedEvent = 'map_view'; + const expectedMetricsEvent = 'event-test'; + const expectedEventSource = 'event-source-test'; + const expectedEventGroupId = '1'; + const expectedResponseCode = '400'; + const expectedMapType = 'static'; + const extraHeaders = { + 'Carto-Event': expectedMetricsEvent, + 'Carto-Event-Source': expectedEventSource, + 'Carto-Event-Group-Id': expectedEventGroupId + }; + const overrideServerOptions = { pubSubMetrics: { enabled: true, topic: 'topic-test' } }; + const template = templateBuilder({ name: 'preview-errored' }); + const testClient = new TestClient(template, apikey, extraHeaders, overrideServerOptions); + const widthTooLarge = 8193; + const params = { + response: { + status: 400, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + } + } + }; + + testClient.getPreview(widthTooLarge, 480, params, (err, res, body) => { + if (err) { + return done(err); + } + + assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.event, expectedEvent); + assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.metrics_event, expectedMetricsEvent); + assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.event_source, expectedEventSource); + assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.event_group_id, expectedEventGroupId); + assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.response_code, expectedResponseCode); + assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.map_type, expectedMapType); + + return testClient.drain(done); + }); + }); }); diff --git a/test/support/test-client.js b/test/support/test-client.js index 0639d402..542855f9 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -1788,11 +1788,10 @@ TestClient.prototype.getPreview = function (width, height, params = {}, callback return callback(err); } - this.keysToDelete['user:localhost:mapviews:global'] = 5; - let body; switch (res.headers['content-type']) { case 'image/png': + this.keysToDelete['user:localhost:mapviews:global'] = 5; body = mapnik.Image.fromBytes(Buffer.from(res.body, 'binary')); break; case 'application/json; charset=utf-8':