feat: add open telemetry instrumentation (#2786)
This commit adds some useful automatic instrumentation for when users are using `--unstable-otel`: - Fresh renames the span for incoming HTTP requests to `$METHOD $ROUTE` and the `http.route` attribute is added. - Fresh creates a span around FS route handlers, showing whether they returned a response or data. - Fresh creates a span around async components, showing how long they took to execute. - Fresh adds a span around preact rendering, showing how long rendering takes. - Fresh adds a span around static file serving, showing how long it took and whether there were requests without a cache bust key. We can improve this in the future with more telemetry, such as metrics.
This commit is contained in:
@@ -251,7 +251,7 @@ export async function fsRoutes<State>(
|
||||
errorComponents.push(mod.component);
|
||||
}
|
||||
let parent = mod.path.slice(0, -"_error".length);
|
||||
parent = parent === "/" ? "*" : parent + "*";
|
||||
parent = parent + "*";
|
||||
|
||||
// Add error route as its own route
|
||||
if (!specialPaths.has(mod.path)) {
|
||||
|
||||
@@ -3,6 +3,8 @@ import type { MiddlewareFn } from "../../middlewares/mod.ts";
|
||||
import type { HandlerFn, PageResponse } from "../../handlers.ts";
|
||||
import type { FreshReqContext, PageProps } from "../../context.ts";
|
||||
import { HttpError } from "../../error.ts";
|
||||
import { tracer } from "../../otel.ts";
|
||||
import { SpanStatusCode } from "@opentelemetry/api";
|
||||
|
||||
export type AsyncAnyComponent<P> = {
|
||||
(
|
||||
@@ -31,7 +33,30 @@ export function renderMiddleware<State>(
|
||||
return async (ctx) => {
|
||||
let result: PageResponse<unknown> | undefined;
|
||||
if (handler !== undefined) {
|
||||
const res = await handler(ctx);
|
||||
const res = await tracer.startActiveSpan("handler", {
|
||||
attributes: { "fresh.span_type": "fs_routes/handler" },
|
||||
}, async (span) => {
|
||||
try {
|
||||
const res = await handler(ctx);
|
||||
span.setAttribute(
|
||||
"fresh.handler_response",
|
||||
res instanceof Response ? "http" : "data",
|
||||
);
|
||||
return res;
|
||||
} catch (err) {
|
||||
if (err instanceof Error) {
|
||||
span.recordException(err);
|
||||
} else {
|
||||
span.setStatus({
|
||||
code: SpanStatusCode.ERROR,
|
||||
message: String(err),
|
||||
});
|
||||
}
|
||||
throw err;
|
||||
} finally {
|
||||
span.end();
|
||||
}
|
||||
});
|
||||
|
||||
if (res instanceof Response) {
|
||||
return res;
|
||||
@@ -56,7 +81,32 @@ export function renderMiddleware<State>(
|
||||
const fn = components[i];
|
||||
|
||||
if (isAsyncAnyComponent(fn)) {
|
||||
const result = (await fn(props)) as VNode | Response;
|
||||
const result = await tracer.startActiveSpan(
|
||||
"invoke async component",
|
||||
async (span) => {
|
||||
span.setAttribute("fresh.span_type", "fs_routes/async_component");
|
||||
try {
|
||||
const result = (await fn(props)) as VNode | Response;
|
||||
span.setAttribute(
|
||||
"fresh.component_response",
|
||||
result instanceof Response ? "http" : "jsx",
|
||||
);
|
||||
return result;
|
||||
} catch (err) {
|
||||
if (err instanceof Error) {
|
||||
span.recordException(err);
|
||||
} else {
|
||||
span.setStatus({
|
||||
code: SpanStatusCode.ERROR,
|
||||
message: String(err),
|
||||
});
|
||||
}
|
||||
throw err;
|
||||
} finally {
|
||||
span.end();
|
||||
}
|
||||
},
|
||||
);
|
||||
if (result instanceof Response) {
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user