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", process_metrics: true, additional_dimensions: {}) ⇒ Publisher

Returns a new instance of Publisher.



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

def initialize(config: Sidekiq, client: Aws::CloudWatch::Client.new, namespace: "Sidekiq", process_metrics: true, 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
  @process_metrics = process_metrics
  @additional_dimensions = additional_dimensions.map { |k, v| {name: k.to_s, value: v.to_s} }
end

Instance Method Details

#publishObject



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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/sidekiq/cloudwatchmetrics.rb', line 87

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",
    },
  ]

  utilization = calculate_utilization(processes) * 100.0

  unless utilization.nan?
    metrics << {
      metric_name: "Utilization",
      timestamp: now,
      value: utilization,
      unit: "Percent",
    }
  end

  processes.group_by do |process|
    process["tag"]
  end.each do |(tag, tag_processes)|
    next if tag.nil?

    tag_utilization = calculate_utilization(tag_processes) * 100.0

    unless tag_utilization.nan?
      metrics << {
        metric_name: "Utilization",
        dimensions: [{name: "Tag", value: tag}],
        timestamp: now,
        value: tag_utilization,
        unit: "Percent",
      }
    end
  end

  if @process_metrics
    processes.each do |process|
      process_utilization = process["busy"] / process["concurrency"].to_f * 100.0

      unless process_utilization.nan?
        process_dimensions = [{name: "Hostname", value: process["hostname"]}]

        if process["tag"]
          process_dimensions << {name: "Tag", value: process["tag"]}
        end

        metrics << {
          metric_name: "Utilization",
          dimensions: process_dimensions,
          timestamp: now,
          value: process_utilization,
          unit: "Percent",
        }
      end
    end
  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



259
260
261
262
# File 'lib/sidekiq/cloudwatchmetrics.rb', line 259

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

#runObject



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

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.debug { "Publishing Sidekiq CloudWatch Metrics" }
    publish

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

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

#running?Boolean

Returns:

  • (Boolean)


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

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

#startObject



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

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

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

#stopObject



264
265
266
267
268
269
270
271
272
# File 'lib/sidekiq/cloudwatchmetrics.rb', line 264

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