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:
+10
-10
@@ -11,6 +11,7 @@ export interface RouteResult<T> {
|
||||
handlers: T[][];
|
||||
methodMatch: boolean;
|
||||
patternMatch: boolean;
|
||||
pattern: string | null;
|
||||
}
|
||||
|
||||
export interface Router<T> {
|
||||
@@ -28,7 +29,7 @@ export interface Router<T> {
|
||||
export const IS_PATTERN = /[*:{}+?()]/;
|
||||
|
||||
export function mergePaths(a: string, b: string) {
|
||||
if (a === "" || a === "/" || a === "*") return b;
|
||||
if (a === "" || a === "/" || a === "/*") return b;
|
||||
if (b === "/") return a;
|
||||
if (a.endsWith("/")) {
|
||||
return a.slice(0, -1) + b;
|
||||
@@ -48,7 +49,7 @@ export class UrlPatternRouter<T> implements Router<T> {
|
||||
|
||||
add(method: Method | "ALL", pathname: string | URLPattern, handlers: T[]) {
|
||||
if (
|
||||
typeof pathname === "string" && pathname !== "*" &&
|
||||
typeof pathname === "string" && pathname !== "/*" &&
|
||||
IS_PATTERN.test(pathname)
|
||||
) {
|
||||
this._routes.push({
|
||||
@@ -71,6 +72,7 @@ export class UrlPatternRouter<T> implements Router<T> {
|
||||
handlers: [],
|
||||
methodMatch: false,
|
||||
patternMatch: false,
|
||||
pattern: null,
|
||||
};
|
||||
|
||||
if (this._middlewares.length > 0) {
|
||||
@@ -84,16 +86,15 @@ export class UrlPatternRouter<T> implements Router<T> {
|
||||
// to be either wildcard `*` match or an exact pathname match.
|
||||
if (
|
||||
typeof route.path === "string" &&
|
||||
(route.path === "*" || route.path === url.pathname)
|
||||
(route.path === "/*" || route.path === url.pathname)
|
||||
) {
|
||||
if (route.method !== "ALL") {
|
||||
result.patternMatch = true;
|
||||
}
|
||||
if (route.method !== "ALL") result.patternMatch = true;
|
||||
result.pattern = route.path;
|
||||
|
||||
if (route.method === "ALL" || route.method === method) {
|
||||
result.handlers.push(route.handlers);
|
||||
|
||||
if (route.path === "*" && route.method === "ALL") {
|
||||
if (route.path === "/*" && route.method === "ALL") {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -104,9 +105,8 @@ export class UrlPatternRouter<T> implements Router<T> {
|
||||
} else if (route.path instanceof URLPattern) {
|
||||
const match = route.path.exec(url);
|
||||
if (match !== null) {
|
||||
if (route.method !== "ALL") {
|
||||
result.patternMatch = true;
|
||||
}
|
||||
if (route.method !== "ALL") result.patternMatch = true;
|
||||
result.pattern = route.path.pathname;
|
||||
|
||||
if (route.method === "ALL" || route.method === method) {
|
||||
result.handlers.push(route.handlers);
|
||||
|
||||
Reference in New Issue
Block a user