Class: DeadBro::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dead_bro/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration = DeadBro.configuration) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/dead_bro/client.rb', line 10

def initialize(configuration = DeadBro.configuration)
  @configuration = configuration
  @circuit_breaker = create_circuit_breaker
end

Instance Method Details

#post_heartbeat(sync: false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dead_bro/client.rb', line 35

def post_heartbeat(sync: false)
  return if @configuration.api_key.nil?

  @configuration.last_heartbeat_attempt_at = Time.now.utc
  body = {event: "heartbeat", payload: {rails_env: DeadBro.env}, sent_at: Time.now.utc.iso8601, revision: @configuration.resolve_deploy_id, gem_version: DeadBro::VERSION}

  if sync
    # Called from the monitor thread on startup — run inline so settings are
    # applied before the first collection tick.
    uri = URI.parse(metrics_endpoint_url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = (uri.scheme == "https")
    http.open_timeout = @configuration.open_timeout
    http.read_timeout = @configuration.read_timeout
    request = Net::HTTP::Post.new(uri.request_uri)
    request["Content-Type"] = "application/json"
    request["Authorization"] = "Bearer #{@configuration.api_key}"
    request.body = JSON.dump(body)
    perform_request(http, request, event_name: "heartbeat", apply_settings: true)
  else
    dispatch_request(
      url: metrics_endpoint_url,
      body: body,
      event_name: "heartbeat",
      apply_settings: true
    )
  end

  nil
end

#post_metric(event_name:, payload:, force: false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dead_bro/client.rb', line 15

def post_metric(event_name:, payload:, force: false)
  return if @configuration.api_key.nil?
  return unless @configuration.enabled
  return if @configuration.skip_tracking?
  return if !force && !@configuration.should_sample?
  return if circuit_open?

  payload = truncate_payload_for_request(payload)
  body = {event: event_name, payload: payload, sent_at: Time.now.utc.iso8601, revision: @configuration.resolve_deploy_id, gem_version: DeadBro::VERSION}

  dispatch_request(
    url: metrics_endpoint_url,
    body: body,
    event_name: event_name,
    apply_settings: true
  )

  nil
end

#post_monitor_stats(payload) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/dead_bro/client.rb', line 66

def post_monitor_stats(payload)
  return if @configuration.api_key.nil?
  return unless @configuration.enabled
  return if @configuration.skip_tracking?
  return unless @configuration.monitor_enabled
  return if circuit_open?

  body = {payload: payload, sent_at: Time.now.utc.iso8601, revision: @configuration.resolve_deploy_id, gem_version: DeadBro::VERSION}

  dispatch_request(
    url: monitor_endpoint_url,
    body: body,
    event_name: nil,
    apply_settings: false
  )

  nil
end