1 import type IMetricsRequestService from './types/IMetricsRequestService';
2 import type MetricSchema from './types/MetricSchema';
3 import type MetricVersions from './types/MetricVersions';
6 * Custom regex based on the following
7 * - https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
8 * - https://gitlab.protontech.ch/proton/be/json-schema-registry
10 const metricRegexp = /^[a-zA-Z]+(?:_[a-zA-Z0-9]+)*$/;
12 function validateMetricName(name: string) {
13 return metricRegexp.test(name);
16 abstract class Metric<D extends MetricSchema> {
19 private version: MetricVersions;
21 private requestService: IMetricsRequestService;
23 constructor(config: { name: string; version: MetricVersions }, requestService: IMetricsRequestService) {
24 this.name = config.name;
25 this.version = config.version;
26 this.requestService = requestService;
28 if (!validateMetricName(this.name)) {
29 throw new Error(`Invalid metric name ${this.name}`);
33 protected addToRequestQueue(data: D) {
34 this.requestService.report({
36 Version: this.version,
37 Timestamp: Math.round(Date.now() / 1000),
43 export default Metric;