UNPKG

sfcoe-ailabs

Version:

AI-powered code review tool with static analysis integration for comprehensive code quality assessment.

167 lines (166 loc) 5.22 kB
import datadogMetrics from 'datadog-metrics'; import { Logger } from '../../utils/observability.js'; /** * Datadog metrics client for sending custom metrics */ export class DatadogMetrics { static metricsClient = null; static config = null; /** * Initialize the Datadog metrics client with configuration * * @param config - The Datadog metrics configuration object * @returns The initialized metrics client instance */ static initialize(config) { if (this.metricsClient) { return this.metricsClient; } this.config = config; try { // Initialize the datadog-metrics with configuration datadogMetrics.init({ apiKey: config.apiKey, prefix: config.prefix, host: config.host, }); this.metricsClient = datadogMetrics; Logger.debug('datadog-metrics successfuly initialised'); } catch (error) { Logger.warn('Metrics will be disabled since datadog-metrics client failed to load with error: ', error); this.metricsClient = null; } return this.metricsClient; } /** * Get the configured metrics client instance * * @returns The metrics client instance * @throws Error if metrics client is not initialized */ static getInstance() { if (!this.metricsClient) { throw new Error('DatadogMetrics not initialized. Call initialize() first.'); } return this.metricsClient; } /** * Check if metrics client is initialized * * @returns True if the metrics client is initialized, false otherwise */ static isInitialized() { return this.metricsClient !== null; } /** * Get current configuration * * @returns The current Datadog metrics configuration or null if not set */ static getConfig() { return this.config; } /** * Send a counter metric * * @param metric - The metric name to increment * @param value - The value to increment by (default: 1) * @param tags - Array of tags to attach to the metric */ static increment(metric, value = 1, tags = []) { if (this.metricsClient) { this.metricsClient.increment(metric, value, tags); } } /** * Send a decrement metric * * @param metric - The metric name to decrement * @param value - The value to decrement by (default: 1) * @param tags - Array of tags to attach to the metric */ static decrement(metric, value = 1, tags = []) { if (this.metricsClient) { this.metricsClient.increment(metric, -value, tags); } } /** * Send a gauge metric * * @param metric - The metric name to set a gauge value for * @param value - The gauge value to set * @param tags - Array of tags to attach to the metric */ static gauge(metric, value, tags = []) { if (this.metricsClient) { this.metricsClient.gauge(metric, value, tags); } } /** * Send a histogram metric * * @param metric - The metric name to send histogram data for * @param value - The histogram value to record * @param tags - Array of tags to attach to the metric */ static histogram(metric, value, tags = []) { if (this.metricsClient) { this.metricsClient.histogram(metric, value, tags); } } /** * Send a timing metric (histogram with time unit) * * @param metric - The metric name to send timing data for * @param value - The timing value in milliseconds * @param tags - Array of tags to attach to the metric */ static timing(metric, value, tags = []) { if (this.metricsClient) { this.metricsClient.histogram(metric, value, tags); } } /** * Send a distribution metric * * @param metric - The metric name to send distribution data for * @param value - The distribution value to record * @param tags - Array of tags to attach to the metric */ static distribution(metric, value, tags = []) { if (this.metricsClient) { this.metricsClient.distribution(metric, value, tags); } } /** * Send a set metric - not directly supported, use gauge instead * * @param metric - The metric name to set a value for * @param value - The value to set (string or number) * @param tags - Array of tags to attach to the metric */ static set(metric, value, tags = []) { if (this.metricsClient) { this.metricsClient.gauge(metric, typeof value === 'string' ? 1 : value, tags); } } /** * Flush buffered metrics immediately */ static flush() { if (this.metricsClient) { this.metricsClient.flush(); } } /** * Close the metrics client */ static close() { if (this.metricsClient) { this.metricsClient.stop(); this.metricsClient = null; this.config = null; } } }