Skip to content

Commit

Permalink
Add cache.tier attribute to spans
Browse files Browse the repository at this point in the history
in prep for changing the cache tiers
  • Loading branch information
danopia committed May 10, 2024
1 parent 756b229 commit 2203bc5
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions generation/deploy/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const caches: Array<Cache> = [
inMemoryCache(40),
s3Cache(s3Api, Deno.env.get('HTTPCACHE_S3_BUCKET') || 'deno-httpcache'),
];
const cacheLabels = ['in-memory', 's3'];

import { AsyncTracer, Span } from "./tracer.ts";
const tracer = new AsyncTracer('cached-fetch');
Expand All @@ -32,17 +33,20 @@ async function cachedFetchInner(mode: 'immutable' | 'mutable', label: string, ur
span.addEvent('cache.match', {
'cache.result': 'fail',
'cache.index': cacheIdx,
'cache.tier': cacheLabels[cacheIdx],
});
return null;
});
if (cached) {
span.addEvent('cache.match', {
'cache.result': 'hit',
'cache.index': cacheIdx,
'cache.tier': cacheLabels[cacheIdx],
});
span.setAttributes({
'cache.result': 'hit',
'cache.index': cacheIdx,
'cache.tier': cacheLabels[cacheIdx],
});

// Copy colder bodies into warmer caches
Expand All @@ -57,12 +61,14 @@ async function cachedFetchInner(mode: 'immutable' | 'mutable', label: string, ur
span.addEvent('cache.match', {
'cache.result': 'miss',
'cache.index': cacheIdx,
'cache.tier': cacheLabels[cacheIdx],
});
}
}

span.setAttributes({
'cache.result': 'miss',
'cache.tier': 'none',
});

const realResp = await fetch(url);
Expand Down Expand Up @@ -96,13 +102,15 @@ async function emitCachePut(span: Span, cacheIdx: number, promise: Promise<void>
span.addEvent('cache.put', {
'cache.result': 'ok',
'cache.index': cacheIdx,
'cache.tier': cacheLabels[cacheIdx],
}))
.catch(err => {
console.error(`WARN: cache store err:`, err.message);
span.recordException(err);
span.addEvent('cache.put', {
'cache.result': 'fail',
'cache.index': cacheIdx,
'cache.tier': cacheLabels[cacheIdx],
});
return null;
});
Expand Down

0 comments on commit 2203bc5

Please sign in to comment.