Class: Tasker::Telemetry::Plugins::JsonExporter

Inherits:
BaseExporter
  • Object
show all
Defined in:
lib/tasker/telemetry/plugins/json_exporter.rb

Overview

JSON format exporter for metrics data

Exports metrics in structured JSON format with optional pretty printing and custom field mapping.

Examples:

Basic Usage

exporter = Tasker::Telemetry::Plugins::JsonExporter.new
result = exporter.export(metrics_data, pretty: true)

With Custom Field Mapping

exporter = Tasker::Telemetry::Plugins::JsonExporter.new(
  field_mapping: {
    timestamp: 'collected_at',
    metrics: 'data'
  }
)

Constant Summary collapse

VERSION =
'1.0.0'
DESCRIPTION =
'JSON format exporter with customizable field mapping'

Constants included from Concerns::StructuredLogging

Concerns::StructuredLogging::CORRELATION_ID_KEY

Instance Attribute Summary

Attributes inherited from BaseExporter

#description, #name, #version

Instance Method Summary collapse

Methods inherited from BaseExporter

#log_plugin_event, #log_plugin_exception, #log_plugin_performance, #metadata, #on_cache_sync, #on_export_complete, #on_export_request, #safe_export

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(options = {}) ⇒ JsonExporter

Returns a new instance of JsonExporter.

Parameters:

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

    Configuration options

Options Hash (options):

  • :field_mapping (Hash)

    Custom field name mapping

  • :include_metadata (Boolean)

    Include export metadata



31
32
33
34
35
# File 'lib/tasker/telemetry/plugins/json_exporter.rb', line 31

def initialize(options = {})
  super()
  @field_mapping = options.fetch(:field_mapping, {})
  @include_metadata = options.fetch(:include_metadata, true)
end

Instance Method Details

#export(metrics_data, options = {}) ⇒ Hash

Export metrics data as JSON

Parameters:

  • metrics_data (Hash)

    Metrics data from MetricsBackend

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

    Export options

Options Hash (options):

  • :pretty (Boolean)

    Pretty print JSON output

  • :additional_fields (Hash)

    Extra fields to include

Returns:

  • (Hash)

    Export result with JSON string



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tasker/telemetry/plugins/json_exporter.rb', line 44

def export(metrics_data, options = {})
  pretty = options.fetch(:pretty, false)
  additional_fields = options.fetch(:additional_fields, {})

  # Build export data with field mapping
  export_data = build_export_data(metrics_data, additional_fields)

  # Generate JSON
  json_output = if pretty
                  JSON.pretty_generate(export_data)
                else
                  JSON.generate(export_data)
                end

  {
    success: true,
    format: 'json',
    data: json_output,
    size_bytes: json_output.bytesize,
    metrics_count: metrics_data[:metrics]&.size || 0
  }
end

#supported_formatsArray<String>

Get supported formats

Returns:

  • (Array<String>)

    List of supported formats



78
79
80
# File 'lib/tasker/telemetry/plugins/json_exporter.rb', line 78

def supported_formats
  %w[json]
end

#supports_format?(format) ⇒ Boolean

Check if format is supported

Parameters:

  • format (String, Symbol)

    Format to check

Returns:

  • (Boolean)

    True if JSON format is supported



71
72
73
# File 'lib/tasker/telemetry/plugins/json_exporter.rb', line 71

def supports_format?(format)
  %w[json].include?(format.to_s.downcase)
end