-
Notifications
You must be signed in to change notification settings - Fork 11
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
refactor: add sentry dsn rotation logic #466
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8f962ff
refactor: add sentry dsn rotation logic
lsndr eeed3de
refactor: reexport `Sentry` from utils
lsndr 63e05e6
refactor: use shorthand syntax
lsndr 648fd1d
refactor: make `client` readonly
lsndr 0f1a93e
refactor: reorganize code
lsndr 0d053a7
refactor: remove mutation
lsndr 969a520
refactor: extract rotation detection logic
lsndr 2b64e7a
refactor: remove config file existing checks
lsndr ab4e472
refactor: move sentry logic to `wrapWithSentry`
lsndr ee07c4d
refactor: replace `fs.promises` with promisify
lsndr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,135 @@ | ||
import { logger } from '../Utils'; | ||
import request, { RequestPromiseAPI } from 'request-promise'; | ||
import { readFile, writeFile } from 'fs'; | ||
import { join } from 'path'; | ||
import { homedir } from 'os'; | ||
import { promisify } from 'util'; | ||
|
||
export interface SystemConfig { | ||
sentryDsn?: string; | ||
} | ||
|
||
interface SystemConfigFile { | ||
data: SystemConfig; | ||
updatedAt: Date; | ||
} | ||
|
||
export class SystemConfigReader { | ||
private readonly rotationInterval = 3600000; | ||
private readonly path = join(homedir(), '.brightclirc'); | ||
private readonly client: RequestPromiseAPI; | ||
|
||
constructor(baseUrl: string) { | ||
this.client = request.defaults({ | ||
baseUrl, | ||
timeout: 1500, | ||
json: true | ||
}); | ||
} | ||
|
||
public async read(): Promise<SystemConfig> { | ||
await this.rotateIfNecessary(); | ||
const configFile = await this.getConfigFile(); | ||
|
||
return { | ||
sentryDsn: process.env['SENTRY_DSN'], | ||
...configFile.data | ||
}; | ||
} | ||
|
||
private needsRotation(configFile: SystemConfigFile) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
return; | ||
} | ||
|
||
const lifeTime = Date.now() - configFile.updatedAt.getTime(); | ||
|
||
return lifeTime >= this.rotationInterval; | ||
} | ||
|
||
private async rotateIfNecessary() { | ||
logger.debug('Trying to rotate system config'); | ||
|
||
const configFile = await this.getConfigFile(); | ||
|
||
if (!this.needsRotation(configFile)) { | ||
logger.debug( | ||
'Rotation is not needed, last updated on: %s ms', | ||
configFile.updatedAt | ||
); | ||
|
||
return; | ||
} | ||
|
||
logger.debug( | ||
'Rotating system config last updated on: %s ms', | ||
configFile.updatedAt | ||
); | ||
|
||
const newConfig = await this.fetchNewConfig(); | ||
|
||
if (newConfig) { | ||
await this.updateConfigFile({ | ||
data: newConfig, | ||
updatedAt: new Date() | ||
}); | ||
} else { | ||
logger.debug('Rotation failed'); | ||
|
||
await this.updateConfigFile({ | ||
...configFile, | ||
updatedAt: new Date() | ||
}); | ||
} | ||
} | ||
|
||
private defaultConfigFile(): SystemConfigFile { | ||
return { | ||
data: {}, | ||
updatedAt: new Date() | ||
}; | ||
} | ||
|
||
private async getConfigFile() { | ||
const defaultConfigFile = this.defaultConfigFile(); | ||
|
||
try { | ||
logger.debug('Loading system config file'); | ||
|
||
const file = await promisify(readFile)(this.path); | ||
const fileConfig = JSON.parse(file.toString()) as SystemConfigFile; | ||
|
||
return { | ||
...fileConfig, | ||
updatedAt: new Date(fileConfig.updatedAt) | ||
}; | ||
} catch (e) { | ||
logger.debug('Error during loading system config file', e); | ||
logger.debug('Using default system config file'); | ||
|
||
return defaultConfigFile; | ||
} | ||
} | ||
|
||
private async updateConfigFile(configFile: SystemConfigFile) { | ||
logger.debug('Updating system config file'); | ||
|
||
try { | ||
await promisify(writeFile)(this.path, JSON.stringify(configFile)); | ||
} catch (e) { | ||
logger.debug('Error during updating system config file', e); | ||
} | ||
} | ||
|
||
private async fetchNewConfig(): Promise<SystemConfig | undefined> { | ||
logger.debug('Fetching new system config'); | ||
|
||
try { | ||
return await this.client.get({ | ||
uri: '/api/v1/cli/config' | ||
}); | ||
} catch (e) { | ||
logger.debug('Error during fetching new system config: ', e); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,20 +1,3 @@ | ||
import { init } from '@sentry/node'; | ||
import { init, setContext, runWithAsyncContext } from '@sentry/node'; | ||
|
||
export function sentry() { | ||
init({ | ||
attachStacktrace: true, | ||
dsn: process.env.SENTRY_DSN, | ||
release: process.env.VERSION, | ||
beforeSend: (event) => { | ||
if (event.contexts.args) { | ||
event.contexts.args = { | ||
...event.contexts.args, | ||
t: event.contexts.args.t && '[Filtered]', | ||
token: event.contexts.args.token && '[Filtered]' | ||
}; | ||
} | ||
|
||
return event; | ||
} | ||
}); | ||
} | ||
export default { init, setContext, runWithAsyncContext }; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems this is redundant. Should we update the configuration as though the rotation was successful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think yes, just because if we don't do that, it will try to perform rotation on every
bright-cli
call causing at most 1.5 seconds delay.Since rotation logic is not crucial for the cli for now, I think it's ok in case of failure to repeat it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you wish. However, I'm still uncertain about how it is supposed to work for long-running processes like the repeater which is intended to work for months.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's have a separate PR for the background rotation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lsndr please open a ticket in Jira