Skip to content

Commit

Permalink
Improve trace logging of GETs
Browse files Browse the repository at this point in the history
  • Loading branch information
awlayton committed Feb 18, 2022
1 parent fcef1c9 commit 3fa1ac8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion oada/services/http-handler/src/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ const plugin: FastifyPluginAsync<Options> = async (fastify, options) => {
oadaGraph.resource_id,
oadaGraph.path_leftover
);
request.log.trace(document, 'DOC IS');
request.log.trace({ document }, 'Document is');

// TODO: Allow null values in OADA?
if (document === undefined || document === null) {
Expand Down
66 changes: 36 additions & 30 deletions oada/services/write-handler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,34 @@ export interface WriteResponse extends KafkaBase, WriteContext {
change_id?: string;
}

async function checkPreconditions(request: WriteRequest) {
if (request['if-match']) {
const rev = (await resources.getResource(
request.resource_id,
'/_rev'
)) as unknown as number;
if (request['if-match'] !== rev) {
error(rev);
error(request['if-match']);
error(request);
throw new Error('if-match failed');
}
}

if (request['if-none-match']) {
const rev = (await resources.getResource(
request.resource_id,
'/_rev'
)) as unknown as number;
if (request['if-none-match'].includes(rev)) {
error(rev);
error(request['if-none-match']);
error(request);
throw new Error('if-none-match failed');
}
}
}

export async function handleRequest(
request: WriteRequest
): Promise<WriteResponse> {
Expand All @@ -187,32 +215,8 @@ export async function handleRequest(
async (
body: unknown
): Promise<{ rev?: number; orev?: number; changeId?: string }> => {
trace(body, 'FIRST BODY');
if (request['if-match']) {
const rev = (await resources.getResource(
request.resource_id,
'/_rev'
)) as unknown as number;
if (request['if-match'] !== rev) {
error(rev);
error(request['if-match']);
error(request);
throw new Error('if-match failed');
}
}

if (request['if-none-match']) {
const rev = (await resources.getResource(
request.resource_id,
'/_rev'
)) as unknown as number;
if (request['if-none-match'].includes(rev)) {
error(rev);
error(request['if-none-match']);
error(request);
throw new Error('if-none-match failed');
}
}
trace({ body }, 'FIRST BODY');
await checkPreconditions(request);

let cacheRev = cache.get(request.resource_id);
if (!cacheRev) {
Expand Down Expand Up @@ -271,7 +275,9 @@ export async function handleRequest(
);
if (request.resourceExists === false) {
trace(
`initializing arango: resource_id = ${request.resource_id}, path_leftover = ${request.path_leftover}`
'initializing arango: resource_id = %s, path_leftover = %s',
request.resource_id,
request.path_leftover
);
id = request.resource_id.replace(/^\//, '');
path = path.slice(2);
Expand All @@ -289,7 +295,7 @@ export async function handleRequest(
},
},
});
trace(object, 'Intializing resource');
trace({ resource: object }, 'Intializing resource');
}

// Create object to recursively merge into the resource
Expand All @@ -312,7 +318,7 @@ export async function handleRequest(
o[endK] = body as DeepPartial<Resource>;
}

trace(object, 'Setting body on arango object');
trace({ body: object }, 'Setting body on arango object');

// Update meta
const meta: Partial<Resource['_meta']> & Record<string, unknown> = {
Expand Down Expand Up @@ -344,7 +350,7 @@ export async function handleRequest(

// Compute new change
const children = request.from_change_id ?? [];
trace(object, 'Putting change');
trace({ change: object }, 'Putting change');
const changeId = await changes.putChange({
change: { ...object, _rev: rev },
resId: id,
Expand Down

0 comments on commit 3fa1ac8

Please sign in to comment.