-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a logger for sending logs to GCP (#636)
## Summary: Created a logger that will be used throughout the release process in order to create an audit log trail in GCP. Issue: FEI-5057 ## Test plan: How the console log looks like <img width="786" alt="image" src="https://user-images.githubusercontent.com/34088613/231329939-0ff70c1e-a57b-43ce-860e-7d4c280b689d.png"> How logs look in GCP (we might want to increase our log retention in GCP from 30 days to something higher?) <img width="1019" alt="image" src="https://user-images.githubusercontent.com/34088613/231330145-765a78a6-1ab9-4095-bcda-91708cc2b6b8.png"> Author: jlauzy Reviewers: jlauzy, jeresig, somewhatabstract, kevinbarabash Required Reviewers: Approved By: jeresig Checks: ✅ codecov/project, ✅ Test (macos-latest, 16.x), ✅ CodeQL, ✅ Lint, typecheck, and coverage check (ubuntu-latest, 16.x), ⏭ dependabot, ✅ Analyze (javascript), ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 16.x), ✅ gerald Pull Request URL: #636
- Loading branch information
Showing
10 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@khanacademy/wonder-stuff-server": patch | ||
"@khanacademy/wonder-stuff-ci": patch | ||
--- | ||
|
||
Added GCP logger for mobile release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/wonder-stuff-ci/src/__tests__/get-gcp-log-transport.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {getGCPLogTransport} from "../get-gcp-log-transport"; | ||
|
||
describe("#getGCPLogTransport", () => { | ||
it("return the mobile release logger", () => { | ||
// Act | ||
const result = getGCPLogTransport({ | ||
projectId: "mobile-365917", | ||
logName: "release-raccoon", | ||
level: "info", | ||
redirectToStdout: true, | ||
labels: {}, | ||
}); | ||
|
||
// Assert | ||
expect(result).toBeTruthy(); | ||
expect(result.level).toStrictEqual("info"); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/wonder-stuff-ci/src/__tests__/get-mobile-release-logger.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {getMobileReleaseLogger} from "../get-mobile-release-logger"; | ||
|
||
describe("#getMobileReleaseLogger", () => { | ||
it("return the mobile release logger", () => { | ||
// Act | ||
const result = getMobileReleaseLogger(); | ||
|
||
// Assert | ||
expect(result).toBeTruthy(); | ||
expect(result.level).toStrictEqual("info"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as winston from "winston"; | ||
import * as lw from "@google-cloud/logging-winston"; | ||
import type {GCPTransportOptions} from "./types"; | ||
|
||
/** | ||
* Winston logging transport for emitting logs to GCP | ||
* @param {GCPTransportOptions} options for the transport | ||
* @returns {winston.transport} logger set up to send logs to GCP. | ||
*/ | ||
/* istanbul ignore file */ | ||
export const getGCPLogTransport = ( | ||
options: GCPTransportOptions, | ||
): winston.transport => { | ||
const transport = new lw.LoggingWinston({ | ||
projectId: options.projectId, | ||
logName: options.logName, | ||
level: options.level, | ||
resource: { | ||
labels: options.labels, | ||
}, | ||
defaultCallback: (err) => { | ||
if (err) { | ||
// eslint-disable-next-line no-console | ||
console.log("Error occurred while sending log to GCP: " + err); | ||
} | ||
}, | ||
redirectToStdout: options.redirectToStdout, | ||
}); | ||
|
||
return transport; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { | ||
createLogger, | ||
Runtime, | ||
setRootLogger, | ||
} from "@khanacademy/wonder-stuff-server"; | ||
|
||
import * as winston from "winston"; | ||
|
||
import {getGCPLogTransport} from "./get-gcp-log-transport"; | ||
|
||
let logger: winston.Logger | null = null; | ||
|
||
/** | ||
* Logger for auditing mobile release events. | ||
* @param {boolean} redirectToStdout If true, logs will be written to stdout. | ||
* @param {{[key: string]: string}} labels K/V metadata that will be add to the logs | ||
*/ | ||
export const getMobileReleaseLogger = ( | ||
{ | ||
redirectToStdout, | ||
labels, | ||
}: { | ||
redirectToStdout: boolean; | ||
labels: {[key: string]: string}; | ||
} = { | ||
redirectToStdout: false, | ||
labels: {}, | ||
}, | ||
): winston.Logger => { | ||
if (!logger) { | ||
logger = createLogger({ | ||
mode: Runtime.Production, | ||
level: "info", | ||
defaultMetadata: {}, | ||
transport: getGCPLogTransport({ | ||
// Hardcoded so that the client does not need to know these values | ||
projectId: "mobile-365917", | ||
logName: "release-raccoon", | ||
level: "info", | ||
redirectToStdout: redirectToStdout, | ||
labels: {}, | ||
}), | ||
}); | ||
setRootLogger(logger); | ||
} | ||
return logger; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ | |
"rootDir": "src", | ||
}, | ||
"references": [ | ||
{"path": "../wonder-stuff-server"} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters