diff --git a/plugins/catalog-backend/src/next/DefaultCatalogProcessingEngine.ts b/plugins/catalog-backend/src/next/DefaultCatalogProcessingEngine.ts index c7069960d8..41e81b1777 100644 --- a/plugins/catalog-backend/src/next/DefaultCatalogProcessingEngine.ts +++ b/plugins/catalog-backend/src/next/DefaultCatalogProcessingEngine.ts @@ -25,6 +25,7 @@ import stableStringify from 'fast-json-stable-stringify'; import { DateTime } from 'luxon'; import { Logger } from 'winston'; import { ProcessingDatabase, RefreshStateItem } from './database/types'; +import { createCounterMetric, createSummaryMetric } from './metrics'; import { CatalogProcessingOrchestrator } from './processing/types'; import { Stitcher } from './stitching/Stitcher'; import { startTaskPipeline } from './TaskPipeline'; @@ -34,7 +35,6 @@ import { EntityProviderConnection, EntityProviderMutation, } from './types'; -import { Counter, Summary } from 'prom-client'; class Connection implements EntityProviderConnection { readonly validateEntityEnvelope = entityEnvelopeSchemaValidator(); @@ -87,15 +87,15 @@ class Connection implements EntityProviderConnection { export class DefaultCatalogProcessingEngine implements CatalogProcessingEngine { private stopFunc?: () => void; private readonly metrics = { - processedEntities: new Counter({ + processedEntities: createCounterMetric({ name: 'catalog_processed_entities_count', help: 'Amount of entities processed', }), - processingDuration: new Summary({ + processingDuration: createSummaryMetric({ name: 'catalog_processing_duration_seconds', help: 'Processing duration', }), - processingQueueDelay: new Summary({ + processingQueueDelay: createSummaryMetric({ name: 'catalog_processing_queue_delay_seconds', help: 'The amount of delay between being scheduled for processing, and the start of actually being processed', }), diff --git a/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.ts b/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.ts index faf7197bc5..7326a25a63 100644 --- a/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.ts +++ b/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.ts @@ -480,12 +480,10 @@ export class DefaultProcessingDatabase implements ProcessingDatabase { next_update_at: tx.client.config.client === 'sqlite3' ? tx.raw(`datetime('now', ?)`, [ - `${this.options.refreshIntervalSeconds} seconds`, + `${this.options.refreshInterval()} seconds`, ]) : tx.raw( - `now() + interval '${Number( - this.options.refreshIntervalSeconds, - )} seconds'`, + `now() + interval '${this.options.refreshInterval()} seconds'`, ), }); diff --git a/plugins/catalog-backend/src/next/database/metrics.ts b/plugins/catalog-backend/src/next/database/metrics.ts index c4a5c22ab2..f3e08287d8 100644 --- a/plugins/catalog-backend/src/next/database/metrics.ts +++ b/plugins/catalog-backend/src/next/database/metrics.ts @@ -15,14 +15,14 @@ */ import { Knex } from 'knex'; -import { Gauge } from 'prom-client'; import { DbLocationsRow } from '../../database/types'; +import { createGaugeMetric } from '../metrics'; import { DbRefreshStateRow, DbRelationsRow } from './tables'; export function initDatabaseMetrics(knex: Knex) { const seen = new Set(); return { - entities_count: new Gauge({ + entities_count: createGaugeMetric({ name: 'catalog_entities_count', help: 'Total amount of entities in the catalog', labelNames: ['kind'], @@ -48,7 +48,7 @@ export function initDatabaseMetrics(knex: Knex) { }); }, }), - registered_locations: new Gauge({ + registered_locations: createGaugeMetric({ name: 'catalog_registered_locations_count', help: 'Total amount of registered locations in the catalog', async collect() { @@ -56,7 +56,7 @@ export function initDatabaseMetrics(knex: Knex) { this.set(total[0]['count(*)'] as number); }, }), - relations: new Gauge({ + relations: createGaugeMetric({ name: 'catalog_relations_count', help: 'Total amount of relations between entities', async collect() { diff --git a/plugins/catalog-backend/src/next/metrics.ts b/plugins/catalog-backend/src/next/metrics.ts new file mode 100644 index 0000000000..4988af82ea --- /dev/null +++ b/plugins/catalog-backend/src/next/metrics.ts @@ -0,0 +1,55 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + Counter, + CounterConfiguration, + Gauge, + GaugeConfiguration, + Histogram, + HistogramConfiguration, + register, + Summary, + SummaryConfiguration, +} from 'prom-client'; + +export function createCounterMetric( + config: CounterConfiguration, +): Counter { + const existing = register.getSingleMetric(config.name) as Counter; + return existing || new Counter(config); +} + +export function createGaugeMetric( + config: GaugeConfiguration, +): Gauge { + const existing = register.getSingleMetric(config.name) as Gauge; + return existing || new Gauge(config); +} + +export function createSummaryMetric( + config: SummaryConfiguration, +): Summary { + const existing = register.getSingleMetric(config.name) as Summary; + return existing || new Summary(config); +} + +export function createHistogramMetric( + config: HistogramConfiguration, +): Histogram { + const existing = register.getSingleMetric(config.name) as Histogram; + return existing || new Histogram(config); +}