Skip to content

Commit 58fa323

Browse files
authored
fix: skip writing RequestLogEntry for Cloud Run with flag enabled (#821)
* Fix to skip writing RequestLogEntry for Cloud Run * Parse transport to middleware constructor
1 parent aab7d78 commit 58fa323

4 files changed

Lines changed: 26 additions & 6 deletions

File tree

packages/nodejs-logging-winston/.readme-partials.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ body: |-
226226
would be picked up by the Cloud Logging Agent running in Google Cloud managed environment.
227227
Note that there is also a `useMessageField` option which controls if "message" field is used to store
228228
structured, non-text data inside `jsonPayload` field when `redirectToStdout` is set. By default `useMessageField` is always `true`.
229+
Set the `skipParentEntryForCloudRun` option to skip creating an entry for the request itself as Cloud Run already automatically creates
230+
such log entries. This might become the default behaviour in a next major version.
229231
230232
```js
231233
// Imports the Google Cloud client library for Winston

packages/nodejs-logging-winston/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ decrease logging record loss upon execution termination - since all logs are wri
303303
would be picked up by the Cloud Logging Agent running in Google Cloud managed environment.
304304
Note that there is also a `useMessageField` option which controls if "message" field is used to store
305305
structured, non-text data inside `jsonPayload` field when `redirectToStdout` is set. By default `useMessageField` is always `true`.
306+
Set the `skipParentEntryForCloudRun` option to skip creating an entry for the request itself as Cloud Run already automatically creates
307+
such log entries. This might become the default behaviour in a next major version.
306308

307309
```js
308310
// Imports the Google Cloud client library for Winston

packages/nodejs-logging-winston/src/middleware/express.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@ type Middleware = ReturnType<typeof commonMiddleware.express.makeMiddleware>;
3535

3636
export async function makeMiddleware(
3737
logger: winston.Logger,
38-
transport: LoggingWinston
38+
transport: LoggingWinston,
39+
skipParentEntryForCloudRun?: boolean
3940
): Promise<Middleware>;
4041
export async function makeMiddleware(
4142
logger: winston.Logger,
42-
options?: Options
43+
options?: Options,
44+
skipParentEntryForCloudRun?: boolean
4345
): Promise<Middleware>;
4446
export async function makeMiddleware(
4547
logger: winston.Logger,
46-
optionsOrTransport?: Options | LoggingWinston
48+
optionsOrTransport?: Options | LoggingWinston,
49+
skipParentEntryForCloudRun?: boolean
4750
): Promise<Middleware> {
4851
let transport: LoggingWinston;
4952

@@ -79,8 +82,15 @@ export async function makeMiddleware(
7982
// parent request log entry that all the request specific logs ("app logs")
8083
// will nest under. GAE and GCF generate the parent request logs
8184
// automatically.
85+
// Cloud Run also generates the parent request log automatically, but skipping
86+
// the parent request entry has to be explicity enabled until the next major
87+
// release in which we can change the default behavior.
8288
let emitRequestLogEntry;
83-
if (env !== GCPEnv.APP_ENGINE && env !== GCPEnv.CLOUD_FUNCTIONS) {
89+
if (
90+
env !== GCPEnv.APP_ENGINE &&
91+
env !== GCPEnv.CLOUD_FUNCTIONS &&
92+
(env !== GCPEnv.CLOUD_RUN || !skipParentEntryForCloudRun)
93+
) {
8494
const requestLogName = Log.formatName_(
8595
projectId,
8696
`${transport.common.logName}${REQUEST_LOG_SUFFIX}`

packages/nodejs-logging-winston/test/middleware/express.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,16 @@ describe('middleware/express', () => {
144144
assert.strictEqual(passedProjectId, FAKE_PROJECT_ID);
145145
});
146146

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

0 commit comments

Comments
 (0)