Skip to content
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

feat: add prometeus metrics #4

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ const Glob = require('./lib/glob')
const ConfigManager = require('./lib/configManager')
const NopCommand = require('./lib/nopcommand')
const env = require('./lib/env')
const metrics = require('@operate-first/probot-metrics');

let deploymentConfig
module.exports = (robot, _, Settings = require('./lib/settings')) => {



module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) => {

metrics.exposeMetrics(getRouter(), '/metrics');

async function syncAllSettings (nop, context, repo = context.repo(), ref) {
try {
deploymentConfig = await loadYamlFileSystem()
Expand Down
7 changes: 7 additions & 0 deletions lib/metrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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'],
});
7 changes: 7 additions & 0 deletions lib/plugins/branches.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const NopCommand = require('../nopcommand')
const MergeDeep = require('../mergeDeep')
const metric = require("../metrics");
const ignorableFields = []
const previewHeaders = { accept: 'application/vnd.github.hellcat-preview+json,application/vnd.github.luke-cage-preview+json,application/vnd.github.zzzax-preview+json' }

Expand Down Expand Up @@ -83,6 +84,12 @@ module.exports = class Branches {
this.log.debug(`Adding branch protection ${JSON.stringify(params)}`)
return this.github.repos.updateBranchProtection(params).then(res => this.log(`Branch protection applied successfully ${JSON.stringify(res.url)}`)).catch(e => { this.log.error(`Error applying branch protection ${JSON.stringify(e)}`); return [] })
} else {
metric
.labels({
repository: this.repo.repo,
result: 'error',
})
.inc();
this.log.error(e)
// return
}
Expand Down
14 changes: 14 additions & 0 deletions lib/plugins/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// const EndPoints = require('@octokit/plugin-rest-endpoint-methods')
const NopCommand = require('../nopcommand')
const MergeDeep = require('../mergeDeep')
const metric = require("../metrics");
const ignorableFields = [
'id',
'node_id',
Expand Down Expand Up @@ -56,6 +57,7 @@ module.exports = class Repository {
}

sync () {

const resArray = []
this.log.debug(`Syncing Repo ${this.settings.name}`)
this.settings.name = this.settings.name || this.settings.repo
Expand Down Expand Up @@ -150,6 +152,12 @@ module.exports = class Repository {
}
}
} else {
metric
.labels({
repository: this.repo.repo,
result: 'error',
})
.inc();
this.log.error(` Error ${JSON.stringify(e)}`)
}
})
Expand Down Expand Up @@ -190,6 +198,12 @@ module.exports = class Repository {
return this.github.repos.renameBranch(parms)
}
} else {
metric
.labels({
repository: this.settings.repo,
result: 'error',
})
.inc();
this.log.error(`Error ${JSON.stringify(e)}`)
}
})
Expand Down
6 changes: 6 additions & 0 deletions lib/plugins/validator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const NopCommand = require('../nopcommand')
const metric = require("../metrics");
module.exports = class Validator {
constructor (nop, github, repo, settings, log) {
this.github = github
Expand Down Expand Up @@ -59,6 +60,11 @@ module.exports = class Validator {
})
.catch(() => {})
} catch (error) {
metric
.labels({
repository: this.repo.repo,
result: 'error',
})
this.log(`Error in Validation checking ${error}`)
}
}
Expand Down
14 changes: 14 additions & 0 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ const Glob = require('./glob')
const NopCommand = require('./nopcommand')
const MergeDeep = require('./mergeDeep')
const env = require('./env')
const metric = require("./metrics");
const CONFIG_PATH = env.CONFIG_PATH


class Settings {
static async syncAll (nop, context, repo, config, ref) {
const settings = new Settings(nop, context, repo, config, ref)
Expand Down Expand Up @@ -261,9 +263,21 @@ ${stripAllWhitespace(JSON.stringify(quietResults))}
return new Plugin(this.nop, this.github, repo, config, this.log).sync()
}))
}).then(res => {
metric
.labels({
repository: repo.repo,
result: 'success',
})
.inc();
this.appendToResults(res)
})
} catch (e) {
metric
.labels({
repository: this.repo.repo,
result: 'error',
})
.inc();
if (this.nop) {
const nopcommand = new NopCommand(this.constructor.name, this.repo, null, e, 'ERROR')
this.log.error(`NOPCOMMAND ${JSON.stringify(nopcommand)}`)
Expand Down
65 changes: 65 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"author": "Yadhav Jayaraman",
"license": "ISC",
"dependencies": {
"@operate-first/probot-metrics": "^1.0.0",
"@probot/adapter-aws-lambda-serverless": "^3.0.2",
"deepmerge": "^4.3.1",
"eta": "^2.0.1",
Expand Down