Improved API tracing
This commit is contained in:
@@ -22,7 +22,8 @@ import events from "./events";
|
||||
import fileOperationsRoute from "./fileOperations";
|
||||
import groups from "./groups";
|
||||
import integrations from "./integrations";
|
||||
import apiWrapper from "./middlewares/apiWrapper";
|
||||
import apiResponse from "./middlewares/apiResponse";
|
||||
import apiTracer from "./middlewares/apiTracer";
|
||||
import editor from "./middlewares/editor";
|
||||
import notifications from "./notifications";
|
||||
import pins from "./pins";
|
||||
@@ -54,7 +55,8 @@ api.use(
|
||||
);
|
||||
api.use(coalesceBody());
|
||||
api.use<BaseContext, UserAgentContext>(userAgent);
|
||||
api.use(apiWrapper());
|
||||
api.use(apiTracer());
|
||||
api.use(apiResponse());
|
||||
api.use(editor());
|
||||
|
||||
// register package API routes before others to allow for overrides
|
||||
|
||||
@@ -1,17 +1,9 @@
|
||||
import stream from "stream";
|
||||
import { Context, Next } from "koa";
|
||||
import { Readable } from "readable-stream";
|
||||
import { addTags } from "@server/logging/tracer";
|
||||
|
||||
export default function apiWrapper() {
|
||||
return async function apiWrapperMiddleware(ctx: Context, next: Next) {
|
||||
const id = ctx.request.body?.id ?? ctx.request.query?.id;
|
||||
if (id) {
|
||||
addTags({
|
||||
"resource.id": `${id}`,
|
||||
});
|
||||
}
|
||||
|
||||
export default function apiResponse() {
|
||||
return async function apiResponseMiddleware(ctx: Context, next: Next) {
|
||||
await next();
|
||||
const ok = ctx.status < 400;
|
||||
|
||||
21
server/routes/api/middlewares/apiTracer.ts
Normal file
21
server/routes/api/middlewares/apiTracer.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Context, Next } from "koa";
|
||||
import { addTags } from "@server/logging/tracer";
|
||||
|
||||
export default function apiTracer() {
|
||||
return async function apiTracerMiddleware(ctx: Context, next: Next) {
|
||||
const params = ctx.request.body ?? ctx.request.query;
|
||||
|
||||
for (const key in params) {
|
||||
if (key === "id" || key.endsWith("Id")) {
|
||||
const value = params[key];
|
||||
if (typeof value === "string") {
|
||||
addTags({
|
||||
[`resource.${key}`]: value,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await next();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user