Class: Sidekiq::CloudWatchMetrics::Publisher

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

Instance Method Summary collapse

Constructor Details

#initialize(config: Sidekiq, client: Aws::CloudWatch::Client.new(region: 'us-west-2'), namespace: ENV.fetch('NAMESPACE', 'Default-Sidekiq-CloudWatchMetrics'), process_metrics: true, additional_dimensions: {}, interval: 60, **kwargs) ⇒ Publisher

Returns a new instance of Publisher.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sidekiq/cloud_watch_metrics.rb', line 45

def initialize(config: Sidekiq, client: Aws::CloudWatch::Client.new(region: 'us-west-2'), namespace: ENV.fetch('NAMESPACE', 'Default-Sidekiq-CloudWatchMetrics'), process_metrics: true, additional_dimensions: {}, interval: 60, **kwargs)
  # 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} }
  @metrics_to_publish = kwargs[:metrics_to_publish] || []
  @interval = interval
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
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
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/sidekiq/cloud_watch_metrics.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",
    },
  ]

  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

  highest_latency_metric = metrics.select { |metric| metric[:metric_name] == 'QueueLatency' }
                            .max_by { |metric| metric[:value] }

  metrics << {
    metric_name: "MaxQueueLatency",
    dimensions: [{name: "QueueName", value: "AllQueues"}],
    timestamp: now,
    value: highest_latency_metric[:value],
    unit: "Seconds",
  }

  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
  custom_metrics = @metrics_to_publish&.any? ? metrics.select { |metric| @metrics_to_publish.include?(metric[:metric_name]) } : metrics

  custom_metrics.each_slice(20) do |metric_to_publish|
    @client.put_metric_data(
      namespace: @namespace,
      metric_data: metric_to_publish,
      )
  end
end

#quietObject



270
271
272
273
# File 'lib/sidekiq/cloud_watch_metrics.rb', line 270

def quiet
  logger.debug { "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/cloud_watch_metrics.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.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)


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

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

#startObject



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

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

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

#stopObject



275
276
277
278
279
280
281
282
283
# File 'lib/sidekiq/cloud_watch_metrics.rb', line 275

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