Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support field customization #179

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/ecs-morgan-format/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IncomingMessage, ServerResponse } from 'http';
import type { FormatFn } from "morgan";

interface Config {
Expand Down Expand Up @@ -42,6 +43,8 @@ interface Config {
/** Specify "event.dataset" field. Defaults `${serviceName}`. */
eventDataset?: string;

/** Callback for custom modification of the fields */
customize: (fields: any, req: IncomingMessage, res: ServerResponse) => void;
}

declare function ecsFormat(config?: Config): FormatFn;
Expand Down
2 changes: 2 additions & 0 deletions packages/ecs-morgan-format/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ function ecsFormat (opts) {
formatHttpRequest(ecsFields, req)
formatHttpResponse(ecsFields, res)

opts.customize && opts.customize(ecsFields, req, res);
jpage-godaddy marked this conversation as resolved.
Show resolved Hide resolved

return stringify(ecsFields)
}
}
Expand Down
23 changes: 23 additions & 0 deletions packages/ecs-morgan-format/test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,26 @@ test('can configure correlation fields', t => {
t.end()
})
})

test('can provide custom fields', t => {
t.plan(2)

const stream = split().on('data', line => {
const rec = JSON.parse(line)
t.equal(rec.labels.custom, 'customValue')
})
const logger = morgan(
ecsFormat({
customize(fields, req, res) {
fields.labels ??= {};
fields.labels.custom = 'customValue';
}
}),
{ stream }
)

makeExpressServerAndRequest(logger, '/', {}, null, function (err) {
t.error(err)
t.end()
})
})
Loading