Class: MysqlFramework::Stats::AwsMetricPublisher

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_framework/stats/aws_metric_publisher.rb

Constant Summary collapse

THREAD_NAME =
'mysql-connector-pool-stats'
JOIN_TIMEOUT =

seconds to wait for clean thread exit before force-killing

5
METRIC_UNIT =
'Count'
METRIC_NAME_MAP =
{
  size: 'MysqlConnectionPoolSize',
  available: 'MysqlConnectionPoolAvailable',
  idle: 'MysqlConnectionPoolIdle'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(connector: nil, dimension_map: nil, cloudwatch_client: nil, publish_interval: 300) ⇒ void

Initializes AWS metric publishing dependencies.

Parameters:

  • connector (MysqlFramework::Connector, nil) (defaults to: nil)

    connector used to read connection-pool stats

  • dimension_map (MysqlFramework::Stats::DimensionMap, nil) (defaults to: nil)

    CloudWatch namespace and dimensions

  • cloudwatch_client (Aws::CloudWatch::Client, nil) (defaults to: nil)

    CloudWatch client instance

  • publish_interval (Integer) (defaults to: 300)

    metric publish interval in seconds



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mysql_framework/stats/aws_metric_publisher.rb', line 25

def initialize(
  connector: nil,
  dimension_map: nil,
  cloudwatch_client: nil,
  publish_interval: 300
)
  @thread = nil
  @connector = connector
  @cloudwatch_client = cloudwatch_client
  @dimension_map = dimension_map || MysqlFramework::Stats::DimensionMap.new
  @publish_interval = publish_interval
end

Instance Method Details

#running?Boolean

Returns true when the reporter thread is alive.

Returns:

  • (Boolean)


74
75
76
# File 'lib/mysql_framework/stats/aws_metric_publisher.rb', line 74

def running?
  @thread&.alive? || false
end

#startThread?

Spawns the background sampling thread. Safe to call more than once –subsequent calls are no-ops while the thread is already running.

Returns:

  • (Thread, nil)

    reporter thread when started, or nil when already running



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mysql_framework/stats/aws_metric_publisher.rb', line 42

def start
  return if running?

  thread_name = "#{THREAD_NAME}-#{object_id}"
  thread = Thread.new do
    Thread.current.name = thread_name
    loop do
      sleep @publish_interval
      break unless Thread.current == @thread

      sample
    end
  end

  thread.abort_on_exception = false
  @thread = thread
end

#stopvoid

This method returns an undefined value.

Cooperatively stops the background thread and waits up to JOIN_TIMEOUT seconds for it to exit before force-killing it.



64
65
66
67
68
69
# File 'lib/mysql_framework/stats/aws_metric_publisher.rb', line 64

def stop
  thread = @thread
  @thread = nil # cooperative stop signal: loop checks this after each sleep
  thread&.join(JOIN_TIMEOUT)
  thread&.kill # force-kill only if still alive after timeout
end