create metrics only if not already existing

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2021-08-19 10:49:15 +02:00
parent be87a0e96d
commit 24b6045691
4 changed files with 65 additions and 12 deletions
@@ -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',
}),
@@ -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'`,
),
});
@@ -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<string>();
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() {
@@ -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<T extends string>(
config: CounterConfiguration<T>,
): Counter<T> {
const existing = register.getSingleMetric(config.name) as Counter<T>;
return existing || new Counter<T>(config);
}
export function createGaugeMetric<T extends string>(
config: GaugeConfiguration<T>,
): Gauge<T> {
const existing = register.getSingleMetric(config.name) as Gauge<T>;
return existing || new Gauge<T>(config);
}
export function createSummaryMetric<T extends string>(
config: SummaryConfiguration<T>,
): Summary<T> {
const existing = register.getSingleMetric(config.name) as Summary<T>;
return existing || new Summary<T>(config);
}
export function createHistogramMetric<T extends string>(
config: HistogramConfiguration<T>,
): Histogram<T> {
const existing = register.getSingleMetric(config.name) as Histogram<T>;
return existing || new Histogram<T>(config);
}