Class: CloseYourIt::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/closeyourit/transport.rb

Overview

Spedisce un payload a un path di ingest (errori → /events, metriche → /metrics) via HTTP POST con Authorization: Bearer. Mai solleva: ogni errore di rete è loggato e ingoiato.

Constant Summary collapse

OPEN_TIMEOUT =
2
READ_TIMEOUT =
3
MAX_REDIRECTS =

Net::HTTP non segue i redirect: l'host canonico può rispondere 301 (es. apex → www). Ri-POSTiamo a Location preservando metodo + body, così l'evento non si perde in silenzio.

2
TIMEOUT_ERRORS =

Un timeout di rete (apertura o lettura) è un fallimento d'invio speciale: lo isoliamo dai non-2xx e dagli altri errori di rete perché segnala tipicamente problemi di connettività (CYRB-12).

[ Net::OpenTimeout, Net::ReadTimeout, Timeout::Error ].freeze

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Transport

Returns a new instance of Transport.



22
23
24
# File 'lib/closeyourit/transport.rb', line 22

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#send_event(payload, path:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/closeyourit/transport.rb', line 26

def send_event(payload, path:)
  response = post(payload, path)
  if response.is_a?(Net::HTTPSuccess)
    CloseYourIt.stats.increment(:sent)
    CloseYourIt.notify_diagnostic(:send, status: response.code.to_i)
  else
    CloseYourIt.stats.increment(:failed)
    CloseYourIt.internal_logger.warn("CloseYourIt transport: HTTP #{response.code}#{error_detail(response)} su #{path}")
    CloseYourIt.notify_diagnostic(:drop, reason: :response, status: response.code.to_i)
  end
  response
rescue StandardError => e
  CloseYourIt.stats.increment(:failed)
  CloseYourIt.internal_logger.error("CloseYourIt transport: #{e.class}: #{e.message}")
  if timeout_error?(e)
    CloseYourIt.stats.increment(:timeout)
    CloseYourIt.notify_diagnostic(:timeout, error: e.class.name)
  else
    CloseYourIt.notify_diagnostic(:drop, reason: :network, error: e.class.name)
  end
  nil
end