Class: A2A::Monitoring::WebhookAlertChannel

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

Overview

Webhook alert channel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, headers: {}, timeout: 10) ⇒ WebhookAlertChannel

Initialize webhook channel

Parameters:

  • Webhook URL

  • (defaults to: {})

    HTTP headers

  • (defaults to: 10)

    Request timeout



347
348
349
350
351
# File 'lib/a2a/monitoring/alerting.rb', line 347

def initialize(url, headers: {}, timeout: 10)
  @url = url
  @headers = headers
  @timeout = timeout
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



339
340
341
# File 'lib/a2a/monitoring/alerting.rb', line 339

def headers
  @headers
end

#urlObject (readonly)

Returns the value of attribute url.



339
340
341
# File 'lib/a2a/monitoring/alerting.rb', line 339

def url
  @url
end

Instance Method Details

#format_alert_payload(alert) ⇒ Hash (private)

Format alert for webhook payload

Parameters:

  • Alert data

Returns:

  • Formatted payload



384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/a2a/monitoring/alerting.rb', line 384

def format_alert_payload(alert)
  {
    alert_name: alert[:rule_name],
    metric: alert[:metric],
    value: alert[:value],
    severity: alert[:severity],
    state: alert[:state],
    description: alert[:description],
    fired_at: alert[:fired_at]&.iso8601,
    resolved_at: alert[:resolved_at]&.iso8601,
    tags: alert[:tags]
  }
end

#send_alert(alert) ⇒ Object

Send alert via webhook

Parameters:

  • Alert data



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/a2a/monitoring/alerting.rb', line 357

def send_alert(alert)
  payload = format_alert_payload(alert)

  uri = URI(@url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.read_timeout = @timeout

  request = Net::HTTP::Post.new(uri.path)
  request["Content-Type"] = "application/json"
  @headers.each { |key, value| request[key] = value }
  request.body = payload.to_json

  response = http.request(request)

  return if response.is_a?(Net::HTTPSuccess)

  raise "Webhook request failed: #{response.code} #{response.message}"
end