Skip to content

Metric-Based Logging — Generating Business Metrics from Logs

DodaTech Updated 2026-06-28 1 min read

In this tutorial, you'll learn about Metric Based Logging. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Logs contain valuable business and operational data that can be extracted and aggregated into metrics.

// Log-based metric extraction
class LogMetricExtractor {
  constructor() {
    this.extractors = {
      'scan.completed': (entry) => ({
        metric: 'scans_completed',
        value: 1,
        tags: { scanType: entry.context?.scanType, status: entry.context?.status }
      }),
      'scan.duration': (entry) => ({
        metric: 'scan_duration_ms',
        value: entry.context?.duration,
        tags: { scanType: entry.context?.scanType }
      }),
      'file.uploaded': (entry) => ({
        metric: 'file_size_bytes',
        value: entry.context?.fileSize,
        tags: { fileType: entry.context?.fileType }
      }),
      'user.login': (entry) => ({
        metric: 'user_logins',
        value: 1,
        tags: { method: entry.context?.authMethod, result: entry.result }
      })
    };
  }

  async processLogEntry(entry) {
    const extractor = this.extractors[entry.type];
    if (!extractor) return;

    const metric = extractor(entry);
    await this.emitMetric(metric);
  }

  async emitMetric(metric) {
    // Emit to Prometheus
    prometheusClient.histogram(metric.metric, metric.value, metric.tags);

    // Emit to StatsD
    statsdClient.increment(metric.metric, metric.tags);
  }

  async generateBusinessReport(period) {
    const report = {
      totalScans: await this.queryMetric('scans_completed', period),
      averageDuration: await this.queryMetric('scan_duration_ms', period),
      topFileTypes: await this.queryTopTags('file_size_bytes', 'fileType', period),
      loginSuccessRate: await this.calculateSuccessRate('user_logins', period)
    };
    return report;
  }
}

// Dashboard queries
app.get('/metrics/business', async (req, res) => {
  const period = req.query.period || '24h';
  const report = await metricExtractor.generateBusinessReport(period);
  res.json(report);
});

Metric extraction from logs converts operational data into actionable business intelligence.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro