Skip to content

Commit

Permalink
feat: support option for logging minimal data
Browse files Browse the repository at this point in the history
  • Loading branch information
swain committed Nov 29, 2023
1 parent f1d23a9 commit 165adb7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
15 changes: 11 additions & 4 deletions src/dynamo-streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export type DynamoStreamHandlerConfig<Entity, Context> = {
* Create a "context" for the lambda execution. (e.g. "data sources")
*/
createRunContext: (base: BaseContext) => Context | Promise<Context>;

useMinimalLogging?: boolean;

/**
* The maximum concurrency for processing records.
*
Expand Down Expand Up @@ -227,7 +230,9 @@ export class DynamoStreamHandler<Entity, Context> {
};

context.logger.info(
{ event: this.obfuscateEvent(event) },
this.config.useMinimalLogging
? { eventIds: event.Records.map((r) => r.eventID) }
: { event: this.obfuscateEvent(event) },
'Processing DynamoDB stream event',
);

Expand Down Expand Up @@ -260,9 +265,11 @@ export class DynamoStreamHandler<Entity, Context> {
concurrency: this.config.concurrency ?? 5,
},
async (record) => {
const recordLogger = this.config.logger.child({
record: this.obfuscateRecord(record),
});
const recordLogger = this.config.logger.child(
this.config.useMinimalLogging
? { recordEventId: record.eventID }
: { record: this.obfuscateRecord(record) },
);
if (!record.dynamodb) {
recordLogger.error(
'The dynamodb property was not present on event',
Expand Down
8 changes: 8 additions & 0 deletions src/kinesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export type KinesisEventHandlerConfig<Event, Context> = {
* Create a "context" for the lambda execution. (e.g. "data sources")
*/
createRunContext: (base: BaseContext) => Context | Promise<Context>;

useMinimalLogging?: boolean;
/**
* The maximum concurrency for processing events.
*
Expand Down Expand Up @@ -90,6 +92,12 @@ export class KinesisEventHandler<Event, Context> {
Object.assign(context, await this.config.createRunContext(context));

// 2. Process all the records.
context.logger.info(
this.config.useMinimalLogging
? { eventIds: event.Records.map((r) => r.eventID) }
: { event },
'Processing Kinesis event',
);
const processingResult = await processWithOrdering(
{
items: event.Records,
Expand Down
30 changes: 23 additions & 7 deletions src/sqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export type SQSMessageHandlerConfig<Message, Context> = {
* Create a "context" for the lambda execution. (e.g. "data sources")
*/
createRunContext: (base: BaseContext) => Context | Promise<Context>;

useMinimalLogging?: boolean;

/**
* The maximum concurrency for processing messages.
*
Expand Down Expand Up @@ -107,7 +110,12 @@ export class SQSMessageHandler<Message, Context> {
Object.assign(context, await this.config.createRunContext(context));

// 2. Process all the records.
context.logger.info({ event }, 'Processing SQS topic message');
context.logger.info(
this.config.useMinimalLogging
? { messageIds: event.Records.map((r) => r.messageId) }
: { event },
'Processing SQS message',
);

const processingResult = await processWithOrdering(
{
Expand Down Expand Up @@ -151,12 +159,20 @@ export class SQSMessageHandler<Message, Context> {
.map(([groupId, record]) => {
const [failedRecord, ...subsequentUnprocessedRecords] = record.items;
context.logger.error(
{
groupId,
err: record.error,
failedRecord,
subsequentUnprocessedRecords,
},
this.config.useMinimalLogging
? {
groupId,
err: record.error,
failedRecordMessageId: failedRecord.messageId,
subsequentUnprocessedRecordsMessageIds:
subsequentUnprocessedRecords.map((r) => r.messageId),
}
: {
groupId,
err: record.error,
failedRecord,
subsequentUnprocessedRecords,
},
'Failed to fully process message group',
);

Expand Down

0 comments on commit 165adb7

Please sign in to comment.