Skip to content

Commit

Permalink
fix: gateway metrics route with cached response (#1311)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Feb 9, 2022
1 parent f2faf79 commit 7a5a72a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
14 changes: 6 additions & 8 deletions packages/gateway/src/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ import { contentLengthHistogram } from './durable-objects/summary-metrics.js'
* @returns {Promise<Response>}
*/
export async function metricsGet(request, env, ctx) {
// TODO: Set cache
// const cache = caches.default
// let res = await cache.match(request)
const cache = caches.default
let res = await cache.match(request)

// if (res) {
// return res
// }
let res
if (res) {
return res
}

const [summaryMetrics, ipfsGateways, gatewayRedirectCount] =
await Promise.all([
Expand Down Expand Up @@ -277,7 +275,7 @@ export async function metricsGet(request, env, ctx) {
},
})

// ctx.waitUntil(cache.put(request, res.clone()))
ctx.waitUntil(cache.put(request, res.clone()))

return res
}
Expand Down
28 changes: 11 additions & 17 deletions packages/gateway/test/metrics.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava'

import { gateways } from './constants.js'
import { getMiniflare } from './utils.js'
import { getMiniflare, getSummaryMetricsName } from './utils.js'

test.beforeEach((t) => {
// Create a new Miniflare environment for each test
Expand Down Expand Up @@ -76,21 +76,15 @@ test('Gets Metrics content when empty state', async (t) => {
test('Gets Metrics content', async (t) => {
const { mf } = t.context

let response = await mf.dispatchFetch('http://localhost:8787/metrics')
let metricsResponse = await response.text()
// Get Durable object current state
const ns = await mf.getDurableObjectNamespace(getSummaryMetricsName())
const id = ns.idFromName('summary-metrics')
const stub = ns.get(id)
const doRes = await stub.fetch(`http://localhost:8787/metrics`)

t.is(
metricsResponse.includes(
`_responses_content_length_total{le="524288",env="test"} 0`
),
true
)
t.is(
metricsResponse.includes(
`_responses_content_length_bytes_total{env="test"} 0`
),
true
)
const doMetrics = await doRes.json()
t.is(doMetrics.totalContentLengthBytes, '0')
t.is(doMetrics.contentLengthHistogram['524288'], 0)

// Trigger two requests with content length of 23 each
const p = await Promise.all([
Expand All @@ -105,8 +99,8 @@ test('Gets Metrics content', async (t) => {
// Wait for waitUntil
await Promise.all(p.map((p) => p.waitUntil()))

response = await mf.dispatchFetch('http://localhost:8787/metrics')
metricsResponse = await response.text()
const response = await mf.dispatchFetch('http://localhost:8787/metrics')
const metricsResponse = await response.text()

t.is(
metricsResponse.includes(
Expand Down
4 changes: 4 additions & 0 deletions packages/gateway/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ export function getCIDsTrackerName() {
export function getGatewayRateLimitsName() {
return 'GATEWAYRATELIMITS'
}

export function getSummaryMetricsName() {
return 'SUMMARYMETRICS'
}

0 comments on commit 7a5a72a

Please sign in to comment.