forked from github/safe-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wrap github api updates with metric counter (#5)
the plugins/settings tend to all handle updates different, sometimes catching errors using the promises, sometimes propagating, sometimes catching with a try/catch block. This lack of consistency makes it very hard to create a clean metric abstraction layer. As an alternative, have created a sort of higher order function for wrapping plugin github api calls with a metric. We can decorate all the github update calls and it will increment a counter based on the call result and propagate any errors up the stack. Not ideal, but cognitively fairly simple, and the changes are just around octokits, which hopefully are unlikely to change (the params might but we dont interfere with that). We pass in the plugins so we can refer to their name and access the repoName that is part of the instance. Not sure how best to test this, as loads of the tests fail and tried fixing them quickly but no luck. I have tested locally with our safe-settings local dev setup in k8s and seen the metrics perform better. Some functionality is only available on orgs so will need some additional testing in sbx
- Loading branch information
Showing
11 changed files
with
84 additions
and
89 deletions.
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 |
---|---|---|
@@ -1,7 +1,33 @@ | ||
const metrics = require("@operate-first/probot-metrics"); | ||
|
||
module.exports = metrics.useCounter({ | ||
name: 'num_of_actions_total', | ||
help: 'Total number of actions received', | ||
labelNames: ['repository', 'result'], | ||
const counter = metrics.useCounter({ | ||
name: 'num_of_actions_total', | ||
help: 'Total number of actions received', | ||
labelNames: ['repository', 'result', 'action'], | ||
}); | ||
|
||
const meteredPlugin = async (plugin, fn) => { | ||
try { | ||
const result = await fn() | ||
counter | ||
.labels({ | ||
repository: plugin.repo.repo, | ||
result: "success", | ||
action: plugin.constructor.name | ||
}) | ||
.inc(); | ||
return result | ||
} catch (e) { | ||
console.dir(e) | ||
counter | ||
.labels({ | ||
repository: plugin.repo.repo, | ||
result: "error", | ||
action: plugin.constructor.name | ||
}) | ||
.inc(); | ||
throw e; | ||
} | ||
} | ||
|
||
module.exports = {meteredPlugin} |
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
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
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
Oops, something went wrong.