Improved API tracing

This commit is contained in:
Tom Moor
2023-12-09 10:23:53 -05:00
parent cb0d84a803
commit aab5697b21
3 changed files with 27 additions and 12 deletions

View File

@@ -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

View File

@@ -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;

View 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();
};
}