Class: NatsWork::Monitoring::WebhookReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/natswork/monitoring.rb

Overview

Generic webhook reporter

Instance Method Summary collapse

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, options = {})
  @webhook_url = webhook_url
  @batch_size = options.fetch(:batch_size, 100)
  @flush_interval = options.fetch(:flush_interval, 60)
  @metrics_buffer = []
  @mutex = Mutex.new

  start_flush_timer
end

Instance Method Details

#flush_metricsObject



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, tags = {})
  report_metric('counter', metric, value, tags)
end

#report_gauge(metric, value, tags = {}) ⇒ Object



196
197
198
# File 'lib/natswork/monitoring.rb', line 196

def report_gauge(metric, value, tags = {})
  report_metric('gauge', metric, value, tags)
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, tags = {})
  @mutex.synchronize do
    @metrics_buffer << {
      type: type,
      metric: metric,
      value: value,
      tags: 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, tags = {})
  report_metric('timer', metric, value, tags)
end