Class: Tasker::Telemetry::PrometheusExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/tasker/telemetry/prometheus_exporter.rb

Overview

Prometheus exporter for converting native metrics to Prometheus format

This exporter transforms metrics from the MetricsBackend singleton into standard Prometheus text format for consumption by monitoring systems.

Examples:

Basic usage

exporter = PrometheusExporter.new
prometheus_text = exporter.export

With custom backend

exporter = PrometheusExporter.new(MetricsBackend.instance)
prometheus_text = exporter.export

Instance Method Summary collapse

Constructor Details

#initialize(backend = nil) ⇒ PrometheusExporter

Initialize exporter with optional metrics backend

Parameters:

  • backend (MetricsBackend) (defaults to: nil)

    The metrics backend to export from



21
22
23
# File 'lib/tasker/telemetry/prometheus_exporter.rb', line 21

def initialize(backend = nil)
  @backend = backend || MetricsBackend.instance
end

Instance Method Details

#exportString

Export all metrics in Prometheus text format

Returns metrics in the standard Prometheus exposition format as specified at: https://prometheus.io/docs/instrumenting/exposition_formats/

Returns:

  • (String)

    Prometheus format text



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tasker/telemetry/prometheus_exporter.rb', line 31

def export
  return '' unless telemetry_enabled?

  metrics_data = @backend.export
  return '' if metrics_data[:metrics].empty?

  output = []
  output << (metrics_data)
  output << export_metrics(metrics_data[:metrics])
  "#{output.flatten.compact.join("\n")}\n"
rescue StandardError => e
  # Log error but don't fail the export
  log_export_error(e)
  export_error_metric(e)
end

#safe_exportHash

Export metrics with error handling

Returns:

  • (Hash)

    Export result with success status and data



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tasker/telemetry/prometheus_exporter.rb', line 50

def safe_export
  {
    success: true,
    data: export,
    timestamp: Time.current.iso8601,
    total_metrics: @backend.export[:total_metrics] || 0
  }
rescue StandardError => e
  {
    success: false,
    error: e.message,
    timestamp: Time.current.iso8601,
    total_metrics: 0
  }
end