diff --git a/lib/api/middlewares/metrics.js b/lib/api/middlewares/metrics.js index c0095913..33f81670 100644 --- a/lib/api/middlewares/metrics.js +++ b/lib/api/middlewares/metrics.js @@ -50,6 +50,7 @@ function getEventData (req, res, tags) { map_id: getLayergroupid({ res }), cache_buster: getCacheBuster({ res }), template_hash: getTemplateHash({ res }), + stat_tag: getStatTag({ res }), response_code: res.statusCode.toString(), response_time: getResponseTime(res), source_domain: req.hostname, @@ -98,6 +99,17 @@ function getTemplateHash ({ res }) { return res.locals.templateHash; } +function getStatTag ({ res }) { + if (res.locals.mapConfig) { + return res.locals.mapConfig.obj().stat_tag; + } + + // FIXME: don't expect that mapConfig is already set + if (res.locals.mapConfigProvider && res.locals.mapConfigProvider.mapConfig) { + return res.locals.mapConfigProvider.mapConfig.obj().stat_tag; + } +} + // FIXME: 'X-Tiler-Profiler' might not be accurate enough function getResponseTime (res) { const profiler = res.get('X-Tiler-Profiler'); diff --git a/test/integration/metrics-test.js b/test/integration/metrics-test.js index 72354263..789a10d1 100644 --- a/test/integration/metrics-test.js +++ b/test/integration/metrics-test.js @@ -23,6 +23,7 @@ function templateBuilder ({ name }) { version: '0.0.1', name: `metrics-template-${name}`, layergroup: { + stat_tag: `stat-tag-${name}`, version: '1.8.0', layers: [ { @@ -328,6 +329,7 @@ describe('metrics middleware', function () { assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.map_id, token); assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.cache_buster, cacheBuster); assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.template_hash, templateHash); + assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.stat_tag, template.layergroup.stat_tag); return done(); }); @@ -346,10 +348,11 @@ describe('metrics middleware', function () { 'Carto-Event-Group-Id': expectedEventGroupId }; const overrideServerOptions = { pubSubMetrics: { enabled: true, topic: 'topic-test' } }; - const templateMissingCartoCSS = { + const templateMissingCartoCSSVersion = { version: '0.0.1', - name: 'metrics-template', + name: 'metrics-template-missing-cartocss-version', layergroup: { + stat_tag: 'stat-tag-missing-cartocss-version', version: '1.8.0', layers: [ { @@ -363,7 +366,7 @@ describe('metrics middleware', function () { } }; - this.testClient = new TestClient(templateMissingCartoCSS, apikey, extraHeaders, overrideServerOptions); + this.testClient = new TestClient(templateMissingCartoCSSVersion, apikey, extraHeaders, overrideServerOptions); const params = { response: { @@ -390,6 +393,7 @@ describe('metrics middleware', function () { assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.cache_buster, 'string'); // TODO: uncomment this // assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.template_hash, 'string'); + assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.stat_tag, templateMissingCartoCSSVersion.layergroup.stat_tag); return done(); }); @@ -423,6 +427,7 @@ describe('metrics middleware', function () { assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.map_id, 'string'); assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.cache_buster, 'string'); assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.template_hash, 'string'); + assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.stat_tag, template.layergroup.stat_tag); return done(); }); @@ -461,6 +466,7 @@ describe('metrics middleware', function () { assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.cache_buster, 'string'); // TODO: uncomment this // assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.template_hash, 'string'); + assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.stat_tag, template.layergroup.stat_tag); return done(); }); @@ -508,6 +514,7 @@ describe('metrics middleware', function () { assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.cache_buster, 'string'); // TODO: uncomment this // assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.template_hash, 'string'); + assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.stat_tag, template.layergroup.stat_tag); return done(); }); diff --git a/test/support/test-client.js b/test/support/test-client.js index 542855f9..36183a0d 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -1055,6 +1055,9 @@ TestClient.prototype.getLayergroup = function (params, callback) { self.keysToDelete['map_cfg|' + LayergroupToken.parse(parsedBody.layergroupid).token] = 0; self.keysToDelete['user:localhost:mapviews:global'] = 5; } + if (res.statusCode === 200 && self.template && self.template.layergroup && self.template.layergroup.stat_tag) { + self.keysToDelete[`user:localhost:mapviews:stat_tag:${self.template.layergroup.stat_tag}`] = 5; + } } if (err) { return callback(err); @@ -1792,6 +1795,9 @@ TestClient.prototype.getPreview = function (width, height, params = {}, callback switch (res.headers['content-type']) { case 'image/png': this.keysToDelete['user:localhost:mapviews:global'] = 5; + if (this.template.layergroup && this.template.layergroup.stat_tag) { + this.keysToDelete[`user:localhost:mapviews:stat_tag:${this.template.layergroup.stat_tag}`] = 5; + } body = mapnik.Image.fromBytes(Buffer.from(res.body, 'binary')); break; case 'application/json; charset=utf-8':