Skip to content

Commit

Permalink
refactor: test using hr dates
Browse files Browse the repository at this point in the history
  • Loading branch information
esroyo committed May 9, 2024
1 parent 3bb8356 commit 15dffcc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/create-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
calcExpires,
cloneHeaders,
denyHeaders,
getTime,
isForbidden,
isJsResponse,
isNotFound,
Expand Down Expand Up @@ -56,6 +57,7 @@ export function createRequestHandler(
if (willCache) {
const cacheKey = [url, buildTarget];
const cacheWriteSpan = tracer.startSpan('cache-write', {
startTime: getTime(),
attributes: {
'span.type': 'cache',
'systemjs.cache.key': cacheKey,
Expand All @@ -66,7 +68,7 @@ export function createRequestHandler(
responseProps,
{ expireIn: calcExpires(headers, CACHE_REDIRECT) },
);
cacheWriteSpan.end();
cacheWriteSpan.end(getTime());
}
if (
CACHE_CLIENT_REDIRECT &&
Expand Down Expand Up @@ -100,6 +102,7 @@ export function createRequestHandler(
}
const cacheKey = [redirectLocation, buildTarget];
const redirectCacheReadSpan = tracer.startSpan('redirect-cache-read', {
startTime: getTime(),
attributes: {
'span.type': 'cache',
'systemjs.cache.key': cacheKey,
Expand All @@ -109,7 +112,7 @@ export function createRequestHandler(
redirectCacheReadSpan.addEvent(
cachedValue ? 'redirect-cache-hit' : 'redirect-cache-miss',
);
redirectCacheReadSpan.end();
redirectCacheReadSpan.end(getTime());
if (cachedValue) {
return createFinalResponse(
{
Expand Down Expand Up @@ -189,14 +192,15 @@ export function createRequestHandler(
if (CACHE) {
const cacheKey = [publicSelfUrl, buildTarget];
const cacheReadSpan = tracer.startSpan('cache-read', {
startTime: getTime(),
attributes: {
'span.type': 'cache',
'systemjs.cache.key': cacheKey,
},
});
const cachedValue = await cache?.get(cacheKey);
cacheReadSpan.addEvent(cachedValue ? 'cache-hit' : 'cache-miss');
cacheReadSpan.end();
cacheReadSpan.end(getTime());
if (cachedValue) {
const response = await createFinalResponse(
{
Expand All @@ -218,6 +222,7 @@ export function createRequestHandler(
}
const upstreamUrlString = upstreamUrl.toString();
const upstreamSpan = tracer.startSpan('upstream', {
startTime: getTime(),
attributes: {
'span.type': 'http',
'http.url': upstreamUrlString,
Expand All @@ -228,13 +233,15 @@ export function createRequestHandler(
redirect: 'manual',
});
let body = await upstreamResponse.text();
upstreamSpan.end();
upstreamSpan.end(getTime());
if (isJsResponse(upstreamResponse)) {
const buildSpan = tracer.startSpan('build');
const buildSpan = tracer.startSpan('build', {
startTime: getTime(),
});
body = replaceOrigin(
await toSystemjs(body, { banner: OUTPUT_BANNER }, config),
);
buildSpan.end();
buildSpan.end(getTime());
} else {
body = replaceOrigin(body);
}
Expand Down
4 changes: 3 additions & 1 deletion src/instrument-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
SpanKind,
SpanProcessor,
} from '../deps.ts';
import { getTime } from './utils.ts';

export const instrumentRequestHandler = <T extends Deno.ServeHandler>(
requestHandler: T,
Expand Down Expand Up @@ -42,6 +43,7 @@ export const instrumentRequestHandler = <T extends Deno.ServeHandler>(
): Promise<Response> => {
// Create a new root span for this request
const requestSpan = tracer.startSpan('total', {
startTime: getTime(),
attributes: {
'http.client_ip':
(req.headers.get('x-forwarded-for') ?? '').split(',')
Expand Down Expand Up @@ -75,7 +77,7 @@ export const instrumentRequestHandler = <T extends Deno.ServeHandler>(
);

requestSpan.setAttribute('http.status_code', response.status);
requestSpan.end();
requestSpan.end(getTime());

response.headers.set(
...serverTimingExporter.getServerTimingHeader(requestSpan),
Expand Down
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,8 @@ export const sanitizeUpstreamOrigin = (
}
return `${url.origin}${url.pathname}`;
};

// @ts-ignore
export const variableTimeOrigin = () => new Date() - performance.now();

export const getTime = () => variableTimeOrigin() + performance.now();

0 comments on commit 15dffcc

Please sign in to comment.