Class: Tasker::Telemetry::MetricsExportService

Inherits:
Object
  • Object
show all
Includes:
Concerns::StructuredLogging
Defined in:
lib/tasker/telemetry/metrics_export_service.rb

Overview

Service for exporting metrics in various formats with delivery coordination

Phase 4.2.2.3.3: Service handles the business logic of metrics export, including format conversion, delivery, and storage. This is called by MetricsExportJob but can also be used independently.

Key Features:

  • Multiple export formats (Prometheus, JSON, CSV)
  • Prometheus remote write integration
  • Configurable storage backends
  • Comprehensive error handling and logging

Examples:

Basic usage

service = MetricsExportService.new
result = service.export_metrics(format: :prometheus, metrics_data: data)

With custom configuration

service = MetricsExportService.new(
  prometheus_config: { endpoint: 'http://localhost:9090/api/v1/write' }
)

Constant Summary

Constants included from Concerns::StructuredLogging

Concerns::StructuredLogging::CORRELATION_ID_KEY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::StructuredLogging

#correlation_id, #correlation_id=, #log_exception, #log_orchestration_event, #log_performance_event, #log_step_event, #log_structured, #log_task_event, #with_correlation_id

Constructor Details

#initialize(config = {}) ⇒ MetricsExportService

Initialize metrics export service

Parameters:

  • config (Hash) (defaults to: {})

    Service configuration overrides



34
35
36
# File 'lib/tasker/telemetry/metrics_export_service.rb', line 34

def initialize(config = {})
  @config = default_config.merge(config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



29
30
31
# File 'lib/tasker/telemetry/metrics_export_service.rb', line 29

def config
  @config
end

Instance Method Details

#export_metrics(format:, metrics_data:, context: {}) ⇒ Hash

Export metrics in specified format

Main entry point for exporting metrics. Handles format routing, data conversion, delivery, and comprehensive error handling.

Parameters:

  • format (Symbol)

    Export format (:prometheus, :json, :csv)

  • metrics_data (Hash)

    Raw metrics data to export

  • context (Hash) (defaults to: {})

    Additional context (job_id, coordinator_instance, etc.)

Returns:

  • (Hash)

    Export result with success status and details



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tasker/telemetry/metrics_export_service.rb', line 47

def export_metrics(format:, metrics_data:, context: {})
  @export_context = context
  start_time = Time.current

  log_export_started(format, metrics_data, context)

  case format.to_sym
  when :prometheus
    result = export_prometheus_metrics(metrics_data)
  when :json
    result = export_json_metrics(metrics_data)
  when :csv
    result = export_csv_metrics(metrics_data)
  else
    return unsupported_format_result(format)
  end

  duration = Time.current - start_time
  log_export_completed(format, result, duration)

  result.merge(
    format: format,
    duration: duration,
    exported_at: Time.current.iso8601,
    context: context
  )
rescue StandardError => e
  log_export_error(format, e, context)
  {
    success: false,
    format: format,
    error: e.message,
    error_type: e.class.name,
    context: context
  }
end