Skip to content

metrics

awekrx edited this page May 29, 2026 · 1 revision

metrics

Import

import { metrics } from '@dev-suite/decorators/metrics'

Category

  • method

Use Case

Capture method duration and success/failure for observability.

Replaces

  • Manual timer and reporter calls in each method
  • Inconsistent metric naming conventions

Example 1

Without decorator

class ImportService {
  async run(jobId: string) {
    const start = performance.now();
    try {
      const result = await this.worker.run(jobId);
      report({ name: 'import.run', ok: true, ms: performance.now() - start });
      return result;
    } catch (error) {
      report({ name: 'import.run', ok: false, ms: performance.now() - start, error });
      throw error;
    }
  }
}

With decorator

import { metrics } from '@dev-suite/decorators/metrics';

class ImportService {
  @metrics({ reporter: report })
  async run(jobId: string) {
    return this.worker.run(jobId);
  }
}

Why better

  • Metrics contract is reused and less error-prone.
  • Method body stays domain-oriented.

Example 2

Without decorator

class PdfService {
  async render(id: string) {
    const started = Date.now();
    const result = await this.engine.render(id);
    statsd.timing('pdf.render.ms', Date.now() - started);
    return result;
  }
}

With decorator

import { metrics } from '@dev-suite/decorators/metrics';

class PdfService {
  @metrics({ reporter: ({ durationMs }) => statsd.timing('pdf.render.ms', durationMs) })
  async render(id: string) {
    return this.engine.render(id);
  }
}

Why better

  • Reporter can map to existing telemetry backend cleanly.
  • No per-method timing boilerplate.

Clone this wiki locally