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:
+27
-8
@@ -6,6 +6,7 @@ import {
|
||||
type VNode,
|
||||
} from "preact";
|
||||
import { renderToString } from "preact-render-to-string";
|
||||
import { SpanStatusCode } from "@opentelemetry/api";
|
||||
import type { ResolvedFreshConfig } from "./config.ts";
|
||||
import type { BuildCache } from "./build_cache.ts";
|
||||
import {
|
||||
@@ -15,6 +16,7 @@ import {
|
||||
} from "./runtime/server/preact_hooks.tsx";
|
||||
import { DEV_ERROR_OVERLAY_URL } from "./constants.ts";
|
||||
import { BUILD_ID } from "./runtime/build_id.ts";
|
||||
import { tracer } from "./otel.ts";
|
||||
|
||||
export interface Island {
|
||||
file: string | URL;
|
||||
@@ -184,14 +186,31 @@ export class FreshReqContext<State>
|
||||
headers.set("X-Fresh-Id", partialId);
|
||||
}
|
||||
|
||||
const html = preactRender(
|
||||
vnode,
|
||||
this,
|
||||
this.#islandRegistry,
|
||||
this.#buildCache,
|
||||
partialId,
|
||||
headers,
|
||||
);
|
||||
const html = tracer.startActiveSpan("render", (span) => {
|
||||
span.setAttribute("fresh.span_type", "render");
|
||||
try {
|
||||
return preactRender(
|
||||
vnode,
|
||||
this,
|
||||
this.#islandRegistry,
|
||||
this.#buildCache,
|
||||
partialId,
|
||||
headers,
|
||||
);
|
||||
} catch (err) {
|
||||
if (err instanceof Error) {
|
||||
span.recordException(err);
|
||||
} else {
|
||||
span.setStatus({
|
||||
code: SpanStatusCode.ERROR,
|
||||
message: String(err),
|
||||
});
|
||||
}
|
||||
throw err;
|
||||
} finally {
|
||||
span.end();
|
||||
}
|
||||
});
|
||||
return new Response(html, responseInit);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user