Skip to content

Commit

Permalink
fix: skip writing RequestLogEntry for Cloud Run with flag enabled (#821)
Browse files Browse the repository at this point in the history
* Fix to skip writing RequestLogEntry for Cloud Run

* Parse transport to middleware constructor
  • Loading branch information
cindy-peng committed Oct 24, 2023
1 parent 8e1d7cd commit 7a0ec6f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .readme-partials.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ body: |-
would be picked up by the Cloud Logging Agent running in Google Cloud managed environment.
Note that there is also a `useMessageField` option which controls if "message" field is used to store
structured, non-text data inside `jsonPayload` field when `redirectToStdout` is set. By default `useMessageField` is always `true`.
Set the `skipParentEntryForCloudRun` option to skip creating an entry for the request itself as Cloud Run already automatically creates
such log entries. This might become the default behaviour in a next major version.
```js
// Imports the Google Cloud client library for Winston
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ decrease logging record loss upon execution termination - since all logs are wri
would be picked up by the Cloud Logging Agent running in Google Cloud managed environment.
Note that there is also a `useMessageField` option which controls if "message" field is used to store
structured, non-text data inside `jsonPayload` field when `redirectToStdout` is set. By default `useMessageField` is always `true`.
Set the `skipParentEntryForCloudRun` option to skip creating an entry for the request itself as Cloud Run already automatically creates
such log entries. This might become the default behaviour in a next major version.

```js
// Imports the Google Cloud client library for Winston
Expand Down
18 changes: 14 additions & 4 deletions src/middleware/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ type Middleware = ReturnType<typeof commonMiddleware.express.makeMiddleware>;

export async function makeMiddleware(
logger: winston.Logger,
transport: LoggingWinston
transport: LoggingWinston,
skipParentEntryForCloudRun?: boolean
): Promise<Middleware>;
export async function makeMiddleware(
logger: winston.Logger,
options?: Options
options?: Options,
skipParentEntryForCloudRun?: boolean
): Promise<Middleware>;
export async function makeMiddleware(
logger: winston.Logger,
optionsOrTransport?: Options | LoggingWinston
optionsOrTransport?: Options | LoggingWinston,
skipParentEntryForCloudRun?: boolean
): Promise<Middleware> {
let transport: LoggingWinston;

Expand Down Expand Up @@ -79,8 +82,15 @@ export async function makeMiddleware(
// parent request log entry that all the request specific logs ("app logs")
// will nest under. GAE and GCF generate the parent request logs
// automatically.
// Cloud Run also generates the parent request log automatically, but skipping
// the parent request entry has to be explicity enabled until the next major
// release in which we can change the default behavior.
let emitRequestLogEntry;
if (env !== GCPEnv.APP_ENGINE && env !== GCPEnv.CLOUD_FUNCTIONS) {
if (
env !== GCPEnv.APP_ENGINE &&
env !== GCPEnv.CLOUD_FUNCTIONS &&
(env !== GCPEnv.CLOUD_RUN || !skipParentEntryForCloudRun)
) {
const requestLogName = Log.formatName_(
projectId,
`${transport.common.logName}${REQUEST_LOG_SUFFIX}`
Expand Down
10 changes: 8 additions & 2 deletions test/middleware/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,16 @@ describe('middleware/express', () => {
assert.strictEqual(passedProjectId, FAKE_PROJECT_ID);
});

[GCPEnv.APP_ENGINE, GCPEnv.CLOUD_FUNCTIONS].forEach(env => {
[GCPEnv.APP_ENGINE, GCPEnv.CLOUD_FUNCTIONS, GCPEnv.CLOUD_RUN].forEach(env => {
it(`should not generate the request logger on ${env}`, async () => {
authEnvironment = env;
await makeMiddleware(logger);
const t = new FakeLoggingWinston({});
if (env === GCPEnv.CLOUD_RUN) {
// Cloud Run needs explicit set skipParentEntryForCloudRun flag to enable this behavior until we can make breaking change in next major version
await makeMiddleware(logger, t, /*skipParentEntryForCloudRun=*/ true);
} else {
await makeMiddleware(logger, t);
}
assert.ok(passedOptions);
assert.strictEqual(passedOptions.length, 1);
// emitRequestLog parameter to makeChildLogger should be undefined.
Expand Down

0 comments on commit 7a0ec6f

Please sign in to comment.