ds-sfcoe-ailabs
Version:
AI-powered code review tool with static analysis integration for comprehensive code quality assessment.
258 lines (257 loc) • 8.11 kB
JavaScript
import datadogMetrics from 'datadog-metrics';
import { Logger } from '../../utils/observability.js';
/**
* Datadog metrics client for sending custom metrics
* Provides a wrapper around the datadog-metrics library with initialization and error handling
*
* @example
* ```typescript
* // Initialize the metrics client
* const config = { apiKey: 'your-key', prefix: 'app.' };
* DatadogMetrics.initialize(config);
*
* // Send metrics
* DatadogMetrics.increment('requests.count', 1, ['service:api']);
* DatadogMetrics.gauge('memory.usage', 75.5, ['unit:percentage']);
* ```
*/
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
* @example
* ```typescript
* const config = {
* apiKey: 'your-datadog-api-key',
* prefix: 'myapp.',
* host: 'localhost'
* };
* const client = DatadogMetrics.initialize(config);
* ```
*/
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
* @example
* ```typescript
* try {
* const client = DatadogMetrics.getInstance();
* client.increment('custom.metric');
* } catch (error) {
* console.error('Metrics client 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
* @example
* ```typescript
* if (DatadogMetrics.isInitialized()) {
* DatadogMetrics.increment('app.ready');
* }
* ```
*/
static isInitialized() {
return this.metricsClient !== null;
}
/**
* Get current configuration
*
* @returns The current Datadog metrics configuration or null if not set
* @example
* ```typescript
* const config = DatadogMetrics.getConfig();
* if (config) {
* console.log(`Using prefix: ${config.prefix}`);
* }
* ```
*/
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
* @example
* ```typescript
* DatadogMetrics.increment('api.requests'); // Increment by 1
* DatadogMetrics.increment('user.actions', 5, ['action:login']);
* ```
*/
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
* @example
* ```typescript
* DatadogMetrics.decrement('queue.size'); // Decrement by 1
* DatadogMetrics.decrement('active.connections', 3, ['service:db']);
* ```
*/
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
* @example
* ```typescript
* DatadogMetrics.gauge('memory.usage', 75.5, ['unit:percentage']);
* DatadogMetrics.gauge('queue.depth', 42, ['queue:processing']);
* ```
*/
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
* @example
* ```typescript
* DatadogMetrics.histogram('response.time', 250, ['endpoint:users']);
* DatadogMetrics.histogram('file.size', 1024, ['type:image']);
* ```
*/
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
* @example
* ```typescript
* const start = Date.now();
* // ... perform operation ...
* DatadogMetrics.timing('operation.duration', Date.now() - start, ['operation:create']);
* ```
*/
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
* @example
* ```typescript
* DatadogMetrics.distribution('score.review', 8.5, ['reviewer:ai']);
* DatadogMetrics.distribution('latency.network', 125, ['region:us-east']);
* ```
*/
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
* @example
* ```typescript
* DatadogMetrics.set('users.unique', 'user123', ['action:login']);
* DatadogMetrics.set('ips.unique', '192.168.1.1', ['endpoint:api']);
* ```
*/
static set(metric, value, tags = []) {
if (this.metricsClient) {
this.metricsClient.gauge(metric, typeof value === 'string' ? 1 : value, tags);
}
}
/**
* Flush buffered metrics immediately
*
* @example
* ```typescript
* // Send all pending metrics immediately
* DatadogMetrics.flush();
* ```
*/
static flush() {
if (this.metricsClient) {
this.metricsClient.flush();
}
}
/**
* Close the metrics client and clean up resources
*
* @example
* ```typescript
* // Clean shutdown of metrics client
* DatadogMetrics.close();
* ```
*/
static close() {
if (this.metricsClient) {
this.metricsClient.stop();
this.metricsClient = null;
this.config = null;
}
}
}