Class: Hatchet::Features::Metrics

Inherits:
Object
  • Object
show all
Defined in:
lib/hatchet/features/metrics.rb

Overview

Metrics client for reading metrics out of Hatchet’s metrics API

This class provides a high-level interface for retrieving queue metrics, Prometheus metrics, task statistics, and task metrics from the Hatchet system.

Examples:

Getting queue metrics

metrics = metrics_client.get_queue_metrics

Getting task metrics

task_metrics = metrics_client.get_task_metrics(
  since: Time.now - 86400,
  workflow_ids: ["wf-1"]
)

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(rest_client, config) ⇒ void

Initializes a new Metrics client instance

Parameters:

  • rest_client (Object)

    The configured REST client for API communication

  • config (Hatchet::Config)

    The Hatchet configuration containing tenant_id and other settings

Since:

  • 0.1.0



32
33
34
35
36
37
# File 'lib/hatchet/features/metrics.rb', line 32

def initialize(rest_client, config)
  @rest_client = rest_client
  @config = config
  @task_api = HatchetSdkRest::TaskApi.new(rest_client)
  @tenant_api = HatchetSdkRest::TenantApi.new(rest_client)
end

Instance Method Details

#get_queue_metricsHash

Retrieve the current queue metrics for the tenant

Examples:

queues = metrics_client.get_queue_metrics

Returns:

  • (Hash)

    The current queue metrics

Raises:

Since:

  • 0.1.0



45
46
47
48
# File 'lib/hatchet/features/metrics.rb', line 45

def get_queue_metrics
  result = @tenant_api.tenant_get_step_run_queue_metrics(@config.tenant_id)
  result.queues || {}
end

#get_task_metrics(since: nil, until_time: nil, workflow_ids: nil, parent_task_external_id: nil, triggering_event_external_id: nil) ⇒ TaskMetrics

Retrieve task metrics grouped by status (queued, running, completed, failed, cancelled)

Examples:

metrics = metrics_client.get_task_metrics(
  since: Time.now - 86400,
  workflow_ids: ["wf-1"]
)
puts "Completed: #{metrics.completed}, Failed: #{metrics.failed}"

Parameters:

  • since (Time, nil) (defaults to: nil)

    Start time for the metrics query (defaults to the past day if unset)

  • until_time (Time, nil) (defaults to: nil)

    End time for the metrics query

  • workflow_ids (Array<String>, nil) (defaults to: nil)

    List of workflow IDs to filter the metrics by

  • parent_task_external_id (String, nil) (defaults to: nil)

    ID of the parent task to filter by

  • triggering_event_external_id (String, nil) (defaults to: nil)

    ID of the triggering event to filter by

Returns:

  • (TaskMetrics)

    Task metrics with counts per status

Raises:

Since:

  • 0.1.0



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hatchet/features/metrics.rb', line 85

def get_task_metrics(since: nil, until_time: nil, workflow_ids: nil, parent_task_external_id: nil, triggering_event_external_id: nil)
  since_time = since || (Time.now - (24 * 60 * 60))
  until_val = until_time || Time.now

  result = @task_api.v1_task_list_status_metrics(
    @config.tenant_id,
    since_time.utc.iso8601,
    {
      _until: until_val.utc.iso8601,
      workflow_ids: workflow_ids,
      parent_task_external_id: parent_task_external_id,
      triggering_event_external_id: triggering_event_external_id,
    },
  )

  # Build metrics hash from the status metric objects
  metrics = { cancelled: 0, completed: 0, failed: 0, queued: 0, running: 0 }

  if result.is_a?(Array)
    result.each do |m|
      status_name = m.respond_to?(:status) ? m.status.to_s.downcase.to_sym : nil
      count = m.respond_to?(:count) ? m.count : 0
      metrics[status_name] = count if metrics.key?(status_name)
    end
  end

  TaskMetrics.new(**metrics)
end

#get_task_statsObject

Get task statistics for the tenant

Examples:

stats = metrics_client.get_task_stats

Returns:

  • (Object)

    The task statistics

Raises:

Since:

  • 0.1.0



66
67
68
# File 'lib/hatchet/features/metrics.rb', line 66

def get_task_stats
  @tenant_api.tenant_get_task_stats(@config.tenant_id)
end

#scrape_tenant_prometheus_metricsString

Scrape Prometheus metrics for the tenant

Examples:

prometheus_text = metrics_client.scrape_tenant_prometheus_metrics

Returns:

  • (String)

    The metrics in Prometheus text format

Raises:

Since:

  • 0.1.0



56
57
58
# File 'lib/hatchet/features/metrics.rb', line 56

def scrape_tenant_prometheus_metrics
  @tenant_api.tenant_get_prometheus_metrics(@config.tenant_id)
end