From e397d51a16ffe6c428600f6aa7ae09c818e71fc3 Mon Sep 17 00:00:00 2001 From: StevenPJ Date: Thu, 23 Nov 2023 15:26:04 +0000 Subject: [PATCH] feat: add prometeus metrics --- index.js | 9 +++++- lib/metrics.js | 7 +++++ lib/plugins/branches.js | 7 +++++ lib/plugins/repository.js | 14 +++++++++ lib/plugins/validator.js | 6 ++++ lib/settings.js | 14 +++++++++ package-lock.json | 65 +++++++++++++++++++++++++++++++++++++++ package.json | 1 + 8 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 lib/metrics.js diff --git a/index.js b/index.js index dc1f82bc..2e7fb29b 100644 --- a/index.js +++ b/index.js @@ -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() diff --git a/lib/metrics.js b/lib/metrics.js new file mode 100644 index 00000000..75e772c3 --- /dev/null +++ b/lib/metrics.js @@ -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'], +}); diff --git a/lib/plugins/branches.js b/lib/plugins/branches.js index 2d257e44..015c0c39 100644 --- a/lib/plugins/branches.js +++ b/lib/plugins/branches.js @@ -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' } @@ -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 } diff --git a/lib/plugins/repository.js b/lib/plugins/repository.js index d5194484..1c1dbe77 100644 --- a/lib/plugins/repository.js +++ b/lib/plugins/repository.js @@ -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', @@ -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 @@ -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)}`) } }) @@ -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)}`) } }) diff --git a/lib/plugins/validator.js b/lib/plugins/validator.js index ee16c281..ccb5398a 100644 --- a/lib/plugins/validator.js +++ b/lib/plugins/validator.js @@ -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 @@ -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}`) } } diff --git a/lib/settings.js b/lib/settings.js index 09419c26..a43d41b7 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -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) @@ -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)}`) diff --git a/package-lock.json b/package-lock.json index 0da15970..d4be2fa5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0-rc.26", "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", @@ -1631,6 +1632,17 @@ "resolved": "https://registry.npmjs.org/@octokit/webhooks-types/-/webhooks-types-5.2.0.tgz", "integrity": "sha512-OZhKy1w8/GF4GWtdiJc+o8sloWAHRueGB78FWFLZnueK7EHV9MzDVr4weJZMflJwMK4uuYLzcnJVnAoy3yB35g==" }, + "node_modules/@operate-first/probot-metrics": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@operate-first/probot-metrics/-/probot-metrics-1.0.0.tgz", + "integrity": "sha512-E1Zj6DYbmnsKMNjQzXp0+Ufx3qshum1nOXWznQ65jPhwI/S/qCOAb4d/AGQNXTCY8DarwuudJnFn8fQzOFqXDw==", + "dependencies": { + "prom-client": "^14.0.1" + }, + "peerDependencies": { + "express": "^4.18.1" + } + }, "node_modules/@probot/adapter-aws-lambda-serverless": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/@probot/adapter-aws-lambda-serverless/-/adapter-aws-lambda-serverless-3.0.2.tgz", @@ -2547,6 +2559,11 @@ "node": ">=8" } }, + "node_modules/bintrees": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/bintrees/-/bintrees-1.0.2.tgz", + "integrity": "sha512-VOMgTMwjAaUG580SXn3LacVgjurrbMme7ZZNYGSSV7mmtY6QQRh0Eg3pwIcntQ77DErK1L0NxkbetjcoXzVwKw==" + }, "node_modules/bluebird": { "version": "3.7.2", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", @@ -8403,6 +8420,17 @@ "resolved": "https://registry.npmjs.org/process-warning/-/process-warning-1.0.0.tgz", "integrity": "sha512-du4wfLyj4yCZq1VupnVSZmRsPJsNuxoDQFdCFHLaYiEbFBD7QE0a+I4D7hOxrVnh78QE/YipFAj9lXHiXocV+Q==" }, + "node_modules/prom-client": { + "version": "14.2.0", + "resolved": "https://registry.npmjs.org/prom-client/-/prom-client-14.2.0.tgz", + "integrity": "sha512-sF308EhTenb/pDRPakm+WgiN+VdM/T1RaHj1x+MvAuT8UiQP8JmOEbxVqtkbfR4LrvOg5n7ic01kRBDGXjYikA==", + "dependencies": { + "tdigest": "^0.1.1" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/prompts": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.2.tgz", @@ -9476,6 +9504,14 @@ "node": ">=8.0.0" } }, + "node_modules/tdigest": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/tdigest/-/tdigest-0.1.2.tgz", + "integrity": "sha512-+G0LLgjjo9BZX2MfdvPfH+MKLCrxlXSYec5DaPYP1fe6Iyhf0/fSmJ0bFiZ1F8BT6cGXl2LpltQptzjXKWEkKA==", + "dependencies": { + "bintrees": "1.0.2" + } + }, "node_modules/test-exclude": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-6.0.0.tgz", @@ -11290,6 +11326,14 @@ "resolved": "https://registry.npmjs.org/@octokit/webhooks-types/-/webhooks-types-5.2.0.tgz", "integrity": "sha512-OZhKy1w8/GF4GWtdiJc+o8sloWAHRueGB78FWFLZnueK7EHV9MzDVr4weJZMflJwMK4uuYLzcnJVnAoy3yB35g==" }, + "@operate-first/probot-metrics": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@operate-first/probot-metrics/-/probot-metrics-1.0.0.tgz", + "integrity": "sha512-E1Zj6DYbmnsKMNjQzXp0+Ufx3qshum1nOXWznQ65jPhwI/S/qCOAb4d/AGQNXTCY8DarwuudJnFn8fQzOFqXDw==", + "requires": { + "prom-client": "^14.0.1" + } + }, "@probot/adapter-aws-lambda-serverless": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/@probot/adapter-aws-lambda-serverless/-/adapter-aws-lambda-serverless-3.0.2.tgz", @@ -12061,6 +12105,11 @@ "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", "dev": true }, + "bintrees": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/bintrees/-/bintrees-1.0.2.tgz", + "integrity": "sha512-VOMgTMwjAaUG580SXn3LacVgjurrbMme7ZZNYGSSV7mmtY6QQRh0Eg3pwIcntQ77DErK1L0NxkbetjcoXzVwKw==" + }, "bluebird": { "version": "3.7.2", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", @@ -16401,6 +16450,14 @@ "resolved": "https://registry.npmjs.org/process-warning/-/process-warning-1.0.0.tgz", "integrity": "sha512-du4wfLyj4yCZq1VupnVSZmRsPJsNuxoDQFdCFHLaYiEbFBD7QE0a+I4D7hOxrVnh78QE/YipFAj9lXHiXocV+Q==" }, + "prom-client": { + "version": "14.2.0", + "resolved": "https://registry.npmjs.org/prom-client/-/prom-client-14.2.0.tgz", + "integrity": "sha512-sF308EhTenb/pDRPakm+WgiN+VdM/T1RaHj1x+MvAuT8UiQP8JmOEbxVqtkbfR4LrvOg5n7ic01kRBDGXjYikA==", + "requires": { + "tdigest": "^0.1.1" + } + }, "prompts": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.2.tgz", @@ -17186,6 +17243,14 @@ "wordwrapjs": "^4.0.0" } }, + "tdigest": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/tdigest/-/tdigest-0.1.2.tgz", + "integrity": "sha512-+G0LLgjjo9BZX2MfdvPfH+MKLCrxlXSYec5DaPYP1fe6Iyhf0/fSmJ0bFiZ1F8BT6cGXl2LpltQptzjXKWEkKA==", + "requires": { + "bintrees": "1.0.2" + } + }, "test-exclude": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-6.0.0.tgz", diff --git a/package.json b/package.json index e372ddbc..e4ab54bd 100644 --- a/package.json +++ b/package.json @@ -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",