Class: NatsWork::Monitoring::WebhookReporter
- Inherits:
-
Object
- Object
- NatsWork::Monitoring::WebhookReporter
- Defined in:
- lib/natswork/monitoring.rb
Overview
Generic webhook reporter
Instance Method Summary collapse
- #flush_metrics ⇒ Object
-
#initialize(webhook_url, options = {}) ⇒ WebhookReporter
constructor
A new instance of WebhookReporter.
- #report_counter(metric, value, tags = {}) ⇒ Object
- #report_gauge(metric, value, tags = {}) ⇒ Object
- #report_metric(type, metric, value, tags = {}) ⇒ Object
- #report_timer(metric, value, tags = {}) ⇒ Object
Constructor Details
#initialize(webhook_url, options = {}) ⇒ WebhookReporter
Returns a new instance of WebhookReporter.
168 169 170 171 172 173 174 175 176 |
# File 'lib/natswork/monitoring.rb', line 168 def initialize(webhook_url, = {}) @webhook_url = webhook_url @batch_size = .fetch(:batch_size, 100) @flush_interval = .fetch(:flush_interval, 60) @metrics_buffer = [] @mutex = Mutex.new start_flush_timer end |
Instance Method Details
#flush_metrics ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/natswork/monitoring.rb', line 204 def flush_metrics return if @metrics_buffer.empty? metrics = @metrics_buffer.dup @metrics_buffer.clear # Send webhook asynchronously Thread.new do send_webhook(metrics) end end |
#report_counter(metric, value, tags = {}) ⇒ Object
192 193 194 |
# File 'lib/natswork/monitoring.rb', line 192 def report_counter(metric, value, = {}) report_metric('counter', metric, value, ) end |
#report_gauge(metric, value, tags = {}) ⇒ Object
196 197 198 |
# File 'lib/natswork/monitoring.rb', line 196 def report_gauge(metric, value, = {}) report_metric('gauge', metric, value, ) end |
#report_metric(type, metric, value, tags = {}) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/natswork/monitoring.rb', line 178 def report_metric(type, metric, value, = {}) @mutex.synchronize do @metrics_buffer << { type: type, metric: metric, value: value, tags: , timestamp: Time.now.to_f } flush_metrics if @metrics_buffer.size >= @batch_size end end |
#report_timer(metric, value, tags = {}) ⇒ Object
200 201 202 |
# File 'lib/natswork/monitoring.rb', line 200 def report_timer(metric, value, = {}) report_metric('timer', metric, value, ) end |