Skip to content

Commit

Permalink
chore: Log an incoming request properly (#1097)
Browse files Browse the repository at this point in the history
* chore: Properly log a request

* chore: Doc comments
  • Loading branch information
ukstv authored Jul 11, 2023
1 parent 2412920 commit 1eb849d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
25 changes: 18 additions & 7 deletions src/ancillary/anchor-request-params-parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Request as ExpReq } from 'express'
import type { CID } from 'multiformats/cid'
import type { StreamID } from '@ceramicnetwork/streamid'
import { CARFactory, type CAR } from 'cartonne'
import { ServiceMetrics as Metrics } from '@ceramicnetwork/observability'
import { METRIC_NAMES } from '../settings.js'
Expand All @@ -23,6 +22,8 @@ import {
isLeft,
decode,
validate,
type,
union,
type TypeOf,
type Decoder,
type Validation,
Expand All @@ -49,15 +50,25 @@ const RequestAnchorParamsV2Root = strict({
tip: cid,
})

export type RequestAnchorParamsV2 = {
streamId: StreamID
timestamp: Date
cid: CID
genesisFields: GenesisFields
}
/**
* Used to encode request params for logging purposes
*/
export const RequestAnchorParamsV2 = type({
streamId: streamIdAsString,
timestamp: date,
cid: cidAsString,
genesisFields: GenesisFields,
})

export type RequestAnchorParamsV2 = TypeOf<typeof RequestAnchorParamsV2>

export type RequestAnchorParams = RequestAnchorParamsV1 | RequestAnchorParamsV2

/**
* Encode request params for logging purposes.
*/
export const RequestAnchorParamsCodec = union([RequestAnchorParamsV1, RequestAnchorParamsV2])

const DAG_JOSE_CODE = 133
const DAG_CBOR_CODE = 113

Expand Down
5 changes: 3 additions & 2 deletions src/controllers/request-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { METRIC_NAMES } from '../settings.js'
import {
AnchorRequestParamsParser,
RequestAnchorParams,
RequestAnchorParamsCodec,
} from '../ancillary/anchor-request-params-parser.js'
import bodyParser from 'body-parser'
import type { RequestService } from '../services/request-service.js'
Expand Down Expand Up @@ -94,8 +95,6 @@ export class RequestController {
async createRequest(req: ExpReq, res: ExpRes): Promise<ExpRes<any>> {
const origin = parseOrigin(req)

logger.debug(`Create request ${JSON.stringify(req.body)}`)

const validation = this.anchorRequestParamsParser.parse(req)

if (isLeft(validation)) {
Expand All @@ -108,6 +107,8 @@ export class RequestController {
}
const requestParams = validation.right

logger.debug(`Create request ${JSON.stringify(RequestAnchorParamsCodec.encode(requestParams))}`)

try {
const found = await this.requestService.findByCid(requestParams.cid)
if (found) {
Expand Down

0 comments on commit 1eb849d

Please sign in to comment.