From dbc5d65d908194cae3ee62a580f13cc608879d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 29 Apr 2020 17:26:33 +0200 Subject: [PATCH] Send template_hash as part of the metrics event --- lib/api/middlewares/layergroup-id-header.js | 3 ++- lib/api/middlewares/layergroup-token.js | 4 ++++ lib/api/middlewares/metrics.js | 5 +++++ test/integration/metrics-test.js | 10 +++++++++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/api/middlewares/layergroup-id-header.js b/lib/api/middlewares/layergroup-id-header.js index 94470972..edc452ed 100644 --- a/lib/api/middlewares/layergroup-id-header.js +++ b/lib/api/middlewares/layergroup-id-header.js @@ -6,8 +6,9 @@ module.exports = function setLayergroupIdHeader (templateMaps, useTemplateHash) const layergroup = res.body; if (useTemplateHash) { - var templateHash = templateMaps.fingerPrint(template).substring(0, 8); + const templateHash = templateMaps.fingerPrint(template).substring(0, 8); layergroup.layergroupid = `${user}@${templateHash}@${layergroup.layergroupid}`; + res.locals.templateHash = templateHash; } res.set('X-Layergroup-Id', layergroup.layergroupid); diff --git a/lib/api/middlewares/layergroup-token.js b/lib/api/middlewares/layergroup-token.js index 88e9295d..7d8003bb 100644 --- a/lib/api/middlewares/layergroup-token.js +++ b/lib/api/middlewares/layergroup-token.js @@ -13,6 +13,10 @@ module.exports = function layergroupToken () { res.locals.token = layergroupToken.token; res.locals.cache_buster = layergroupToken.cacheBuster; + if (layergroupToken.templateHash) { + res.locals.templateHash = layergroupToken.templateHash; + } + if (layergroupToken.signer) { res.locals.signer = layergroupToken.signer; diff --git a/lib/api/middlewares/metrics.js b/lib/api/middlewares/metrics.js index 898c2069..c0095913 100644 --- a/lib/api/middlewares/metrics.js +++ b/lib/api/middlewares/metrics.js @@ -49,6 +49,7 @@ function getEventData (req, res, tags) { user_agent: req.get('User-Agent'), map_id: getLayergroupid({ res }), cache_buster: getCacheBuster({ res }), + template_hash: getTemplateHash({ res }), response_code: res.statusCode.toString(), response_time: getResponseTime(res), source_domain: req.hostname, @@ -93,6 +94,10 @@ function getCacheBuster ({ res }) { } } +function getTemplateHash ({ res }) { + return res.locals.templateHash; +} + // 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 56ff4f7f..72354263 100644 --- a/test/integration/metrics-test.js +++ b/test/integration/metrics-test.js @@ -316,7 +316,7 @@ describe('metrics middleware', function () { assert.strictEqual(typeof body.layergroupid, 'string'); - const { token, cacheBuster } = LayergroupToken.parse(body.layergroupid); + const { token, cacheBuster, templateHash } = LayergroupToken.parse(body.layergroupid); assert.ok(this.pubSubMetricsBackendSendMethodCalled); assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.event, expectedEvent); @@ -327,6 +327,7 @@ describe('metrics middleware', function () { assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.map_type, expectedMapType); assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.map_id, token); assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.cache_buster, cacheBuster); + assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.template_hash, templateHash); return done(); }); @@ -387,6 +388,8 @@ describe('metrics middleware', function () { assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.map_type, expectedMapType); assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.map_id, 'string'); assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.cache_buster, 'string'); + // TODO: uncomment this + // assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.template_hash, 'string'); return done(); }); @@ -419,6 +422,7 @@ describe('metrics middleware', function () { assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.response_code, expectedResponseCode); 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'); return done(); }); @@ -455,6 +459,8 @@ describe('metrics middleware', function () { assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.map_type, expectedMapType); assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.map_id, 'string'); assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.cache_buster, 'string'); + // TODO: uncomment this + // assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.template_hash, 'string'); return done(); }); @@ -500,6 +506,8 @@ describe('metrics middleware', function () { assert.strictEqual(this.pubSubMetricsBackendSendMethodCalledWith.attributes.map_type, expectedMapType); assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.map_id, 'string'); assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.cache_buster, 'string'); + // TODO: uncomment this + // assert.strictEqual(typeof this.pubSubMetricsBackendSendMethodCalledWith.attributes.template_hash, 'string'); return done(); });