MCPcopy
hub / github.com/FlowiseAI/Flowise / OpenTelemetry

Class OpenTelemetry

packages/server/src/metrics/OpenTelemetry.ts:12–257  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10const createdMetrics = new Map<string, boolean>()
11
12export class OpenTelemetry implements IMetricsProvider {
13 private app: express.Application
14 private resource: Resource
15 private otlpMetricExporter: any
16 // private otlpTraceExporter: any
17 // private tracerProvider: NodeTracerProvider
18 private metricReader: PeriodicExportingMetricReader
19 private meterProvider: MeterProvider
20
21 // Map to hold all counters and histograms
22 private counters = new Map<string, Counter | Histogram>()
23 private httpRequestCounter: Counter
24 private httpRequestDuration: any
25
26 constructor(app: express.Application) {
27 this.app = app
28
29 if (!process.env.METRICS_OPEN_TELEMETRY_METRIC_ENDPOINT) {
30 throw new Error('METRICS_OPEN_TELEMETRY_METRIC_ENDPOINT is not defined')
31 }
32
33 if (process.env.METRICS_OPEN_TELEMETRY_DEBUG === 'true') {
34 diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG)
35 }
36
37 // Clear metrics tracking on new instance
38 createdMetrics.clear()
39 }
40
41 public getName(): string {
42 return 'OpenTelemetry'
43 }
44
45 async initializeCounters(): Promise<void> {
46 try {
47 // Define the resource with the service name for trace grouping
48 const flowiseVersion = await getVersion()
49
50 this.resource = new Resource({
51 [ATTR_SERVICE_NAME]: process.env.METRICS_SERVICE_NAME || 'FlowiseAI',
52 [ATTR_SERVICE_VERSION]: flowiseVersion.version // Version as a label
53 })
54
55 const metricProtocol = process.env.METRICS_OPEN_TELEMETRY_PROTOCOL || 'http' // Default to 'http'
56 // Conditionally import the correct OTLP exporters based on protocol
57 let OTLPMetricExporter
58 if (metricProtocol === 'http') {
59 OTLPMetricExporter = require('@opentelemetry/exporter-metrics-otlp-http').OTLPMetricExporter
60 } else if (metricProtocol === 'grpc') {
61 OTLPMetricExporter = require('@opentelemetry/exporter-metrics-otlp-grpc').OTLPMetricExporter
62 } else if (metricProtocol === 'proto') {
63 OTLPMetricExporter = require('@opentelemetry/exporter-metrics-otlp-proto').OTLPMetricExporter
64 } else {
65 console.error('Invalid METRICS_OPEN_TELEMETRY_PROTOCOL specified. Please set it to "http", "grpc", or "proto".')
66 process.exit(1) // Exit if invalid protocol type is specified
67 }
68
69 // Handle any existing metric exporter

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected