Skip to content

Commit

Permalink
feat: changed metrics to be initialized later
Browse files Browse the repository at this point in the history
  • Loading branch information
CptSchnitz committed Oct 26, 2024
1 parent 743a562 commit 7e0db06
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
13 changes: 7 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');

Expand Down Expand Up @@ -102,10 +103,6 @@ export async function config<T extends { [typeSymbol]: unknown; $id: string }>(
// 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<TPath extends string>(path: TPath): GetFieldType<T[typeof typeSymbol], TPath> {
debug('get called with path: %s', path);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
Expand All @@ -131,5 +128,9 @@ export async function config<T extends { [typeSymbol]: unknown; $id: string }>(
return getOptions();
}

return { get, getAll, getConfigParts, getResolvedOptions };
function initializeMetrics(registry: Registry): void {
initializeMetricsInternal(registry, baseSchema.$id, serverConfigResponse?.version);
}

return { get, getAll, getConfigParts, getResolvedOptions, initializeMetrics };
}
24 changes: 13 additions & 11 deletions src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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 */
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { typeSymbol } from '@map-colonies/schemas/build/schemas/symbol';
import { JSONSchemaType } from 'ajv';
import { initial } from 'lodash';

Check warning on line 3 in src/types.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/types.ts#L3

'initial' is defined but never used (@typescript-eslint/no-unused-vars)
import type { Registry } from 'prom-client';

type Prettify<T> = {
Expand Down Expand Up @@ -139,4 +140,10 @@ export interface ConfigInstance<T> {
* @returns The resolved options.
*/
getResolvedOptions: () => BaseOptions;

/**
* Initializes the metrics for the configuration object.
* @param registry - The registry for the metrics.
*/
initializeMetrics: (registry: Registry) => void;
}

0 comments on commit 7e0db06

Please sign in to comment.