Class: Sidekiq::CloudWatchMetrics::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/cloudwatchmetrics.rb

Constant Summary collapse

INTERVAL =

seconds

60

Instance Method Summary collapse

Constructor Details

#initialize(config: Sidekiq, client: Aws::CloudWatch::Client.new, namespace: "Sidekiq", additional_dimensions: {}) ⇒ Publisher

Returns a new instance of Publisher.



48
49
50
51
52
53
54
55
# File 'lib/sidekiq/cloudwatchmetrics.rb', line 48

def initialize(config: Sidekiq, client: Aws::CloudWatch::Client.new, namespace: "Sidekiq", additional_dimensions: {})
  # Sidekiq 6.5+ requires @config, which defaults to the top-level
  # `Sidekiq` module, but can be overridden when running multiple Sidekiqs.
  @config = config
  @client = client
  @namespace = namespace
  @additional_dimensions = additional_dimensions.map { |k, v| {name: k.to_s, value: v.to_s} }
end

Instance Method Details

#publishObject



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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/sidekiq/cloudwatchmetrics.rb', line 86

def publish
  now = Time.now
  stats = Sidekiq::Stats.new
  processes = Sidekiq::ProcessSet.new.to_enum(:each).to_a
  queues = stats.queues

  metrics = [
    {
      metric_name: "ProcessedJobs",
      timestamp: now,
      value: stats.processed,
      unit: "Count",
    },
    {
      metric_name: "FailedJobs",
      timestamp: now,
      value: stats.failed,
      unit: "Count",
    },
    {
      metric_name: "EnqueuedJobs",
      timestamp: now,
      value: stats.enqueued,
      unit: "Count",
    },
    {
      metric_name: "ScheduledJobs",
      timestamp: now,
      value: stats.scheduled_size,
      unit: "Count",
    },
    {
      metric_name: "RetryJobs",
      timestamp: now,
      value: stats.retry_size,
      unit: "Count",
    },
    {
      metric_name: "DeadJobs",
      timestamp: now,
      value: stats.dead_size,
      unit: "Count",
    },
    {
      metric_name: "Workers",
      timestamp: now,
      value: stats.workers_size,
      unit: "Count",
    },
    {
      metric_name: "Processes",
      timestamp: now,
      value: stats.processes_size,
      unit: "Count",
    },
    {
      metric_name: "DefaultQueueLatency",
      timestamp: now,
      value: stats.default_queue_latency,
      unit: "Seconds",
    },
    {
      metric_name: "Capacity",
      timestamp: now,
      value: calculate_capacity(processes),
      unit: "Count",
    },
    {
      metric_name: "Utilization",
      timestamp: now,
      value: calculate_utilization(processes) * 100.0,
      unit: "Percent",
    },
  ]

  processes.each do |process|
    metrics << {
      metric_name: "Utilization",
      dimensions: [{name: "Hostname", value: process["hostname"]}],
      timestamp: now,
      value: process["busy"] / process["concurrency"].to_f * 100.0,
      unit: "Percent",
    }

    metrics << {
      metric_name: "Utilization",
      dimensions: [{name: "Tag", value: process["tag"]}],
      timestamp: now,
      value: process["busy"] / process["concurrency"].to_f * 100.0,
      unit: "Percent",
    }
  end

  queues.each do |(queue_name, queue_size)|
    metrics << {
      metric_name: "QueueSize",
      dimensions: [{name: "QueueName", value: queue_name}],
      timestamp: now,
      value: queue_size,
      unit: "Count",
    }

    queue_latency = Sidekiq::Queue.new(queue_name).latency

    metrics << {
      metric_name: "QueueLatency",
      dimensions: [{name: "QueueName", value: queue_name}],
      timestamp: now,
      value: queue_latency,
      unit: "Seconds",
    }
  end

  unless @additional_dimensions.empty?
    metrics = metrics.each do |metric|
      metric[:dimensions] = (metric[:dimensions] || []) + @additional_dimensions
    end
  end
  # We can only put 20 metrics at a time
  metrics.each_slice(20) do |some_metrics|
    @client.put_metric_data(
      namespace: @namespace,
      metric_data: some_metrics,
    )
  end
end

#quietObject



227
228
229
230
# File 'lib/sidekiq/cloudwatchmetrics.rb', line 227

def quiet
  logger.info { "Quieting Sidekiq CloudWatch Metrics Publisher" }
  @stop = true
end

#runObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sidekiq/cloudwatchmetrics.rb', line 68

def run
  logger.info { "Started Sidekiq CloudWatch Metrics Publisher" }

  # Publish stats every INTERVAL seconds, sleeping as required between runs
  now = Time.now.to_f
  tick = now
  until @stop
    logger.info { "Publishing Sidekiq CloudWatch Metrics" }
    publish

    now = Time.now.to_f
    tick = [tick + INTERVAL, now].max
    sleep(tick - now) if tick > now
  end

  logger.info { "Stopped Sidekiq CloudWatch Metrics Publisher" }
end

#running?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/sidekiq/cloudwatchmetrics.rb', line 64

def running?
  !@thread.nil? && @thread.alive?
end

#startObject



57
58
59
60
61
62
# File 'lib/sidekiq/cloudwatchmetrics.rb', line 57

def start
  logger.info { "Starting Sidekiq CloudWatch Metrics Publisher" }

  @done = false
  @thread = safe_thread("cloudwatch metrics publisher", &method(:run))
end

#stopObject



232
233
234
235
236
237
238
239
240
# File 'lib/sidekiq/cloudwatchmetrics.rb', line 232

def stop
  logger.info { "Stopping Sidekiq CloudWatch Metrics Publisher" }
  @stop = true
  @thread.wakeup
  @thread.join
rescue ThreadError
  # Don't raise if thread is already dead.
  nil
end