fix: add more request instrumentation (#3300)
This commit is contained in:
@@ -16,6 +16,7 @@ import {
|
|||||||
newRouteCmd,
|
newRouteCmd,
|
||||||
} from "./commands.ts";
|
} from "./commands.ts";
|
||||||
import { isLazy } from "./utils.ts";
|
import { isLazy } from "./utils.ts";
|
||||||
|
import { recordSpanError, tracer } from "./otel.ts";
|
||||||
|
|
||||||
export interface FreshFsMod<State> {
|
export interface FreshFsMod<State> {
|
||||||
config?: RouteConfig;
|
config?: RouteConfig;
|
||||||
@@ -140,8 +141,19 @@ export function fsItemsToCommands<State>(
|
|||||||
let config: RouteConfig = {};
|
let config: RouteConfig = {};
|
||||||
if (isLazy(rawMod)) {
|
if (isLazy(rawMod)) {
|
||||||
normalized = async () => {
|
normalized = async () => {
|
||||||
const result = await rawMod();
|
return await tracer.startActiveSpan("lazy-route", {
|
||||||
return normalizeRoute(filePath, result, routePattern);
|
attributes: { "fresh.route_name": rawMod.name ?? "anonymous" },
|
||||||
|
}, async (span) => {
|
||||||
|
try {
|
||||||
|
const result = await rawMod();
|
||||||
|
return normalizeRoute(filePath, result, routePattern);
|
||||||
|
} catch (err) {
|
||||||
|
recordSpanError(span, err);
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
span.end();
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
config.methods = item.overrideConfig?.methods ?? "ALL";
|
config.methods = item.overrideConfig?.methods ?? "ALL";
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { type Context, getInternals } from "../context.ts";
|
import { type Context, getInternals } from "../context.ts";
|
||||||
import type { App as _App } from "../app.ts";
|
import type { App as _App } from "../app.ts";
|
||||||
import type { Define as _Define } from "../define.ts";
|
import type { Define as _Define } from "../define.ts";
|
||||||
|
import { recordSpanError, tracer } from "../otel.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A middleware function is the basic building block of Fresh. It allows you
|
* A middleware function is the basic building block of Fresh. It allows you
|
||||||
@@ -87,38 +88,49 @@ export type MaybeLazyMiddleware<State> = (
|
|||||||
ctx: Context<State>,
|
ctx: Context<State>,
|
||||||
) => Response | Promise<Response | Middleware<State>>;
|
) => Response | Promise<Response | Middleware<State>>;
|
||||||
|
|
||||||
export function runMiddlewares<State>(
|
export async function runMiddlewares<State>(
|
||||||
middlewares: MaybeLazyMiddleware<State>[],
|
middlewares: MaybeLazyMiddleware<State>[],
|
||||||
ctx: Context<State>,
|
ctx: Context<State>,
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
let fn = ctx.next;
|
return await tracer.startActiveSpan("middlewares", {
|
||||||
let i = middlewares.length;
|
attributes: { "fresh.middleware.count": middlewares.length },
|
||||||
while (i--) {
|
}, async (span) => {
|
||||||
const local = fn;
|
try {
|
||||||
let next = middlewares[i];
|
let fn = ctx.next;
|
||||||
const idx = i;
|
let i = middlewares.length;
|
||||||
fn = async () => {
|
while (i--) {
|
||||||
const internals = getInternals(ctx);
|
const local = fn;
|
||||||
const { app: prevApp, layouts: prevLayouts } = internals;
|
let next = middlewares[i];
|
||||||
|
const idx = i;
|
||||||
|
fn = async () => {
|
||||||
|
const internals = getInternals(ctx);
|
||||||
|
const { app: prevApp, layouts: prevLayouts } = internals;
|
||||||
|
|
||||||
ctx.next = local;
|
ctx.next = local;
|
||||||
try {
|
try {
|
||||||
const result = await next(ctx);
|
const result = await next(ctx);
|
||||||
if (typeof result === "function") {
|
if (typeof result === "function") {
|
||||||
middlewares[idx] = result;
|
middlewares[idx] = result;
|
||||||
next = result;
|
next = result;
|
||||||
return await result(ctx);
|
return await result(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
ctx.error = err;
|
ctx.error = err;
|
||||||
throw err;
|
throw err;
|
||||||
} finally {
|
} finally {
|
||||||
internals.app = prevApp;
|
internals.app = prevApp;
|
||||||
internals.layouts = prevLayouts;
|
internals.layouts = prevLayouts;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
};
|
return await fn();
|
||||||
}
|
} catch (err) {
|
||||||
return fn();
|
recordSpanError(span, err);
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
span.end();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user