diff --git a/README.md b/README.md index 042a7e6..4054241 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,10 @@ The `ConfigInstance` interface represents the your way to interact with the conf - **Description**: Retrieves the resolved options from the configuration object. Useful for debugging. - **Returns**: The resolved options, which are an instance of `BaseOptions`. +##### `initializeMetrics(registry: promClient.Registry): void` +- **Description**: Initializes the metrics for the configuration. +- **Parameters**: + - `registry` (`promClient.Registry`): The prometheus registry to use for the metrics. # Configuration Options @@ -114,11 +118,6 @@ This package allows you to configure various options for loading and managing co - **Default**: `./config` - **Description**: The path to the local configuration folder. -### `metricsRegistry` -- **Type**: `promClient.Registry` -- **Optional**: `true` -- **Description**: The prometheus registry to use for metrics. If not provided, metrics are not provided. - ## Environment Variable Configuration The following environment variables can be used to configure the options: diff --git a/src/config.ts b/src/config.ts index 74dc1c0..9c53aba 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2,6 +2,7 @@ import deepmerge from 'deepmerge'; import { typeSymbol } from '@map-colonies/schemas/build/schemas/symbol'; import configPkg from 'config'; import semver from 'semver'; +import type { Registry } from 'prom-client'; import lodash, { type GetFieldType } from 'lodash'; import { getEnvValues } from './env'; import { BaseOptions, ConfigOptions, ConfigInstance, Config } from './types'; @@ -12,7 +13,7 @@ import { ajvConfigValidator, validate } from './validator'; import { createDebug } from './utils/debug'; import { LOCAL_SCHEMAS_PACKAGE_VERSION } from './constants'; import { createConfigError } from './errors'; -import { initializeMetrics } from './metrics'; +import { initializeMetrics as initializeMetricsInternal } from './metrics'; const debug = createDebug('config'); @@ -102,10 +103,6 @@ export async function config( // freeze the merged config so it can't be modified by the package user Object.freeze(validatedConfig); - if (metricsRegistry) { - initializeMetrics(metricsRegistry, baseSchema.$id, serverConfigResponse?.version); - } - function get(path: TPath): GetFieldType { debug('get called with path: %s', path); // eslint-disable-next-line @typescript-eslint/no-unsafe-return @@ -131,5 +128,9 @@ export async function config( return getOptions(); } - return { get, getAll, getConfigParts, getResolvedOptions }; + function initializeMetrics(registry: Registry): void { + initializeMetricsInternal(registry, baseSchema.$id, serverConfigResponse?.version); + } + + return { get, getAll, getConfigParts, getResolvedOptions, initializeMetrics }; } diff --git a/src/metrics.ts b/src/metrics.ts index fb0c45a..c1a4d8c 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -11,16 +11,18 @@ const MILLISECONDS_PER_SECOND = 1000; let promClient: typeof import('prom-client') | undefined; function loadPromClient(): void { - if (promClient === undefined) { - debug('loading prom-client'); - try { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - promClient = require('prom-client'); - } catch (error) { - console.log('error', error); - - throw createConfigError('promClientNotInstalledError', 'prom-client is not installed and metrics was initialized', error as Error); - } + if (promClient !== undefined) { + return; + } + + debug('loading prom-client'); + try { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + promClient = require('prom-client'); + } catch (error) { + console.log('error', error); + + throw createConfigError('promClientNotInstalledError', 'prom-client is not installed and metrics was initialized', error as Error); } } @@ -51,7 +53,7 @@ export function initializeMetrics(registry: Registry, schemaId: string, actualVe schema_id: schemaId, }, - Date.now() / MILLISECONDS_PER_SECOND + Math.round(Date.now() / MILLISECONDS_PER_SECOND) ); /* eslint-enable @typescript-eslint/naming-convention */ diff --git a/src/types.ts b/src/types.ts index dac9dfa..a0a12cc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,6 @@ import { typeSymbol } from '@map-colonies/schemas/build/schemas/symbol'; import { JSONSchemaType } from 'ajv'; +import { initial } from 'lodash'; import type { Registry } from 'prom-client'; type Prettify = { @@ -139,4 +140,10 @@ export interface ConfigInstance { * @returns The resolved options. */ getResolvedOptions: () => BaseOptions; + + /** + * Initializes the metrics for the configuration object. + * @param registry - The registry for the metrics. + */ + initializeMetrics: (registry: Registry) => void; }