@mdf.js/service-registry
Version:
MMS - API - Service Registry
101 lines • 4.37 kB
TypeScript
/**
* Copyright 2024 Mytra Control S.L. All rights reserved.
*
* Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
* or at https://opensource.org/licenses/MIT.
*/
import { Health, Layer } from '@mdf.js/core';
import EventEmitter from 'events';
import express from 'express';
import { Metric, MetricObjectWithValues, MetricValue, Registry } from 'prom-client';
import * as Metrics from './types';
import { MetricsRegistryOptions } from './types';
/**
* MetricsFacade class serves as a facade to simplify metrics management across all services
* in an application. It leverages the prom-client library for metrics management and the express
* library to expose these metrics via an HTTP endpoint.
*
* It accommodates working in cluster environments by optionally creating a new AggregatorRegistry
* to hold metrics from all worker nodes, in addition to a separate registry for
* application-specific metrics and the default prom-client registry. Thus, it can manage up to
* three different registries:
*
* 1. Default prom-client registry for default metrics.
* 2. Application-specific metrics registry.
* 3. Cluster-wide metrics registry for environments running in a cluster.
*
* Depending on the environment and the node type (primary or worker), it responds to metric
* requests by merging and presenting metrics from appropriate registries.
*/
export declare class MetricsFacade extends EventEmitter implements Layer.App.Service {
private readonly options;
/** Debug logger for development and deep troubleshooting */
private readonly logger;
/** Metrics aggregator */
private readonly aggregator;
/** Metrics aggregator registry */
private readonly port;
/** Metrics router */
private readonly _router;
/**
* Create an instance of metrics manager
* @param options - Configuration options for the metrics manager
*/
constructor(options: MetricsRegistryOptions);
/**
* Conditionally creates an AggregatorRegistry if the service is running in a cluster.
* @param isCluster - Boolean indicating if the service is running in cluster mode.
* @returns An AggregatorRegistry instance or undefined if not in cluster mode.
*/
private getPort;
/** @returns The application name */
get name(): string;
/** @returns The application identifier */
get componentId(): string;
/** @returns An Express router with access to metrics information */
get router(): express.Router;
/** @returns Links offered by this service */
get links(): {
[link: string]: string;
};
/** @returns The health status of the component */
get status(): Health.Status;
/** @returns Health checks for this service */
get checks(): Health.Checks;
/**
* Registers one or multiple services to be monitored.
* @param services - A single service or an array of services to be registered.
*/
register(services: Layer.App.Service | Layer.App.Service[]): void;
/** @returns The registry used by the aggregator */
get registry(): Registry;
/** @returns Metrics in text/plain format */
metricsText(): Promise<Metrics.Response>;
/** @returns Metrics in JSON format */
metricsJSON(): Promise<Metrics.Response>;
/**
* Retrieves a single metric by name.
* @param name - The name of the metric.
* @returns The metric or undefined if not found.
*/
getMetric(name: string): Metric | undefined;
/**
* Retrieves a single metric value in Prometheus format.
* @param name - The name of the metric.
* @returns A promise resolved with the metric value as a string.
*/
getMetricAsString(name: string): Promise<string>;
/**
* Retrieves a single metric value in JSON format.
* @param name - The name of the metric.
* @returns A promise resolved with the metric object or undefined if not found.
*/
getMetricAsJSON(name: string): Promise<MetricObjectWithValues<MetricValue<string>> | undefined>;
/** Placeholder for starting the metrics service. */
start(): Promise<void>;
/** Placeholder for stopping the metrics service. */
stop(): Promise<void>;
/** Clears all metrics from the registry. */
close(): Promise<void>;
}
//# sourceMappingURL=MetricsFacade.d.ts.map