diff --git a/packages/backend-tasks/package.json b/packages/backend-tasks/package.json index 221e1f3a69..197caae774 100644 --- a/packages/backend-tasks/package.json +++ b/packages/backend-tasks/package.json @@ -36,12 +36,12 @@ "@backstage/config": "workspace:^", "@backstage/errors": "workspace:^", "@backstage/types": "workspace:^", + "@opentelemetry/api": "^1.3.0", "@types/luxon": "^3.0.0", "cron": "^2.0.0", "knex": "^2.0.0", "lodash": "^4.17.21", "luxon": "^3.0.0", - "prom-client": "^14.0.1", "uuid": "^8.0.0", "winston": "^3.2.1", "zod": "^3.21.4" diff --git a/packages/backend-tasks/src/tasks/TaskWorker.ts b/packages/backend-tasks/src/tasks/TaskWorker.ts index c83bccb197..a2ed75fef5 100644 --- a/packages/backend-tasks/src/tasks/TaskWorker.ts +++ b/packages/backend-tasks/src/tasks/TaskWorker.ts @@ -18,13 +18,12 @@ import { ConflictError, NotFoundError } from '@backstage/errors'; import { CronTime } from 'cron'; import { Knex } from 'knex'; import { DateTime, Duration } from 'luxon'; -import { Counter, Summary } from 'prom-client'; import { v4 as uuid } from 'uuid'; import { Logger } from 'winston'; import { DbTasksRow, DB_TASKS_TABLE } from '../database/tables'; -import { createCounterMetric, createSummaryMetric } from './metrics'; import { TaskFunction, TaskSettingsV2, taskSettingsV2Schema } from './types'; import { delegateAbortController, nowPlus, sleep } from './util'; +import { metrics, Counter, Histogram } from '@opentelemetry/api'; const DEFAULT_WORK_CHECK_FREQUENCY = Duration.fromObject({ seconds: 5 }); @@ -35,7 +34,7 @@ const DEFAULT_WORK_CHECK_FREQUENCY = Duration.fromObject({ seconds: 5 }); */ export class TaskWorker { private readonly counter: Counter; - private readonly duration: Summary; + private readonly duration: Histogram; constructor( private readonly taskId: string, @@ -44,15 +43,13 @@ export class TaskWorker { private readonly logger: Logger, private readonly workCheckFrequency: Duration = DEFAULT_WORK_CHECK_FREQUENCY, ) { - this.counter = createCounterMetric({ - name: 'backstage_task_runs_total', - help: 'Total number of times a task has been run', - labelNames: ['task_id', 'result'], + const meter = metrics.getMeter('default'); + this.counter = meter.createCounter('backend_task_runs_total', { + description: 'Total number of times a task has been run', }); - this.duration = createSummaryMetric({ - name: 'backstage_task_run_duration_seconds', - help: 'Duration of task runs in seconds', - labelNames: ['task_id', 'result'], + this.duration = meter.createHistogram('backend_task_run_duration', { + description: 'Histogram of task run durations', + unit: 'seconds', }); } @@ -66,7 +63,7 @@ export class TaskWorker { this.logger.info( `Task worker starting: ${this.taskId}, ${JSON.stringify(settings)}`, ); - this.counter.inc({ task_id: this.taskId, result: 'started' }, 1); + this.counter.add(1, { task_id: this.taskId, result: 'started' }); let attemptNum = 1; (async () => { @@ -80,11 +77,13 @@ export class TaskWorker { } while (!options?.signal?.aborted) { - const endDuration = this.duration.startTimer(); + const startTime = process.hrtime(); const runResult = await this.runOnce(options?.signal); - endDuration({ task_id: this.taskId, result: 'completed' }); + const delta = process.hrtime(startTime); + const endTime = delta[0] + delta[1] / 1e9; - this.counter.inc({ task_id: this.taskId, result: 'completed' }, 1); + this.duration.record(endTime, { task_id: this.taskId }); + this.counter.add(1, { task_id: this.taskId, result: 'completed' }); if (runResult.result === 'abort') { break; @@ -94,7 +93,6 @@ export class TaskWorker { } this.logger.info(`Task worker finished: ${this.taskId}`); - attemptNum = 0; break; } catch (e) { @@ -102,7 +100,7 @@ export class TaskWorker { this.logger.warn( `Task worker failed unexpectedly, attempt number ${attemptNum}, ${e}`, ); - this.counter.inc({ task_id: this.taskId, result: 'failed' }, 1); + this.counter.add(1, { task_id: this.taskId, result: 'failed' }); await sleep(Duration.fromObject({ seconds: 1 })); } } diff --git a/yarn.lock b/yarn.lock index fee4c29a3a..79fa81b6f3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3622,13 +3622,13 @@ __metadata: "@backstage/config": "workspace:^" "@backstage/errors": "workspace:^" "@backstage/types": "workspace:^" + "@opentelemetry/api": ^1.3.0 "@types/cron": ^2.0.0 "@types/luxon": ^3.0.0 cron: ^2.0.0 knex: ^2.0.0 lodash: ^4.17.21 luxon: ^3.0.0 - prom-client: ^14.0.1 uuid: ^8.0.0 wait-for-expect: ^3.0.2 winston: ^3.2.1