mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Add requestId middleware
This commit is contained in:
parent
f683642478
commit
0cdb7b8cd5
5 changed files with 12 additions and 5 deletions
|
|
@ -1,11 +1,13 @@
|
||||||
import { MiddlewareHandler } from '@hono/hono';
|
|
||||||
import { logi } from '@soapbox/logi';
|
import { logi } from '@soapbox/logi';
|
||||||
|
|
||||||
export const logiMiddleware: MiddlewareHandler = async (c, next) => {
|
import type { DittoMiddleware } from '@ditto/mastoapi/router';
|
||||||
|
|
||||||
|
export const logiMiddleware: DittoMiddleware = async (c, next) => {
|
||||||
|
const { requestId } = c.var;
|
||||||
const { method } = c.req;
|
const { method } = c.req;
|
||||||
const { pathname } = new URL(c.req.url);
|
const { pathname } = new URL(c.req.url);
|
||||||
|
|
||||||
logi({ level: 'info', ns: 'ditto.http.request', method, pathname });
|
logi({ level: 'info', ns: 'ditto.http.request', method, pathname, requestId });
|
||||||
|
|
||||||
const start = new Date();
|
const start = new Date();
|
||||||
|
|
||||||
|
|
@ -15,5 +17,5 @@ export const logiMiddleware: MiddlewareHandler = async (c, next) => {
|
||||||
const duration = (end.getTime() - start.getTime()) / 1000;
|
const duration = (end.getTime() - start.getTime()) / 1000;
|
||||||
const level = c.res.status >= 500 ? 'error' : 'info';
|
const level = c.res.status >= 500 ? 'error' : 'info';
|
||||||
|
|
||||||
logi({ level, ns: 'ditto.http.response', method, pathname, status: c.res.status, duration });
|
logi({ level, ns: 'ditto.http.response', method, pathname, status: c.res.status, duration, requestId });
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ export class DittoApp extends Hono<DittoEnv> {
|
||||||
c.set('conf', opts.conf);
|
c.set('conf', opts.conf);
|
||||||
c.set('relay', opts.relay);
|
c.set('relay', opts.relay);
|
||||||
c.set('signal', c.req.raw.signal);
|
c.set('signal', c.req.raw.signal);
|
||||||
|
c.set('requestId', c.req.header('X-Request-Id') ?? crypto.randomUUID());
|
||||||
return next();
|
return next();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,5 +16,7 @@ export interface DittoEnv extends Env {
|
||||||
db: DittoDB;
|
db: DittoDB;
|
||||||
/** Abort signal for the request. */
|
/** Abort signal for the request. */
|
||||||
signal: AbortSignal;
|
signal: AbortSignal;
|
||||||
|
/** Unique ID for the request. */
|
||||||
|
requestId: string;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,4 +2,4 @@ import type { MiddlewareHandler } from '@hono/hono';
|
||||||
import type { DittoEnv } from './DittoEnv.ts';
|
import type { DittoEnv } from './DittoEnv.ts';
|
||||||
|
|
||||||
// deno-lint-ignore ban-types
|
// deno-lint-ignore ban-types
|
||||||
export type DittoMiddleware<T extends {}> = MiddlewareHandler<DittoEnv & { Variables: T }>;
|
export type DittoMiddleware<T extends {} = {}> = MiddlewareHandler<DittoEnv & { Variables: T }>;
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ export class DittoRoute extends Hono<DittoEnv> {
|
||||||
if (!vars.conf) this.throwMissingVar('conf');
|
if (!vars.conf) this.throwMissingVar('conf');
|
||||||
if (!vars.relay) this.throwMissingVar('relay');
|
if (!vars.relay) this.throwMissingVar('relay');
|
||||||
if (!vars.signal) this.throwMissingVar('signal');
|
if (!vars.signal) this.throwMissingVar('signal');
|
||||||
|
if (!vars.requestId) this.throwMissingVar('requestId');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...vars,
|
...vars,
|
||||||
|
|
@ -32,6 +33,7 @@ export class DittoRoute extends Hono<DittoEnv> {
|
||||||
conf: vars.conf,
|
conf: vars.conf,
|
||||||
relay: vars.relay,
|
relay: vars.relay,
|
||||||
signal: vars.signal,
|
signal: vars.signal,
|
||||||
|
requestId: vars.requestId,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue