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 metrics for start/end syncs #39

Merged
merged 1 commit into from
Mar 26, 2024
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
82 changes: 78 additions & 4 deletions lib/metrics.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const metrics = require("@operate-first/probot-metrics");

const counter = metrics.useCounter({
const actionCounter = metrics.useCounter({
name: 'num_of_actions_total',
help: 'Total number of actions received',
labelNames: ['repository', 'result', 'action', 'nop'],
Expand All @@ -9,7 +9,7 @@ const counter = metrics.useCounter({
const meteredPlugin = async (plugin, fn) => {
try {
const result = await fn()
counter
actionCounter
.labels({
repository: plugin.repo.repo,
nop: plugin.nop,
Expand All @@ -20,7 +20,7 @@ const meteredPlugin = async (plugin, fn) => {
return result
} catch (e) {
console.dir(e)
counter
actionCounter
.labels({
repository: plugin.repo.repo,
result: "error",
Expand All @@ -32,4 +32,78 @@ const meteredPlugin = async (plugin, fn) => {
}
}

module.exports = {meteredPlugin}
const syncStartCounter = metrics.useCounter({
name: 'sync_start_count',
help: 'Number of sync start events',
labelNames: ['nop', 'type', 'name']
})

const syncEndCounter = metrics.useCounter({
name: 'sync_end_count',
help: 'Number of sync end events',
labelNames: ['nop', 'type', 'status', 'name']
})

/**
* @typedef {"all" | "suborg" | "repo"} SyncType
*/

/**
* Increase sync start counter
*
* @param {boolean} nop Is dry run on PR?
* @param {SyncType} type Type of sync
* @param {string | undefined} name Name of suborg or repo
* @returns
*/
const syncStart = (nop, type, name) => {
if (name) {
syncStartCounter.inc({
nop,
type,
name
})
return
}

syncStartCounter.inc({
nop,
type
})
}

/**
* Increase sync end counter
*
* @param {boolean} nop Is dry run on PR?
* @param {SyncType} type Type of sync
* @param {boolean} hasError At least one error occurred
* @param {boolean} hasException An exception potentially prevented syncing subsequent settings
* @param {string | undefined} name Name of suborg or repo
* @returns
*/
const syncEnd = (nop, type, hasException, hasError, name) => {
const status = hasException ? 'fail' : hasError ? 'error' : 'ok'

if (name) {
syncEndCounter.inc({
nop,
type,
status,
name
})
return
}

syncEndCounter.inc({
nop,
type,
status
})
}

module.exports = {
meteredPlugin,
syncStart,
syncEnd
}
17 changes: 16 additions & 1 deletion lib/settings.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,62 @@

const path = require('path')
const { Eta } = require('eta')
const Glob = require('./glob')
const NopCommand = require('./nopcommand')
const MergeDeep = require('./mergeDeep')
const env = require('./env')
const { meteredPlugin } = require('./metrics')
const { meteredPlugin, syncStart, syncEnd } = require('./metrics')

const CONFIG_PATH = env.CONFIG_PATH
const eta = new Eta({ views: path.join(__dirname) })
const SCOPE = { ORG: 'org', REPO: 'repo' } // Determine if the setting is a org setting or repo setting
class Settings {
static async syncAll (nop, context, repo, config, ref) {
syncStart(nop, 'all')
const settings = new Settings(nop, context, repo, config, ref)
try {
await settings.loadConfigs()
// settings.repoConfigs = await settings.getRepoConfigs()
await settings.updateOrg()
await settings.updateAll()
await settings.handleResults()
syncEnd(nop, 'all', false, settings.hasError())
} catch (error) {
syncEnd(nop, 'all', true, true)
settings.logError(error.message)
await settings.handleResults()
}
}

static async syncSubOrgs (nop, context, suborg, repo, config, ref) {
syncStart(nop, 'suborg', suborg)
const settings = new Settings(nop, context, repo, config, ref, suborg)
try {
await settings.loadConfigs()
await settings.updateAll()
await settings.handleResults()
syncStart(nop, 'suborg', false, settings.hasError(), suborg)
} catch (error) {
syncEnd(nop, 'suborg', true, true, suborg)
settings.logError(error.message)
await settings.handleResults()
}
}

static async sync (nop, context, repo, config, ref) {
syncStart(nop, 'repo', repo?.repo)
const settings = new Settings(nop, context, repo, config, ref)
try {
await settings.loadConfigs(repo)
if (settings.isRestricted(repo.repo)) {
syncEnd(nop, 'repo', false, false, repo?.repo)
return
}
await settings.updateRepos(repo)
await settings.handleResults()
syncEnd(nop, 'repo', false, settings.hasError(), repo?.repo)
} catch (error) {
syncEnd(nop, 'repo', true, true, repo?.repo)
settings.logError(error.message)
await settings.handleResults()
}
Expand Down Expand Up @@ -152,6 +163,10 @@ class Settings {
})
}

hasError () {
return !!this.errors.length
}

async handleResults () {
const { payload } = this.context

Expand Down
Loading