Bỏ qua, đến nội dung

Cấu hình chi tiết — Telemetry

Biến môi trường

Core

BiếnMặc địnhMô tả
TELEMETRY_ENABLEDfalseBật/tắt toàn bộ hệ thống telemetry
TELEMETRY_SERVICE_NAMEdigiforceTên service hiển thị trong traces và metrics
TELEMETRY_SERVICE_VERSION(tự động)Phiên bản service — nếu không đặt sẽ lấy từ package.json

Traces

BiếnMặc địnhMô tả
TELEMETRY_TRACE_PROCESSORbatchLoại processor: batch (gom rồi gửi) hoặc simple (gửi ngay)
TELEMETRY_TRACE_EXPORTERconsoleLoại exporter: console (in ra log), otlp (gửi đến collector)
TELEMETRY_TRACE_SAMPLE_RATE1.0Tỉ lệ sampling — 1.0 là thu thập tất cả, 0.1 là 10%

Metrics

BiếnMặc địnhMô tả
TELEMETRY_HTTP_URLURL endpoint của OTLP Collector để gửi metrics (bắt buộc nếu dùng OTLP)
TELEMETRY_HTTP_RECORD_THRESHOLD0Ngưỡng tối thiểu (ms) để ghi nhận histogram duration — giúp lọc request nhanh
TELEMETRY_METRIC_READERperiodicLoại metric reader: periodic hoặc prometheus
TELEMETRY_METRIC_INTERVAL60000Khoảng thời gian export metrics (ms) — mặc định 1 phút

OTLP Exporter

BiếnMặc địnhMô tả
OTEL_EXPORTER_OTLP_ENDPOINThttp://localhost:4318Endpoint tổng của OTLP Collector
OTEL_EXPORTER_OTLP_PROTOCOLhttp/protobufProtocol: http/protobuf hoặc grpc
OTEL_EXPORTER_OTLP_HEADERS(trống)Headers bổ sung, định dạng key=value,key2=value2

Chi tiết cách plugin đăng ký metrics

Khi server load plugin, các bước sau xảy ra:

System metrics (đăng ký trong setSystemMetrics)

Plugin dùng pidusage để thu thập thông tin process:

typescript
// Observable gauges — đọc giá trị theo chu kỳ
const cpuGauge = meter.createObservableGauge('process_cpu_percent', {
  description: 'Process CPU usage percentage',
});
const memoryGauge = meter.createObservableGauge('process_memory_mb', {
  description: 'Process memory usage in MB',
});
const heapGauge = meter.createObservableGauge('process_heap_mb', {
  description: 'Process heap used in MB',
});

Gateway request metrics (đăng ký trong registerGatewayRequestMetrics)

Middleware trên Gateway tự động đo:

MetricLoạiLabelsMô tả
http_request_countCounterapp, method, path, status_codeĐếm tổng số request
http_request_costHistogramapp, method, path, status_codePhân phối thời gian xử lý (ms)
http_request_activeUpDownCounterapp, method, pathSố request đang xử lý đồng thời

Custom instrumentation

Plugin khác có thể thêm metric riêng thông qua Meter API:

typescript
class MyPlugin extends Plugin {
  async load() {
    const telemetry = this.app.pm.get('telemetry') as PluginTelemetryServer;
    const meter = this.app.telemetry.metric.getMeter();

    // Counter — đếm sự kiện
    const emailsSent = meter.createCounter('emails_sent_total', {
      description: 'Tổng số email đã gửi',
    });
    emailsSent.add(1, { template: 'welcome' });

    // Histogram — đo phân phối giá trị
    const processingTime = meter.createHistogram('job_processing_ms', {
      description: 'Thời gian xử lý background job',
      unit: 'ms',
    });
    processingTime.record(142, { job_type: 'export' });
  }
}

Chiến lược Sampling

Chiến lượcMô tảKhi nào dùng
Always On (1.0)Thu thập tất cả tracesMôi trường dev, staging
Ratio (0.1)Chỉ 10% request được ghi nhậnProduction có tải cao
Parent-basedQuyết định dựa trên parent traceHệ thống microservices phân tán

Cảnh báo

Đặt sampling rate quá thấp (< 0.01) có thể khiến bạn bỏ lỡ các lỗi hiếm gặp. Khuyến nghị dùng tail-based sampling trên OTLP Collector để giữ lại traces có lỗi.

Prometheus metrics endpoint

Khi dùng Prometheus reader, metrics được expose tại:

GET /api/metrics

Cấu hình Prometheus scrape:

yaml
scrape_configs:
  - job_name: 'digiforce'
    scrape_interval: 60s
    metrics_path: '/api/metrics'
    static_configs:
      - targets: ['localhost:13000']

Temporality preference

Plugin cấu hình AggregationTemporalityPreference.DELTA cho OTLP exporter. Điều này có nghĩa:

  • DELTA: Mỗi lần export chỉ gửi giá trị thay đổi kể từ lần export trước
  • Phù hợp với backend như Prometheus, Datadog, New Relic
  • Nếu backend yêu cầu CUMULATIVE, cần điều chỉnh cấu hình trong collector