diff --git a/.changeset/breezy-dryers-thank.md b/.changeset/breezy-dryers-thank.md index 22be92f530..317d2316b1 100644 --- a/.changeset/breezy-dryers-thank.md +++ b/.changeset/breezy-dryers-thank.md @@ -2,4 +2,9 @@ '@backstage/backend-tasks': patch --- -Instrument `backend-tasks` with some counters and histograms for duration +Instrument `backend-tasks` with some counters and histograms for duration. + +`backend_task_runs_total`: Counter with the total number of times a task has been run. +`backend_task_run_duration`: Histogram with the run durations for each task. + +Both these metrics have come with `result` `taskId` and `scope` labels for finer grained grouping andd diff --git a/packages/backend-tasks/src/tasks/PluginTaskSchedulerImpl.ts b/packages/backend-tasks/src/tasks/PluginTaskSchedulerImpl.ts index 83acc9e972..9971d47ab6 100644 --- a/packages/backend-tasks/src/tasks/PluginTaskSchedulerImpl.ts +++ b/packages/backend-tasks/src/tasks/PluginTaskSchedulerImpl.ts @@ -30,6 +30,7 @@ import { import { validateId } from './util'; import { TaskFunction } from './types'; import { metrics, Counter, Histogram } from '@opentelemetry/api'; + /** * Implements the actual task management. */ @@ -122,20 +123,24 @@ export class PluginTaskSchedulerImpl implements PluginTaskScheduler { opts: { labels: Record }, ): TaskFunction { return async abort => { - this.counter.add(1, { ...opts.labels, result: 'started' }); + const labels = { + ...opts.labels, + }; + this.counter.add(1, { ...labels, result: 'started' }); const startTime = process.hrtime(); try { await fn(abort); - this.counter.add(1, { ...opts.labels, result: 'completed' }); + labels.result = 'completed'; } catch (ex) { - this.counter.add(1, { ...opts.labels, result: 'failed' }); + labels.result = 'failed'; throw ex; } finally { const delta = process.hrtime(startTime); const endTime = delta[0] + delta[1] / 1e9; - this.duration.record(endTime, { ...opts.labels }); + this.counter.add(1, labels); + this.duration.record(endTime, labels); } }; }