fix: add more request instrumentation (#3300)

This commit is contained in:
Marvin Hagemeister
2025-09-01 17:30:12 +02:00
committed by GitHub
parent 1c097cfd3b
commit 5d052c0fdb
2 changed files with 54 additions and 30 deletions
+14 -2
View File
@@ -16,6 +16,7 @@ import {
newRouteCmd,
} from "./commands.ts";
import { isLazy } from "./utils.ts";
import { recordSpanError, tracer } from "./otel.ts";
export interface FreshFsMod<State> {
config?: RouteConfig;
@@ -140,8 +141,19 @@ export function fsItemsToCommands<State>(
let config: RouteConfig = {};
if (isLazy(rawMod)) {
normalized = async () => {
const result = await rawMod();
return normalizeRoute(filePath, result, routePattern);
return await tracer.startActiveSpan("lazy-route", {
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";
+40 -28
View File
@@ -1,6 +1,7 @@
import { type Context, getInternals } from "../context.ts";
import type { App as _App } from "../app.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
@@ -87,38 +88,49 @@ export type MaybeLazyMiddleware<State> = (
ctx: Context<State>,
) => Response | Promise<Response | Middleware<State>>;
export function runMiddlewares<State>(
export async function runMiddlewares<State>(
middlewares: MaybeLazyMiddleware<State>[],
ctx: Context<State>,
): Promise<Response> {
let fn = ctx.next;
let i = middlewares.length;
while (i--) {
const local = fn;
let next = middlewares[i];
const idx = i;
fn = async () => {
const internals = getInternals(ctx);
const { app: prevApp, layouts: prevLayouts } = internals;
return await tracer.startActiveSpan("middlewares", {
attributes: { "fresh.middleware.count": middlewares.length },
}, async (span) => {
try {
let fn = ctx.next;
let i = middlewares.length;
while (i--) {
const local = fn;
let next = middlewares[i];
const idx = i;
fn = async () => {
const internals = getInternals(ctx);
const { app: prevApp, layouts: prevLayouts } = internals;
ctx.next = local;
try {
const result = await next(ctx);
if (typeof result === "function") {
middlewares[idx] = result;
next = result;
return await result(ctx);
}
ctx.next = local;
try {
const result = await next(ctx);
if (typeof result === "function") {
middlewares[idx] = result;
next = result;
return await result(ctx);
}
return result;
} catch (err) {
ctx.error = err;
throw err;
} finally {
internals.app = prevApp;
internals.layouts = prevLayouts;
return result;
} catch (err) {
ctx.error = err;
throw err;
} finally {
internals.app = prevApp;
internals.layouts = prevLayouts;
}
};
}
};
}
return fn();
return await fn();
} catch (err) {
recordSpanError(span, err);
throw err;
} finally {
span.end();
}
});
}