| 4 | import { getVersion } from 'flowise-components' |
| 5 | |
| 6 | export class Prometheus implements IMetricsProvider { |
| 7 | private app: express.Application |
| 8 | private readonly register: Registry |
| 9 | private counters: Map<string, promClient.Counter<string> | promClient.Gauge<string> | promClient.Histogram<string>> |
| 10 | private requestCounter: Counter<string> |
| 11 | private httpRequestDurationMicroseconds: Histogram<string> |
| 12 | |
| 13 | constructor(app: express.Application) { |
| 14 | this.app = app |
| 15 | // Clear any existing default registry metrics to avoid conflicts |
| 16 | promClient.register.clear() |
| 17 | // Create a separate registry for our metrics |
| 18 | this.register = new promClient.Registry() |
| 19 | } |
| 20 | |
| 21 | public getName(): string { |
| 22 | return 'Prometheus' |
| 23 | } |
| 24 | |
| 25 | async initializeCounters(): Promise<void> { |
| 26 | const serviceName: string = process.env.METRICS_SERVICE_NAME || 'FlowiseAI' |
| 27 | this.register.setDefaultLabels({ |
| 28 | app: serviceName |
| 29 | }) |
| 30 | |
| 31 | // look at the FLOWISE_COUNTER enum in Interface.Metrics.ts and get all values |
| 32 | // for each counter in the enum, create a new promClient.Counter and add it to the registry |
| 33 | this.counters = new Map<string, promClient.Counter<string> | promClient.Gauge<string> | promClient.Histogram<string>>() |
| 34 | const enumEntries = Object.entries(FLOWISE_METRIC_COUNTERS) |
| 35 | enumEntries.forEach(([name, value]) => { |
| 36 | // derive proper counter name from the enum value (chatflow_created = Chatflow Created) |
| 37 | const properCounterName: string = name.replace(/_/g, ' ').replace(/\b\w/g, (l) => l.toUpperCase()) |
| 38 | try { |
| 39 | this.counters.set( |
| 40 | value, |
| 41 | new promClient.Counter({ |
| 42 | name: value, |
| 43 | help: `Total number of ${properCounterName}`, |
| 44 | labelNames: ['status'], |
| 45 | registers: [this.register] // Explicitly set the registry |
| 46 | }) |
| 47 | ) |
| 48 | } catch (error) { |
| 49 | // If metric already exists, get it from the registry instead |
| 50 | const existingMetrics = this.register.getSingleMetric(value) |
| 51 | if (existingMetrics) { |
| 52 | this.counters.set(value, existingMetrics as promClient.Counter<string>) |
| 53 | } |
| 54 | } |
| 55 | }) |
| 56 | |
| 57 | // in addition to the enum counters, add a few more custom counters |
| 58 | // version, http_request_duration_ms, http_requests_total |
| 59 | try { |
| 60 | const versionGaugeCounter = new promClient.Gauge({ |
| 61 | name: 'flowise_version_info', |
| 62 | help: 'Flowise version info.', |
| 63 | labelNames: ['version'], |
nothing calls this directly
no outgoing calls
no test coverage detected